Java 确定计算机在Tic Tac Toe中决定的移动
我已经完成了90%的井字游戏,但就是不知道如何确定计算机生成的移动。我尝试了很多事情,但都没有成功 我在那里的代码现在决定了计算机的随机移动,但仍然可以覆盖用户输入的移动,并且有点问题 我已经在代码中标记了需要帮助的特定位置Java 确定计算机在Tic Tac Toe中决定的移动,java,Java,我已经完成了90%的井字游戏,但就是不知道如何确定计算机生成的移动。我尝试了很多事情,但都没有成功 我在那里的代码现在决定了计算机的随机移动,但仍然可以覆盖用户输入的移动,并且有点问题 我已经在代码中标记了需要帮助的特定位置 //12/14/2014 //The purpose of this program is to play a tic tac toe game against the computer //The TicTacToe class. import java.awt.*;
//12/14/2014
//The purpose of this program is to play a tic tac toe game against the computer
//The TicTacToe class.
import java.awt.*;
import java.io.*;
import java.util.*;
public class TicTacToe
{
static boolean GameOver = false ;
public static char DrawBoard (char [] [] Board) //DrawBoard Method
{
char DrawBoard = '-' ;
System.out.println("-------------");
for (int row = 0 ; row < 3 ; row = row + 1)
{
System.out.print ("| ") ;
for (int column = 0 ; column < 3 ; column = column + 1)
{
System.out.print (Board [row] [column] + " | ") ;
DrawBoard = Board [row] [column] ;
}
System.out.println () ;
System.out.println ("-------------") ;
}
return (DrawBoard) ;
}
public static void Winner (char [] [] Board) //Winner Method
{
if (Board [0] [0] == 'X' && Board [0] [1] == 'X' && Board [0] [2] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [1] [0] == 'X' && Board [1] [1] == 'X' && Board [1] [2] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [2] [0] == 'X' && Board [2] [1] == 'X' && Board [2] [2] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [0] [0] == 'X' && Board [1] [0] == 'X' && Board [2] [0] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [0] [1] == 'X' && Board [1] [1] == 'X' && Board [2] [1] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [0] [2] == 'X' && Board [1] [2] == 'X' && Board [2] [2] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [0] [0] == 'X' && Board [1] [1] == 'X' && Board [2] [2] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
if (Board [0] [2] == 'X' && Board [1] [1] == 'X' && Board [2] [0] == 'X')
{
System.out.println ("You have won! Congratulations.") ;
GameOver = true ;
}
}
public static void Loser (char [] [] Board) //Loser Method
{
if (Board [0] [0] == 'O' && Board [0] [1] == 'O' && Board [0] [2] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [1] [0] == 'O' && Board [1] [1] == 'O' && Board [1] [2] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [2] [0] == 'O' && Board [2] [1] == 'O' && Board [2] [2] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [0] [0] == 'O' && Board [1] [0] == 'O' && Board [2] [0] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [0] [1] == 'O' && Board [1] [1] == 'O' && Board [2] [1] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [0] [2] == 'O' && Board [1] [2] == 'O' && Board [2] [2] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [0] [0] == 'O' && Board [1] [1] == 'O' && Board [2] [2] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
if (Board [0] [2] == 'O' && Board [1] [1] == 'O' && Board [2] [0] == 'O')
{
System.out.println ("You have lost! Better luck next time.") ;
GameOver = true ;
}
}
public static void Tie (char [] [] Board) //Tie Method
{
for (int row = 0; row < 3; row = row + 1)
{
for (int column = 0; column < 3; column = column + 1)
{
if (Board [row] [column] == 'X' && Board [row] [column] == 'O')
{
System.out.println ("It is a tie. Cats game!") ;
GameOver = true ;
}
}
}
}
public static void main (String[] args)
{
//Variable declaration
Scanner kbReader = new Scanner(System.in);
char [] [] Board = new char [3] [3] ;
String MenuInput ;
int BoardOutput ;
int UserSpotChoice ;
int ComputerSpotChoice = 0;
int UserTurn = 1 ;
int Winner = 0 ;
Board [0] [0] = '-' ;
Board [0] [1] = '-' ;
Board [0] [2] = '-' ;
Board [1] [0] = '-' ;
Board [1] [1] = '-' ;
Board [1] [2] = '-' ;
Board [2] [0] = '-' ;
Board [2] [1] = '-' ;
Board [2] [2] = '-' ;
//Welcome
System.out.println ("Welcome to Alex Montague's Tic Tac Toe game!.") ;
System.out.println ("") ;
System.out.println ("If you wish to play, type 'Play'") ;
System.out.println ("If you wish to read the instructions, type 'Instructions'") ;
System.out.println ("If you wish to exit, type 'Exit'") ;
MenuInput = kbReader.next () ;
//do
//{
if (MenuInput.equals ("Play") || MenuInput.equals ("play"))
{
while (!GameOver)
{
System.out.println ("\f") ;
System.out.println (" Tic Tac Toe") ;
BoardOutput = DrawBoard (Board) ;
System.out.println (" 1 2 3") ;
System.out.println (" 4 5 6") ;
System.out.println (" 7 8 9") ;
System.out.println ("Please enter the number you would like to move your spot to") ;
UserSpotChoice = kbReader.nextInt () ;
if (UserSpotChoice == 1) Board [0] [0] = 'X' ;
if (UserSpotChoice == 2) Board [0] [1] = 'X' ;
if (UserSpotChoice == 3) Board [0] [2] = 'X' ;
if (UserSpotChoice == 4) Board [1] [0] = 'X' ;
if (UserSpotChoice == 5) Board [1] [1] = 'X' ;
if (UserSpotChoice == 6) Board [1] [2] = 'X' ;
if (UserSpotChoice == 7) Board [2] [0] = 'X' ;
if (UserSpotChoice == 8) Board [2] [1] = 'X' ;
if (UserSpotChoice == 9) Board [2] [2] = 'X' ;
//RIGHT HERE IS WHERE I NEED HELP WITH ``````````````````````````````````````````````
for (int row = 0; row < 3; row = row + 1)
{
for (int column = 0; column < 3; column = column + 1)
{
if (Board [row] [column] == '-')
{
ComputerSpotChoice = (int) (Math.random() * 8 ) + 1 ;
}
}
}
if (ComputerSpotChoice == 1) Board [0] [0] = 'O' ;
if (ComputerSpotChoice == 2) Board [0] [1] = 'O' ;
if (ComputerSpotChoice == 3) Board [0] [2] = 'O' ;
if (ComputerSpotChoice == 4) Board [1] [0] = 'O' ;
if (ComputerSpotChoice == 5) Board [1] [1] = 'O' ;
if (ComputerSpotChoice == 6) Board [1] [2] = 'O' ;
if (ComputerSpotChoice == 7) Board [2] [0] = 'O' ;
if (ComputerSpotChoice == 8) Board [2] [1] = 'O' ;
if (ComputerSpotChoice == 9) Board [2] [2] = 'O' ;
Winner (Board) ;
Loser (Board) ;
Tie (Board) ;
if (GameOver) System.exit (0) ;
}
}
//TILL HERE ``````````````````````````````````````````````````````````````````````````
else if (MenuInput.equals ("Instructions") || MenuInput.equals ("instructions"))
{
System.out.println ("\f") ;
System.out.println ("You will be playing the game of Tic Tac Toe against the computer.") ;
System.out.println ("The object of this game is to get three of your own x's or o's in a line.") ;
System.out.println ("You take turns placing the x's and o's and whoever gets three in a row first wins.") ;
System.out.println ("Good Luck!") ;
System.out.println ("") ;
System.out.println ("If you wish to play, type 'Play'") ;
System.out.println ("If you wish to exit, type 'Exit'") ;
MenuInput = kbReader.next () ;
}
else if (MenuInput.equals ("Exit") || MenuInput.equals ("exit"))
{
System.out.println ("Thank you for using Alex Montague's Tic Tac Toe game!") ;
System.exit (0) ;
}
else
{
System.out.println ("Sorry, that is not a valid choice.") ;
System.out.println ("If you wish to play, type 'Play'") ;
System.out.println ("If you wish to read the instructions, type 'Instructions'") ;
System.out.println ("If you wish to exit, type 'Exit'") ;
MenuInput = kbReader.next () ;
}
//}
//while (!MenuInput.equals ("Instructions") || !MenuInput.equals ("instructions") || !MenuInput.equals ("Play") || !MenuInput.equals ("play") || !MenuInput.equals ("Exit") || !MenuInput.equals ("exit")) ;
} // main method
} // TicTacToe class
//12/14/2014
//这个程序的目的是对计算机玩一个井字游戏
//蒂克塔克托班。
导入java.awt.*;
导入java.io.*;
导入java.util.*;
公共类Tictatcoe
{
静态布尔GameOver=false;
公共静态char DrawBoard(char[][]Board)//DrawBoard方法
{
char DrawBoard='-';
System.out.println(“--------------”;
对于(int行=0;行<3;行=行+1)
{
System.out.print(“|”);
for(int列=0;列<3;列=列+1)
{
System.out.print(板[行][列]+“|”);
DrawBoard=板[行][列];
}
System.out.println();
System.out.println(“----------------”;
}
返回(绞车);
}
公共静态void Winner(char[]Board)//Winner方法
{
如果(单板[0][0]='X'和单板[0][1]='X'和单板[0][2]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(单板[1][0]=='X'和单板[1][1]=='X'和单板[1][2]=='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(电路板[2][0]='X'和电路板[2][1]='X'和电路板[2][2]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(电路板[0][0]='X'和电路板[1][0]='X'和电路板[2][0]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(单板[0][1]='X'和单板[1][1]='X'和单板[2][1]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(电路板[0][2]='X'和电路板[1][2]='X'和电路板[2][2]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(电路板[0][0]='X'和电路板[1][1]='X'和电路板[2][2]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
如果(电路板[0][2]='X'和电路板[1][1]='X'和电路板[2][0]='X')
{
System.out.println(“你赢了!祝贺你”);
GameOver=true;
}
}
公共静态void Loser(char[]Board)//Loser方法
{
如果(板[0][0]='O'和板[0][1]='O'和板[0][2]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(单板[1][0]='O'和单板[1][1]='O'和单板[1][2]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(电路板[2][0]='O'和电路板[2][1]='O'和电路板[2][2]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(电路板[0][0]='O'和电路板[1][0]='O'和电路板[2][0]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(电路板[0][1]='O'和电路板[1][1]='O'和电路板[2][1]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(电路板[0][2]='O'和电路板[1][2]='O'和电路板[2][2]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(板[0][0]='O'和板[1][1]='O'和板[2][2]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
如果(单板[0][2]='O'和单板[1][1]='O'和单板[2][0]='O')
{
System.out.println(“你输了!祝你下次好运。”);
GameOver=true;
}
}
公共静态无效绑定(char[]Board)//绑定方法
{
对于(int行=0;行<3;行=行+1)
{
for(int列=0;列<3;列=列+1)
{
如果(板[行][列]='X'&板[行][列]='O')
{
System.out.println(“这是一场平局的猫咪游戏!”);
GameOver=true;
}
}
}
}
公共静态void main(字符串[]args)
{
//变量声明
扫描仪kbReader=新扫描仪(System.in);
字符[][]板=新字符[3][3];
字符串菜单输入;
输入输出;
int用户选择;
int ComputerSpotChoice=0;
int UserTurn=1;
int=0;
董事会[0][0]='-';
董事会[0][1]='-';
董事会[0][2]='-';
董事会[1][0]='-';
董事会[1][1]='-';
董事会[1][2]='-';
董事会[2][0]='-';
董事会[2][1]='-';
董事会[2][2]='-';
//欢迎光临
System.out.println(“欢迎来到Alex Montague的Tic-Tac-Toe游戏!”);
System.out.println(“”);
System.out.println(“如果希望播放,请键入“播放”);
System.out.println(“如果您希望阅读说明,请键入‘说明’”
public Move getMove(Board board) {
for (Move move: board.possibleMoves()) {
if (move will win the game)
add move to list of good moves
else for (Move counterMove: board.apply(move).possibleMoves()) {
if (counterMove will NOT lose the game)
call getMove recursively on board.apply(move).apply(counterMove);
add move to list of good moves only if recursive call is not null
}
}
return a random move from the list of good moves, or null if it's empty
}
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());
}