河北住房和城乡建设厅网站6,pc网站建设的三大条件,公众号开发者绑定,外贸业务推广五子棋是一种双人对弈的棋类游戏#xff0c;通常在棋盘上进行。棋盘为 1515 的方格#xff0c;黑白双方各执棋子#xff0c;轮流在棋盘的格点上落子#xff0c;先在横、竖、斜线上形成五个相连的同色棋子者获胜。五子棋规则简单#xff0c;易学难精#xff0c;兼具攻防和…五子棋是一种双人对弈的棋类游戏通常在棋盘上进行。棋盘为 15×15 的方格黑白双方各执棋子轮流在棋盘的格点上落子先在横、竖、斜线上形成五个相连的同色棋子者获胜。五子棋规则简单易学难精兼具攻防和谋略是一种极具智慧和趣味性的游戏。
以下是使用Java编写的五子棋游戏的示例代码
棋盘类
public class ChessBoard {private int[][] board;private final int rows;private final int cols;private final int winCount;public ChessBoard(int rows, int cols, int winCount) {this.rows rows;this.cols cols;this.winCount winCount;board new int[rows][cols];}public int getRows() {return rows;}public int getCols() {return cols;}public int getWinCount() {return winCount;}public int getChessman(int row, int col) {return board[row][col];}public boolean canPutChessman(int row, int col) {return board[row][col] 0;}public void putChessman(int row, int col, int player) {board[row][col] player;}public boolean isFull() {for (int i 0; i rows; i) {for (int j 0; j cols; j) {if (board[i][j] 0) {return false;}}}return true;}public boolean hasWinner(int player) {for (int i 0; i rows; i) {for (int j 0; j cols; j) {if (board[i][j] player) {if (checkHorizontal(i, j, player)) {return true;}if (checkVertical(i, j, player)) {return true;}if (checkDiagonal1(i, j, player)) {return true;}if (checkDiagonal2(i, j, player)) {return true;}}}}return false;}private boolean checkHorizontal(int row, int col, int player) {int count 1;for (int j col 1; j cols board[row][j] player; j) {count;}for (int j col - 1; j 0 board[row][j] player; j--) {count;}return count winCount;}private boolean checkVertical(int row, int col, int player) {int count 1;for (int i row 1; i rows board[i][col] player; i) {count;}for (int i row - 1; i 0 board[i][col] player; i--) {count;}return count winCount;}private boolean checkDiagonal1(int row, int col, int player) {int count 1;for (int i row 1, j col 1; i rows j cols board[i][j] player; i, j) {count;}for (int i row - 1, j col - 1; i 0 j 0 board[i][j] player; i--, j--) {count;}return count winCount;}private boolean checkDiagonal2(int row, int col, int player) {int count 1;for (int i row 1, j col - 1; i rows j 0 board[i][j] player; i, j--) {count;}for (int i row - 1, j col 1; i 0 j cols board[i][j] player; i--, j) {count;}return count winCount;}}游戏界面类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;public class GameUI extends JFrame {private final int rows;private final int cols;private final int winCount;private ChessBoard board;private int currentPlayer;private boolean gameOver;private final JPanel panel;public GameUI(int rows, int cols, int winCount) {this.rows rows;this.cols cols;this.winCount winCount;setTitle(五子棋);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(cols * 40, rows * 40); // 每个棋子为正方形大小为40setLocationRelativeTo(null);currentPlayer 1;board new ChessBoard(rows, cols, winCount);gameOver false;panel new JPanel() {Overridepublic void paintComponent(Graphics g) {super.paintComponent(g);for (int i 0; i rows; i) {for (int j 0; j cols; j) {int chessman board.getChessman(i, j);if (chessman 1) {g.setColor(Color.BLACK);g.fillOval(j * 40 5, i * 40 5, 30, 30);} else if (chessman 2) {g.setColor(Color.WHITE);g.fillOval(j * 40 5, i * 40 5, 30, 30);g.setColor(Color.BLACK);g.drawOval(j * 40 5, i * 40 5, 30, 30);}}}}};panel.addMouseListener(new MouseAdapter() {Overridepublic void mouseClicked(MouseEvent e) {if (gameOver) {return;}int col e.getX() / 40;int row e.getY() / 40;if (row rows col cols board.canPutChessman(row, col)) {board.putChessman(row, col, currentPlayer);panel.repaint();if (board.hasWinner(currentPlayer)) {gameOver true;System.out.println(Player currentPlayer wins.);} else if (board.isFull()) {gameOver true;System.out.println(Tie game.);} else {currentPlayer 3 - currentPlayer; // 切换玩家}}}});add(panel);setVisible(true);}}主程序
public class Main {public static void main(String[] args) {new GameUI(15, 15, 5);}}在主程序中创建游戏界面对象传入行数、列数和获胜所需连续棋子个数即可开始游戏。
效果如下 快去体验一下吧 文章转载自: http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.hgkbj.cn.gov.cn.hgkbj.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.ypdmr.cn.gov.cn.ypdmr.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.fqqlq.cn.gov.cn.fqqlq.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.rwfj.cn.gov.cn.rwfj.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.crrjg.cn.gov.cn.crrjg.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.mprky.cn.gov.cn.mprky.cn