写一段五子棋javs代码

时间: 2023-09-19 11:06:50 浏览: 82
### 回答1: 好的,以下是一段五子棋 Java 代码: ``` import java.awt.*; import javax.swing.*; public class Wuziqi extends JFrame { private static final long serialVersionUID = 1L; int width = 800; int height = 800; int grids = 15; int gridWidth = width / grids; int chessSize = gridWidth / 2; int x = -1; int y = -1; int[][] chessBoard = new int[grids][grids]; public Wuziqi() { setTitle("五子棋"); setSize(width, height); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { x = e.getX() / gridWidth; y = e.getY() / gridWidth; if (chessBoard[x][y] == 0) { chessBoard[x][y] = 1; } repaint(); } }); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLACK); for (int i = 0; i < grids; i++) { g.drawLine(gridWidth, gridWidth * (i + 1), width - gridWidth, gridWidth * (i + 1)); g.drawLine(gridWidth * (i + 1), gridWidth, gridWidth * (i + 1), height - gridWidth); } for (int i = 0; i < grids; i++) { for (int j = 0; j < grids; j++) { if (chessBoard[i][j] == 1) { int xPos = i * gridWidth + gridWidth / 2 - chessSize / 2; int yPos = j * gridWidth + gridWidth / 2 - chessSize / 2; g.setColor(Color.BLACK); g.fillOval(xPos, yPos, chessSize, chessSize); } } } } public static void main(String[] args) { new Wuziqi(); } } ``` 希望对您有所帮助! ### 回答2: 五子棋是一种双人对弈的策略游戏,目标是在棋盘上先行成五子连线的一方取得胜利。下面是一个简单的五子棋Java代码示例: ``` import java.util.Scanner; public class GomokuGame { private char[][] board; // 棋盘 private char currentPlayer; // 当前玩家 private boolean gameEnd; // 游戏是否结束 public GomokuGame() { board = new char[15][15]; currentPlayer = 'X'; gameEnd = false; } public void playGame() { Scanner scanner = new Scanner(System.in); int row, col; while (!gameEnd) { // 打印棋盘 printBoard(); System.out.println("请玩家 " + currentPlayer + " 输入下棋位置(行 列):"); row = scanner.nextInt(); col = scanner.nextInt(); // 验证下棋位置的合法性 if (isValidMove(row, col)) { board[row][col] = currentPlayer; // 判断游戏是否结束 if (isWinningMove(row, col)) { System.out.println("玩家 " + currentPlayer + " 获胜!"); gameEnd = true; } else { // 切换玩家 currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } } else { System.out.println("该位置已被占据或不合法,请重新输入!"); } } scanner.close(); } public boolean isValidMove(int row, int col) { if (row < 0 || row >= 15 || col < 0 || col >= 15 || board[row][col] != '\0') { return false; } return true; } public boolean isWinningMove(int row, int col) { char player = board[row][col]; int count = 1; // 检查横向 for (int i = col - 1; i >= 0 && board[row][i] == player; i--) { count++; } for (int i = col + 1; i < 15 && board[row][i] == player; i++) { count++; } if (count >= 5) { return true; } // 检查纵向 count = 1; for (int i = row - 1; i >= 0 && board[i][col] == player; i--) { count++; } for (int i = row + 1; i < 15 && board[i][col] == player; i++) { count++; } if (count >= 5) { return true; } // 检查斜向(左上到右下) count = 1; for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) { count++; } for (int i = row + 1, j = col + 1; i < 15 && j < 15 && board[i][j] == player; i++, j++) { count++; } if (count >= 5) { return true; } // 检查斜向(左下到右上) count = 1; for (int i = row + 1, j = col - 1; i < 15 && j >= 0 && board[i][j] == player; i++, j--) { count++; } for (int i = row - 1, j = col + 1; i >= 0 && j < 15 && board[i][j] == player; i--, j++) { count++; } if (count >= 5) { return true; } return false; } public void printBoard() { System.out.print(" "); for (int col = 0; col < 15; col++) { System.out.print(col + " "); } System.out.println(); for (int row = 0; row < 15; row++) { System.out.print(row + " "); for (int col = 0; col < 15; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } } public static void main(String[] args) { GomokuGame game = new GomokuGame(); game.playGame(); } } ``` 以上代码实现了一个简单的五子棋游戏。游戏使用字符二维数组表示棋盘,通过输入行和列的坐标进行下棋,并判断胜负。程序会轮流切换玩家,直到有一方获胜。 ### 回答3: 五子棋是一种二人对弈的棋类游戏,玩家需要通过放置黑白两种颜色的棋子在棋盘上获胜。以下是一个简单的五子棋Java代码实现: ```java import java.util.Scanner; public class GomokuGame { private char[][] board; private char currentPlayer; public GomokuGame() { board = new char[15][15]; currentPlayer = 'X'; initBoard(); } private void initBoard() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { board[i][j] = '-'; } } } private void printBoard() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private boolean makeMove(int row, int col) { if (row < 0 || row >= 15 || col < 0 || col >= 15 || board[row][col] != '-') { return false; } board[row][col] = currentPlayer; return true; } private boolean checkWin(int row, int col) { // 检查水平方向 int count = 0; for (int j = 0; j < 15; j++) { if (board[row][j] == currentPlayer) { count++; if (count == 5) { return true; } } else { count = 0; } } // 检查垂直方向 count = 0; for (int i = 0; i < 15; i++) { if (board[i][col] == currentPlayer) { count++; if (count == 5) { return true; } } else { count = 0; } } // 检查对角线方向 count = 0; for (int i = -4; i <= 4; i++) { int x = row + i; int y = col + i; if (x >= 0 && x < 15 && y >= 0 && y < 15) { if (board[x][y] == currentPlayer) { count++; if (count == 5) { return true; } } else { count = 0; } } } // 检查反对角线方向 count = 0; for (int i = -4; i <= 4; i++) { int x = row + i; int y = col - i; if (x >= 0 && x < 15 && y >= 0 && y < 15) { if (board[x][y] == currentPlayer) { count++; if (count == 5) { return true; } } else { count = 0; } } } return false; } public void play() { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("轮到 " + currentPlayer + " 走棋"); System.out.print("请输入行号:"); int row = scanner.nextInt(); System.out.print("请输入列号:"); int col = scanner.nextInt(); if (makeMove(row, col)) { printBoard(); if (checkWin(row, col)) { System.out.println(currentPlayer + " 赢了!"); break; } currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } else { System.out.println("无效的移动,请重新输入"); } } scanner.close(); } public static void main(String[] args) { GomokuGame game = new GomokuGame(); game.play(); } } ``` 这段代码实现了一个简单的五子棋游戏。通过输入行号和列号来放置棋子,每次放置后会检查是否有一方获胜。游戏界面以字符矩阵形式打印在控制台上。游戏中的光标位置(行号和列号)使用0到14的整数表示。每轮轮到玩家走棋时,需要按照提示输入行号和列号,程序会检验输入的有效性并更新棋盘。如果某一方玩家在任意方向上连续放置了五个相同颜色的棋子,则该玩家获胜,游戏结束。

相关推荐

package com.game.gobang; import javax.imageio.ImageIO; import javax.swing.; import java.awt.; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.net.URLEncoder; import java.util.Objects; public class GameFrame extends JFrame implements ActionListener { /** 游戏面板 / private GamePanel gamePanel; /* * 功能:构造函数
/ public GameFrame() { try { JMenuBar jmb = new JMenuBar(); JMenu jm_game = new JMenu("菜单"); jm_game.setFont(new Font("微软雅黑",Font.PLAIN,12)); JMenuItem jmi_game_new = jm_game.add("新游戏"); jmi_game_new.setFont(new Font("微软雅黑",Font.PLAIN,12)); jmi_game_new.addActionListener(this); jmi_game_new.setActionCommand("new"); jmb.add(jm_game); JMenu jm_help = new JMenu("帮助"); jm_help.setFont(new Font("微软雅黑",Font.PLAIN,12)); JMenuItem jmi_help_about = jm_help.add("游戏规则"); jmi_help_about.setFont(new Font("微软雅黑",Font.PLAIN,12)); jmi_help_about.addActionListener(this); jmi_help_about.setActionCommand("about"); JMenuItem jmi_help_about1 = jm_help.add("截图"); jmi_help_about1.setFont(new Font("微软雅黑",Font.PLAIN,12)); jmi_help_about1.addActionListener(this); jmi_help_about1.setActionCommand("about1"); jmb.add(jm_help); this.setJMenuBar(jmb); //面板 this.gamePanel = new GamePanel(); this.add(this.gamePanel); //显示 this.setTitle("五子棋"); this.setLayout(null); this.setSize(760,680); this.setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } catch(Exception e) { JOptionPane.showMessageDialog(this,"程序出现异常错误,即将退出!\r\n\r\n","提示",JOptionPane.ERROR_MESSAGE); System.exit(0); } } /* * 功能:事件监听
*/ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if("new".equals(command)) { this.gamePanel.newGame(); } else if("about".equals(command)) { JOptionPane.showMessageDialog(this,"游戏胜利条件:五颗白棋或黑棋相连","提示",JOptionPane.INFORMATION_MESSAGE); } else if("about1".equals(command)) { } } } 在这段代码里添加截图功能,能自己命名文件,并且保证可以截图后不会替换上一张的截图。

最新推荐

recommend-type

java GUI实现五子棋游戏

主要为大家详细介绍了java GUI实现五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

node-v18.18.2-headers.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

node-v7.7.3-headers.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

使用C#,生成特定格式Word文档

Word文档的自动化生成,可以根据数据自动形成文档
recommend-type

JavaScript_简单的动画为您的顺风项目.zip

JavaScript
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。