gpt4 book ai didi

java - TicTacToe 人工智能 Java

转载 作者:搜寻专家 更新时间:2023-11-01 03:27:12 28 4
gpt4 key购买 nike

所以这是一项大学作业,除了 AI 不知道如何“取胜”之外,一切都已经完成,而且它在开始时的 Action 并不像我希望的那样随机走到顶角,然后往下走。该任务只需要 AI 的两条规则,即它可以“获胜”或“阻止获胜”。

而且当棋盘被填满时游戏似乎并没有结束。它让我选择我的行动。

我希望有人可以就我如何改进它提出建议,或者指出我哪里出错了。

如有任何帮助,我们将不胜感激。

这是戏

import java.util.Scanner;

public class play {



public static void main(String[] args) {


System.out.println("Welcome to Tickle Tackle Toe!!! :D");
System.out.println();

//creat markers
String marker1 = "x";
String marker2 = "o";
boolean playAgain = true;

Scanner s = new Scanner(System.in);



//create player objects
Human human = new Human();
Computer computer = new Computer();

while(playAgain){
//run board setup
set Setup = new set();

Setup.createBoard();
Setup.printBoard();

System.out.println("please choose your marker");
System.out.println("type 1 for 'x' or 2 for 'o'");

//set markers
if(s.nextInt() == 1){
// create player objects
human.setMarker("x");
computer.setMarker("o");
}
else
{
human.setMarker("o");
computer.setMarker("x");

}




// determine who goes first
int first = (int) (Math.random() * 2);





boolean won = false;
int turns = 0;

if(first == 0){
System.out.println("You go first!");
System.out.println();
while(!won){
human.takeTurn(Setup.getBoard());
turns++;
Setup.printBoard();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("Congrats you won!");
}
if(turns == 9){
won = true;
System.out.println("Its a draw!");
break;
}
if(!won){
computer.takeTurn(Setup.getBoard(), human);
turns++;
System.out.println();
Setup.printBoard();
System.out.println();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("You lost!");
}
if(turns == 9){
won = true;
System.out.println("Its a draw!");
break;
}
}

} // close while 1
}
else {

System.out.println("Computer goes first!");
System.out.println();
while(!won){
computer.takeTurn(Setup.getBoard(), human);
turns++;
Setup.printBoard();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("You lost!");
}
if(!won){
human.takeTurn(Setup.getBoard());
turns++;
System.out.println();
Setup.printBoard();
System.out.println();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("Congrats you won!");
}
}



} // close while 2

}

System.out.println("Would you like to play again? Type 1 for yes or 2 to quit");
System.out.println();
if(s.nextInt() == 2){
playAgain = false;
}

}

}
}

这是计算机课

class Computer extends player {

public Computer(){

}

int boardsize = 3;


public void takeTurn(String[][] board, Human human) {

int vertical = 0;
int horizontal = 0;
int diagonal = 0;
boolean mademove = false;

// check if you can take a win horizontally
for(int i = 0; i<3; i++){

if(board[0][i].equals(board[1][i]) && board[0][i].equals(Marker)){

if(board[2][i] != human.getMarker()){
board[2][i] = Marker;
mademove = true;
return;
}

}

}

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

if(board[2][i].equals(board[1][i]) && board[2][i].equals(Marker)){

if(board[0][i] != human.getMarker()){
board[0][i] = Marker;
mademove = true;
return;
}

}


}


// check if you can take a win vertically
for(int i = 0; i<3; i++){

if(board[i][0].equals(board[i][1]) && board[i][0].equals(Marker)){

if(board[i][2] != human.getMarker()){
board[i][2] = Marker;
mademove = true;
return;
}

}

}

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

if(board[i][2].equals(board[i][1]) && board[i][2].equals(Marker)){

if(board[i][0] != human.getMarker()){
board[i][0] = Marker;
mademove = true;
return;
}

}

}


// check if you can take a win diagonally


if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

if(board[2][2] != human.getMarker()){
board[2][2] = Marker;
mademove = true;
return;
}
}

if(board[2][2].equals(board[1][1]) && board[2][2].equals(Marker)){

if(board[0][0] != human.getMarker()){
board[0][0] = Marker;
mademove = true;
return;
}
}

if(board[0][0].equals(board[1][1]) && board[0][0].equals(Marker)){

if(board[2][2] != human.getMarker()){
board[2][2] = Marker;
mademove = true;
return;
}
}

if(board[0][2].equals(board[1][1]) && board[0][2].equals(Marker)){

if(board[2][0] != human.getMarker()){
board[2][0] = Marker;
mademove = true;
return;
}
}

if(board[2][0].equals(board[1][1]) && board[2][0].equals(Marker)){

if(board[0][2] != human.getMarker()){
board[0][2] = Marker;
mademove = true;
return;
}
}


// BLOCKS!!!! //

// check if you can block a win horizontally
for(int i = 0; i<3; i++){

if(board[0][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){
if(board[2][i] != Marker){
board[2][i] = Marker;
mademove = true;
return;
}

}

}

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

if(board[2][i].equals(board[1][i]) && board[0][i].equals(human.getMarker())){

if(board[0][i] != Marker){
board[0][i] = Marker;
mademove = true;
return;
}

}


}



// check if you can block a win horizontally
for(int i = 0; i<3; i++){

if(board[i][0].equals(board[i][1]) && board[i][0].equals(human.getMarker())){

if(board[i][2] != Marker){
board[i][2] = Marker;
mademove = true;
return;
}

}

}

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

if(board[i][2].equals(board[i][1]) && board[i][2].equals(human.getMarker())){

if(board[i][0] != Marker){
board[i][0] = Marker;
mademove = true;
return;
}

}

}


// check if you can block a win diagonally


if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){

if(board[2][2] != Marker){
board[2][2] = Marker;
mademove = true;
return;
}
}

if(board[2][2].equals(board[1][1]) && board[2][2].equals(human.getMarker())){

if(board[0][0] != Marker){
board[0][0] = Marker;
mademove = true;
return;
}
}

if(board[0][0].equals(board[1][1]) && board[0][0].equals(human.getMarker())){
board[2][2] = Marker;
mademove = true;
return;
}

if(board[0][2].equals(board[1][1]) && board[0][2].equals(human.getMarker())){

if(board[2][0] != Marker){
board[2][0] = Marker;
mademove = true;
return;
}
}

if(board[2][0].equals(board[1][1]) && board[2][0].equals(human.getMarker())){

if(board[0][2] != Marker){
board[0][2] = Marker;
mademove = true;
return;
}
}




// make random move if above rules dont apply
for(int i = 0; i<3; i++){
if(board[i][0] != "x" && board[i][0] != "o"){
board[i][0] = Marker;
return;
}
}
for(int i = 0; i<3; i++){
if(board[i][1] != "x" && board[i][0] != "o"){
board[i][1] = Marker;
return;
}
}
for(int i = 0; i<3; i++){
if(board[i][2] != "x" && board[i][0] != "o"){
board[i][2] = Marker;
return;
}
}


}
}

最佳答案

如果您通过放置第 9 个棋子获胜,您将同时获得“您赢了”和“平局!”的信息。消息,我认为这不是预期的行为。如果你在第九回合输了,也是一样。另一方面,当没有人获胜时,抽签代码未达到,这就是游戏没有结束的原因。这很容易修复:

if(!won){
computer.takeTurn(Setup.getBoard(), human);
turns++;
System.out.println();
Setup.printBoard();
System.out.println();
if(Setup.hasWon(Setup.getBoard())){
won = true;
System.out.println("You lost!");
} else if (turns == 9){
won = true;
System.out.println("Its a draw!");
}
}

关于随机移动,您当前的代码循环遍历所有九个图 block ,在它遇到的第一个空白点放置一个标记,然后返回。您想要的是列出所有空白点,然后随机选择其中一个。或者,继续生成介于 0 和 2 之间的 x 和 y 坐标,检查它是否为空,如果是,则放置一个标记。

关于java - TicTacToe 人工智能 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10645381/

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