From c2a4e81adee1f7c56203c799fea32e621724ea7d Mon Sep 17 00:00:00 2001 From: pf4fw5i2c <2842864518@qq.com> Date: Sat, 13 Apr 2024 12:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=20GomokuGame=20=E7=B1=BB=E6=9D=A5=E8=A1=A8=E7=A4=BA=E4=BA=94?= =?UTF-8?q?=E5=AD=90=E6=A3=8B=E6=B8=B8=E6=88=8F=E3=80=82=E5=9C=A8=20playGa?= =?UTF-8?q?me()=20=E6=96=B9=E6=B3=95=E4=B8=AD=EF=BC=8C=E7=8E=A9=E5=AE=B6?= =?UTF-8?q?=E8=BD=AE=E6=B5=81=E8=BE=93=E5=85=A5=E8=A1=8C=E5=92=8C=E5=88=97?= =?UTF-8?q?=E6=9D=A5=E6=94=BE=E7=BD=AE=E6=A3=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/大体框架.java | 141 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/大体框架.java diff --git a/src/大体框架.java b/src/大体框架.java new file mode 100644 index 0000000..acb43ff --- /dev/null +++ b/src/大体框架.java @@ -0,0 +1,141 @@ +import java.util.Scanner; + +public class GomokuGame { + private static final int BOARD_SIZE = 15; + private static final char EMPTY = '.'; + private static final char PLAYER1_SYMBOL = 'X'; + private static final char PLAYER2_SYMBOL = 'O'; + + private char[][] board; + private char currentPlayer; + + public GomokuGame() { + board = new char[BOARD_SIZE][BOARD_SIZE]; + currentPlayer = PLAYER1_SYMBOL; + initializeBoard(); + } + + private void initializeBoard() { + for (int i = 0; i < BOARD_SIZE; i++) { + for (int j = 0; j < BOARD_SIZE; j++) { + board[i][j] = EMPTY; + } + } + } + + private 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 boolean isValidMove(int row, int col) { + return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY; + } + + private boolean hasWon(int row, int col) { + return hasRowWin(row) || hasColumnWin(col) || hasDiagonalWin(row, col) || hasAntiDiagonalWin(row, col); + } + + private boolean hasRowWin(int row) { + int count = 0; + for (int j = 0; j < BOARD_SIZE; j++) { + if (board[row][j] == currentPlayer) { + count++; + if (count == 5) { + return true; + } + } else { + count = 0; + } + } + return false; + } + + private boolean hasColumnWin(int col) { + int count = 0; + for (int i = 0; i < BOARD_SIZE; i++) { + if (board[i][col] == currentPlayer) { + count++; + if (count == 5) { + return true; + } + } else { + count = 0; + } + } + return false; + } + + private boolean hasDiagonalWin(int row, int col) { + int startRow = Math.max(0, row - 4); + int startCol = Math.max(0, col - 4); + int endRow = Math.min(BOARD_SIZE - 1, row + 4); + int endCol = Math.min(BOARD_SIZE - 1, col + 4); + + int count = 0; + for (int i = startRow, j = startCol; i <= endRow && j <= endCol; i++, j++) { + if (board[i][j] == currentPlayer) { + count++; + if (count == 5) { + return true; + } + } else { + count = 0; + } + } + return false; + } + + private boolean hasAntiDiagonalWin(int row, int col) { + int startRow = Math.min(BOARD_SIZE - 1, row + 4); + int startCol = Math.max(0, col - 4); + int endRow = Math.max(0, row - 4); + int endCol = Math.min(BOARD_SIZE - 1, col + 4); + + int count = 0; + for (int i = startRow, j = startCol; i >= endRow && j <= endCol; i--, j++) { + if (board[i][j] == currentPlayer) { + count++; + if (count == 5) { + return true; + } + } else { + count = 0; + } + } + return false; + } + + public void playGame() { + Scanner scanner = new Scanner(System.in); + boolean gameOver = false; + while (!gameOver) { + System.out.println("Current board:"); + printBoard(); + System.out.println("Player " + currentPlayer + ", enter row and column (0-14):"); + int row = scanner.nextInt(); + int col = scanner.nextInt(); + if (isValidMove(row, col)) { + board[row][col] = currentPlayer; + if (hasWon(row, col)) { + System.out.println("Player " + currentPlayer + " wins!"); + gameOver = true; + } else { + currentPlayer = (currentPlayer == PLAYER1_SYMBOL) ? PLAYER2_SYMBOL : PLAYER1_SYMBOL; + } + } else { + System.out.println("Invalid move. Please choose an empty spot."); + } + } + scanner.close(); + } + + public static void main(String[] args) { + GomokuGame game = new GomokuGame(); + game.playGame(); + } +}