Java Tec Tac Toc不';t更新板上的移动
我正试着从我的一本书中制作“技战术战术”游戏 问题是我在棋盘上没有得到玩家移动的更新。我只会移动电脑 我应该怎么做来解决这个问题 我尽量把代码弄清楚 我写的代码是:Java Tec Tac Toc不';t更新板上的移动,java,tic-tac-toe,Java,Tic Tac Toe,我正试着从我的一本书中制作“技战术战术”游戏 问题是我在棋盘上没有得到玩家移动的更新。我只会移动电脑 我应该怎么做来解决这个问题 我尽量把代码弄清楚 我写的代码是: import java.util.Scanner; public class TecTacToe { Scanner input=new Scanner(System.in); private char[][] squares ; ddd dd dd public boolean gameOver(){
import java.util.Scanner;
public class TecTacToe {
Scanner input=new Scanner(System.in);
private char[][] squares ;
ddd
dd
dd
public boolean gameOver(){
if(score()!=0){
return true;
}
for(int row=0;row<3;row++){
for(int column=0;column<3;column++){
if(squares[row][column]=='.'){
return false;
} //end of if statment
}//second loop
}// first loop
return true;
}// end of game over class
ddd
您的代码中有多个错误
- 在
中,playBestMove()
应该放在if语句中,否则它会删除所有以前的移动squares[row][column]='。
- 在
中,您对人类玩家使用字符play()
(零),但在记分线上检查0
(元音)O
另外,你的
playBestMove
算法只是把十字架放在最后一个可用的位置,而不是真正的最佳移动。那些“ddd”和“fff”有更深的含义吗?@CurlyB:我用句点来分隔代码块,虽然没有freedom12那么多。
public String toString(){
String result="";
for(int row=0;row<3;row++){
for(int column=0;column<3;column++){
result+=squares[row][column];
}
result+="\n";
}
return result;
}
public static void main(String[] args) {
TecTacToe game= new TecTacToe();
System.out.println("welcome to Tic Tac Toe");
game.play();
System.out.println(game);
System.out.println("Game over");
}
public boolean gameOver(){
if(score()!=0){
return true;
}
for(int row=0;row<3;row++){
for(int column=0;column<3;column++){
if(squares[row][column]=='.'){
return false;
} //end of if statment
}//second loop
}// first loop
return true;
}// end of game over class
public int score(){
int lineScore;
for(int i=0;i<3;i++){
lineScore=scoreLine(squares[i][0],squares[i][1],squares[i][2]);
if(lineScore!=0){
return lineScore;
}
lineScore=scoreLine(squares[0][i],squares[1][i],squares[2][i]);
if(lineScore!=0){
return lineScore;
}
}
lineScore=scoreLine(squares[0][0],squares[1][1],squares[2][2]);
if(lineScore!=0){
return lineScore;
}
return scoreLine(squares[0][2],squares[1][1],squares[2][0]);
}
protected int scoreLine(char a,char b, char c){
if( (a=='X') && (b=='X')&& (c=='X')){ return 1;}
if((a=='O')&&(b=='O')&&(c=='O')) {return -1;}
return 0;
}
public void play(){
char player ='X';
for(int move=0;move<9;move++){
if(gameOver()){
return;
}
if(player=='X'){
playbestMove();
player='0';
}
else{
System.out.println(this);
System.out.println("Enter the row: ");
int row=input.nextInt();
System.out.println("Enter the column: ");
int column=input.nextInt();
squares[row][column]='0';
player='X';
}
}
}
protected void playbestMove(){
int score;
int bestScore=-2;
int bestRow=-1;
int bestColumn=-1;
for(int row=0;row<3;row++){
for(int column=0;column<3;column++){
if(squares[row][column]=='.'){
squares[row][column]='X';
score=score();
bestRow=row;
bestColumn=column;
}
squares[row][column]='.';
}
}
squares[bestRow][bestColumn]='X';
}
}//end of tec tac toe class