java井字棋人机对弈
时间: 2023-10-14 19:24:22 浏览: 83
好的,我可以和你分享一个简单的 Java 井字棋人机对弈的实现。首先,我们需要定义一个 `TicTacToe` 类,用于表示井字棋游戏的状态和行动。
```java
public class TicTacToe {
private char[][] board;
private char currentPlayer;
public TicTacToe() {
board = new char[3][3];
currentPlayer = 'X';
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
public boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
public boolean checkForWin() {
// Check for rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-') {
return true;
}
}
// Check for columns
for (int j = 0; j < 3; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != '-') {
return true;
}
}
// Check for diagonals
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') {
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '-') {
return true;
}
return false;
}
public void printBoard() {
System.out.println("-------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
public void makeMove(int row, int col) {
board[row][col] = currentPlayer;
if (currentPlayer == 'X') {
currentPlayer = 'O';
} else {
currentPlayer = 'X';
}
}
}
```
以上代码定义了一个 `TicTacToe` 类,包含了井字棋游戏的状态和行动等信息。其中,`board` 数组用于表示游戏的棋盘,`currentPlayer` 表示当前玩家(X或O)。`isBoardFull()` 方法用于检查棋盘是否已经下满,`checkForWin()` 方法用于检查当前状态下是否有一方获胜,`printBoard()` 方法用于输出当前棋盘的状态,`makeMove(row, col)` 方法用于让当前玩家在指定位置下棋。
接下来,我们可以编写一个 `TicTacToeGame` 类,用于实现人机对弈。
```java
import java.util.Scanner;
public class TicTacToeGame {
private TicTacToe game;
private Scanner scanner;
public TicTacToeGame() {
game = new TicTacToe();
scanner = new Scanner(System.in);
}
public void play() {
System.out.println("Welcome to Tic Tac Toe!");
System.out.println("You are playing against the computer.");
System.out.println("Enter row and column numbers to place your mark.");
while (!game.isBoardFull() && !game.checkForWin()) {
game.printBoard();
if (game.currentPlayer == 'X') {
// Human's turn
System.out.print("Enter row number (0-2): ");
int row = scanner.nextInt();
System.out.print("Enter column number (0-2): ");
int col = scanner.nextInt();
if (game.board[row][col] != '-') {
System.out.println("Invalid move. Try again.");
continue;
}
game.makeMove(row, col);
} else {
// Computer's turn
System.out.println("Computer is making a move...");
int[] move = getComputerMove();
game.makeMove(move[0], move[1]);
}
}
game.printBoard();
if (game.checkForWin()) {
System.out.println("Congratulations! " + game.currentPlayer + " wins!");
} else {
System.out.println("It's a tie!");
}
}
private int[] getComputerMove() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (game.board[i][j] == '-') {
TicTacToe copy = new TicTacToe();
copy.board = game.board.clone();
copy.currentPlayer = game.currentPlayer;
copy.makeMove(i, j);
if (copy.checkForWin()) {
return new int[]{i, j};
}
}
}
}
// Random move
while (true) {
int row = (int) (Math.random() * 3);
int col = (int) (Math.random() * 3);
if (game.board[row][col] == '-') {
return new int[]{row, col};
}
}
}
public static void main(String[] args) {
TicTacToeGame game = new TicTacToeGame();
game.play();
}
}
```
以上代码实现了一个人机对弈的井字棋游戏。在游戏中,玩家可以通过输入行和列的编号来下棋,计算机会自动随机选取一个空位下棋。对于计算机的下棋策略,我们先检查是否有一步可以获胜,如果没有,则随机选择一个空位下棋。当游戏结束时,会输出获胜者或者平局的结果。
希望这个简单的实现可以帮助你更好地理解人机对弈的井字棋游戏的实现方式。