用java写一个五子棋游戏
时间: 2023-12-30 12:02:59 浏览: 26
好的,以下是一个简单的五子棋游戏的Java代码。本游戏使用命令行界面,玩家通过输入坐标来下棋。
```java
import java.util.Scanner;
public class GomokuGame {
private static final int ROW = 15;
private static final int COLUMN = 15;
private static final int EMPTY = 0;
private static final int BLACK = 1;
private static final int WHITE = 2;
private static int[][] board = new int[ROW][COLUMN];
private static int currentPlayer = BLACK;
public static void main(String[] args) {
printBoard();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Player " + currentPlayer + " make a move:");
int row = scanner.nextInt();
int column = scanner.nextInt();
if (isValidMove(row, column)) {
makeMove(row, column);
printBoard();
if (isWin()) {
System.out.println("Player " + currentPlayer + " wins!");
break;
}
if (isDraw()) {
System.out.println("The game is a draw!");
break;
}
currentPlayer = currentPlayer == BLACK ? WHITE : BLACK;
} else {
System.out.println("Invalid move, please try again!");
}
}
}
private static boolean isValidMove(int row, int column) {
if (row < 0 || row >= ROW || column < 0 || column >= COLUMN || board[row][column] != EMPTY) {
return false;
}
return true;
}
private static void makeMove(int row, int column) {
board[row][column] = currentPlayer;
}
private static boolean isWin() {
// Check rows
for (int i = 0; i < ROW; i++) {
for (int j = 0; j <= COLUMN - 5; j++) {
if (board[i][j] != EMPTY && board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] == board[i][j+3] && board[i][j] == board[i][j+4]) {
return true;
}
}
}
// Check columns
for (int i = 0; i <= ROW - 5; i++) {
for (int j = 0; j < COLUMN; j++) {
if (board[i][j] != EMPTY && board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] && board[i][j] == board[i+3][j] && board[i][j] == board[i+4][j]) {
return true;
}
}
}
// Check diagonal from top left to bottom right
for (int i = 0; i <= ROW - 5; i++) {
for (int j = 0; j <= COLUMN - 5; j++) {
if (board[i][j] != EMPTY && board[i][j] == board[i+1][j+1] && board[i][j] == board[i+2][j+2] && board[i][j] == board[i+3][j+3] && board[i][j] == board[i+4][j+4]) {
return true;
}
}
}
// Check diagonal from top right to bottom left
for (int i = 0; i <= ROW - 5; i++) {
for (int j = COLUMN - 1; j >= 4; j--) {
if (board[i][j] != EMPTY && board[i][j] == board[i+1][j-1] && board[i][j] == board[i+2][j-2] && board[i][j] == board[i+3][j-3] && board[i][j] == board[i+4][j-4]) {
return true;
}
}
}
return false;
}
private static boolean isDraw() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
private static void printBoard() {
System.out.print(" ");
for (int i = 0; i < COLUMN; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < ROW; i++) {
System.out.print(i + " ");
for (int j = 0; j < COLUMN; j++) {
if (board[i][j] == EMPTY) {
System.out.print("+ ");
} else if (board[i][j] == BLACK) {
System.out.print("B ");
} else {
System.out.print("W ");
}
}
System.out.println(i);
}
System.out.print(" ");
for (int i = 0; i < COLUMN; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}
```
希望这个代码能够帮到你。如果你有任何问题或需要进一步的解释,请随时提出。
阅读全文