全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2149
推到 Plurk!
推到 Facebook!

各位java高手救就命呀有誰會黑白棋

尚未結案
0913885588
一般會員


發表:3
回覆:0
積分:0
註冊:2004-04-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-24 14:32:35 IP:210.60.xxx.xxx 未訂閱
需要有下列功能:請下列程式中要在那一段加入呀 雙人模式 需有輸入姓名 並顯數輪到誰下 import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; //匯入想關圖像及音效 public class Reversi extends Applet implements MouseListener { int totaldisc1 = 0; int totaldisc2 = 0; private int grid[][]; private boolean blnStart, blnPlayerMove, blnWhite, blnLastWhite, blnGameOver; java.awt.Label label; private Image imgDisc1, imgDisc2; private AudioClip a_click,a_start,a_win; private String name1, name2 ; public Reversi() { grid = new int[8][8]; } public void init() { String disc1 = null ; String disc2 = null ; try { disc1 = getParameter("disc1"); disc2 = getParameter("disc2"); name1 = getParameter("name1"); name2 = getParameter("name2"); } catch(Exception e) { e.printStackTrace(); } ClassLoader cl = this.getClass().getClassLoader(); Toolkit tk = Toolkit.getDefaultToolkit(); MediaTracker mt = new MediaTracker(this); imgDisc1 = tk.createImage(cl.getResource("images/" disc1)); if (imgDisc1 != null) mt.addImage(imgDisc1, 0); imgDisc2 = tk.createImage(cl.getResource("images/" disc2)); if (imgDisc2 != null) mt.addImage(imgDisc2, 1); try { mt.waitForID(0); } catch(Exception ex) { ex.printStackTrace(); } a_start = getAudioClip(cl.getResource("audio/start.au")); a_click = getAudioClip(cl.getResource("audio/click.au")); a_win = getAudioClip(cl.getResource("audio/win.au")); a_start.play(); resetGame() ; // resize(400, 400); addMouseListener(this); } //放置黑白棋子各兩個 public void resetGame() { for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { grid[x][y] = -1; // nothing } } grid[3][3] = 1; // black grid[3][4] = 0; // white grid[4][3] = 0; // white grid[4][4] = 1; // black blnStart = false; blnPlayerMove = true; blnWhite = false; blnLastWhite = false; blnGameOver = true; } //處理各棋格中棋子的狀態 public void paint(Graphics g) { update(g); repaint();//顯示雙方分數(重畫) } public void update(Graphics g) { g.setColor(new Color(0, 0, 60)); g.fillRect(0, 0, 400, 400); g.setColor(new Color(105, 150, 105)); for (int i = 1; i < 8; i ) { g.drawLine(0, i * 50, 400, i * 50); g.drawLine(i * 50, 0, i * 50, 400); } totaldisc2 = 0; totaldisc1 = 0; for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (grid[x][y] == 0) { g.drawImage(imgDisc2, x * 50 2, y * 50 2, 46, 46, this) ; totaldisc2 ; } else if (grid[x][y] == 1) { g.drawImage(imgDisc1, x * 50 2, y * 50 2, 46, 46, this); totaldisc1 ; } } } g.setColor(Color.green); g.setFont(new Font("dialog", Font.BOLD | Font.ITALIC,18)); g.drawString("狗方分數 : " (new Integer(totaldisc1).toString()), 20, 20); g.drawString("熊方分數 : " (new Integer(totaldisc2).toString()), 20, 60); } //依點選位置決定棋子顏色 private void paintSquare(int x, int y) { Rectangle r = new Rectangle(x * 50, y * 50, 50, 50); Graphics g = getGraphics(); g.clipRect(r.x, r.y, r.width, r.height); paint(g); r = null; } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} //判斷玩家所點選棋格 public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (!blnGameOver) { resetGame(); repaint(); return ; } int gridx = setGrid(x); int gridy = setGrid(y); if (blnPlayerMove) { if (!blnStart) { switch(gridx) { case 2: if (gridy == 3) { blnWhite = true; blnStart = true; break; } if (gridy == 4) { blnWhite = false; blnStart = true; } else { blnStart = false; } break; case 3: if (gridy == 2) { blnWhite = true; blnStart = true; break; } if (gridy == 5) { blnWhite = false; blnStart = true; } else { blnStart = false; } break; case 4: if (gridy == 2) { blnWhite = false; blnStart = true; break; } if (gridy == 5) { blnWhite = true; blnStart = true; } else { blnStart = false; } break; case 5: if (gridy == 3) { blnWhite = false; blnStart = true; break; } if (gridy == 4) { blnWhite = true; blnStart = true; } else { blnStart = false; } break; default: blnStart = false; break; } } //黑白棋下棋規則 if (isMoveValid(gridx, gridy, !blnWhite)) { paintDisc(gridx, gridy, blnWhite); a_click.play(); blnPlayerMove = false; blnLastWhite = blnWhite; moveNext(); } else { blnPlayerMove = true; } } } private int setGrid(int p) { int i = 0 ; if (p < 50) i = 0; if (p >= 50 && p < 100) i = 1; if (p >= 100 && p < 150) i = 2; if (p >= 150 && p < 200) i = 3; if (p >= 200 && p < 250) i = 4; if (p >= 250 && p < 300) i = 5; if (p >= 300 && p < 350) i = 6; if (p >= 350) i = 7; return i ; } private boolean isMoveValid(int x, int y, boolean white) { boolean blnValid = true; if (grid[x][y] != -1) blnValid = false; boolean blnValidMove = false; for (int i = -1; i < 2; i ) { for (int j = -1; j < 2; j ) { if (getDirection(x, y, i, j, !white) > 0) blnValidMove = true; } } if (blnValidMove && blnValid) return true; else return false; } private int getDirection(int x, int y, int deltax, int deltay, boolean black) { //System.out.println("x: " x " y: " y " deltax: " deltax " deltay: " deltay ); int returnvalue = 0; boolean flag = false; if (x deltax < 0 || x deltax > 7 || y deltay < 0 || y deltay > 7) return 0; if (deltax == 0 && deltay == 0) return 0; if (grid[x deltax][y deltay] == 1) { if (black) flag = true; else return 0; } if (grid[x deltax][y deltay] == 0) { if (!black) flag = true; else return 0; } if (grid[x deltax][y deltay] == -1) return 0; if (flag) { boolean blnLoop = true; boolean blnOpp = false; int nextx = x deltax; int nexty = y deltay; while(blnLoop) { if (grid[nextx][nexty] == 1) { if (black) { returnvalue ; } else { blnOpp = true; blnLoop = false; } } if (grid[nextx][nexty] == 0) { if (!black) { returnvalue ; } else { blnOpp = true; blnLoop = false; } } if (grid[nextx][nexty] == -1) blnLoop = false; nextx = deltax; nexty = deltay; if (nextx < 0 || nextx > 7 || nexty < 0 || nexty > 7) blnLoop = false; } if (!blnOpp) returnvalue = 0; } return returnvalue; } private void paintDisc(int x, int y, boolean white) { if (white) grid[x][y] = 0; else grid[x][y] = 1; paintSquare(x, y); boolean black = true; if (white) black = false; for (int deltax = -1; deltax < 2; deltax ) { for (int deltay = -1; deltay < 2; deltay ) { if (getDirection(x, y, deltax, deltay, white) > 0) { int nextx = x deltax; int nexty = y deltay; for (boolean blnLoop = true; blnLoop;) { if (grid[nextx][nexty] == 1) { if (white){ grid[nextx][nexty] = 0; paintSquare(nextx, nexty); } else { blnLoop = false; } } else if (grid[nextx][nexty] == 0) { if (!white) { grid[nextx][nexty] = 1; paintSquare(nextx, nexty); } else { blnLoop = false; } } nextx = deltax; nexty = deltay; if (nextx < 0 || nextx > 7 || nexty < 0 || nexty > 7) blnLoop = false; } } } } } //電腦人工智慧 private void computerMove() { setDelay(); boolean isComputerBlack = true; if (blnWhite) isComputerBlack = false; int weight[][] = { {8, 2, 7, 6, 6, 7, 2, 8}, {2, 1, 3, 3, 3, 3, 1, 2}, {7, 3, 5, 4, 4, 5, 3, 7}, {6, 3, 4, 0, 0, 4, 3, 6}, {6, 3, 4, 0, 0, 4, 3, 6}, {7, 3, 5, 4, 4, 5, 3, 7}, {2, 1, 3, 3, 3, 3, 1, 2}, {8, 2, 7, 6, 6, 7, 2, 8}}; for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (grid[x][y] != -1) weight[x][y] = 0; } } for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (weight[x][y] != 0 && !isMoveValid(x, y, blnWhite)) weight[x][y] = 0; } } int max_weight = 0; for (int x = 0; x < 8; x ){ for (int y = 0; y < 8; y ) { if (weight[x][y] > max_weight) max_weight = weight[x][y]; } } if (max_weight == 0) { moveNext(); } else { int xpos = -1; int ypos = -1; for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (weight[x][y] == max_weight) { xpos = x; ypos = y; } } } paintDisc(xpos, ypos, isComputerBlack); blnLastWhite = isComputerBlack; moveNext(); } } private void moveNext() { if (isBoardFull()) { checkWin(); } else if (!isPlayerCanMove() && !isComputerCanMove()) { checkWin(); } else { if (blnLastWhite && blnWhite) { if (isComputerCanMove()) { blnPlayerMove = false; computerMove(); } else { blnPlayerMove = true; } } if (blnLastWhite && !blnWhite) { if (isPlayerCanMove()) { blnPlayerMove = true; } else { blnPlayerMove = false; computerMove(); } } if (!blnLastWhite && blnWhite) { if (isPlayerCanMove()) { blnPlayerMove = true; } else { blnPlayerMove = false; computerMove(); } } if (!blnLastWhite && !blnWhite) { if (isComputerCanMove()) { blnPlayerMove = false; computerMove(); } else { blnPlayerMove = true; } } } } //檢查棋盤是否填滿 private boolean isBoardFull() { boolean returnvalue = true; for (int x = 0; x < 8; x ){ for (int y = 0; y < 8; y ) { if (grid[x][y] != -1) continue; returnvalue = false; break; } } return returnvalue; } //決定勝方 private void checkWin() { java.awt.Label label; blnGameOver = false; // int strMsg for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (grid[x][y] == 0) totaldisc2 ; if (grid[x][y] == 1) totaldisc1 ; } } a_win.play(); if (totaldisc1 > totaldisc2) { JOptionPane.showMessageDialog(this, name1 "方勝.", "黑白棋",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this, name2 "方勝.", "黑白棋", JOptionPane.INFORMATION_MESSAGE); } blnPlayerMove = false; } private boolean isPlayerCanMove() { boolean returnvalue = false; for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (isMoveValid(x, y, !blnWhite)) returnvalue = true; } } return returnvalue; } private boolean isComputerCanMove() { boolean returnvalue = false; for (int x = 0; x < 8; x ) { for (int y = 0; y < 8; y ) { if (isMoveValid(x, y, blnWhite)) returnvalue = true; } } return returnvalue; } private synchronized void setDelay() { try { wait(50); } catch (InterruptedException ex) { ex.printStackTrace(); } } } //
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-24 23:05:55 IP:211.76.xxx.xxx 未訂閱
一、技術討論文章請發表於相關討論版面 二、相同文章請勿重覆發表    本站十大必刪文章    一、重覆發表有灌水之嫌 二、答非所問或描述不清楚 三、抄襲他人文章或發表而未具名引用,或轉載未得原作者同意之文章 四、涉及人身攻擊或不雅文字 五、涉及政治立場 六、索求原始碼、序號、破解、非法下載連結 七、貼錯版區或主題不明確 八、答題內容明顯抄襲或重覆前答題者內容 九、很明顯的作業問題或不將自己寫的有問題的程式片段貼上而要求答案的 十、站務組保留任何對 K.Top 有不良形響文章刪除與會員停權處分的權利    敬請配合,如果不清楚發言規定請參考本站規章 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=19264    
系統時間:2024-03-29 14:53:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!