您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java如何實(shí)現(xiàn)五子棋,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
效果圖:
思路:**1.首先構(gòu)建一個(gè)Frame框架,來(lái)設(shè)置菜單選項(xiàng)與按鈕點(diǎn)擊事件。
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;判斷是否可以開(kāi)始游戲 final MyPanel panel = new MyPanel(); public MyFrame() { // 設(shè)置窗體的大小并居中 this.setSize(500, 600); // 設(shè)置窗體大小 this.setTitle("五子棋游戲"); // 設(shè)置窗體標(biāo)題 int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 獲取屏幕的寬度 int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 獲取屏幕的高度 this.setLocation((width - 500) / 2, (height - 500) / 2); // 設(shè)置窗體的位置(居中) this.setResizable(false); // 設(shè)置窗體不可以放大 // this.setLocationRelativeTo(null);//這句話也可以設(shè)置窗體居中 /* * 菜單欄的目錄設(shè)置 */ // 設(shè)置菜單欄 JMenuBar bar = new JMenuBar(); this.setJMenuBar(bar); // 添加菜單欄目錄 JMenu menu1 = new JMenu("游戲菜單"); // 實(shí)例化菜單欄目錄 JMenu menu2 = new JMenu("設(shè)置"); JMenu menu3 = new JMenu("幫助"); bar.add(menu1); // 將目錄添加到菜單欄 bar.add(menu2); bar.add(menu3); JMenu menu4 = new JMenu("博弈模式"); // 將“模式”菜單添加到“設(shè)置”里面 menu2.add(menu4); // JMenuItem item1=new JMenuItem("人人博弈"); // JMenuItem item2=new JMenuItem("人機(jī)博弈"); // 設(shè)置“”目錄下面的子目錄 JRadioButtonMenuItem item1 = new JRadioButtonMenuItem("人人博弈"); JRadioButtonMenuItem item2 = new JRadioButtonMenuItem("人機(jī)博弈"); // item1按鈕添加時(shí)間并且為匿名類 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "人機(jī)博弈設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 設(shè)置item2按鈕的事件監(jiān)聽(tīng)事件,也就是設(shè)置人機(jī)博弈 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "人機(jī)博弈設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 設(shè)置按鈕組并把人機(jī)博弈與人人博弈添加到一個(gè)按鈕組里面 ButtonGroup bg = new ButtonGroup(); bg.add(item1); bg.add(item2); // 將按鈕組添加到菜單里面 menu4.add(item1); menu4.add(item2); item2.setSelected(true); // 先行設(shè)置 JMenu menu5 = new JMenu("先行設(shè)置"); // 將“先行設(shè)置”菜單添加到“設(shè)置”里面 menu2.add(menu5); // 設(shè)置黑子先行還是白字先行的按鈕 JRadioButtonMenuItem item3 = new JRadioButtonMenuItem("黑方先行"); JRadioButtonMenuItem item4 = new JRadioButtonMenuItem("白字先行"); // 設(shè)置item3的鼠標(biāo)點(diǎn)擊事件,設(shè)置黑方先行 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "人機(jī)博弈設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 設(shè)置item4的鼠標(biāo)點(diǎn)擊事件 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "人機(jī)博弈設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 設(shè)置按鈕組并把人機(jī)博弈與人人博弈添加到一個(gè)按鈕組里面 ButtonGroup bg1 = new ButtonGroup(); bg1.add(item3); bg1.add(item4); // 將按鈕組添加到菜單里面 menu5.add(item3); menu5.add(item4); item3.setSelected(true); // 設(shè)置“幫助”下面的子目錄 JMenuItem menu6 = new JMenuItem("幫助"); menu3.add(menu6); /* * 菜單欄的目錄設(shè)置完畢 */ // 開(kāi)始游戲菜單設(shè)置 JMenuItem menu7 = new JMenuItem("開(kāi)始游戲"); menu1.add(menu7); JMenuItem menu8 = new JMenuItem("重新開(kāi)始"); 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 = { "重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否重新開(kāi)始", "消息", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 退出游戲選項(xiàng)設(shè)置 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, "保存并重新開(kāi)始游戲"); 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 } }); // 游戲難度設(shè)置 JRadioButtonMenuItem item5 = new JRadioButtonMenuItem("傻瓜");// 添加按鈕 JRadioButtonMenuItem item6 = new JRadioButtonMenuItem("簡(jiǎn)單"); JRadioButtonMenuItem item7 = new JRadioButtonMenuItem("普通"); // JRadioButtonMenuItem item8= new JRadioButtonMenuItem("困難"); ButtonGroup bg3 = new ButtonGroup();// 設(shè)置按鈕組 bg3.add(item5); bg3.add(item6); bg3.add(item7); // bg3.add(item8); JMenu menu10 = new JMenu("游戲難度設(shè)置");// 添加菜單到主菜單 menu2.add(menu10); menu10.add(item5);// 添加選項(xiàng)到難度設(shè)置菜單 menu10.add(item6); menu10.add(item7); // menu2.add(item8); item5.setSelected(true);// 默認(rèn)選項(xiàng)按鈕 // 傻瓜難度設(shè)置的鼠標(biāo)點(diǎn)擊事件 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "人機(jī)博弈設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 簡(jiǎn)單難度設(shè)置模式 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "難度設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); // 普通難度設(shè)置 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 = { "保存并重新開(kāi)始游戲", "不,謝謝" }; int n = JOptionPane.showOptionDialog(null, "是否保存設(shè)置并重新開(kāi)始", "人機(jī)博弈設(shè)置", 0, 1, icon, options, "保存并重新開(kāi)始游戲"); 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 } }); /* * 窗口里面的容器設(shè)置 */ Container con = this.getContentPane(); // 實(shí)例化一個(gè)容器父類 con.add(panel); // 將容器添加到父類 /* * 窗口里面的容器設(shè)置完畢 */ } }
2.第二步,設(shè)置顯示棋盤的容器,文件源代碼為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; //設(shè)置窗體里面的容器操作 public class MyPanel extends JPanel implements MouseListener, Runnable { private static final Toolkit ResourceUtil = null; public Image boardImg; // 定義背景圖片 static int[][] allChess = new int[15][15]; // 棋盤數(shù)組 static int[][] temporaryChess = new int[15][15]; int x;// 保存棋子的橫坐標(biāo) int y;// 保存棋子的縱坐標(biāo) Boolean canPlay = false; // 游戲是否繼續(xù),默認(rèn)為繼續(xù) Boolean isBlack = true;// 是否是黑子,默認(rèn)為黑子 Boolean isManAgainst = false; // 判斷是否是人人對(duì)戰(zhàn) String message = "用戶下棋"; Thread t = new Thread(this); int maxTime = 120; int blackTime = 120; int whiteTime = 120; String blackMessage = "無(wú)限制"; String whiteMessage = "無(wú)限制"; static int gameDifficulty = 0;// 設(shè)置游戲難度,0為傻瓜模式,1為簡(jiǎn)單,2為普通,3為困難 // 獲取isBlack的值 public boolean getIsBlack() { return this.isBlack; } // 設(shè)置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; } // 設(shè)置setGameDifficulty的值 public void setGameDifficulty(int gameDifficulty) { this.gameDifficulty = gameDifficulty; } // 構(gòu)造函數(shù) public MyPanel() { boardImg = Toolkit.getDefaultToolkit().getImage("./src/StartGame/fiveCHessBourd.jpg"); this.repaint(); // 添加鼠標(biāo)監(jiān)視器 addMouseListener((MouseListener) this); // addMouseMotionListener((MouseMotionListener) this); // this.requestFocus(); t.start(); t.suspend();// 線程掛起 // t.resume(); } // 數(shù)據(jù)初始化 @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; // 標(biāo)記誰(shuí)下棋 int x = (FWidth - imgWidth) / 2; int y = (FHeight - imgHeight) / 2; g.drawImage(boardImg, x, y, null); // 添加背景圖片到容器里面 g.setFont(new Font("宋體", 0, 14)); g.drawString("黑方時(shí)間:" + blackTime, 30, 470); g.drawString("白方時(shí)間:" + 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è)中心點(diǎn) 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);// 繪制左上的正方形 // 定義棋盤數(shù)組 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); // 調(diào)用下棋子函數(shù) } } } // 鼠標(biāo)點(diǎn)擊時(shí)發(fā)生的函數(shù) @Override public void mousePressed(MouseEvent e) { // x = e.getX();// 獲取鼠標(biāo)點(diǎn)擊坐標(biāo)的橫坐標(biāo) // y = e.getY();// 獲取鼠標(biāo)點(diǎn)擊坐標(biāo)的縱坐標(biāo) // if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // 鼠標(biāo)點(diǎn)擊在棋子框里面才有效 // // } if (canPlay == true) {// 判斷是否可以開(kāi)始游戲 x = e.getX(); // 獲取鼠標(biāo)的焦點(diǎn) y = e.getY(); if (isManAgainst == true) {// 判斷是否是人人對(duì)戰(zhàn) manToManChess(); } else { // 否則是人機(jī)對(duì)戰(zhàn),人機(jī)下棋 manToMachine(); } } } // 判斷是否輸贏的函數(shù) private boolean checkWin(int x, int y) { // TODO Auto-generated method stub boolean flag = false; // 保存共有多少相同顏色棋子相連 int count = 1; // 判斷橫向 特點(diǎn):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; } // 判斷相同棋子連接的個(gè)數(shù) 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; } // 機(jī)器人判斷黑棋相連的數(shù)量 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 // 判斷是否有時(shí)間限制 if (maxTime > 0) { while (true) { // System.out.println(canPlay + "11"); if (isManAgainst) { if (isBlack) { blackTime--; if (blackTime == 0) { JOptionPane.showMessageDialog(this, "黑方超時(shí),游戲結(jié)束!"); } } else { whiteTime--; if (whiteTime == 0) { JOptionPane.showMessageDialog(this, "白方超時(shí),游戲結(jié)束!"); } } } else { // 監(jiān)控黑子下棋的時(shí)間,也就是用戶下棋的時(shí)間 blackTime--; if (blackTime == 0) { JOptionPane.showMessageDialog(this, "用戶超時(shí),游戲結(jié)束!"); } // 不監(jiān)控電腦白字下棋 } 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(); } } } } // 點(diǎn)擊開(kāi)始游戲設(shè)置屬性,游戲開(kāi)始 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, "游戲開(kāi)始了,請(qǐng)開(kāi)始下棋"); 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 = "無(wú)限制"; // whiteMessage = "無(wú)限制"; // } // this.repaint();// 如果不重新調(diào)用,則界面不會(huì)刷新 } // 人人對(duì)戰(zhàn)下棋函數(shù) public void manToManChess() { if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // System.out.println("在棋盤范圍內(nèi):"+x+"--"+y); x = (x + 15) / 30 - 1; // 是為了取得交叉點(diǎn)的坐標(biāo) y = (y + 15) / 30 - 1; if (allChess[x][y] == 0) { // 判斷當(dāng)前要下的是什么棋子 if (isBlack == true) { allChess[x][y] = 1; isBlack = false; blackTime = 120; message = "輪到白方"; } else { allChess[x][y] = 2; isBlack = true; whiteTime = 120; message = "輪到黑方"; } } // 判斷這個(gè)棋子是否和其他棋子連成5個(gè) boolean winFlag = this.checkWin(x, y); this.repaint(); // 繪制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戲結(jié)束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "獲勝!"); canPlay = false; } } else { // JOptionPane.showMessageDialog(this, // "當(dāng)前位子已經(jīng)有棋子,請(qǐng)重新落子!??!"); } } // 人機(jī)對(duì)戰(zhàn)下棋函數(shù) public void manToMachine() { if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // System.out.println("在棋盤范圍內(nèi):"+x+"--"+y); x = (x + 15) / 30 - 1; // 是為了取得交叉點(diǎn)的坐標(biāo) y = (y + 15) / 30 - 1; if (allChess[x][y] == 0) { // 判斷當(dāng)前要下的是什么棋子 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, "游戲結(jié)束," + (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, "游戲結(jié)束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "獲勝!"); canPlay = false; } machineChess(gameDifficulty); } } // 判斷這個(gè)棋子是否和其他棋子連成5個(gè) // boolean winFlag = this.checkWin(x, y); // this.repaint(); // 繪制棋子 // // if (winFlag == true) { // JOptionPane.showMessageDialog(this, "游戲結(jié)束," + (allChess[x][y] == // 1 ? "黑方" : "白方") + "獲勝!"); // canPlay = false; // } } } // 機(jī)器人下棋算法 public void machineChess(int gameDifficulty) { if (gameDifficulty == 0) { int i, j; boolean chessSucceed = true;// 下棋成功的標(biāo)志 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];// 對(duì)棋子的四個(gè)方向進(jìn)行打分 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(); } // 對(duì)數(shù)組排序 public void sortFourFigure(int n[]) { Arrays.sort(n);// 正序排序 } }
3.最后設(shè)置整個(gè)游戲的入口主函數(shù)StartEntrance.java
package StartGame; import javax.swing.JFrame; public class StartEntrance { //public static boolean canPlay =false; public static void main(String[] args) { // 實(shí)例化一個(gè)窗口 MyFrame myFrame = new MyFrame(); myFrame.setVisible(true); // 設(shè)置窗口為可見(jiàn) myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設(shè)置窗口關(guān)閉操作屬性 } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java如何實(shí)現(xiàn)五子棋”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。