Lesson 1: Poke Case 撲 克 牌 發 牌 準 備 1 張 撲 克 牌 的 底 圖 :blue.png 準 備 13 張 撲 克 牌 的 圖 片 :1.png~13.png 新 建 場 景 類 別 Table, 設 定 圖 像 corkboard.jpg 新 建 演 員 類 別 Ca



Similar documents
int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addobject(card, x, y); 程式碼說明 : 滑鼠事件 Greenfoot.mouseClic

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

證 券 簡 易 下 單 :2121 證 券 簡 易 下 單 1. 主 工 具 列 的 視 窗 搜 尋 器 直 接 輸 入 點 擊 主 選 單 證 券 專 區 下 單 特 殊 下 單 2121 證 券 簡 易 下 單 畫 面 說 明 1. 下 單 區 2. 個 股 行 情 資 訊 與

6-1-1極限的概念

目 錄 項 目 內 容 頁 數 1 手 機 要 求 3 2 登 記 程 序 3 3 登 入 程 序 4 4 輸 入 買 賣 指 示 6 5 更 改 指 示 14 6 取 消 指 示 18 7 查 詢 股 票 結 存 21 8 查 詢 買 賣 指 示 23 9 更 改 密 碼 查 詢 股

所 3 學 分 課 程, 及 兩 門 跨 領 域 課 程 共 6 學 分 以 上 課 程 學 生 在 修 課 前, 必 須 填 寫 課 程 修 課 認 定 表, 經 班 主 任 或 指 導 教 授 簽 名 後 始 認 定 此 課 程 學 分 ) 10. 本 規 章 未 盡 事 宜, 悉 依 學 位

章節

肆 研 究 方 法 進 行 本 研 究 前, 我 們 首 先 對 研 究 中 所 用 到 名 詞 作 定 義 定 義 : 牌 數 : 玩 牌 時 所 使 用 到 撲 克 牌 數 次 數 : 進 行 猜 心 術 遊 戲 時, 重 複 分 牌 次 數 數 : 進 行 猜 心 術 遊 戲 時, 每 次 分


(Microsoft Word - MOODLE990201\266i\266\245\244\342\245U )

實德證券網上交易系統示範

Microsoft Word - 第四章.doc

<4D F736F F D20B3E6A4B830312D2D2DBCC6BD75BB50BEE3BCC6AABAA55BB4EEB942BAE22E646F6378>

NCKU elearning Manual

(DP_MFP_Training

研究一:n人以『剪刀、石頭、布』猜拳法猜拳一次,決定一人勝

奇 妙 的 24 摘 要 從 撲 克 牌 中 隨 機 抽 取 4 張 牌 可 以 有 1820 種 牌 組, 在 這 1820 種 牌 組 中, 有 1362 組 可 經 由 四 則 運 算 的 方 式, 算 出 24 點, 有 458 組 無 解 快 速 求 解 的 方 法 有 相 加 法 因 數

Microsoft Word doc

目 錄 壹 題 目 1: 新 增 商 品 ( 商 品 名 稱 為 玉 井 芒 果 乾 禮 盒 )... 3 貳 題 目 2: 新 增 商 品 ( 商 品 名 稱 為 紅 磚 布 丁 精 選 禮 盒 )... 5 參 題 目 3: 新 增 商 品 ( 商 品 名 稱 為 晶 鑽 XO 醬 禮 盒 ).

PowerPoint 簡報

Microsoft PowerPoint - 資料庫正規化(ccchen).ppt

101年度社會福利方案 網路線上操作手冊

55202-er-ch03.doc

寫 作 背 景 導 讀 [98] L Lyman Frank Baum

投影片 1

(Microsoft Word - \246\250\301Z\272\336\262z.doc)

題組一 文書排版

(Microsoft Word - \244\361\301\311\263W\253h\244\316\255p\244\300\257\ \(1\))

瑞興銀行

e-Submission System Quick Reference Guide for Publication Related Matters (Chinese version)

內 政 統 計 通 報


BSP 烤箱 - 封面-2

1

life930106

目 錄 頁 1. 歡 迎 使 用 網 上 預 約 面 談 訪 問 系 統 新 用 戶 新 用 戶 登 入 帳 戶 程 序 啟 動 網 上 預 約 面 談 訪 問 帳 戶 核 對 帳 戶 的 地 址 資 料

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

《數學奠基活動模組示例》

1 CH1 環 境 介 面 及 面 板 設 定 1-1 Word 2010 環 境 介 面 與 功 能 區 1-2 環 境 介 面 色 調 處 理 1-3 自 訂 快 速 存 取 工 具 列 1-4 Word 選 項 控 制 CH2 文 字 資 料 2-1 建 立 文 字 2-2 貼 入 網 頁 文

目 錄 一 系 統 登 入... 2 ( 一 ) 系 統 登 入 畫 面... 2 ( 二 ) 首 次 登 入 請 先 註 冊... 3 ( 三 ) 忘 記 單 位 帳 號... 8 ( 四 ) 忘 記 密 碼 ( 五 ) 健 保 卡 更 換 ( 六 ) 重 寄 確 認 信.

ART_RAE16_ticket_cn_p.1

關 於 教 育 部 學 習 拍 立 得 教 育 部 於 (103) 年 度 整 合 各 縣 市 政 府 部 屬 機 構 大 學 及 民 間 的 數 位 資 源 與 服 務, 依 不 同 類 型, 分 別 匯 集 於 教 育 大 市 集 教 育 百 科 教 育 媒 體 影 音 教 育 部 學 習 拍

授 課 老 師 章 節 第 一 章 教 學 教 具 間 3 分 鐘 粉 筆 CNC 銑 床 教 學 內 容 CNC 銑 床 之 基 本 操 作 教 材 來 源 數 值 控 制 機 械 實 習 Ⅰ 1. 了 解 CNC 銑 床 的 發 展 2. 了 解 CNC 銑 床 刀 具 的 選 用 3. 了 解

五 四 五 說 ( 代 序 ) 李 澤 厚 劉 再 復 I I II IV V VII 第 一 篇 五 四 新 文 化 運 動 批 評 提 綱 附 論 一 中 國 貴 族 精 神 的 命 運 ( 提 綱 )

Microsoft PowerPoint - 使用 Word 編輯與排版文件 (II).ppt

Microsoft Word - ch07

HSBC Holdings plc Interim Report Chinese

四 修 正 幼 兒 園 師 資 類 科 應 修 學 分 數 為 四 十 八 學 分, 並 明 定 學 分 數 抵 免 之 相 關 規 定 及 規 範 修 習 幼 兒 園 教 育 專 業 課 程 之 最 低 年 限 ( 修 正 條 文 第 五 條 ) 五 發 給 修 畢 師 資 職 前 教 育 證 明

骨 折 別 日 數 表 1. 鼻 骨 眶 骨 ( 含 顴 骨 ) 14 天 11. 骨 盤 ( 包 括 腸 骨 恥 骨 坐 骨 薦 骨 ) 40 天 2. 掌 骨 指 骨 14 天 12. 臂 骨 40 天 3. 蹠 骨 趾 骨 14 天 13. 橈 骨 與 尺 骨 40 天 4. 下 顎 ( 齒

支 持 機 構 : 社 會 文 化 司 主 辦 機 構 : 澳 門 學 聯 澳 門 青 年 研 究 協 會 電 話 : 傳 真 : 網 址 : 報 告 主 筆 : 李 略 博 士 數 據 錄

1. 安 裝 1.1 手 機 端 安 裝 檔 (cab 檔 ) 請 您 將 下 載 的 cab 檔 案 複 製 到 手 機 內 任 一 資 料 夾, 在 手 機 上 點 擊 cab 檔 案 後 即 可 開 始 安 裝 點 擊 本 檔 案 即 可 開 始 安 裝 請 於 您 的 手 機 上 繼 續 安

sle cover 1

二 兒 歌 選 用 情 形 ( ) 2 ( ) ( )

(Microsoft Word \245\277\244\361\273P\244\317\244\361.doc)

教育實習問與答:

二零零六至零七年施政報告

1

Microsoft Word - Draft circular on Sub Leg Apr (chi)_Traditional


計 算 機 概 論 Computer Siene ))) 8- 啟 動 與 認 識 PowerPoint [ 開 始 ] \ [ 所 有 程 式 ] \ [Mirosoft Offie] \ [Mirosoft Offie PowerPoint 200] PowerPoint [ 新 增 ] \ [

前 項 第 三 款 所 定 有 機 農 產 品 及 有 機 農 產 加 工 品 驗 證 基 準, 如 附 件 一 第 七 條 驗 證 機 構 受 理 有 機 農 產 品 及 有 機 農 產 加 工 品 之 驗 證, 應 辦 理 書 面 審 查 實 地 查 驗 產 品 檢 驗 及 驗 證 決 定 之

節 數 內 容 網 頁 設 計 (1): 利 用 Google Sites 制 作 簡 單 文 字 網 頁, 連 結 不 同 版 面 網 頁 設 計 (2): 在 文 字 網 頁 上 加 插 圖, 上 載 網 頁 影 片 設 計 (1): 利 用 Windows Movie

128 提 示 樞 紐 分 析 表 的 用 途 樞 紐 分 析 表 是 指 可 以 用 來 快 速 合 併 和 比 較 大 量 資 料 的 互 動 式 表 格, 透 過 它 可 以 詳 細 分 析 數 值 資 料, 特 別 適 用 於 下 列 情 況 : 需 要 從 含 有 大 量 資 料 的 清

會 員 專 區 使 用 手 冊 目 錄 一 基 本 介 紹 會 員 專 區 登 入 位 置 主 畫 面 與 網 站 架 構 : 功 能 導 覽 列 說 明 :... 3 二 DOI 查 詢 與 維 護... 4 三 DOI 註 冊 期 刊 類 型...

16

九 -2 國 中 數 學 基 本 學 習 內 容 補 救 教 材 第 六 冊 主 題 二 機 率 的 計 算 二 機 率 怎 麼 算? 想 一 想 : (1) 投 擲 一 枚 公 正 硬 幣 一 次, 會 出 現 哪 幾 種 情 形? 這 些 情 形 各 自 發 生 的 機 率 是 多 少? 會 不

C CH4.tpf

作 品 名 稱 : 永 遠 都 是 一 條 龍 摘 要 本 文 的 研 究 是 根 據 特 定 規 則 下, 如 何 將 撲 克 牌 翻 出 一 條 龍? 的 問 題, 進 行 不 同 方 法 的 研 究, 以 不 同 解 題 方 式 觀 察 問 題 解 決 問 題 壹 研 究 動 機 每 隔 一

十 三. 服 務 學 習 十 四. 座 位 表 管 理 十 五. 班 導 師 通 訊 錄 小 工 具 十 六. 電 子 報 表 十 七. 評 量 成 績 十 八. 學 期 成 績 ( 國 中 ) 十 九. 學 期 成 績 ( 高

進 入 系 統 1. 請 於 首 頁 右 側 使 用 者 登 入 輸 入 帳 號 密 碼 驗 證 碼 後, 點 選 登 入 進 入 系 統 2. 直 接 點 選 右 側 的 進 入 系 統, 直 接 進 入 題 目 檢 索 頁 面 直 接 進 入 系 統 後, 您 仍 可 瀏 覽 選 擇 您 所 需

第 6. 節 不 定 積 分 的 基 本 公 式 我 們 可 以 把 已 經 知 道 反 導 函 數 之 所 有 函 數 都 視 為 不 定 積 分 的 基 本 公 式 基 本 公 式 涵 蓋 的 範 圍 愈 大, 我 們 求 解 積 分 就 愈 容 易, 但 有 記 憶 不 易 的 情 事 研 讀

校 長 遴 選 者 就 相 關 遴 選 事 項, 有 程 序 外 之 接 觸 遴 選 會 委 員 在 任 期 間 因 故 無 法 執 行 任 務 或 有 不 適 當 之 行 為 者, 由 各 該 主 管 機 關 解 聘 之 ; 其 缺 額, 依 第 一 項 至 第 五 項 規 定 聘 ( 派 ) 委

Microsoft Word - BM900HD-2F電腦設定.doc

格 成 績 證 明 第 六 條 第 七 條 本 系 大 四 課 程 中 規 劃 日 本 韓 國 越 南 專 題 研 究, 學 生 需 於 大 四 時 修 習 該 課 程, 並 於 規 定 期 間 內 提 出 專 題 報 告, 取 得 合 格 成 績 證 明 本 系 規 定 學 生 畢 業 時 需 取

iPhone版操作手冊990421

Microsoft Word - 雲林區_免試平台_國中模擬選填_操作手冊.doc

Microsoft Word - 全華Ch2-05.doc

2 飲 料 調 製 丙 級 技 術 士 技 能 檢 定 必 勝 寶 典 Beverage Modulation Preparation 應 考 綜 合 注 意 事 項 A1 A2 A3 A4 A5 A6 B7 B8 B9 B10 B11 B12 C13

「家加關愛在長青」計劃完成表現及評估報告

第二組掃描器規範書

chapter1.indd

1

(Microsoft Word -

本 題 各 點 彼 此 均 有 相 互 關 聯, 作 答 不 完 整, 將 影 響 各 評 分 點 之 得 分, 請 注 意 檔 名 儲 存 錯 誤, 該 題 一 律 0 分 計 算 深 淺 圖 表.xlsx 請 依 下 方 題 目 敘 述 操 作 ( 佔 總 分 :) 儲 存 格 範

<4D F736F F D D313032A7DEC075BAC2BC66B56EB04FB44EC5AAA7D3C440A7C7A874B2CEBEDEA740A4E2A5552E646F63>

<4D F736F F D20B2C433B3B92020B971B8F4A4C0AA52A7DEA5A9>


10. 遊 戲 動 作 結 附 ( 附 加 牌 ) 支 付 魔 力 移 除 展 示 召 喚 造 成 傷 害 連 鎖 抽 牌

<4D F736F F D20AB6EAAF9B0EAA470BCC6BEC7ACEC2E646F63>

<4D F736F F D20AA41B9A2BCD0B7C7A7D6B374A44AAAF92E646F63>

行政院金融監督管理委員會全球資訊網-行政院金融監督管理委員會

附 件 103 年 國 中 教 育 會 考 反 試 場 則 處 理 方 式 覽 表 別 反 試 場 則 事 項 國 英 數 社 自 處 理 方 式 寫 作 測 驗 由 他 人 頂 替 代 考 或 偽 ( 變 ) 造 證 件 應 試 二 脅 迫 其 他 考 生 或 試 務 人 員 協 助 於 考 試

名 師 峻 堯 老 師 地 理 考 科 壹 前 言 ( 筆 者 對 於 指 定 考 科 的 界 定 ) ~

CP70D0026D61ETW0R01-01-印刷

Microsoft PowerPoint - 104年說明會簡報-final-0923.ppt [相容模式]

C 標準文件

大學甄選入學委員會

Microsoft Word - cards.doc

國中數學基本學習內容補救教材 第四冊

國立屏東師範學院教育心理與輔導研究所

Transcription:

Greenfoot 進 階 課 程 (8 小 時 ) 主 講 : 南 港 高 中 高 慧 君 老 師 時 間 :2014 年 6 月 16 日 地 點 : 金 門 高 中 主 辦 : 資 訊 學 科 中 心 Greenfoot 進 階 課 程 (8 小 時 )... 1 Lesson 1: Poke Case... 2 撲 克 牌 發 牌... 2 Lesson 2: Simple Solitare... 8 簡 易 接 龍... 8 Poke1 解 答 :... 9 Poke2 解 答 :... 10 Poke3 解 答 :... 11 Poke4 解 答 :... 14 Poke5 解 答 :... 19 Lesson 3: Solitare... 22 接 龍... 22 Lesson 4: MineSweep... 39 踩 地 雷... 39 Lesson 5: Reversi... 55 黑 白 棋... 55 Lesson 6: Match... 74 對 對 碰... 74 Lesson 7: Puzzle... 85 九 宮 拼 圖... 85 1

Lesson 1: Poke Case 撲 克 牌 發 牌 準 備 1 張 撲 克 牌 的 底 圖 :blue.png 準 備 13 張 撲 克 牌 的 圖 片 :1.png~13.png 新 建 場 景 類 別 Table, 設 定 圖 像 corkboard.jpg 新 建 演 員 類 別 Card 新 建 演 員 類 別 Deck, 設 置 圖 像 blue.png 編 輯 Table 的 程 式 碼 Import greenfoot.; public class Table extends World private Deck deck; public Table() super(600, 400, 1); deck = new Deck(); addobject(deck, 100, 100); 2

public void act() if (Greenfoot.mouseClicked(deck)) Card card = new Card(); card.setimage((1+greenfoot.getrandomnumber(13))+".png"); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()) addobject(card, x, y); 滑 鼠 事 件 Greenfoot.mouseClicked() 方 法 簽 名 : public static boolean mouseclicked(java.lang.object obj) 方 法 說 明 : 如 果 滑 鼠 在 某 物 件 上 按 完 左 鍵 ( 按 下 並 放 開 ), 則 傳 回 true 參 數 : obj - Typically one of Actor, World or null 傳 回 值 True / False 更 多 滑 鼠 事 件 請 參 閱 Greennfoot 類 別 說 明 文 件 : 1. static boolean mousedragended(java.lang.object obj) True if a mouse drag has ended. 2. static boolean mousedragged(java.lang.object obj) True if the mouse has been dragged on the given object. 3. static boolean mousemoved(java.lang.object obj) 3

True if the mouse has been moved on the given object. 4. static boolean mousepressed(java.lang.object obj) True if the mouse has been pressed (changed from a non-pressed state to being pressed) on the given object. 5. static MouseInfo getmouseinfo() Return a mouse info object with information about the state of the mouse. 問 題 改 進 在 上 例 中, 我 們 使 用 亂 數 來 取 卡 片, 這 樣 做 會 有 重 複 發 同 一 張 牌 的 可 能 性 所 以 接 下 來 我 們 要 用 陣 列 串 列 (ArrayList) 來 儲 存 13 張 牌 增 加 Card 類 別 的 程 式 碼 private int value; public Card(int value) this.value = value; publicint getvalue() return value; import java.util.arraylist; privatearraylist<card> cards; public Deck() cards = new ArrayList<Card>(); for (int i=0; i< 13; i++) cards.add(new Card(i+1)); 4

public Card getcard() if(getsize()==0) return null; Card card = cards.get(0); cards.remove(card); return card; publicintgetsize() return cards.size(); 修 改 Table 類 別 的 程 式 碼 public void act() if (Greenfoot.mouseClicked(deck) &&deck.getsize()>0) Card card = deck.getcard(); card.setimage(card.getvalue()+".png"); nt x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()) addobject(card, x, y); ArrayList ArrayList 是 JAVA 當 中 的 一 個 類 別, 與 Array 差 在 於 因 為 他 是 一 個 寫 好 的 類 別, 有 很 多 可 以 直 接 用 的 程 式, 所 以 很 方 便 使 用 方 法 : 1. 建 構 ArrayList<String> mylist = new ArrayList<String>(); // 指 定 是 String 的 型 態 ArrayList mylist = new ArrayList(); // 也 可 以 不 指 定 2. 加 入 元 素 String s = new String(); 5

mylist.add(s); 3. 查 詢 list 大 小 int thesize = mylist.size(); 4. 查 詢 特 定 元 素 boolean isin = mylist.contains(s); // 若 用 上 面 的 例 子 因 為 有 s 字 串 所 以 回 傳 true 5. 查 詢 特 定 元 素 位 置 int idx = mylist.indexof(s); // 會 回 傳 0 表 第 0 個 位 置 6. 判 斷 List 是 否 為 空 boolean empty = mylist.isempty(); // 因 為 有 一 個 元 素 會 回 傳 false 7. 刪 除 特 定 元 素 mylist.remove(s); 問 題 改 進 在 上 例 中, 我 們 會 依 序 發 出 13 張 牌, 但 實 務 上, 我 們 在 發 牌 前 會 先 做 洗 牌 的 動 作, 我 們 可 以 匯 入 Collections 套 件, 來 做 打 亂 陣 列 串 列 內 容 的 動 作 在 Deck 類 別 中 增 加 程 式 碼 : Import java.util.collections; 在 Deck 的 建 構 子 中 增 加 程 式 碼 : Collections.shuffle(cards); 最 後, 當 我 們 發 完 所 有 的 13 張 牌 後, 就 把 發 牌 的 物 件 刪 除, 讓 使 用 者 不 能 再 發 牌 在 Deck 類 別 中 修 改 程 式 碼 : public Card getcard() 6

if(getsize()==0) return null; Card card = cards.get(0); cards.remove(card); if(getsize()==0) getworld().removeobject(this); return card; Review: 1. 滑 鼠 事 件 2. ArrayList 套 件 3. Collections 套 件 7

Lesson 2: Simple Solitare 簡 易 接 龍 Poke1.zip 任 務 : 隨 機 發 出 13 張 牌 要 求 : 1. 使 用 ArrayList 來 存 放 13 張 牌 2. 使 用 Collections 來 將 ArrayList 中 的 物 件 打 亂 3. 發 出 的 牌 需 要 一 張 一 張 排 列 整 齊 Poke2.zip 任 務 : 可 以 拖 曳 撲 克 牌 提 示 : Greenfoot.mousePressed Greenfoot.mouseDragged Greenfoot.mouseDragEnded Poke3.zip 任 務 : 拖 曳 中 的 撲 克 牌 會 在 最 上 層, 放 開 後 會 回 到 原 地 點 原 圖 層 提 示 : 新 增 一 個 牌 疊 類 別 Pile 將 牌 疊 中 的 撲 克 牌 重 畫 :removeobject addobject Poke4.zip 任 務 : 可 以 將 撲 克 牌 放 到 目 標 牌 疊 (CardStack) 中, 放 好 後 就 不 能 拖 曳 提 示 : Poke5.zip 任 務 : 目 標 牌 疊 必 須 按 點 數 大 小 放 入 8

Poke1 解 答 : Table 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private Deck deck; private int number=0; private int card_x=150; Constructor for objects of class Table. public Table() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); deck = new Deck(); addobject(deck, 50, 50); public void act() if (Greenfoot.mouseClicked(deck)) number++; card_x = card_x + 20; addobject(deck.getcard(), card_x, 50); if (number == 13) removeobject(deck); 9

Poke2 解 答 : Card 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Card here. @author (your name) @version (a version number or a date) public class Card extends Actor private int x, y; public Card(int value) setimage(value + ".png"); Act - do whatever the Card wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mousePressed(this)) x = getx(); y = gety(); if (Greenfoot.mouseDragged(this)) 10

setlocation(mouse.getx(), mouse.gety()); if (Greenfoot.mouseDragEnded(this)) setlocation(x, y); Poke3 解 答 : Table 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private Deck deck; private Pile pile = new Pile(); private int number=0; private int card_x=150; Constructor for objects of class Table. public Table() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); deck = new Deck(); 11

addobject(deck, 50, 50); addobject(pile, 150, 50); public Pile getpile() return pile; public void act() if (Greenfoot.mouseClicked(deck)) number++; Card card = deck.getcard(); pile.addcard(card); if (number == 13) removeobject(deck); Pile 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class Pile here. @author (your name) @version (a version number or a date) public class Pile extends Actor private ArrayList<Card> cards = new ArrayList<Card>(); public Pile() 12

public void addcard(card card) cards.add(card); getworld().addobject(card, getx()+cards.size()15, gety()); public void redraw() for (Card card: cards) int x = card.getx(); int y = card.gety(); World world = getworld(); world.removeobject(card); world.addobject(card, x, y); Card 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Card here. @author (your name) @version (a version number or a date) public class Card extends Actor private int x, y; public Card(int value) setimage(value + ".png"); Act - do whatever the Card wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. 13

MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mousePressed(this)) x = getx(); y = gety(); World world = getworld(); world.removeobject(this); world.addobject(this, x, y); if (Greenfoot.mouseDragged(this)) setlocation(mouse.getx(), mouse.gety()); if (Greenfoot.mouseDragEnded(this)) setlocation(x, y); Table table = (Table) getworld(); table.getpile().redraw(); Poke4 解 答 : Table 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private Deck deck; 14

private Pile pile = new Pile(); private CardStack cardstack = new CardStack(); private int number=0; private int card_x=150; Constructor for objects of class Table. public Table() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); deck = new Deck(); addobject(deck, 50, 50); addobject(pile, 150, 50); addobject(cardstack, 500, 50); public Pile getpile() return pile; public CardStack getcardstack() return cardstack; public void act() if (Greenfoot.mouseClicked(deck)) number++; Card card = deck.getcard(); pile.addcard(card); if (number == 13) removeobject(deck); 15

CardStack 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class CardStack here. @author (your name) @version (a version number or a date) public class CardStack extends Actor private ArrayList<Card> cards = new ArrayList<Card>(); public void addcard(card card) cards.add(card); getworld().addobject(card, getx(), gety()+cards.size()15); Pile 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class Pile here. @author (your name) @version (a version number or a date) public class Pile extends Actor private ArrayList<Card> cards = new ArrayList<Card>(); public Pile() 16

public void addcard(card card) cards.add(card); getworld().addobject(card, getx()+cards.size()15, gety()); public void removecard(card card) cards.remove(card); getworld().removeobject(card); public void redraw() for (Card card: cards) int x = card.getx(); int y = card.gety(); World world = getworld(); world.removeobject(card); world.addobject(card, x, y); Card 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Card here. @author (your name) @version (a version number or a date) public class Card extends Actor private int x, y; private boolean candrag=true; public Card(int value) setimage(value + ".png"); 17

Act - do whatever the Card wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mousePressed(this) && candrag) x = getx(); y = gety(); World world = getworld(); world.removeobject(this); world.addobject(this, x, y); if (Greenfoot.mouseDragged(this) && candrag) setlocation(mouse.getx(), mouse.gety()); if (Greenfoot.mouseDragEnded(this) && candrag) Table table = (Table) getworld(); if (getoneintersectingobject(cardstack.class)!=null) table.getpile().removecard(this); table.getcardstack().addcard(this); candrag = false; else setlocation(x, y); table.getpile().redraw(); 18

Poke5 解 答 : CardStack 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class CardStack here. @author (your name) @version (a version number or a date) public class CardStack extends Actor private ArrayList<Card> cards = new ArrayList<Card>(); public void addcard(card card) cards.add(card); getworld().addobject(card, getx(), gety()+cards.size()15); public boolean isacceptcard(card card) if (cards.size()==0 && card.getvalue()==1) return true; if (cards.size()>0) if (cards.get(cards.size()-1).getvalue() +1 == card.getvalue()) return true; return false; 19

Card 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Card here. @author (your name) @version (a version number or a date) public class Card extends Actor private int x, y; private int value; private boolean candrag=true; public Card(int value) this.value = value; setimage(value + ".png"); public int getvalue() return value; Act - do whatever the Card wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mousePressed(this) && candrag) x = getx(); y = gety(); World world = getworld(); world.removeobject(this); world.addobject(this, x, y); 20

if (Greenfoot.mouseDragged(this) && candrag) setlocation(mouse.getx(), mouse.gety()); if (Greenfoot.mouseDragEnded(this) && candrag) Table table = (Table) getworld(); if (getoneintersectingobject(cardstack.class)!=null && table.getcardstack().isacceptcard(this)) table.getpile().removecard(this); table.getcardstack().addcard(this); candrag = false; else setlocation(x, y); table.getpile().redraw(); 21

Lesson 3: Solitare 接 龍 準 備 一 張 牌 桌 的 圖 片 :porkboard.jpg 準 備 一 張 撲 克 牌 的 底 圖 :blueflip.png 準 備 一 張 空 的 撲 克 牌 :empty.png 準 備 52 張 撲 克 牌 的 圖 片 :spades1.png~diamonds13.png 開 啟 專 案 Poke03 新 建 場 景 類 別 Table, 設 置 圖 像 porkboard.jpg 新 建 演 員 類 別 Card 新 建 演 員 類 別 Deck, 設 置 圖 像 blueflip.png 新 建 演 員 類 別 UnflippedPile, 設 置 圖 像 empty.png, 實 作 Accepter 新 建 演 員 類 別 Pile, 設 置 圖 像 empty.png, 實 作 Accepter 新 建 演 員 類 別 CardStack, 設 置 圖 像 empty.png, 實 作 Accepter 新 建 其 它 類 別 Accepter 編 輯 Accepter 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Accepter here. @author Jean, Hue-Ching Kao @version 2013.3.31 public interface Accepter public void addcard(card card); 編 輯 Pile 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 22

Write a description of class Pile here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class Pile extends Actor implements Accepter public void addcard(card card) 編 輯 CardStack 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class CardStack here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class CardStack extends Actor implements Accepter public void addcard(card card) 編 輯 Card 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Card here. @author Jean, Hue-Ching Kao @version 2013.3.31 23

public class Card extends Actor public enum Suit spades, hearts, clubs, diamonds; private Suit suit; private int value; private boolean candrag=false; private boolean flipped=true; public Card(Suit suit, int value) this.suit = suit; this.value = value; setimage("blueflip.png"); public Suit getsuit() return suit; public int getvalue() return value; public void setcandrag(boolean candrag) this.candrag = candrag; public void setflipped(boolean flipped) if (flipped) setimage("blueflip.png"); else setimage(suit+""+value+".png"); this.flipped = flipped; 24

編 輯 Deck 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; import java.util.collections; Write a description of class Deck here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class Deck extends Actor private ArrayList<Card> cards = new ArrayList<Card>(); public Deck() for (Card.Suit suit: Card.Suit.values()) for (int i=1; i<=13; i++) Card card = new Card(suit, i); cards.add(card); Collections.shuffle(cards); public Card getcard() if (cards.size()>0) Card card = cards.get(0); cards.remove(card); return card; return null; public int getsize() return cards.size(); 編 輯 Pile 類 別 的 程 式 碼 25

import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class Pile here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class Pile extends Actor implements Accepter private ArrayList<Card> cards = new ArrayList<Card>(); public Pile(ArrayList<Card> cards) this.cards = cards; public void addedtoworld(world world) setimage("empty.png"); GreenfootImage image = new GreenfootImage(getImage().getWidth(), getimage().getheight()+200); image.drawimage(new GreenfootImage("empty.png"), 0, 0); setimage(image); for (Card card: cards) world.addobject(card, getx(), gety()+cards.indexof(card)15-100); public void addcard(card card) 編 輯 Table 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class Table here. 26

@author Jean, Hue-Ching Kao @version 2013.3.31 public class Table extends World private Deck deck = new Deck(); private UnflippedPile unflippedpile = new UnflippedPile(); private ArrayList<Pile> piles = new ArrayList<Pile>(); private ArrayList<CardStack> cardstacks = new ArrayList<CardStack>(); Constructor for objects of class Table. public Table() // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addobject(deck, 50, 50); addobject(unflippedpile, 150, 50); for (int i=0; i<7; i++) ArrayList<Card> cards = new ArrayList<Card>(); for (int j=0; j<=i; j++) Card card = deck.getcard(); if (j==i) card.setflipped(false); card.setcandrag(true); cards.add(card); Pile pile = new Pile(cards); addobject(pile, 50+i80, 250); for (int i=0; i<4; i++) CardStack cardstack = new CardStack(); cardstacks.add(cardstack); addobject(cardstack, 250+i100, 50); 27

任 務 一 : 將 Deck 的 牌 發 到 UnflippedPile, 發 完 牌 後, 可 以 再 重 新 發 一 次 為 了 測 試 方 便, 先 將 牌 減 少 數 量 編 輯 Deck 類 別 的 程 式 碼 public Deck() for (Card.Suit suit: Card.Suit.values()) for (int i=1; i<=8; i++) Card card = new Card(suit, i); cards.add(card); Collections.shuffle(cards); public void addcard(card card) cards.add(card); 編 輯 UnflippedPile 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class UnflippedPile here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class UnflippedPile extends Actor private ArrayList<Card> cards = new ArrayList<Card>(); public void addcard(card card) cards.add(card); getworld().addobject(card, getx(), gety()); 28

public void removecard(card card) cards.remove(card); public void removeallcard() for (Card card: cards) getworld().removeobject(card); cards.clear(); public ArrayList<Card> getallcard() return cards; 編 輯 Table 類 別 的 程 式 碼 public void act() if (Greenfoot.mouseClicked(deck)) if (deck.getsize()>0) Card card = deck.getcard(); card.setflipped(false); card.setcandrag(true); unflippedpile.addcard(card); if (deck.getsize()==0) deck.setimage("empty.png"); else for (Card card: unflippedpile.getallcard()) deck.addcard(card); unflippedpile.removeallcard(); deck.setimage("blueflip.png"); 測 試 完 成, 將 牌 的 數 量 設 回 來 29

編 輯 Deck 類 別 的 程 式 碼 public Deck() for (Card.Suit suit: Card.Suit.values()) for (int i=1; i<=13; i++) Card card = new Card(suit, i); cards.add(card); Collections.shuffle(cards); 任 務 二 : 可 以 拖 曳 翻 開 的 撲 克 牌 編 輯 Card 類 別 的 程 式 碼 public class Card extends Actor public enum Suit spades, hearts, clubs, diamonds; private Suit suit; private int value; private boolean candrag=false; private boolean flipped=true; private int initx, inity; private Accepter accepter=null; public Card(Suit suit, int value) this.suit = suit; this.value = value; setimage("blueflip.png"); public Suit getsuit() return suit; public int getvalue() return value; 30

public void setcandrag(boolean candrag) this.candrag = candrag; public void setflipped(boolean flipped) if (flipped) setimage("blueflip.png"); else setimage(suit+""+value+".png"); this.flipped = flipped; public void setaccepter(accepter accepter) this.accepter = accepter; public Accepter getaccepter() return accepter; public void act() if (!candrag) return; MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mousePressed(this)) initx = getx(); inity = gety(); World world = getworld(); world.removeobject(this); world.addobject(this, initx, inity); if (Greenfoot.mouseDragged(this)) setlocation(mouse.getx(), mouse.gety()); 31

if (Greenfoot.mouseDragEnded(this)) setlocation(initx, inity); 編 輯 Pile 類 別 的 程 式 碼 public Pile(ArrayList<Card> cards) this.cards = cards; for (Card card: cards) card.setaccepter(this); 任 務 三 : 可 以 將 撲 克 牌 放 到 牌 疊 (Pile) 或 目 標 牌 疊 (CardStack) 中 牌 疊 (Pile) 點 數 比 上 面 的 牌 少 1 點 就 可 以 放 上 去 ( 花 色 要 不 同 色 ) 目 標 牌 疊 ((CardStack) 點 數 比 上 面 的 牌 多 1 點 就 可 以 放 上 去 ( 花 色 要 一 樣 ) 編 輯 Pile 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class Pile here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class Pile extends Actor implements Accepter private ArrayList<Card> cards = new ArrayList<Card>(); public Pile(ArrayList<Card> cards) this.cards = cards; for (Card card: cards) card.setaccepter(this); 32

public void addedtoworld(world world) setimage("empty.png"); GreenfootImage image = new GreenfootImage(getImage().getWidth(), getimage().getheight()+200); image.drawimage(new GreenfootImage("empty.png"), 0, 0); setimage(image); for (Card card: cards) world.addobject(card, getx(), gety()+cards.indexof(card)15-100); public void addcard(card card) cards.add(card); Accepter accepter = card.getaccepter(); if (accepter!=null) if (accepter instanceof Pile) Pile pile = (Pile) card.getaccepter(); pile.removecard(card); if (accepter instanceof CardStack) CardStack cardstack = (CardStack) card.getaccepter(); cardstack.removecard(card); else Table table = (Table) getworld(); table.getunflippedpile().getallcard().remove(card); card.setlocation(getx(), gety()+(cards.size()-1)15-100); card.setaccepter(this); public void removecard(card card) cards.remove(card); if (cards.size()>0) Card lastcard = cards.get(cards.size()-1); lastcard.setflipped(false); lastcard.setcandrag(true); 33

public boolean isacceptcard(card card) if (cards.size()>0) Card lastcard = cards.get(cards.size()-1); if ((lastcard.getsuit()==card.suit.diamonds lastcard.getsuit()==card.suit.hearts) && (card.getsuit()==card.suit.diamonds card.getsuit()==card.suit.hearts)) return false; if ((lastcard.getsuit()==card.suit.spades lastcard.getsuit()==card.suit.clubs) && (card.getsuit()==card.suit.spades card.getsuit()==card.suit.clubs)) return false; if (lastcard.getvalue()-1==card.getvalue()) return true; else if (card.getvalue()==13) return true; return false; 編 輯 CardStack 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class CardStack here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class CardStack extends Actor implements Accepter private ArrayList<Card> cards = new ArrayList<Card>(); public void addcard(card card) if (card.getaccepter()!=null) Pile pile = (Pile) card.getaccepter(); 34

pile.removecard(card); else Table table = (Table) getworld(); table.getunflippedpile().getallcard().remove(card); cards.add(card); card.setaccepter(this); card.setlocation(getx(), gety()); public void removecard(card card) cards.remove(card); public boolean isacceptcard(card card) if (cards.size()>0) Card lastcard = cards.get(cards.size()-1); if (lastcard.getvalue()+1==card.getvalue()) if (lastcard.getsuit() == card.getsuit()) return true; else if (card.getvalue()==1) return true; return false; 編 輯 Card 類 別 的 程 式 碼 public void act() if (!candrag) return; MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mousePressed(this)) initx = getx(); inity = gety(); 35

World world = getworld(); world.removeobject(this); world.addobject(this, initx, inity); if (Greenfoot.mouseDragged(this)) setlocation(mouse.getx(), mouse.gety()); if (Greenfoot.mouseDragEnded(this)) Pile pile = (Pile) getoneintersectingobject(pile.class); if (pile!= null && pile.isacceptcard(this)) pile.addcard(this); return; CardStack cardstack = (CardStack) getoneintersectingobject(cardstack.class); if (cardstack!= null && cardstack.isacceptcard(this)) cardstack.addcard(this); return; setlocation(initx, inity); 任 務 四 : 如 果 是 從 牌 疊 (Pile) 中 拿 起 撲 克 牌, 則 上 面 的 牌 也 會 跟 著 移 動 編 輯 Pile 類 別 的 程 式 碼 public ArrayList<Card> getcardson(card card) if (cards.contains(card)) ArrayList<Card> cardson = new ArrayList<Card>(); for (int i=cards.indexof(card)+1; i<cards.size(); i++) cardson.add(cards.get(i)); return cardson; return null; 36

編 輯 Card 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class Card here. @author Jean, Hue-Ching Kao @version 2013.3.31 public class Card extends Actor public void act() if (!candrag) return; MouseInfo mouse = Greenfoot.getMouseInfo(); ArrayList<Card> cards = new ArrayList<Card>(); Accepter accepter = getaccepter(); if (accepter!= null) if (accepter instanceof Pile) Pile pile = (Pile) getaccepter(); cards = pile.getcardson(this); if (Greenfoot.mousePressed(this)) initx = getx(); inity = gety(); World world = getworld(); world.removeobject(this); world.addobject(this, initx, inity); for (Card card: cards) world.removeobject(card); world.addobject(card, initx, inity+(cards.indexof(card)+1)15); if (Greenfoot.mouseDragged(this)) 37

setlocation(mouse.getx(), mouse.gety()); for (Card card: cards) card.setlocation(mouse.getx(), mouse.gety()+(cards.indexof(card)+1)15); if (Greenfoot.mouseDragEnded(this)) Pile pile = (Pile) getoneintersectingobject(pile.class); if (pile!= null && pile.isacceptcard(this)) pile.addcard(this); for (Card card: cards) pile.addcard(card); return; CardStack cardstack = (CardStack) getoneintersectingobject(cardstack.class); if (cardstack!= null && cardstack.isacceptcard(this)) cardstack.addcard(this); return; setlocation(initx, inity); for (Card card: cards) card.setlocation(initx, inity+(cards.indexof(card)+1)15); 38

Lesson 4: MineSweep 踩 地 雷 玩 法 掀 開 一 個 地 雷, 遊 戲 便 結 束 掀 開 一 個 空 的 方 塊, 您 可 以 繼 續 玩 掀 開 一 個 號 碼, 表 示 在 周 圍 八 個 方 塊 內 隱 藏 多 少 個 地 雷, 這 可 用 來 推 論 附 近 的 方 塊 是 否 安 全 提 示 和 秘 訣 如 果 確 定 某 方 塊 有 地 雷, 請 在 該 方 塊 上 按 一 下 滑 鼠 右 鍵 該 方 塊 會 出 現 一 個 旗 標 製 作 流 程 任 務 一 : 產 生 30X20 個 磚 塊 任 務 二 : 使 用 ArrayList 來 儲 存 磚 塊 任 務 三 : 產 生 20 個 有 炸 彈 的 磚 塊, 放 在 隨 機 的 位 置 任 務 四 : 按 下 滑 鼠 右 鍵, 可 以 插 旗 子 或 取 消 插 旗 子 任 務 五 : 按 下 滑 鼠 左 鍵, 可 以 打 開 磚 塊 如 果 是 炸 彈, 則 顯 示 炸 彈, 如 果 不 是 炸 彈, 則 顯 示 此 磚 塊 週 邊 有 幾 個 炸 彈 任 務 六 : 打 開 後 的 磚 塊 不 能 再 插 旗 子 任 務 七 : 若 是 開 到 炸 彈, 則 顯 示 所 有 炸 彈, 然 後 遊 戲 結 束 任 務 八 : 若 是 該 磚 塊 周 圍 的 磚 塊 的 炸 彈 數 是 零, 則 再 展 開 該 磚 塊 ( 遞 迴 ) 任 務 九 : 檢 查 20 個 炸 彈 磚 塊 是 否 都 被 標 上 旗 子, 若 是, 則 播 放 音 樂, 遊 戲 停 止 39

任 務 一 : 產 生 30X20 個 磚 塊 編 輯 Table 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class MyWorld here. @author Jean, Hue-Ching Kao @version 2014.2.3 public class MyWorld extends World Constructor for objects of class MyWorld. public MyWorld() // Create a new world with 32x22 cells with a cell size of 25x25 pixels. super(32, 22, 25); for (int i=1; i<31; i++) for (int j=1; j<21; j++) addobject(new Block(), i, j); 任 務 二 : 使 用 ArrayList 來 儲 存 磚 塊 修 改 Table 的 程 式 碼 如 下 : import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class MyWorld here. 40

@author Jean, Hue-Ching Kao @version 2014.2.3 public class MyWorld extends World private ArrayList<Block> blocks = new ArrayList<Block>(); Constructor for objects of class MyWorld. public MyWorld() // Create a new world with 32x22 cells with a cell size of 25x25 pixels. super(32, 22, 25); for (int i=1; i<31; i++) for (int j=1; j<21; j++) Block block = new Block(); blocks.add(block); addobject(block, i, j); 任 務 三 : 產 生 20 個 有 炸 彈 的 磚 塊, 放 在 隨 機 的 位 置 編 輯 Block 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Block here. @author Jean, Hue-Ching Kao @version 2013.4.3 public class Block extends Actor private boolean isbomb=false; 41

public void setbomb() isbomb = true; public boolean getbomb() return isbomb; Act - do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. 編 輯 MyWorld 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; import java.util.collections; Write a description of class MyWorld here. @author Jean, Hue-Ching Kao @version 2014.2.3 public class MyWorld extends World private ArrayList<Block> blocks = new ArrayList<Block>(); Constructor for objects of class MyWorld. 42

public MyWorld() // Create a new world with 32x22 cells with a cell size of 25x25 pixels. super(32, 22, 25); for (int i=1; i<31; i++) for (int j=1; j<21; j++) Block block = new Block(); blocks.add(block); addobject(block, i, j); Colletions.shuffle(blocks); for (int i=0; i<20; i++) blocks.get(i).setbomb(); 任 務 四 : 按 下 滑 鼠 右 鍵, 可 以 插 旗 子 或 取 消 插 旗 子 編 輯 Block 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Block here. @author (your name) @version (a version number or a date) public class Block extends Actor private boolean isbomb=false; private boolean isflagged = false; public boolean getflag() return isflagged; public void setbomb() 43

isbomb = true; public boolean getbomb() return isbomb; Act - do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3) if (!isflagged) setimage("blockflagged.png"); isflagged=true; else setimage("block.png"); isflagged=false; 任 務 五 : 按 下 滑 鼠 左 鍵, 可 以 打 開 磚 塊 如 果 是 炸 彈, 則 顯 示 炸 彈, 如 果 不 是 炸 彈, 則 顯 示 此 磚 塊 週 邊 有 幾 個 炸 彈 編 輯 Block 類 別 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; Write a description of class Block here. @author (your name) 44

@version (a version number or a date) public class Block extends Actor private boolean isbomb=false; private boolean isflagged = false; private boolean isopen=false; public boolean getflag() return isflagged; public void setbomb() isbomb = true; public boolean getbomb() return isbomb; Act - do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3) if (!isflagged) setimage("blockflagged.png"); isflagged=true; else setimage("block.png"); isflagged=false; 45

if (!isflagged) if (isbomb) setimage("bomb.png"); Greenfoot.playSound("bomb.wav"); else isopen=true; setimage("blockclicked["+getbombnumber(this)+"].png"); private int getbombnumber(block block) int bombnumber=0; List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock: blocks) if (nblock.getbomb()) bombnumber++; return bombnumber; 任 務 六 : 打 開 後 的 磚 塊 不 能 再 插 旗 子 修 改 Block 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; Write a description of class Block here. @author (your name) @version (a version number or a date) public class Block extends Actor private boolean isbomb=false; 46

private boolean isflagged = false; private boolean isopen=false; public boolean getflag() return isflagged; public void setbomb() isbomb = true; public boolean getbomb() return isbomb; Act - do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3 &&!isopen) if (!isflagged) setimage("blockflagged.png"); isflagged=true; else setimage("block.png"); isflagged=false; return; if (!isflagged) if (isbomb) setimage("bomb.png"); 47

Greenfoot.playSound("bomb.wav"); else isopen=true; setimage("blockclicked["+getbombnumber(this)+"].png"); private int getbombnumber(block block) int bombnumber=0; List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock: blocks) if (nblock.getbomb()) bombnumber++; return bombnumber; 任 務 七 : 若 是 開 到 炸 彈, 則 顯 示 所 有 炸 彈, 然 後 遊 戲 結 束 修 改 Block 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; Write a description of class Block here. @author (your name) @version (a version number or a date) public class Block extends Actor private boolean isbomb=false; private boolean isflagged = false; private boolean isopen=false; public boolean getflag() 48

return isflagged; public void setbomb() isbomb = true; public boolean getbomb() return isbomb; Act - do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3 &&!isopen) if (!isflagged) setimage("blockflagged.png"); isflagged=true; else setimage("block.png"); isflagged=false; return; if (!isflagged) if (isbomb) MyWorld world = (MyWorld) getworld(); world.showallbomb(); Greenfoot.playSound("bomb.wav"); Greenfoot.stop(); 49

else isopen=true; setimage("blockclicked["+getbombnumber(this)+"].png"); private int getbombnumber(block block) int bombnumber=0; List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock: blocks) if (nblock.getbomb()) bombnumber++; return bombnumber; 編 輯 MyWorld 類 別 的 程 式 碼 public void showallbomb() for (int i=0; i<20; i++) blocks.get(i).setimage("bomb.png"); 任 務 八 : 若 是 該 磚 塊 周 圍 的 磚 塊 的 炸 彈 數 是 零, 則 再 展 開 該 磚 塊 ( 遞 迴 ) 修 改 Block 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; Write a description of class Block here. @author (your name) @version (a version number or a date) public class Block extends Actor 50

private boolean isbomb=false; private boolean isflagged = false; private boolean isopen=false; public boolean getflag() return isflagged; public void setbomb() isbomb = true; public boolean getbomb() return isbomb; Act - do whatever the Block wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) if (mouse.getbutton()==3 &&!isopen) if (!isflagged) setimage("blockflagged.png"); isflagged=true; else setimage("block.png"); isflagged=false; return; 51

if (!isflagged) if (isbomb) MyWorld world = (MyWorld) getworld(); world.showallbomb(); Greenfoot.playSound("bomb.wav"); Greenfoot.stop(); else openblock(this); private void openblock(block block) block.isopen=true; block.setimage("blockclicked["+block.getbombnumber(block)+"].png"); if (block.getbombnumber(block)==0) List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock: blocks) if (!nblock.isopen) openblock(nblock); private int getbombnumber(block block) int bombnumber=0; List<Block> blocks = block.getneighbours(1, true, Block.class); for (Block nblock: blocks) if (nblock.getbomb()) bombnumber++; return bombnumber; 52

任 務 九 : 檢 查 20 個 炸 彈 磚 塊 是 否 都 被 標 上 旗 子, 若 是, 則 播 放 音 樂, 遊 戲 停 止 修 改 MyWorld 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; import java.util.collections; Write a description of class MyWorld here. @author Jean, Hue-Ching Kao @version 2014.2.3 public class MyWorld extends World private ArrayList<Block> blocks = new ArrayList<Block>(); Constructor for objects of class MyWorld. public MyWorld() // Create a new world with 32x22 cells with a cell size of 25x25 pixels. super(32, 22, 25); for (int i=1; i<31; i++) for (int j=1; j<21; j++) Block block = new Block(); blocks.add(block); addobject(block, i, j); Collections.shuffle(blocks); for (int i=0; i<20; i++) blocks.get(i).setbomb(); 53

public void showallbomb() for (int i=0; i<20; i++) blocks.get(i).setimage("bomb.png"); public void act() for (int i=0; i<20; i++) if (!blocks.get(i).getflag()) return; Greenfoot.playSound("win.wav"); Greenfoot.stop(); 54

Lesson 5: Reversi 黑 白 棋 玩 法 持 黑 子 方 先 攻, 黑 白 雙 方 輪 流 下 子 圍 住 對 方 連 續 的 棋 子 兩 端 時, 被 圍 住 的 棋 子 會 翻 轉 成 我 方 棋 子 ( 直 橫 斜 向 均 可 ) 棋 子 只 能 下 在 可 以 吃 子 的 地 方 當 棋 盤 下 滿 或 無 法 出 手 時, 依 照 雙 方 棋 子 數 多 寡 判 定 勝 負 製 作 流 程 任 務 一 : 產 生 一 個 陣 列 來 儲 存 盤 面 資 訊 黑 :0, 白 :1, 空 :-1 任 務 二 : 產 生 起 始 盤 面 棋 子, 及 下 一 步 的 棋 子 標 示 任 務 三 : 在 空 白 處 可 以 下 棋, 黑 白 棋 輪 流 下 任 務 四 : 顯 示 目 前 的 雙 方 棋 子 數 目 任 務 五 : 檢 查 上 方 是 否 可 以 下 棋 子 任 務 六 : 檢 查 各 方 是 否 可 以 下 棋 子 ( 下 左 右 左 上 左 下 右 上 右 下 ) 55

任 務 一 : 產 生 一 個 陣 列 來 儲 存 盤 面 資 訊 黑 :0, 白 :1, 空 :-1 編 輯 Table 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private int[][] value = new int[8][8]; Constructor for objects of class Table. public Table() // Create a new world with 11x8 cells with a cell size of 50x50 pixels. super(11, 8, 50); for (int i=0; i<8; i++) for (int j=0; j<8; j++) value[i][j] = -1; 任 務 二 : 產 生 起 始 盤 面 棋 子, 及 下 一 步 的 棋 子 標 示 編 輯 Chess 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Chess here. 56

@author (your name) @version (a version number or a date) public class Chess extends Actor public Chess(boolean color) if (color) setimage("white.png"); else setimage("black.png"); Act - do whatever the Chess wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. public void act() // Add your action code here. 編 輯 Table 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private int[][] value = new int[8][8]; private Sign sign; Constructor for objects of class Table. 57

public Table() // Create a new world with 11x8 cells with a cell size of 50x50 pixels. super(11, 8, 50); for (int i=0; i<8; i++) for (int j=0; j<8; j++) value[i][j] = -1; sign = new Sign(); addobject(sign, 9, 6); value[3][3] = 1; value[4][3] = 0; value[3][4] = 0; value[4][4] = 1; addobject(new Chess(true), 3, 3); addobject(new Chess(false), 4, 3); addobject(new Chess(false), 3, 4); addobject(new Chess(true), 4, 4); 任 務 三 : 在 空 白 處 可 以 下 棋, 黑 白 棋 輪 流 下 編 輯 Table 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private int[][] value = new int[8][8]; 58

private Sign sign; private boolean color=false; Constructor for objects of class Table. public Table() // Create a new world with 11x8 cells with a cell size of 50x50 pixels. super(11, 8, 50); for (int i=0; i<8; i++) for (int j=0; j<8; j++) value[i][j] = -1; sign = new Sign(); addobject(sign, 9, 6); value[3][3] = 1; value[4][3] = 0; value[3][4] = 0; value[4][4] = 1; addobject(new Chess(true), 3, 3); addobject(new Chess(false), 4, 3); addobject(new Chess(false), 3, 4); addobject(new Chess(true), 4, 4); public void act() MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) int x = mouse.getx(); int y = mouse.gety(); if (value[x][y]==-1) if (color) value[x][y] =1; addobject(new Chess(true), x, y); sign.setimage("black.png"); 59

else value[x][y] =0; addobject(new Chess(false), x, y); sign.setimage("white.png"); color =!color; 任 務 四 : 顯 示 目 前 的 雙 方 棋 子 數 目 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; import java.awt.font; Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private int[][] value = new int[8][8]; private Sign sign; private boolean color=false; private Counter counterblack, counterwhite; Constructor for objects of class Table. public Table() // Create a new world with 11x8 cells with a cell size of 50x50 pixels. 60

super(11, 8, 50); for (int i=0; i<8; i++) for (int j=0; j<8; j++) value[i][j] = -1; sign = new Sign(); addobject(sign, 9, 6); value[3][3] = 1; value[4][3] = 0; value[3][4] = 0; value[4][4] = 1; addobject(new Chess(true), 3, 3); addobject(new Chess(false), 4, 3); addobject(new Chess(false), 3, 4); addobject(new Chess(true), 4, 4); // 顯 示 棋 子 數 目 Font font = new Font("Arial", Font.PLAIN, 30); counterblack = new Counter(); counterblack.getimage().setfont(font); counterblack.getimage().drawstring(""+2, 5, 25); addobject(counterblack, 9, 1); counterwhite = new Counter(); counterwhite.getimage().setfont(font); counterwhite.getimage().drawstring(""+2, 5, 25); addobject(counterwhite, 9, 2); public void act() MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) int x = mouse.getx(); int y = mouse.gety(); if (x>=8) return; if (value[x][y]==-1) if (color) value[x][y] =1; 61

addobject(new Chess(true), x, y); sign.setimage("black.png"); else value[x][y] =0; addobject(new Chess(false), x, y); sign.setimage("white.png"); color =!color; // 計 算 棋 子 數 目 int numberwhite=0; int numberblack=0; for (int i=0; i<8; i++) for (int j=0; j<8; j++) if (value[i][j]==0) numberblack++; if (value[i][j]==1) numberwhite++; counterwhite.getimage().clear(); counterwhite.getimage().drawstring(""+numberwhite, 5, 25); counterblack.getimage().clear(); counterblack.getimage().drawstring(""+numberblack, 5, 25); 任 務 五 : 檢 查 上 方 是 否 可 以 下 棋 子 編 輯 Table 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; import java.awt.font; Write a description of class Table here. @author (your name) @version (a version number or a date) 62

public class Table extends World private int[][] value = new int[8][8]; private Sign sign; private boolean color=false; private Counter counterblack, counterwhite; Constructor for objects of class Table. public Table() // Create a new world with 11x8 cells with a cell size of 50x50 pixels. super(11, 8, 50); for (int i=0; i<8; i++) for (int j=0; j<8; j++) value[i][j] = -1; sign = new Sign(); addobject(sign, 9, 6); value[3][3] = 1; value[4][3] = 0; value[3][4] = 0; value[4][4] = 1; addobject(new Chess(true), 3, 3); addobject(new Chess(false), 4, 3); addobject(new Chess(false), 3, 4); addobject(new Chess(true), 4, 4); // 顯 示 棋 子 數 目 Font font = new Font("Arial", Font.PLAIN, 30); counterblack = new Counter(); counterblack.getimage().setfont(font); counterblack.getimage().drawstring(""+2, 5, 25); addobject(counterblack, 9, 1); counterwhite = new Counter(); 63

counterwhite.getimage().setfont(font); counterwhite.getimage().drawstring(""+2, 5, 25); addobject(counterwhite, 9, 2); public void act() MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) int x = mouse.getx(); int y = mouse.gety(); if (x>=8) return; if (value[x][y]==-1 && checkvalid(x, y)) if (color) value[x][y] =1; addobject(new Chess(true), x, y); sign.setimage("black.png"); else value[x][y] =0; addobject(new Chess(false), x, y); sign.setimage("white.png"); color =!color; // 計 算 棋 子 數 目 int numberwhite=0; int numberblack=0; for (int i=0; i<8; i++) for (int j=0; j<8; j++) if (value[i][j]==0) numberblack++; if (value[i][j]==1) numberwhite++; counterwhite.getimage().clear(); counterwhite.getimage().drawstring(""+numberwhite, 5, 25); counterblack.getimage().clear(); counterblack.getimage().drawstring(""+numberblack, 5, 25); 64

public boolean checkvalid(int x, int y) int chessvalue; boolean valid = false; if (color) chessvalue = 1; else chessvalue = 0; // 檢 查 上 方 // 判 斷 上 方 是 否 到 達 邊 界 y-1>0 // 判 斷 上 方 是 否 有 子 value[x][y-1]>-1 // 判 斷 上 方 棋 子 是 否 為 對 方 value[x][y-1]!=chessvalue if (y-1>0 && value[x][y-1]>-1 && value[x][y-1]!=chessvalue) // 往 上 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (y-c>0) if (value[x][y-c]==chessvalue) valid = true; for (int i=1; i<c; i++) eatchess(x, y-i); break; else c++; return valid; public void eatchess(int x, int y) List<Chess> chess = getobjectsat(x, y, Chess.class); if (color) value[x][y] = 1; chess.get(0).setimage("whilte.png"); 65

else value[x][y] = 0; chess.get(0).setimage("black.png"); 任 務 六 : 檢 查 各 方 是 否 可 以 下 棋 子 ( 下 左 右 左 上 左 下 右 上 右 下 ) 編 輯 Table 的 程 式 碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.list; import java.awt.font; Write a description of class Table here. @author (your name) @version (a version number or a date) public class Table extends World private int[][] value = new int[8][8]; private Sign sign; private boolean color=false; private Counter counterblack, counterwhite; Constructor for objects of class Table. public Table() // Create a new world with 11x8 cells with a cell size of 50x50 pixels. super(11, 8, 50); for (int i=0; i<8; i++) for (int j=0; j<8; j++) value[i][j] = -1; 66

sign = new Sign(); addobject(sign, 9, 6); value[3][3] = 1; value[4][3] = 0; value[3][4] = 0; value[4][4] = 1; addobject(new Chess(true), 3, 3); addobject(new Chess(false), 4, 3); addobject(new Chess(false), 3, 4); addobject(new Chess(true), 4, 4); // 顯 示 棋 子 數 目 Font font = new Font("Arial", Font.PLAIN, 30); counterblack = new Counter(); counterblack.getimage().setfont(font); counterblack.getimage().drawstring(""+2, 5, 25); addobject(counterblack, 9, 1); counterwhite = new Counter(); counterwhite.getimage().setfont(font); counterwhite.getimage().drawstring(""+2, 5, 25); addobject(counterwhite, 9, 2); public void act() MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked(this)) int x = mouse.getx(); int y = mouse.gety(); if (x>=8) return; if (value[x][y]==-1 && checkvalid(x, y)) if (color) value[x][y] =1; addobject(new Chess(true), x, y); sign.setimage("black.png"); else value[x][y] =0; 67

addobject(new Chess(false), x, y); sign.setimage("white.png"); color =!color; // 計 算 棋 子 數 目 int numberwhite=0; int numberblack=0; for (int i=0; i<8; i++) for (int j=0; j<8; j++) if (value[i][j]==0) numberblack++; if (value[i][j]==1) numberwhite++; counterwhite.getimage().clear(); counterwhite.getimage().drawstring(""+numberwhite, 5, 25); counterblack.getimage().clear(); counterblack.getimage().drawstring(""+numberblack, 5, 25); public boolean checkvalid(int x, int y) int chessvalue; boolean valid = false; if (color) chessvalue = 1; else chessvalue = 0; // 檢 查 上 方 // 判 斷 上 方 是 否 到 達 邊 界 y-1>0 // 判 斷 上 方 是 否 有 子 value[x][y-1]>-1 // 判 斷 上 方 棋 子 是 否 為 對 方 value[x][y-1]!=chessvalue if (y-1>0 && value[x][y-1]>-1 && value[x][y-1]!=chessvalue) // 往 上 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (y-c>0) if (value[x][y-c]==chessvalue) 68

valid = true; for (int i=1; i<c; i++) eatchess(x, y-i); break; else c++; // 檢 查 下 方 // 判 斷 下 方 是 否 到 達 邊 界 y+1<8 // 判 斷 下 方 是 否 有 子 value[x][y+1]>-1 // 判 斷 下 方 棋 子 是 否 為 對 方 value[x][y+1]!=chessvalue if (y+1<8 && value[x][y+1]>-1 && value[x][y+1]!=chessvalue) // 往 下 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (y+c<8) if (value[x][y+c]==chessvalue) valid = true; for (int i=1; i<c; i++) eatchess(x, y+i); break; else c++; // 檢 查 左 方 // 判 斷 左 方 是 否 到 達 邊 界 x-1>0 // 判 斷 左 方 是 否 有 子 value[x-1][y]>-1 // 判 斷 左 方 棋 子 是 否 為 對 方 value[x-1][y]!=chessvalue if (x-1>0 && value[x-1][y]>-1 && value[x-1][y]!=chessvalue) // 往 左 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (x-c>0) if (value[x-c][y]==chessvalue) 69

valid = true; for (int i=1; i<c; i++) eatchess(x-i, y); break; else c++; // 檢 查 右 方 // 判 斷 右 方 是 否 到 達 邊 界 x+1<8 // 判 斷 右 方 是 否 有 子 value[x+1][y]>-1 // 判 斷 右 方 棋 子 是 否 為 對 方 value[x+1][y]!=chessvalue if (x+1<8 && value[x+1][y]>-1 && value[x+1][y]!=chessvalue) // 往 右 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (x+c<8) if (value[x+c][y]==chessvalue) valid = true; for (int i=1; i<c; i++) eatchess(x+i, y); break; else c++; // 檢 查 左 上 方 // 判 斷 左 上 方 是 否 到 達 邊 界 x-1>0, y-1>0 // 判 斷 左 上 方 是 否 有 子 value[x-1][y-1]>-1 // 判 斷 左 上 方 棋 子 是 否 為 對 方 value[x-1][y-1]!=chessvalue if (x-1>0 && y-1>0 && value[x-1][y-1]>-1 && value[x-1][y-1]!=chessvalue) // 往 左 上 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (y-c>0) if (value[x-c][y-c]==chessvalue) 70

valid = true; for (int i=1; i<c; i++) eatchess(x-i, y-i); break; else c++; // 檢 查 左 下 方 // 判 斷 左 下 方 是 否 到 達 邊 界 x-1>0,y+1<8 // 判 斷 左 下 方 是 否 有 子 value[x-1][y+1]>-1 // 判 斷 左 下 方 棋 子 是 否 為 對 方 value[x-1][y+1]!=chessvalue if (x-1>0 && y+1<8 && value[x-1][y+1]>-1 && value[x-1][y+1]!=chessvalue) // 往 左 下 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (x-c>0 && y+c<8) if (value[x-c][y+c]==chessvalue) valid = true; for (int i=1; i<c; i++) eatchess(x-i, y+i); break; else c++; // 檢 查 右 上 方 // 判 斷 右 上 方 是 否 到 達 邊 界 x+1<8, y-1>0 // 判 斷 右 上 方 是 否 有 子 value[x+1][y-1]>-1 // 判 斷 右 上 方 棋 子 是 否 為 對 方 value[x+1][y-1]!=chessvalue if (x+1<8 && y-1>0 && value[x+1][y-1]>-1 && value[x+1][y-1]!=chessvalue) // 往 右 上 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; 71

while (x+c<8 && y-1>0) if (value[x+c][y-1]==chessvalue) valid = true; for (int i=1; i<c; i++) eatchess(x+i, y-i); break; else c++; // 檢 查 右 下 方 // 判 斷 右 下 方 是 否 到 達 邊 界 x+1<8, y+1<8 // 判 斷 右 下 方 是 否 有 子 value[x+1][y+1]>-1 // 判 斷 右 下 方 棋 子 是 否 為 對 方 value[x+1][y+1]!=chessvalue if (x+1<8 && y+1<8 && value[x+1][y+1]>-1 && value[x+1][y+1]!=chessvalue) // 往 右 下 檢 查 是 否 有 我 方 棋 子 圍 住, 若 有, 則 吃 子 並 下 棋 int c = 2; while (x+c<8 && y+c<8) if (value[x+c][y+c]==chessvalue) valid = true; for (int i=1; i<c; i++) eatchess(x+i, y+i); break; else c++; return valid; public void eatchess(int x, int y) List<Chess> chess = getobjectsat(x, y, Chess.class); 72

if (color) value[x][y] = 1; chess.get(0).setimage("white.png"); else value[x][y] = 0; chess.get(0).setimage("black.png"); 73

Lesson 6: Match 對 對 碰 連 續 點 選 同 樣 圖 案 的 撲 克 牌 即 可, 考 驗 你 的 記 憶 力 翻 牌 次 數 越 少 越 好 任 務 一 : 準 備 撲 克 牌 物 件, 可 以 儲 存 撲 克 牌 的 點 數 任 務 二 : 產 生 八 張 撲 克 牌, 點 數 為 1,1,2,2,3,3,4,4, 打 亂 排 成 二 排, 每 排 四 張 任 務 三 : 點 選 撲 克 牌, 可 以 將 其 翻 開 任 務 四 : 計 算 翻 牌 次 數 任 務 五 : 記 住 第 一 次 的 牌, 若 與 第 二 次 翻 的 牌 一 樣, 則 播 放 correct.wav, 若 不 一 樣, 則 播 放 fail.wav, 等 待 一 下 後 再 將 二 張 牌 蓋 住 任 務 六 : 若 四 對 牌 都 翻 開, 則 播 放 win.wav, 然 後 遊 戲 結 束 74