gpt4 book ai didi

java - 使用 MiniMax 返回移动

转载 作者:太空宇宙 更新时间:2023-11-04 14:15:35 25 4
gpt4 key购买 nike

我的代码可以正常工作并且程序运行完美!事实证明,问题还涉及计算机在需要时未返回 0。感谢所有提供帮助的人。如果有人像我一样需要任何帮助,请查看下面的代码。

import java.util.Scanner;

public class TicTacToeSolver{
public static void main(String[] args){
Scanner input=new Scanner(System.in);

char[] board={0,0,0,0,0,0,0,0,0};
int player=1,move;

System.out.println("Tic-Tac-Toe:");
while (true){
PrintBoard(board);
if (player==1){
System.out.print("Enter move: ");
move=input.nextInt();
if (ValidMove(board,move)==true){
board[move-1]='X';
player=-1;
}
}
else{
board[MiniMax(board,player,0,9)[1]]='O';
player=1;
}
if(CheckWin(board)!=0){
PrintBoard(board);
System.out.println("You lost!");
System.exit(0);
}
else if (CheckDraw(board)==true){
PrintBoard(board);
System.out.println("Draw!");
System.exit(0);
}
}
}
public static boolean ValidMove(char[]board,int move){
if(move>=1 && move<=9){
if (board[move-1]==0){
return true;
}
}
return false;
}
public static int CheckWin(char[] board){
if (board[0]=='X' && board[1]=='X' && board[2]=='X'||//Horizontal.
board[3]=='X' && board[4]=='X' && board[5]=='X' ||
board[6]=='X' && board[7]=='X' && board[8]=='X' ||
board[0]=='X' && board[4]=='X' && board[8]=='X' ||//Diagonal.
board[2]=='X' && board[4]=='X' && board[6]=='X' ||
board[0]=='X' && board[3]=='X' && board[6]=='X' ||//Vertical.
board[1]=='X' && board[4]=='X' && board[7]=='X' ||
board[2]=='X' && board[5]=='X' && board[8]=='X'){
return -1;
}
else if (board[0]=='O' && board[1]=='O' && board[2]=='O'||//Horizontal.
board[3]=='O' && board[4]=='O' && board[5]=='O' ||
board[6]=='O' && board[7]=='O' && board[8]=='O' ||
board[0]=='O' && board[4]=='O' && board[8]=='O' ||//Diagonal.
board[2]=='O' && board[4]=='O' && board[6]=='O' ||
board[0]=='O' && board[3]=='O' && board[6]=='O' ||//Vertical.
board[1]=='O' && board[4]=='O' && board[7]=='O' ||
board[2]=='O' && board[5]=='O' && board[8]=='O'){
return 1;
}
return 0;
}
public static boolean CheckDraw(char[] board){
int total=0;
for (int i=0;i<9;i++){
if (board[i]!=0){
total+=1;
}
}
if (total==9){
return true;
}
return false;
}
public static void PrintBoard(char[] board){
System.out.println("------------------------------");
System.out.println(board[0]+"|"+board[1]+"|"+board[2]);
System.out.println("-+-+-");
System.out.println(board[3]+"|"+board[4]+"|"+board[5]);
System.out.println("-+-+-");
System.out.println(board[6]+"|"+board[7]+"|"+board[8]);
System.out.println("\n");
}
public static int[] MiniMax(char[] board,int player,int depth, int maxDepth){
int bestScore,bestMove=-1,score;

if (CheckWin(board)!=0||depth==maxDepth){
return new int[] {CheckWin(board),bestMove};
}
if (CheckDraw(board)==true){
return new int[] {0,bestMove};
}
if (player==-1){
bestScore=-9999;
}
else{
bestScore=9999;
}
for (int i=0;i<9;i++){
if (board[i]==0){
if (player==-1){
board[i]='O';
score=MiniMax(board,player*-1,depth+1,maxDepth)[0];
board[i]=0;

if (score>bestScore){
bestScore=score;
bestMove=i;
}
}
else{
board[i]='X';
score=MiniMax(board,player*-1,depth+1,maxDepth)[0];
board[i]=0;

if(score<bestScore){
bestScore=score;
bestMove=i;
}
}
}
}
return new int[] {bestScore,bestMove};
}
}

最佳答案

我想我已经在其他地方回答过这个问题,所以你们中肯定有一些人在做这项作业!在第一个级别以下,您应该只检查下面的级别是否返回任何获胜 Action 。您不需要使用它们的任何值。示例代码:

public List<Location> bestMoves() {
List<Location> winningMoves = possibleMoves().stream()
.filter(move -> apply(move).isWinFor(nextPlayer))
.collect(Collectors.toList());
if (!winningMoves.isEmpty())
return winningMoves;
else
return possibleMoves().stream()
.filter(move -> apply(move).bestMoves().isEmpty())
.collect(Collectors.toList());
}

关于java - 使用 MiniMax 返回移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com