java制作五子棋游戏

时间: 2023-11-24 07:20:36 浏览: 24
下面是一个简单的 Java 五子棋游戏示例,供参考: ```java import java.util.Scanner; public class Gomoku { private static final int ROWS = 15; private static final int COLS = 15; private static final int EMPTY = 0; private static final int BLACK = 1; private static final int WHITE = 2; private int[][] board = new int[ROWS][COLS]; private int player = BLACK; public void start() { Scanner scanner = new Scanner(System.in); while (true) { // 打印棋盘 printBoard(); // 获取玩家输入的坐标 System.out.print("请玩家 " + player + " 输入坐标,格式为 行 列:"); int row = scanner.nextInt(); int col = scanner.nextInt(); // 判断是否落子成功 if (placePiece(row, col)) { // 判断是否获胜 if (checkWin(row, col)) { System.out.println("玩家 " + player + " 获胜!"); printBoard(); break; } // 切换玩家 player = (player == BLACK) ? WHITE : BLACK; } else { System.out.println("该位置已经有子,请重新输入!"); } } } // 打印棋盘 private void printBoard() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } // 落子 private boolean placePiece(int row, int col) { if (board[row][col] == EMPTY) { board[row][col] = player; return true; } else { return false; } } // 判断是否获胜 private boolean checkWin(int row, int col) { int count = 1; // 横向 for (int i = col - 1; i >= 0; i--) { if (board[row][i] == player) { count++; } else { break; } } for (int i = col + 1; i < COLS; i++) { if (board[row][i] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 纵向 count = 1; for (int i = row - 1; i >= 0; i--) { if (board[i][col] == player) { count++; } else { break; } } for (int i = row + 1; i < ROWS; i++) { if (board[i][col] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 左上右下斜线 count = 1; for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == player) { count++; } else { break; } } for (int i = row + 1, j = col + 1; i < ROWS && j < COLS; i++, j++) { if (board[i][j] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 右上左下斜线 count = 1; for (int i = row - 1, j = col + 1; i >= 0 && j < COLS; i--, j++) { if (board[i][j] == player) { count++; } else { break; } } for (int i = row + 1, j = col - 1; i < ROWS && j >= 0; i++, j--) { if (board[i][j] == player) { count++; } else { break; } } if (count >= 5) { return true; } // 没有获胜 return false; } public static void main(String[] args) { Gomoku game = new Gomoku(); game.start(); } } ``` 这个示例使用了一个二维数组来表示棋盘,玩家输入坐标后,通过 `placePiece` 方法把棋子放到相应的位置上,然后判断是否获胜,如果获胜就结束游戏,否则切换玩家。`checkWin` 方法实现了判断是否获胜的逻辑,通过遍历棋盘上的位置,计算横向、纵向和斜线方向上是否有连续的五个相同颜色的棋子。

相关推荐

以下是 Java 实现五子棋游戏的简单步骤: 1. 创建一个棋盘类(Board),在棋盘上绘制出 15×15 的网格。 2. 定义一个棋子类(Piece),包括棋子的颜色和位置。 3. 创建一个游戏类(Game),它包括一个棋盘和两个玩家。可以通过键盘输入或鼠标点击来落子。 4. 实现落子规则,每当一个玩家下完一步棋后,判断是否有五个同色棋子相连,如果有则该玩家获胜。 5. 实现 AI 玩家,让计算机能够下棋。可以使用简单的算法,如随机下棋或简单的搜索算法。 以下是示例代码: 棋盘类 Board: public class Board { private int[][] grid; public Board() { grid = new int[15][15]; } public void draw() { // 绘制棋盘 } public boolean isValidMove(int x, int y) { // 判断落子是否合法 } public void placePiece(Piece piece) { // 在棋盘上放置棋子 } public boolean checkWin(Piece piece) { // 判断是否获胜 } } 棋子类 Piece: public class Piece { private int x; private int y; private int color; public Piece(int x, int y, int color) { this.x = x; this.y = y; this.color = color; } public int getX() { return x; } public int getY() { return y; } public int getColor() { return color; } } 游戏类 Game: import java.util.Scanner; public class Game { private Board board; private Player player1; private Player player2; private Player currentPlayer; public Game() { board = new Board(); player1 = new Player("Player 1", 1); player2 = new AIPlayer("AI", 2); currentPlayer = player1; } public void start() { while (true) { board.draw(); System.out.println(currentPlayer.getName() + " turn."); int x, y; if (currentPlayer instanceof AIPlayer) { x = ((AIPlayer) currentPlayer).getNextMove(board); y = ((AIPlayer) currentPlayer).getNextMove(board); System.out.println("AI placed a piece at (" + x + ", " + y + ")"); } else { Scanner scanner = new Scanner(System.in); System.out.print("Enter x: "); x = scanner.nextInt(); System.out.print("Enter y: "); y = scanner.nextInt(); } Piece piece = new Piece(x, y, currentPlayer.getColor()); if (board.isValidMove(x, y)) { board.placePiece(piece); if (board.checkWin(piece)) { System.out.println(currentPlayer.getName() + " wins!"); break; } currentPlayer = (currentPlayer == player1) ? player2 : player1; } else { System.out.println("Invalid move."); } } } } 玩家类 Player: public class Player { private String name; private int color; public Player(String name, int color) { this.name = name; this.color = color; } public String getName() { return name; } public int getColor() { return color; } } AI 玩家类 AIPlayer: import java.util.Random; public class AIPlayer extends Player { private Random random; public AIPlayer(String name, int color) { super(name, color); random = new Random(); } public int getNextMove(Board board) { int x = random.nextInt(15); int y = random.nextInt(15); while (!board.isValidMove(x, y)) { x = random.nextInt(15); y = random.nextInt(15); } return x; } } 以上是一个基本的五子棋游戏实现,可以根据需求进行修改和扩展。
以下是一个简单的 Java 五子棋游戏代码,供参考: java import java.util.Scanner; public class Gobang { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[][] board = new int[15][15]; int player = 1; int row, col; while (true) { System.out.print("Player " + player + " make a move (row, col): "); row = scanner.nextInt(); col = scanner.nextInt(); if (board[row][col] != 0) { System.out.println("Invalid move, try again."); continue; } board[row][col] = player; printBoard(board); if (checkWin(board, row, col, player)) { System.out.println("Player " + player + " wins!"); break; } player = 3 - player; // switch to the other player } } public static void printBoard(int[][] board) { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } public static boolean checkWin(int[][] board, int row, int col, int player) { int count = 0; // horizontal for (int j = col - 4; j <= col; j++) { if (j < 0 || j + 4 >= 15) { continue; } count = 0; for (int k = 0; k < 5; k++) { if (board[row][j + k] == player) { count++; } } if (count == 5) { return true; } } // vertical for (int i = row - 4; i <= row; i++) { if (i < 0 || i + 4 >= 15) { continue; } count = 0; for (int k = 0; k < 5; k++) { if (board[i + k][col] == player) { count++; } } if (count == 5) { return true; } } // diagonal (top-left to bottom-right) for (int i = row - 4, j = col - 4; i <= row && j <= col; i++, j++) { if (i < 0 || j < 0 || i + 4 >= 15 || j + 4 >= 15) { continue; } count = 0; for (int k = 0; k < 5; k++) { if (board[i + k][j + k] == player) { count++; } } if (count == 5) { return true; } } // diagonal (bottom-left to top-right) for (int i = row + 4, j = col - 4; i >= row && j <= col; i--, j++) { if (i >= 15 || j < 0 || i - 4 < 0 || j + 4 >= 15) { continue; } count = 0; for (int k = 0; k < 5; k++) { if (board[i - k][j + k] == player) { count++; } } if (count == 5) { return true; } } return false; } } 该代码使用一个二维数组 board 表示棋盘,每个空格的值为 0 表示未落子,1 表示黑子,2 表示白子。每个回合轮流由两个玩家输入坐标,在棋盘上落子,然后判断是否有任何一方胜利。checkWin() 方法用于检查一个玩家在该回合是否获胜,它依次检查横向、纵向和两个对角线上是否有连续的五颗同色棋子。
以下是一个简单的五子棋游戏的 Java 代码: java import java.util.Scanner; public class FiveInARow { private static final int ROWS = 15; private static final int COLS = 15; private static final int EMPTY = 0; private static final int PLAYER1 = 1; private static final int PLAYER2 = 2; private static int[][] board = new int[ROWS][COLS]; public static void main(String[] args) { int player = PLAYER1; Scanner scanner = new Scanner(System.in); while (true) { printBoard(); System.out.print("Player " + player + ", enter row and column (e.g. 3 5): "); int row = scanner.nextInt(); int col = scanner.nextInt(); if (makeMove(row, col, player)) { System.out.println("Player " + player + " wins!"); printBoard(); break; } player = (player == PLAYER1) ? PLAYER2 : PLAYER1; } scanner.close(); } private static boolean makeMove(int row, int col, int player) { board[row][col] = player; int count = 1; // check horizontal for (int c = col - 1; c >= 0 && board[row][c] == player; c--) count++; for (int c = col + 1; c < COLS && board[row][c] == player; c++) count++; if (count >= 5) return true; // check vertical count = 1; for (int r = row - 1; r >= 0 && board[r][col] == player; r--) count++; for (int r = row + 1; r < ROWS && board[r][col] == player; r++) count++; if (count >= 5) return true; // check diagonal (top-left to bottom-right) count = 1; for (int r = row - 1, c = col - 1; r >= 0 && c >= 0 && board[r][c] == player; r--, c--) count++; for (int r = row + 1, c = col + 1; r < ROWS && c < COLS && board[r][c] == player; r++, c++) count++; if (count >= 5) return true; // check diagonal (bottom-left to top-right) count = 1; for (int r = row + 1, c = col - 1; r < ROWS && c >= 0 && board[r][c] == player; r++, c--) count++; for (int r = row - 1, c = col + 1; r >= 0 && c < COLS && board[r][c] == player; r--, c++) count++; if (count >= 5) return true; return false; } private static void printBoard() { for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { if (board[r][c] == EMPTY) System.out.print("."); else if (board[r][c] == PLAYER1) System.out.print("X"); else if (board[r][c] == PLAYER2) System.out.print("O"); } System.out.println(); } } } 这个程序使用一个二维数组 board 来存储棋盘上的状态,使用常量 EMPTY、PLAYER1、PLAYER2 来表示空、玩家1、玩家2的棋子。makeMove 方法用来检查每一个新落子是否构成五子连珠,使用四个循环分别检查水平、竖直、左上到右下和右上到左下方向上的连珠情况。printBoard 方法用来打印当前的棋盘状态。程序使用一个 while 循环来不断交替让两个玩家下棋,直到某一方获胜。
好的,以下是一个简单的 Java 五子棋游戏示例: java import java.util.Scanner; public class GomokuGame { private static final int BOARD_SIZE = 15; private static final char[][] board = new char[BOARD_SIZE][BOARD_SIZE]; private static final char BLACK = 'X'; private static final char WHITE = 'O'; private static boolean blackTurn = true; public static void main(String[] args) { initializeBoard(); displayBoard(); Scanner scanner = new Scanner(System.in); while (true) { System.out.print(getCurrentPlayer() + " player's turn. Enter row and column (e.g. '3 4'): "); int row = scanner.nextInt(); int col = scanner.nextInt(); if (isValidMove(row, col)) { makeMove(row, col); displayBoard(); if (isGameOver(row, col)) { break; } blackTurn = !blackTurn; } else { System.out.println("Invalid move. Try again."); } } System.out.println(getCurrentPlayer() + " player wins!"); } private static void initializeBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = '-'; } } } private static void displayBoard() { System.out.print(" "); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + " "); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private static char getCurrentPlayer() { return blackTurn ? BLACK : WHITE; } private static boolean isValidMove(int row, int col) { if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) { return false; } return board[row][col] == '-'; } private static void makeMove(int row, int col) { board[row][col] = getCurrentPlayer(); } private static boolean isGameOver(int row, int col) { return isHorizontalWin(row) || isVerticalWin(col) || isDiagonalWin(row, col); } private static boolean isHorizontalWin(int row) { int count = 0; for (int i = 0; i < BOARD_SIZE; i++) { if (board[row][i] == getCurrentPlayer()) { count++; if (count == 5) { return true; } } else { count = 0; } } return false; } private static boolean isVerticalWin(int col) { int count = 0; for (int i = 0; i < BOARD_SIZE; i++) { if (board[i][col] == getCurrentPlayer()) { count++; if (count == 5) { return true; } } else { count = 0; } } return false; } private static boolean isDiagonalWin(int row, int col) { int count = 0; int i = row, j = col; while (i >= 0 && j >= 0 && board[i][j] == getCurrentPlayer()) { count++; i--; j--; } i = row + 1; j = col + 1; while (i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == getCurrentPlayer()) { count++; i++; j++; } if (count >= 5) { return true; } count = 0; i = row; j = col; while (i >= 0 && j < BOARD_SIZE && board[i][j] == getCurrentPlayer()) { count++; i--; j++; } i = row + 1; j = col - 1; while (i < BOARD_SIZE && j >= 0 && board[i][j] == getCurrentPlayer()) { count++; i++; j--; } return count >= 5; } } 这个示例使用一个二维字符数组来表示棋盘,其中 '-' 表示空格,'X' 表示黑棋,'O' 表示白棋。程序会交替让黑棋和白棋下棋,直到有一方胜利。胜利的条件是其中一方在横、竖、斜方向上连成五个棋子。
非常感谢您的提问,以下是 Java 编写五子棋游戏代码的示例: java import java.util.Scanner; public class Gobang { private static final int BOARD_SIZE = 15; private String[][] board = new String[BOARD_SIZE][BOARD_SIZE]; private String player1; private String player2; public Gobang(String player1, String player2) { this.player1 = player1; this.player2 = player2; initBoard(); } private void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = "+"; } } } public void start() { Scanner scanner = new Scanner(System.in); String currentPlayer = player1; while (true) { printBoard(); System.out.println(currentPlayer + ",请输入您要下的位置,格式为x,y:"); String input = scanner.nextLine(); String[] positions = input.split(","); int x = Integer.parseInt(positions[0]); int y = Integer.parseInt(positions[1]); if (board[x][y].equals("+")) { board[x][y] = currentPlayer.equals(player1) ? "O" : "X"; if (isWin(x, y)) { printBoard(); System.out.println(currentPlayer + "获胜!"); break; } currentPlayer = currentPlayer.equals(player1) ? player2 : player1; } else { System.out.println("该位置已经有棋子,请重新输入!"); } } } private boolean isWin(int x, int y) { String symbol = board[x][y]; int count = 1; int i, j; // 横向判断 i = x; j = y - 1; while (j >= 0 && board[i][j].equals(symbol)) { count++; j--; } j = y + 1; while (j < BOARD_SIZE && board[i][j].equals(symbol)) { count++; j++; } if (count >= 5) { return true; } // 竖向判断 count = 1; i = x - 1; j = y; while (i >= 0 && board[i][j].equals(symbol)) { count++; i--; } i = x + 1; while (i < BOARD_SIZE && board[i][j].equals(symbol)) { count++; i++; } if (count >= 5) { return true; } // 左上到右下判断 count = 1; i = x - 1; j = y - 1; while (i >= 0 && j >= 0 && board[i][j].equals(symbol)) { count++; i--; j--; } i = x + 1; j = y + 1; while (i < BOARD_SIZE && j < BOARD_SIZE && board[i][j].equals(symbol)) { count++; i++; j++; } if (count >= 5) { return true; } // 右上到左下判断 count = 1; i = x - 1; j = y + 1; while (i >= 0 && j < BOARD_SIZE && board[i][j].equals(symbol)) { count++; i--; j++; } i = x + 1; j = y - 1; while (i < BOARD_SIZE && j >= 0 && board[i][j].equals(symbol)) { count++; i++; j--; } if (count >= 5) { return true; } return false; } private void printBoard() { System.out.print(" "); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + " "); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } } 如果您有任何问题或需要进一步的帮助,请随时联系我。
好的,我可以为您提供一些关于Java五子棋小游戏的信息。五子棋是一种非常受欢迎的策略游戏,它可以在控制台或图形用户界面中实现。以下是一个简单的Java五子棋小游戏的示例代码: java import java.util.Scanner; public class Gobang { private static int BOARD_SIZE = 15; private String[][] board; public void initBoard() { board = new String[BOARD_SIZE][BOARD_SIZE]; for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = "+"; } } } public void printBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j]); } System.out.print("\n"); } } public void play() { Scanner scanner = new Scanner(System.in); int x = 0, y = 0; String color = "x"; while (true) { System.out.print("请输入x坐标:"); x = scanner.nextInt(); System.out.print("请输入y坐标:"); y = scanner.nextInt(); if (board[x][y].equals("+")) { board[x][y] = color; if (isWin(x, y)) { System.out.println(color + "赢了!"); break; } color = color.equals("x") ? "o" : "x"; } else { System.out.println("该位置已经有棋子了,请重新输入!"); } printBoard(); } } public boolean isWin(int x, int y) { String color = board[x][y]; int count = 1; // 横向 for (int i = x - 1; i >= 0; i--) { if (board[i][y].equals(color)) { count++; } else { break; } } for (int i = x + 1; i < BOARD_SIZE; i++) { if (board[i][y].equals(color)) { count++; } else { break; } } if (count >= 5) { return true; } // 纵向 count = 1; for (int i = y - 1; i >= 0; i--) { if (board[x][i].equals(color)) { count++; } else { break; } } for (int i = y + 1; i < BOARD_SIZE; i++) { if (board[x][i].equals(color)) { count++; } else { break; } } if (count >= 5) { return true; } // 左上到右下 count = 1; for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j].equals(color)) { count++; } else { break; } } for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE; i++, j++) { if (board[i][j].equals(color)) { count++; } else { break; } } if (count >= 5) { return true; } // 右上到左下 count = 1; for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE; i--, j++) { if (board[i][j].equals(color)) { count++; } else { break; } } for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0; i++, j--) { if (board[i][j].equals(color)) { count++; } else { break; } } if (count >= 5) { return true; } return false; } public static void main(String[] args) { Gobang game = new Gobang(); game.initBoard(); game.printBoard(); game.play(); } } 这个示例代码实现了一个简单的五子棋游戏,玩家可以在控制台中输入坐标来下棋,程序会判断是否有玩家获胜。
下面是一个简单的 Java 五子棋游戏实现的代码示例: java import java.util.Scanner; public class Gomoku { private static final int BOARD_SIZE = 15; private static final char[][] board = new char[BOARD_SIZE][BOARD_SIZE]; private static final Scanner scanner = new Scanner(System.in); private static int x, y; public static void main(String[] args) { initBoard(); printBoard(); while (true) { playerRound('X'); if (isWin(x, y)) { System.out.println("Player X wins!"); break; } if (isDraw()) { System.out.println("Draw!"); break; } playerRound('O'); if (isWin(x, y)) { System.out.println("Player O wins!"); break; } if (isDraw()) { System.out.println("Draw!"); break; } } } private static void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = '-'; } } } private static void printBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } private static void playerRound(char player) { System.out.println("Player " + player + ", enter your move (row[1-" + BOARD_SIZE + "] column[1-" + BOARD_SIZE + "]):"); while (true) { String input = scanner.nextLine(); String[] parts = input.split(" "); if (parts.length != 2) { System.out.println("Invalid input!"); continue; } try { int row = Integer.parseInt(parts[0]) - 1; int col = Integer.parseInt(parts[1]) - 1; if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) { System.out.println("Invalid input!"); continue; } if (board[row][col] != '-') { System.out.println("Invalid move!"); continue; } board[row][col] = player; x = row; y = col; printBoard(); break; } catch (NumberFormatException e) { System.out.println("Invalid input!"); } } } private static boolean isWin(int row, int col) { char player = board[row][col]; int count = 0; for (int i = col - 4; i <= col + 4; i++) { if (i < 0 || i >= BOARD_SIZE) { continue; } if (board[row][i] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } count = 0; for (int i = row - 4; i <= row + 4; i++) { if (i < 0 || i >= BOARD_SIZE) { continue; } if (board[i][col] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } count = 0; for (int i = -4; i <= 4; i++) { int r = row + i; int c = col + i; if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) { continue; } if (board[r][c] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } count = 0; for (int i = -4; i <= 4; i++) { int r = row + i; int c = col - i; if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) { continue; } if (board[r][c] == player) { count++; if (count == 5) { return true; } } else { count = 0; } } return false; } private static boolean isDraw() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] == '-') { return false; } } } return true; } } 这个代码实现了一个简单的控制台五子棋游戏。在游戏中,玩家执黑子('X'),电脑执白子('O')。玩家和电脑轮流下棋,如果某一方连成五子,则游戏结束。如果棋盘填满而没有任何一方连成五子,则游戏平局。
以下是一个简单的Java五子棋游戏代码示例: import java.util.Scanner; public class Gobang { public static final int BOARD_SIZE = 15; public String[][] board = new String[BOARD_SIZE][BOARD_SIZE]; public static final String PLAYER1 = "X"; public static final String PLAYER2 = "O"; public String currentPlayer; public void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = "+"; } } } public void printBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } public boolean isBoardFull() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j].equals("+")) { return false; } } } return true; } public boolean isWinningMove(int row, int col) { String player = board[row][col]; int count = 0; for (int i = 0; i < BOARD_SIZE; i++) { if (board[row][i].equals(player)) { count++; } else { count = 0; } if (count == 5) { return true; } } count = 0; for (int i = 0; i < BOARD_SIZE; i++) { if (board[i][col].equals(player)) { count++; } else { count = 0; } if (count == 5) { return true; } } count = 0; for (int i = -4; i <= 4; i++) { int r = row + i; int c = col + i; if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE) { if (board[r][c].equals(player)) { count++; } else { count = 0; } if (count == 5) { return true; } } } count = 0; for (int i = -4; i <= 4; i++) { int r = row + i; int c = col - i; if (r >= 0 && r < BOARD_SIZE && c >= 0 && c < BOARD_SIZE) { if (board[r][c].equals(player)) { count++; } else { count = 0; } if (count == 5) { return true; } } } return false; } public void play() { initBoard(); currentPlayer = PLAYER1; Scanner scanner = new Scanner(System.in); while (true) { printBoard(); System.out.print("Player " + currentPlayer + ", enter row number: "); int row = scanner.nextInt(); System.out.print("Player " + currentPlayer + ", enter column number: "); int col = scanner.nextInt(); if (board[row][col].equals("+")) { board[row][col] = currentPlayer; if (isWinningMove(row, col)) { printBoard(); System.out.println("Player " + currentPlayer + " wins!"); break; } else if (isBoardFull()) { printBoard(); System.out.println("Game is a tie!"); break; } else { if (currentPlayer.equals(PLAYER1)) { currentPlayer = PLAYER2; } else { currentPlayer = PLAYER1; } } } else { System.out.println("That position is already taken. Try again."); } } } public static void main(String[] args) { Gobang game = new Gobang(); game.play(); } } 这个代码实现了一个交互式的五子棋游戏,玩家可以通过输入行和列号来下棋,程序会检查是否有五子连珠,以及棋盘是否已经下满,如果有则宣布胜者或平局。

最新推荐

基于c++ ege图形库实现五子棋游戏

主要为大家详细介绍了基于c++ ege图形库实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

java GUI实现五子棋游戏

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

javafx实现五子棋游戏

主要为大家详细介绍了javafx实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

MATLAB实现五子棋游戏(双人对战、可悔棋)

主要为大家详细介绍了MATLAB实现五子棋游戏,可以进行双人对战、也可悔棋,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

python五子棋游戏的设计与实现

主要为大家详细介绍了python五子棋游戏的设计与实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

DAC简介及参考电路PPT学习教案.pptx

DAC简介及参考电路PPT学习教案.pptx

管理建模和仿真的文件

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

OceanBase数据库简介及原理解析

# 1. OceanBase数据库概述 ## 1.1 OceanBase数据库的发展历程 OceanBase数据库是由阿里巴巴集团自主研发的一款分布式关系型数据库系统,起源于阿里集团的业务需求和技术挑战。自2010年开始研发,经过多年的迭代和优化,OceanBase已经成为阿里巴巴集团重要的核心数据库产品之一。在实际的业务场景中,OceanBase已经被广泛应用于金融、电商、物流等各个领域。 ## 1.2 OceanBase数据库的特点和优势 OceanBase数据库具有以下特点和优势: - **分布式架构**:OceanBase采用多副本分布式架构,可以实现高可用和数据容灾。 -

python的pandas读取excel怎么将excel表里的列从1开始而不是0

在 Pandas 中,数据框的列默认从0开始编号,但我们可以通过自定义函数来进行数据列的转换。可以先将读取的数据框列的第一个值设为1,然后对后续列进行递增处理。 以下是示例代码: ```python import pandas as pd # 读取 Excel 文件 df = pd.read_excel('your_excel_file.xlsx') # 定义函数将列从1开始 def reset_column(x): return str(int(x) + 1) # 应用函数到所有列名 df = df.rename(columns=reset_column) # 打印数据框

第三章薪酬水平、薪酬系统的运行与控制.pptx

第三章薪酬水平、薪酬系统的运行与控制.pptx