当前位置: 首页 > news >正文

北京网站建设北京谷歌搜索引擎363

北京网站建设北京,谷歌搜索引擎363,环球旅行社网站建设规划书论文,网上花钱做ppt的网站五子棋是一种双人对弈的棋类游戏,通常在棋盘上进行。棋盘为 1515 的方格,黑白双方各执棋子,轮流在棋盘的格点上落子,先在横、竖、斜线上形成五个相连的同色棋子者获胜。五子棋规则简单,易学难精,兼具攻防和…

五子棋是一种双人对弈的棋类游戏,通常在棋盘上进行。棋盘为 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.ds6.com.cn/news/17482.html

相关文章:

  • 自定义wordpress背景图片网站seo软件
  • 室内装修培训搜狗网站seo
  • 做网站工作的怎么填职务郑州seo哪家专业
  • 如何建设企业电子商务网站seo交互论坛
  • 自己做的网站做登录武汉关键词seo排名
  • 北京做网站个人优化深圳seo
  • 南平网站建设宁波网站seo诊断工具
  • b2b网站建设费用舆情信息报送
  • 美食网站开发的难点seo网站排名优化公司
  • 如何注册网站免费的吗seo优化工程师
  • 辽宁专业模板网站建设公司360推广平台登录入口
  • 做外贸如何浏览国外网站网络营销个人感悟小结
  • 做公司的网站有哪些国外常用的seo站长工具
  • 论坛网站建设费用网站友链
  • 新吴区推荐做网站公司电脑培训学校
  • 电子商务网站推广方法科学新概念seo外链
  • 开发员给我用织梦做的网站怎么买到精准客户的电话
  • 怎么给网站做超链接教育培训机构招生方案
  • 万网网站建设方案书 备案百度一下官方网站
  • 敦煌网站销售员怎么做永久免费个人网站申请注册
  • 展台设计灵感网站广告营销的经典案例
  • 怎样自己做网站推广如何网络推广
  • 重庆游戏网站开发应用商店下载
  • 龙华区住房和建设局网站官网每天三分钟新闻天下事
  • 香港主机做擦边球网站长尾关键词排名工具
  • 祖庙网站建设百度收录的网站多久更新一次
  • 用织梦模板做网站东莞网络营销推广公司
  • 深圳网站开发报价在线外链推广
  • durable创建网站抖音搜索关键词排名
  • 无锡定制网站建设app线上推广是什么工作