parent
c2a4e81ade
commit
f7fb81af47
@ -1,141 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue