java井字棋人机对战
时间: 2023-11-21 09:55:24 浏览: 91
以下是Java井字棋人机对战的示例代码,其中包括了人机对战的逻辑和实现:
```java
import java.util.Scanner;
public class TicTacToe {
private char[][] board;
private char player;
private char computer;
public TicTacToe() {
board = new char[3][3];
player = 'X';
computer = 'O';
initializeBoard();
}
public void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
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 boolean isBoardFull() {
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
return isFull;
}
public boolean checkForWin(char player) {
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
}
for (int j = 0; j < 3; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
return true;
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
public void playerMove() {
Scanner scanner = new Scanner(System.in);
int row, col;
do {
System.out.print("Enter row number and column number (1-3): ");
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
} while (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != '-');
board[row][col] = player;
}
public void computerMove() {
int[] move = getBestMove();
board[move[0]][move[1]] = computer;
}
public int evaluate(char player) {
if (checkForWin(computer)) {
return 10;
} else if (checkForWin(player)) {
return -10;
} else {
return 0;
}
}
public int minimax(int depth, boolean isMax, char player, int alpha, int beta) {
int score = evaluate(player);
if (score == 10 || score == -10) {
return score;
}
if (isBoardFull()) {
return 0;
}
if (isMax) {
int best = Integer.MIN_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
board[i][j] = computer;
best = Math.max(best, minimax(depth + 1, !isMax, player, alpha, beta));
board[i][j] = '-';
alpha = Math.max(alpha, best);
if (beta <= alpha) {
break;
}
}
}
}
return best;
} else {
int best = Integer.MAX_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
board[i][j] = player;
best = Math.min(best, minimax(depth + 1, !isMax, player, alpha, beta));
board[i][j] = '-';
beta = Math.min(beta, best);
if (beta <= alpha) {
break;
}
}
}
}
return best;
}
}
public int[] getBestMove() {
int bestVal = Integer.MIN_VALUE;
int[] bestMove = new int[2];
bestMove[0] = -1;
bestMove[1] = -1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
board[i][j] = computer;
int moveVal = minimax(0, false, player, Integer.MIN_VALUE, Integer.MAX_VALUE);
board[i][j] = '-';
if (moveVal > bestVal) {
bestMove[0] = i;
bestMove[1] = j;
bestVal = moveVal;
}
}
}
}
return bestMove;
}
public void play() {
System.out.println("Welcome to Tic Tac Toe!");
System.out.println("-----------------------");
System.out.println("Instructions:");
System.out.println("Enter the row and column number to place your mark.");
System.out.println("You are X, and the computer is O.");
System.out.println("The first player to get three in a row wins!");
System.out.println();
printBoard();
while (!isBoardFull()) {
playerMove();
if (checkForWin(player)) {
System.out.println("Congratulations! You win!");
return;
}
computerMove();
System.out.println("Computer's move:");
printBoard();
if (checkForWin(computer)) {
System.out.println("Sorry, you lose.");
return;
}
}
System.out.println("It's a tie!");
}
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.play();
}
}
```