最新天津科技大學Java課程設計_第1頁
最新天津科技大學Java課程設計_第2頁
最新天津科技大學Java課程設計_第3頁
免費預覽已結束,剩余9頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、高級語言程序設計期末作業題目五子棋學 號:王琪姓 名: 11103333 指導老師:程嵐嵐 時間: 2013年 12月 16 一、開發工具: MyEclipse二、程序的功能介紹 系統實現五子棋人人對戰。三、系統分析與設計(一)五子棋基本規則1. 五子棋行棋時, 黑棋先下第一子, 由天元開始, 后白棋在黑棋周圍的交叉點 的落子。接著黑方再以天元中心的 25 個交叉點的范圍內落盤面的第三子,之后 黑白雙方相互順序子。2. 最先在棋盤線交點橫向,縱向,斜向形成已方的五個棋子連續的一方為勝。3. 出現禁手對方指出后將判負, 其中包括兩個或兩個以上的活三, 活四, 長連 均稱之為禁手。4. 禁手只對黑

2、棋有效,白棋無禁手。5. 黑方禁手形成時, 白方需立即指出, 黑方將被判負。 若白方未發現黑方禁手 存在而繼續應子,其后指出黑方禁手不能判黑方負。6. 黑方連五與禁手同時形成, 因黑方已連成五即黑方已獲勝, 故禁手規則失效。7. 在對局中,在盤上落下的子又拿起來, 此動作稱為拔子, 若拔子將被判為負。8. 在對局中棋子掉落在棋盤上將被判負。 若推子或蹭子,以盤面第一落點為準。 用手將棋子推正不算違犯規則。9. 在對局中,一方自行中止比賽如:中途退場,將被判負。10. 在對局中對方宣布認輸,本局獲勝。11. 超過比賽規定所用時間限制,將被判為負:正式比賽期間,遲到時間超過 比賽容許時間將被判負。

3、12. 如下至最終一子仍不分勝負則定為平局。13. 中盤期間雙方 同意和局提議,判定為平局(二)( 1)1. 本款游戲有“游戲開始”和“重置游戲”和“黑白哪個子先行”三 個選項。2. 繪制棋盤, 14 條橫線, 14 條豎線,在直線交點處下棋子(實心黑白圓形) 。3.棋盤處于鼠標監聽狀態,當鼠標在棋盤上有點擊操作的時候,程序會獲得鼠標點擊的坐標然后換算成對應的棋盤的位置,再判斷此處是否有棋子,假如沒有, 那么在此處畫出對應顏色的實心棋子, 假如已經有棋子了,則提示玩家此處已經 有棋子請重新下棋。四、系統功能實現及部分核心代碼(1)系統功能實現1游戲開始界面選擇哪種子先開始2.游戲開始進行3游戲

4、結束( 2)public class GoBang extends Applet implements ActionListener, MouseListener, MouseMotionListener, ItemListener int color = 0;/ 旗子的顏色標識 0:白子 1:黑子boolean isStart = false;/ 游戲開始標志int bodyArray = new int1616; /設置棋盤棋子狀態 0 無子 1 白子 2 黑子Button b1 = new Button(" 游戲開始 ");Button b2 = new Button

5、(" 重置游戲 ");Label lblWin = new Label(" ");Checkbox ckbHB = new Checkbox2;CheckboxGroup ckgHB = new CheckboxGroup();public void init() setLayout(null);addMouseListener(this);add(b1);b1.setBounds(330, 50, 80, 30);b1.addActionListener(this);add(b2);b2.setBounds(330, 90, 80, 30); b2.a

6、ddActionListener(this);ckbHB0 = new Checkbox(" 白子先 ", ckgHB, false); ckbHB0.setBounds(320, 20, 60, 30);ckbHB1 = new Checkbox(" 黑子先 ", ckgHB, false); ckbHB1.setBounds(380, 20, 60, 30);add(ckbHB0);add(ckbHB1); ckbHB0.addItemListener(this);ckbHB1.addItemListener(this); add(lblWin);l

7、blWin.setBounds(330, 130, 80, 30);gameInit();this.resize(new Dimension(450,350);public void itemStateChanged(ItemEvent e) if (ckbHB0.getState() / 選擇黑子先還是白子先color = 0; else color= 1;public void actionPerformed(ActionEvent e) if (e.getSource() = b1) gameStart(); else reStart();public void mousePressed

8、(MouseEvent e) public void mouseClicked(MouseEvent e) int x1, y1;x1 = e.getX();y1 = e.getY();if (e.getX() < 20 | e.getX() > 300 | e.getY() < 20 | e.getY() > 300) return;if (x1 % 20 > 10) x1 += 20;if (y1 % 20 > 10) y1 += 20;x1 = x1 / 20 * 20;y1 = y1 / 20 * 20;setDown(x1, y1);public

9、void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) public void paint(Graphics g) g.setColor(Color.lightGray); g.fill3DRect(10, 10, 300, 300, true); g.setColor(Color.

10、black);for (int i = 1; i < 16; i+) g.drawLine(20, 20 * i, 300, 20 * i);g.drawLine(20 * i, 20, 20 * i, 300);public void setDown(int x, int y) / 落子if (!isStart) / 判斷游戲未開始return;if (bodyArrayx / 20y / 20 != 0) return;Graphics g = getGraphics();if (color = 1)/ 判斷黑子還是白子g.setColor(Color.black);color =

11、0; else g.setColor(Color.white);color = 1;g.fillOval(x - 10, y - 10, 20, 20);bodyArrayx / 20y / 20 = color + 1;if (gameWin1(x / 20, y / 20) / 判斷輸贏lblWin.setText(startColor(color) + " 贏了 !"); isStart = false;if (gameWin2(x / 20, y / 20) / 判斷輸贏lblWin.setText(startColor(color) + " 贏了 !&q

12、uot;);isStart = false;if (gameWin3(x / 20, y / 20) / 判斷輸贏lblWin.setText(startColor(color) + " 贏了 !");isStart = false;if (gameWin4(x / 20, y / 20) / 判斷輸贏lblWin.setText(startColor(color) + " 贏了 !");isStart = false;public String startColor(int x) if (x = 0) return " 黑子 " e

13、lse return " 白子 "public void gameStart() / 游戲開始isStart = true; enableGame(false); b2.setEnabled(true);public void gameInit() / 游戲開始初始化isStart = false; enableGame(true); b2.setEnabled(false); ckbHB0.setState(true);for (int i = 0; i < 16; i+) for (int j = 0; j < 16; j+) bodyArrayij = 0

14、;lblWin.setText("");public void reStart() / 游戲重新開始repaint(); gameInit();public void enableGame(boolean e) / 設置組件狀態b1.setEnabled(e);b2.setEnabled(e);ckbHB0.setEnabled(e);ckbHB1.setEnabled(e);public boolean gameWin1(int x, int y) / 判斷輸贏 橫 int x1, y1, t = 1;x1 = x;y1 = y;for (int i = 1; i <

15、; 5; i+) if (x1 > 15) break;if (bodyArrayx1 + iy1 = bodyArrayxy) t += 1; else break;for (int i = 1; i < 5; i+) if (x1 < 1) break;if (bodyArrayx1 - iy1 = bodyArrayxy) t += 1; else break;if (t > 4) return true; else return false;public boolean gameWin2(int x, int y) / 判斷輸贏 豎 int x1, y1, t

16、= 1;x1 = x;y1 = y;for (int i = 1; i < 5; i+) if (x1 > 15) break;if (bodyArrayx1y1 + i = bodyArrayxy) t += 1; else break;for (int i = 1; i < 5; i+) if (x1 < 1) break;if (bodyArrayx1y1 - i = bodyArrayxy) t += 1; else break;if (t > 4) return true; else return false;public boolean gameWin

17、3(int x, int y) / 判斷輸贏 左斜 int x1, y1, t = 1;x1 = x;y1 = y;for (int i = 1; i < 5; i+) if (x1 > 15) break;if (bodyArrayx1 + iy1 - i = bodyArrayxy) t += 1; else break;for (int i = 1; i < 5; i+) if (x1 < 1) break;if (bodyArrayx1 - iy1 + i = bodyArrayxy) t += 1; else break;if (t > 4) return true; else return false;public boolean gameWin4(int x, int y) / 判斷輸贏 左斜 int x1, y1, t = 1;x1 = x;y1 = y;for (int i = 1; i < 5; i+) if (x1 > 15) break;if (bodyArrayx1 + iy1 + i = bodyArrayxy) t += 1; else break;for

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論