您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java如何實現五子棋,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
效果圖:
思路:**1.首先構建一個Frame框架,來設置菜單選項與按鈕點擊事件。
MyFrame.java文件代碼如下
package StartGame; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JRadioButtonMenuItem; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; //窗體框架類 public class MyFrame extends JFrame { // static boolean canPlay1 = false;判斷是否可以開始游戲 final MyPanel panel = new MyPanel(); public MyFrame() { // 設置窗體的大小并居中 this.setSize(500, 600); // 設置窗體大小 this.setTitle("五子棋游戲"); // 設置窗體標題 int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 獲取屏幕的寬度 int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 獲取屏幕的高度 this.setLocation((width - 500) / 2, (height - 500) / 2); // 設置窗體的位置(居中) this.setResizable(false); // 設置窗體不可以放大 // this.setLocationRelativeTo(null);//這句話也可以設置窗體居中 /* * 菜單欄的目錄設置 */ // 設置菜單欄 JMenuBar bar = new JMenuBar(); this.setJMenuBar(bar); // 添加菜單欄目錄 JMenu menu1 = new JMenu("游戲菜單"); // 實例化菜單欄目錄 JMenu menu2 = new JMenu("設置"); JMenu menu3 = new JMenu("幫助"); bar.add(menu1); // 將目錄添加到菜單欄 bar.add(menu2); bar.add(menu3); JMenu menu4 = new JMenu("博弈模式"); // 將“模式”菜單添加到“設置”里面 menu2.add(menu4); // JMenuItem item1=new JMenuItem("人人博弈"); // JMenuItem item2=new JMenuItem("人機博弈"); // 設置“”目錄下面的子目錄 JRadioButtonMenuItem item1 = new JRadioButtonMenuItem("人人博弈"); JRadioButtonMenuItem item2 = new JRadioButtonMenuItem("人機博弈"); // item1按鈕添加時間并且為匿名類 item1.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "人機博弈設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setIsManAgainst(true); panel.Start(); item1.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設置item2按鈕的事件監聽事件,也就是設置人機博弈 item2.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "人機博弈設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setIsManAgainst(false); panel.Start(); item2.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設置按鈕組并把人機博弈與人人博弈添加到一個按鈕組里面 ButtonGroup bg = new ButtonGroup(); bg.add(item1); bg.add(item2); // 將按鈕組添加到菜單里面 menu4.add(item1); menu4.add(item2); item2.setSelected(true); // 先行設置 JMenu menu5 = new JMenu("先行設置"); // 將“先行設置”菜單添加到“設置”里面 menu2.add(menu5); // 設置黑子先行還是白字先行的按鈕 JRadioButtonMenuItem item3 = new JRadioButtonMenuItem("黑方先行"); JRadioButtonMenuItem item4 = new JRadioButtonMenuItem("白字先行"); // 設置item3的鼠標點擊事件,設置黑方先行 item3.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "人機博弈設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setIsBlack(true); panel.Start(); item3.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設置item4的鼠標點擊事件 item4.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "人機博弈設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setIsBlack(false); panel.Start(); item4.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 設置按鈕組并把人機博弈與人人博弈添加到一個按鈕組里面 ButtonGroup bg1 = new ButtonGroup(); bg1.add(item3); bg1.add(item4); // 將按鈕組添加到菜單里面 menu5.add(item3); menu5.add(item4); item3.setSelected(true); // 設置“幫助”下面的子目錄 JMenuItem menu6 = new JMenuItem("幫助"); menu3.add(menu6); /* * 菜單欄的目錄設置完畢 */ // 開始游戲菜單設置 JMenuItem menu7 = new JMenuItem("開始游戲"); menu1.add(menu7); JMenuItem menu8 = new JMenuItem("重新開始"); menu1.add(menu8); menu7.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub panel.Start(); // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); menu8.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否重新開始", "消息", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.Start(); } // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); // 退出游戲選項設置 JMenuItem menu9 = new JMenuItem("退出游戲"); menu1.add(menu9); menu9.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "退出游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否退出游戲", "消息", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { System.exit(0);// 退出程序 } // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); // 游戲難度設置 JRadioButtonMenuItem item5 = new JRadioButtonMenuItem("傻瓜");// 添加按鈕 JRadioButtonMenuItem item6 = new JRadioButtonMenuItem("簡單"); JRadioButtonMenuItem item7 = new JRadioButtonMenuItem("普通"); // JRadioButtonMenuItem item8= new JRadioButtonMenuItem("困難"); ButtonGroup bg3 = new ButtonGroup();// 設置按鈕組 bg3.add(item5); bg3.add(item6); bg3.add(item7); // bg3.add(item8); JMenu menu10 = new JMenu("游戲難度設置");// 添加菜單到主菜單 menu2.add(menu10); menu10.add(item5);// 添加選項到難度設置菜單 menu10.add(item6); menu10.add(item7); // menu2.add(item8); item5.setSelected(true);// 默認選項按鈕 // 傻瓜難度設置的鼠標點擊事件 item5.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "人機博弈設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setGameDifficulty(0); panel.Start(); item5.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 簡單難度設置模式 item6.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "難度設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setGameDifficulty(1); panel.Start(); item6.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 普通難度設置 item7.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新開始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設置并重新開始", "人機博弈設置", 0, 1, icon, options, "保存并重新開始游戲"); if (n == 0) { panel.setGameDifficulty(2); panel.Start(); item7.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); //游戲幫助提示信息 menu6.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; JOptionPane.showMessageDialog(null, "制作人員:韓紅劍"); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); /* * 窗口里面的容器設置 */ Container con = this.getContentPane(); // 實例化一個容器父類 con.add(panel); // 將容器添加到父類 /* * 窗口里面的容器設置完畢 */ } }
2.第二步,設置顯示棋盤的容器,文件源代碼為MyPanel.java
package StartGame; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Panel; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.*; import java.io.File; import java.net.URL; import java.util.Arrays; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.JPanel; //設置窗體里面的容器操作 public class MyPanel extends JPanel implements MouseListener, Runnable { private static final Toolkit ResourceUtil = null; public Image boardImg; // 定義背景圖片 static int[][] allChess = new int[15][15]; // 棋盤數組 static int[][] temporaryChess = new int[15][15]; int x;// 保存棋子的橫坐標 int y;// 保存棋子的縱坐標 Boolean canPlay = false; // 游戲是否繼續,默認為繼續 Boolean isBlack = true;// 是否是黑子,默認為黑子 Boolean isManAgainst = false; // 判斷是否是人人對戰 String message = "用戶下棋"; Thread t = new Thread(this); int maxTime = 120; int blackTime = 120; int whiteTime = 120; String blackMessage = "無限制"; String whiteMessage = "無限制"; static int gameDifficulty = 0;// 設置游戲難度,0為傻瓜模式,1為簡單,2為普通,3為困難 // 獲取isBlack的值 public boolean getIsBlack() { return this.isBlack; } // 設置isBlack的值 public void setIsBlack(boolean isBlack) { this.isBlack = isBlack; } // 獲取isManAgainst的值 public boolean getIsManAgainst() { return this.isManAgainst; } // 獲取isManAgainst的值 public void setIsManAgainst(boolean isManAgainst) { this.isManAgainst = isManAgainst; } // 獲取isManAgainst的值 public int getGameDifficulty() { return this.gameDifficulty; } // 設置setGameDifficulty的值 public void setGameDifficulty(int gameDifficulty) { this.gameDifficulty = gameDifficulty; } // 構造函數 public MyPanel() { boardImg = Toolkit.getDefaultToolkit().getImage("./src/StartGame/fiveCHessBourd.jpg"); this.repaint(); // 添加鼠標監視器 addMouseListener((MouseListener) this); // addMouseMotionListener((MouseMotionListener) this); // this.requestFocus(); t.start(); t.suspend();// 線程掛起 // t.resume(); } // 數據初始化 @Override public void paint(Graphics g) { super.paint(g); int imgWidth = boardImg.getWidth(this); // 獲取圖片的寬度 int imgHeight = boardImg.getHeight(this); // 獲取圖片的高度 int FWidth = getWidth(); int FHeight = getHeight(); String message; // 標記誰下棋 int x = (FWidth - imgWidth) / 2; int y = (FHeight - imgHeight) / 2; g.drawImage(boardImg, x, y, null); // 添加背景圖片到容器里面 g.setFont(new Font("宋體", 0, 14)); g.drawString("黑方時間:" + blackTime, 30, 470); g.drawString("白方時間:" + whiteTime, 260, 470); // 繪制棋盤 for (int i = 0; i < 15; i++) { g.drawLine(30, 30 + 30 * i, 450, 30 + 30 * i); g.drawLine(30 + 30 * i, 30, 30 + 30 * i, 450); } // 繪制五個中心點 g.fillRect(240 - 5, 240 - 5, 10, 10); // 繪制最中心的正方形 g.fillRect(360 - 5, 360 - 5, 10, 10); // 繪制右下的正方形 g.fillRect(360 - 5, 120 - 5, 10, 10); // 繪制右上的正方形 g.fillRect(120 - 5, 360 - 5, 10, 10);// 繪制左下的正方形 g.fillRect(120 - 5, 120 - 5, 10, 10);// 繪制左上的正方形 // 定義棋盤數組 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { // if (allChess[i][j] == 1) { // // 黑子 // int tempX = i * 30 + 30; // int tempY = j * 30 + 30; // g.fillOval(tempX - 7, tempY - 7, 14, 14); // } // if (allChess[i][j] == 2) { // // 白子 // int tempX = i * 30 + 30; // int tempY = j * 30 + 30; // g.setColor(Color.WHITE); // g.fillOval(tempX - 7, tempY - 7, 14, 14); // g.setColor(Color.BLACK); // g.drawOval(tempX - 7, tempY - 7, 14, 14); // } draw(g, i, j); // 調用下棋子函數 } } } // 鼠標點擊時發生的函數 @Override public void mousePressed(MouseEvent e) { // x = e.getX();// 獲取鼠標點擊坐標的橫坐標 // y = e.getY();// 獲取鼠標點擊坐標的縱坐標 // if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // 鼠標點擊在棋子框里面才有效 // // } if (canPlay == true) {// 判斷是否可以開始游戲 x = e.getX(); // 獲取鼠標的焦點 y = e.getY(); if (isManAgainst == true) {// 判斷是否是人人對戰 manToManChess(); } else { // 否則是人機對戰,人機下棋 manToMachine(); } } } // 判斷是否輸贏的函數 private boolean checkWin(int x, int y) { // TODO Auto-generated method stub boolean flag = false; // 保存共有多少相同顏色棋子相連 int count = 1; // 判斷橫向 特點:allChess[x][y]中y值相同 int color = allChess[x][y]; // 判斷橫向 count = this.checkCount(x, y, 1, 0, color); if (count >= 5) { flag = true; } else { // 判斷縱向 count = this.checkCount(x, y, 0, 1, color); if (count >= 5) { flag = true; } else { // 判斷右上左下 count = this.checkCount(x, y, 1, -1, color); if (count >= 5) { flag = true; } else { // 判斷左下右上 count = this.checkCount(x, y, 1, 1, color); if (count >= 5) { flag = true; } } } } return flag; } // 判斷相同棋子連接的個數 private int checkCount(int x, int y, int xChange, int yChange, int color) { // TODO Auto-generated method stub int count = 1; int tempX = xChange; int tempY = yChange; while (x + xChange >= 0 && x + xChange <= 14 && y + yChange >= 0 && y + yChange <= 14 && color == allChess[x + xChange][y + yChange]) { count++; if (xChange != 0) { xChange++; } if (yChange != 0) { if (yChange > 0) { yChange++; } else { yChange--; } } } xChange = tempX; yChange = tempY; while (x - xChange >= 0 && x - xChange <= 14 && y - yChange >= 0 && y - yChange <= 14 && color == allChess[x - xChange][y - yChange]) { count++; if (xChange != 0) { xChange++; } if (yChange != 0) { if (yChange > 0) { yChange++; } else { yChange--; } } } return count; } // 機器人判斷黑棋相連的數量 private int checkCountMachine(int x, int y, int xChange, int yChange, int color) { // TODO Auto-generated method stub int count = 0; int tempX = xChange; int tempY = yChange; while (x + xChange >= 0 && x + xChange <= 14 && y + yChange >= 0 && y + yChange <= 14 && color == allChess[x + xChange][y + yChange]) { count++; if (xChange != 0) { xChange++; } if (yChange != 0) { if (yChange > 0) { yChange++; } else { yChange--; } } } xChange = tempX; yChange = tempY; while (x - xChange >= 0 && x - xChange <= 14 && y - yChange >= 0 && y - yChange <= 14 && color == allChess[x - xChange][y - yChange]) { count++; if (xChange != 0) { xChange++; } if (yChange != 0) { if (yChange > 0) { yChange++; } else { yChange--; } } } return count; } public void paintConmponents(Graphics g) { super.paintComponents(g); } // 繪制黑白棋子 public void draw(Graphics g, int i, int j) { if (allChess[i][j] == 1) { g.setColor(Color.black);// 黑色棋子 g.fillOval(30 * i + 30 - 7, 30 * j + 30 - 7, 14, 14); g.drawString(message, 230, 20); } if (allChess[i][j] == 2) { g.setColor(Color.white);// 白色棋子 g.fillOval(30 * i + 30 - 7, 30 * j + 30 - 7, 14, 14); g.drawString(message, 230, 20); } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void run() { // TODO Auto-generated method stub // 判斷是否有時間限制 if (maxTime > 0) { while (true) { // System.out.println(canPlay + "11"); if (isManAgainst) { if (isBlack) { blackTime--; if (blackTime == 0) { JOptionPane.showMessageDialog(this, "黑方超時,游戲結束!"); } } else { whiteTime--; if (whiteTime == 0) { JOptionPane.showMessageDialog(this, "白方超時,游戲結束!"); } } } else { // 監控黑子下棋的時間,也就是用戶下棋的時間 blackTime--; if (blackTime == 0) { JOptionPane.showMessageDialog(this, "用戶超時,游戲結束!"); } // 不監控電腦白字下棋 } blackMessage = blackTime / 3600 + ":" + (blackTime / 60 - blackTime / 3600 * 60) + ":" + (blackTime - blackTime / 60 * 60); whiteMessage = whiteTime / 3600 + ":" + (whiteTime / 60 - whiteTime / 3600 * 60) + ":" + (whiteTime - whiteTime / 60 * 60); this.repaint(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } // 點擊開始游戲設置屬性,游戲開始 public void Start() { this.canPlay = true; for (int i = 0; i < 14; i++) { for (int j = 0; j < 14; j++) { allChess[i][j] = 0; } } if (canPlay == true) { t.resume(); } this.repaint(); JOptionPane.showMessageDialog(this, "游戲開始了,請開始下棋"); if (isBlack == false && isManAgainst == false) { machineChess(gameDifficulty); } // 另一種方式 allChess=new int[19][19] // message = "黑方先行"; // // isBlack = true; // blackTime = maxTime; // whiteTime = maxTime; // if (maxTime > 0) { // blackMessage = maxTime / 3600 + ":" + (maxTime / 60 - maxTime / 3600 // * 60) + ":" // + (maxTime - maxTime / 60 * 60); // whiteMessage = maxTime / 3600 + ":" + (maxTime / 60 - maxTime / 3600 // * 60) + ":" // + (maxTime - maxTime / 60 * 60); // t.resume(); // } else { // blackMessage = "無限制"; // whiteMessage = "無限制"; // } // this.repaint();// 如果不重新調用,則界面不會刷新 } // 人人對戰下棋函數 public void manToManChess() { if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // System.out.println("在棋盤范圍內:"+x+"--"+y); x = (x + 15) / 30 - 1; // 是為了取得交叉點的坐標 y = (y + 15) / 30 - 1; if (allChess[x][y] == 0) { // 判斷當前要下的是什么棋子 if (isBlack == true) { allChess[x][y] = 1; isBlack = false; blackTime = 120; message = "輪到白方"; } else { allChess[x][y] = 2; isBlack = true; whiteTime = 120; message = "輪到黑方"; } } // 判斷這個棋子是否和其他棋子連成5個 boolean winFlag = this.checkWin(x, y); this.repaint(); // 繪制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戲結束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "獲勝!"); canPlay = false; } } else { // JOptionPane.showMessageDialog(this, // "當前位子已經有棋子,請重新落子!!!"); } } // 人機對戰下棋函數 public void manToMachine() { if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // System.out.println("在棋盤范圍內:"+x+"--"+y); x = (x + 15) / 30 - 1; // 是為了取得交叉點的坐標 y = (y + 15) / 30 - 1; if (allChess[x][y] == 0) { // 判斷當前要下的是什么棋子 if (isBlack == true) { allChess[x][y] = 1; this.repaint(); // 繪制棋子 machineChess(gameDifficulty); isBlack = true; blackTime = 120; message = "用戶下棋"; whiteTime = 120; boolean winFlag = this.checkWin(x, y); this.repaint(); // 繪制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戲結束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "獲勝!"); canPlay = false; } } else { allChess[x][y] = 1; // allChess[x][y] = 2; this.repaint(); isBlack = false; whiteTime = 120; blackTime = 120; boolean winFlag = this.checkWin(x, y); this.repaint(); // 繪制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戲結束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "獲勝!"); canPlay = false; } machineChess(gameDifficulty); } } // 判斷這個棋子是否和其他棋子連成5個 // boolean winFlag = this.checkWin(x, y); // this.repaint(); // 繪制棋子 // // if (winFlag == true) { // JOptionPane.showMessageDialog(this, "游戲結束," + (allChess[x][y] == // 1 ? "黑方" : "白方") + "獲勝!"); // canPlay = false; // } } } // 機器人下棋算法 public void machineChess(int gameDifficulty) { if (gameDifficulty == 0) { int i, j; boolean chessSucceed = true;// 下棋成功的標志 while (chessSucceed) { i = (int) (Math.random() * 15); j = (int) (Math.random() * 15); if (allChess[i][j] == 0) { allChess[i][j] = 2; chessSucceed = false; } } } else if (gameDifficulty == 1) { int max = 0; int m = 0, n = 0; int figureSort[] = new int[4];// 對棋子的四個方向進行打分 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (allChess[i][j] == 0) { figureSort[0] = checkCountMachine(i, j, 0, 1, 1); figureSort[1] = checkCountMachine(i, j, 1, 0, 1); figureSort[2] = checkCountMachine(i, j, 1, -1, 1); figureSort[3] = checkCountMachine(i, j, 1, 1, 1); sortFourFigure(figureSort); temporaryChess[i][j] = figureSort[0] * 12 + figureSort[1] * 25 + figureSort[2] * 50// 算出空白棋子的最大分 + figureSort[3] * 100; } // System.out.print(temporaryChess[i][j] + " "); } // System.out.print("\n"); } // System.out.print("=============================\n"); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (temporaryChess[i][j] > max && allChess[i][j] != 2 && allChess[i][j] != 1) { m = i; n = j; max = temporaryChess[i][j]; } } } allChess[m][n] = 2; } else if (gameDifficulty == 2) { } else { } this.repaint(); } // 對數組排序 public void sortFourFigure(int n[]) { Arrays.sort(n);// 正序排序 } }
3.最后設置整個游戲的入口主函數StartEntrance.java
package StartGame; import javax.swing.JFrame; public class StartEntrance { //public static boolean canPlay =false; public static void main(String[] args) { // 實例化一個窗口 MyFrame myFrame = new MyFrame(); myFrame.setVisible(true); // 設置窗口為可見 myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設置窗口關閉操作屬性 } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java如何實現五子棋”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。