java 井字棋 人机_井字游戏 人机对战 java实现

package com.ecnu.Main;

/**

* 主函数触发游戏

*/

public class MainApplication {

public static void main(String[] args){

TicTacToeGame ticTacToeGame = new TicTacToeGame();

ticTacToeGame.start();

}

}

//TicTacToeGame 方法类

import java.util.Scanner;

public class TicTacToeGame {

private int stepCount = 0;

private int[][] gameBoard;

private Scanner scanner = new Scanner(System.in);

private final int humanFlag = 1;

private final int computerFlag = -1;

private final int emptyFlag = 0;

public void start() {

initGameBoard();

System.out.println("Game Board is ready!!! Game start!!!");

computerThink();

}

private void initGameBoard() {

this.gameBoard = new int[3][3];

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

gameBoard[i][j] = emptyFlag;

}

}

showGameBoard();

}

private void computerThink() {

System.out.println("Computer:");

Move move = calculateTheBestMove();

int x = move.getX();

int y = move.getY();

gameBoard[y][x] = computerFlag;

stepCount++;

showGameBoard();

if(!isGameOver(x, y)){

humanAction();

}

}

private Move calculateTheBestMove(){

Move move = new Move();

Integer bestWeight = null;

Integer bestX = null;

Integer bestY = null;

for(int y=0; y<3; y++){

for(int x=0; x<3; x++){

if(gameBoard[y][x] == 0){

gameBoard[y][x] = computerFlag;

stepCount ++;

if(isWin(x,y)){

stepCount --;

move.setX(x);

move.setY(y);

move.setWeight(1000);

gameBoard[y][x] = emptyFlag;

return move;

}else if(isTie()){

stepCount --;

move.setX(x);

move.setY(y);

move.setWeight(0);

gameBoard[y][x] = emptyFlag;

return move;

}else{

Move worstMove = calculateTheWorstMove();

stepCount --;

gameBoard[y][x] = emptyFlag;

if(bestWeight == null || worstMove.getWeight()>= bestWeight){

bestX = x;

bestY = y;

bestWeight =worstMove.getWeight();

}

}

}

}

}

move.setWeight(bestWeight);

move.setX(bestX);

move.setY(bestY);

return move;

}

private Move calculateTheWorstMove(){

Move move = new Move();

Integer bestWeight = null;

Integer bestX = null;

Integer bestY = null;

for(int y=0; y<3; y++){

for(int x=0; x<3; x++){

if(gameBoard[y][x] == 0){

gameBoard[y][x] = humanFlag;

stepCount ++;

if(isWin(x,y)){

stepCount --;

move.setX(x);

move.setY(y);

move.setWeight(-1000);

gameBoard[y][x] = emptyFlag;

return move;

}else if(isTie()){

stepCount --;

move.setX(x);

move.setY(y);

move.setWeight(0);

gameBoard[y][x] = emptyFlag;

return move;

}else{

Move bestMove = calculateTheBestMove();

stepCount --;

gameBoard[y][x] = emptyFlag;

if(bestX == null || bestMove.getWeight() < bestWeight){

bestX = x;

bestY = y;

bestWeight = bestMove.getWeight();

}

}

}

}

}

move.setWeight(bestWeight);

move.setX(bestX);

move.setY(bestY);

return move;

}

private void humanAction() {

System.out.println("It is your turn now!");

boolean isHumanTurn = true;

int x = 0;

int y = 0;

while(isHumanTurn){

System.out.println("Please input the row number (1~3):");

y = scanner.nextInt() - 1;

System.out.println("Please input the column number (1~3):");

x = scanner.nextInt() - 1;

if (isInputValid(x, y)){

isHumanTurn = false;

gameBoard[y][x] = humanFlag;

}else{

System.out.println(String.format("You cannot place on row %d, column %d", y + 1, x + 1));

}

}

stepCount++;

showGameBoard();

if(!isGameOver(x, y)){

computerThink();

}

}

private boolean isWin(int x, int y) {

return (Math.abs(gameBoard[y][0] + gameBoard[y][1] + gameBoard[y][2]) == 3) ||

(Math.abs(gameBoard[0][x] + gameBoard[1][x] + gameBoard[2][x]) == 3) ||

(Math.abs(gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2]) == 3) ||

(Math.abs(gameBoard[2][0] + gameBoard[1][1] + gameBoard[0][2]) == 3);

}

private boolean isTie() {

return stepCount >= 9;

}

private boolean isInputValid(int x, int y){

return x>=0 && x<3 && y>=0 && y<3 && gameBoard[y][x] == 0;

}

private boolean isGameOver(int x, int y){

boolean isGameOver = true;

if(isWin(x, y)){

if(gameBoard[y][x] == -1){

System.out.println("Computer Win!!!!");

}else{

System.out.println("You Win!!!!");

}

}else if(isTie()){

System.out.println("Tie!!!");

}else{

isGameOver = false;

}

return isGameOver;

}

private void showGameBoard(){

for(int y=0; y<3; y++){

for(int x=0; x<3; x++){

if(gameBoard[y][x] == -1){

System.out.print("2 ");

}else {

System.out.print(gameBoard[y][x] + " ");

}

}

System.out.println();

}

System.out.println();

}

}

class Move{

private int x;

private int y;

private int weight;

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

public int getWeight() {

return weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值