java 五子棋游戏实现
时间: 2023-10-21 13:05:22 浏览: 83
下面是一个简单的 Java 五子棋游戏实现的代码示例:
```java
import java.util.Scanner;
public class Gomoku {
private static final int BOARD_SIZE = 15;
private static final char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
private static final Scanner scanner = new Scanner(System.in);
private static int x, y;
public static void main(String[] args) {
initBoard();
printBoard();
while (true) {
playerRound('X');
if (isWin(x, y)) {
System.out.println("Player X wins!");
break;
}
if (isDraw()) {
System.out.println("Draw!");
break;
}
playerRound('O');
if (isWin(x, y)) {
System.out.println("Player O wins!");
break;
}
if (isDraw()) {
System.out.println("Draw!");
break;
}
}
}
private static void initBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '-';
}
}
}
private static 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 static void playerRound(char player) {
System.out.println("Player " + player + ", enter your move (row[1-" + BOARD_SIZE + "] column[1-" + BOARD_SIZE + "]):");
while (true) {
String input = scanner.nextLine();
String[] parts = input.split(" ");
if (parts.length != 2) {
System.out.println("Invalid input!");
continue;
}
try {
int row = Integer.parseInt(parts[0]) - 1;
int col = Integer.parseInt(parts[1]) - 1;
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
System.out.println("Invalid input!");
continue;
}
if (board[row][col] != '-') {
System.out.println("Invalid move!");
continue;
}
board[row][col] = player;
x = row;
y = col;
printBoard();
break;
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
}
}
private static boolean isWin(int row, int col) {
char player = board[row][col];
int count = 0;
for (int i = col - 4; i <= col + 4; i++) {
if (i < 0 || i >= BOARD_SIZE) {
continue;
}
if (board[row][i] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
count = 0;
for (int i = row - 4; i <= row + 4; i++) {
if (i < 0 || i >= BOARD_SIZE) {
continue;
}
if (board[i][col] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
count = 0;
for (int i = -4; i <= 4; i++) {
int r = row + i;
int c = col + i;
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;
}
}
count = 0;
for (int i = -4; i <= 4; i++) {
int r = row + i;
int c = col - i;
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;
}
private static boolean isDraw() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
```
这个代码实现了一个简单的控制台五子棋游戏。在游戏中,玩家执黑子('X'),电脑执白子('O')。玩家和电脑轮流下棋,如果某一方连成五子,则游戏结束。如果棋盘填满而没有任何一方连成五子,则游戏平局。
相关推荐
![](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)