java简易五子棋
时间: 2023-08-10 13:07:21 浏览: 48
好的,我可以为您提供一个简易的Java五子棋游戏,以下是代码实现:
```java
import java.util.Scanner;
public class FiveInARow {
static final int BOARD_SIZE = 15;
static int[][] board = new int[BOARD_SIZE][BOARD_SIZE];
static final int BLACK = 1;
static final int WHITE = 2;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int player = BLACK;
while (true) {
printBoard();
System.out.print("Player " + player + ", please enter your move (row column): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (isValidMove(row, col)) {
board[row][col] = player;
if (isWinningMove(row, col)) {
System.out.println("Player " + player + " wins!");
break;
}
player = (player == BLACK) ? WHITE : BLACK;
} else {
System.out.println("Invalid move. Please try again.");
}
}
scanner.close();
}
static boolean isValidMove(int row, int col) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return false;
}
return board[row][col] == 0;
}
static boolean isWinningMove(int row, int col) {
int player = board[row][col];
// check horizontal
int count = 0;
for (int c = col - 4; c <= col + 4; c++) {
if (c < 0 || c >= BOARD_SIZE) {
continue;
}
if (board[row][c] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// check vertical
count = 0;
for (int r = row - 4; r <= row + 4; r++) {
if (r < 0 || r >= BOARD_SIZE) {
continue;
}
if (board[r][col] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// check diagonal (top-left to bottom-right)
count = 0;
for (int r = row - 4, c = col - 4; r <= row + 4; r++, c++) {
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;
}
}
// check diagonal (top-right to bottom-left)
count = 0;
for (int r = row - 4, c = col + 4; r <= row + 4; r++, c--) {
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;
}
static void printBoard() {
System.out.print(" ");
for (int c = 0; c < BOARD_SIZE; c++) {
System.out.print(c + " ");
}
System.out.println();
for (int r = 0; r < BOARD_SIZE; r++) {
System.out.print(r + " ");
for (int c = 0; c < BOARD_SIZE; c++) {
if (board[r][c] == BLACK) {
System.out.print("B ");
} else if (board[r][c] == WHITE) {
System.out.print("W ");
} else {
System.out.print("+ ");
}
}
System.out.println();
}
}
}
```
这个程序实现了一个简单的五子棋游戏,玩家可以交替下棋,直到有一方获胜。游戏界面使用字符表示,"B" 表示黑棋,"W" 表示白棋,"+" 表示空位。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)