java五子棋游戏代码

java
import java.util.Scanner; public class GobangGame { private static final int BOARD_SIZE = 15; private static final char EMPTY = '-'; private static final char BLACK = '●'; private static final char WHITE = '○'; private char[][] board; public GobangGame() { board = new char[BOARD_SIZE][BOARD_SIZE]; initializeBoard(); } private void initializeBoard() { for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { board[row][col] = EMPTY; } } } private void printBoard() { System.out.print(" "); for (int col = 0; col < BOARD_SIZE; col++) { System.out.print(col + " "); } System.out.println(); for (int row = 0; row < BOARD_SIZE; row++) { System.out.print(row + " "); for (int col = 0; col < BOARD_SIZE; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } } private boolean placePiece(int row, int col, char piece) { if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY) { board[row][col] = piece; return true; } return false; } private boolean isWin(int row, int col, char piece) { // 检查水平方向 int count = 1; int i = 1; while (col - i >= 0 && board[row][col - i] == piece) { count++; i++; } i = 1; while (col + i < BOARD_SIZE && board[row][col + i] == piece) { count++; i++; } if (count >= 5) { return true; } // 检查垂直方向 count = 1; i = 1; while (row - i >= 0 && board[row - i][col] == piece) { count++; i++; } i = 1; while (row + i < BOARD_SIZE && board[row + i][col] == piece) { count++; i++; } if (count >= 5) { return true; } // 检查左上到右下斜线 count = 1; i = 1; while (row - i >= 0 && col - i >= 0 && board[row - i][col - i] == piece) { count++; i++; } i = 1; while (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == piece) { count++; i++; } if (count >= 5) { return true; } // 检查右上到左下斜线 count = 1; i = 1; while (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == piece) { count++; i++; } i = 1; while (row + i < BOARD_SIZE && col - i >= 0 && board[row + i][col - i] == piece) { count++; i++; } return count >= 5; } public void startGame() { Scanner scanner = new Scanner(System.in); boolean isBlack = true; boolean isOver = false; while (!isOver) { printBoard(); if (isBlack) { System.out.println("黑棋,请输入下棋的位置(row col):"); } else { System.out.println("白棋,请输入下棋的位置(row col):"); } int row = scanner.nextInt(); int col = scanner.nextInt(); if (placePiece(row, col, isBlack ? BLACK : WHITE)) { if (isWin(row, col, isBlack ? BLACK : WHITE)) { printBoard(); System.out.println((isBlack ? "黑棋" : "白棋") + "获胜!"); isOver = true; } else { isBlack = !isBlack; } } else { System.out.println("输入位置不合法,请重新输入!"); } } scanner.close(); } public static void main(String[] args) { GobangGame game = new GobangGame(); game.startGame(); } }

这段代码实现了一个简单的控制台五子棋游戏。玩家可以依次输入棋盘上的行和列来放置黑色或白色棋子。游戏会检查是否有一方获胜,如果有则游戏结束并宣布获胜方。

上面的代码已经实现了基本的五子棋游戏逻辑,包括棋盘的初始化、棋子的放置、胜利条件的检查等。接下来,我会为这个游戏添加一些额外的功能,使其更加完善。

平局判断:当棋盘填满但没有一方获胜时,游戏应该宣布平局。

友好的用户界面:可以添加一些简单的界面元素来使用户体验更友好,例如添加棋盘边界的坐标标签。

java
import java.util.Scanner; public class GobangGame { private static final int BOARD_SIZE = 15; private static final char EMPTY = '-'; private static final char BLACK = '●'; private static final char WHITE = '○'; private char[][] board; public GobangGame() { board = new char[BOARD_SIZE][BOARD_SIZE]; initializeBoard(); } private void initializeBoard() { for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { board[row][col] = EMPTY; } } } private void printBoard() { System.out.print(" "); for (int col = 0; col < BOARD_SIZE; col++) { System.out.print(col + " "); } System.out.println(); for (int row = 0; row < BOARD_SIZE; row++) { System.out.print(row + " "); for (int col = 0; col < BOARD_SIZE; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } } private boolean placePiece(int row, int col, char piece) { if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY) { board[row][col] = piece; return true; } return false; } private boolean isWin(int row, int col, char piece) { // 检查水平方向 int count = 1; int i = 1; while (col - i >= 0 && board[row][col - i] == piece) { count++; i++; } i = 1; while (col + i < BOARD_SIZE && board[row][col + i] == piece) { count++; i++; } if (count >= 5) { return true; } // 检查垂直方向 count = 1; i = 1; while (row - i >= 0 && board[row - i][col] == piece) { count++; i++; } i = 1; while (row + i < BOARD_SIZE && board[row + i][col] == piece) { count++; i++; } if (count >= 5) { return true; } // 检查左上到右下斜线 count = 1; i = 1; while (row - i >= 0 && col - i >= 0 && board[row - i][col - i] == piece) { count++; i++; } i = 1; while (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == piece) { count++; i++; } if (count >= 5) { return true; } // 检查右上到左下斜线 count = 1; i = 1; while (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == piece) { count++; i++; } i = 1; while (row + i < BOARD_SIZE && col - i >= 0 && board[row + i][col - i] == piece) { count++; i++; } return count >= 5; } private boolean isBoardFull() { for (int row = 0; row < BOARD_SIZE; row++) { for (int col = 0; col < BOARD_SIZE; col++) { if (board[row][col] == EMPTY) { return false; } } } return true; } public void startGame() { Scanner scanner = new Scanner(System.in); boolean isBlack = true; boolean isOver = false; while (!isOver) { printBoard(); if (isBlack) { System.out.println("黑棋,请输入下棋的位置(row col):"); } else { System.out.println("白棋,请输入下棋的位置(row col):"); } int row = scanner.nextInt(); int col = scanner.nextInt(); if (placePiece(row, col, isBlack ? BLACK : WHITE)) { if (isWin(row, col, isBlack ? BLACK : WHITE)) { printBoard(); System.out.println((isBlack ? "黑棋" : "白棋") + "获胜!"); isOver = true; } else if (isBoardFull()) { printBoard(); System.out.println("平局!"); isOver = true; } else { isBlack = !isBlack; } } else { System.out.println("输入位置不合法,请重新输入!"); } } scanner.close(); } public static void main(String[] args) { GobangGame game = new GobangGame(); game.startGame(); } }

这个版本的代码在原有的基础上增加了平局判断,当棋盘填满而没有一方获胜时,游戏宣布平局。此外,通过修改printBoard()方法,我们在打印棋盘时添加了行和列的标签,使得用户更容易理解棋盘的布局。