人机对战五子棋游戏Java 语言
时间: 2023-09-04 10:06:34 浏览: 106
以下是一个简单的人机对战五子棋游戏Java语言实现:
```java
import java.util.Scanner;
public class Gobang {
private static final int BOARD_SIZE = 15; // 棋盘大小
private static final int WIN_COUNT = 5; // 获胜所需棋子数
private static final char EMPTY = ' '; // 空位
private static final char HUMAN = 'X'; // 玩家棋子
private static final char COMPUTER = 'O'; // 电脑棋子
private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE]; // 棋盘
private static Scanner scanner = new Scanner(System.in); // 输入流
public static void main(String[] args) {
initBoard();
printBoard();
while (true) {
humanTurn();
if (checkWin(HUMAN)) {
System.out.println("恭喜你,你赢了!");
break;
}
computerTurn();
if (checkWin(COMPUTER)) {
System.out.println("很遗憾,你输了!");
break;
}
}
}
// 初始化棋盘
private static void initBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
// 打印棋盘
private static void printBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// 玩家下棋
private static void humanTurn() {
int x, y;
while (true) {
System.out.print("请输入您要下棋的坐标(例如:3 4):");
x = scanner.nextInt();
y = scanner.nextInt();
if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == EMPTY) {
board[x][y] = HUMAN;
printBoard();
break;
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
// 电脑下棋
private static void computerTurn() {
int x, y;
while (true) {
x = (int) (Math.random() * BOARD_SIZE);
y = (int) (Math.random() * BOARD_SIZE);
if (board[x][y] == EMPTY) {
board[x][y] = COMPUTER;
printBoard();
break;
}
}
}
// 判断是否获胜
private static boolean checkWin(char player) {
int count;
// 水平方向
for (int i = 0; i < BOARD_SIZE; i++) {
count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == player) {
count++;
if (count == WIN_COUNT) {
return true;
}
} else {
count = 0;
}
}
}
// 垂直方向
for (int i = 0; i < BOARD_SIZE; i++) {
count = 0;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[j][i] == player) {
count++;
if (count == WIN_COUNT) {
return true;
}
} else {
count = 0;
}
}
}
// 左上-右下方向
for (int i = 0; i < BOARD_SIZE - WIN_COUNT + 1; i++) {
for (int j = 0; j < BOARD_SIZE - WIN_COUNT + 1; j++) {
count = 0;
for (int k = 0; k < WIN_COUNT; k++) {
if (board[i + k][j + k] == player) {
count++;
if (count == WIN_COUNT) {
return true;
}
} else {
count = 0;
}
}
}
}
// 左下-右上方向
for (int i = 0; i < BOARD_SIZE - WIN_COUNT + 1; i++) {
for (int j = WIN_COUNT - 1; j < BOARD_SIZE; j++) {
count = 0;
for (int k = 0; k < WIN_COUNT; k++) {
if (board[i + k][j - k] == player) {
count++;
if (count == WIN_COUNT) {
return true;
}
} else {
count = 0;
}
}
}
}
return false;
}
}
```
代码中使用一个二维字符数组来表示棋盘,'X'表示玩家的棋子,'O'表示电脑的棋子,空格表示空位。下棋时,玩家通过输入坐标来下棋,电脑通过随机生成坐标来下棋。判断获胜时,分别检查水平方向、垂直方向、左上-右下方向、左下-右上方向是否满足获胜条件。如果满足条件,则返回true,否则返回false。游戏的主循环中,交替进行玩家和电脑的回合,直到有一方获胜。
阅读全文