Java Tictaoe计划的问题
所以我在Tictatcoe程序中已经做了这么多,终端上说找不到符号“i”和“j”。我不知道从这一点上该怎么办。类和类客户端将不会编译,我必须声明“I”和“j”。如果您能帮我解答这个问题,我们将不胜感激Java Tictaoe计划的问题,java,Java,所以我在Tictatcoe程序中已经做了这么多,终端上说找不到符号“i”和“j”。我不知道从这一点上该怎么办。类和类客户端将不会编译,我必须声明“I”和“j”。如果您能帮我解答这个问题,我们将不胜感激 public class TicTacToe { private char[][] board; private String xPlayer; private String oPlayer; private boolean xturn; private int plays; public Ti
public class TicTacToe {
private char[][] board;
private String xPlayer;
private String oPlayer;
private boolean xturn;
private int plays;
public TicTacToe(Player X, Player O) {
this("Player X", "Player O", true);
}
public TicTacToe(String xPlayer, String oPlayer, boolean xturn) {
this.xPlayer = xPlayer;
this.oPlayer = oPlayer;
this.xturn = xturn;
this.plays = 0;
this.board = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
this.board[i][j] = '-';
}
}
}
public boolean isXturn() {
return xturn;
}
public boolean play(int row, int col) {
this.makeMove(row, col);
if (this.makeMove(row, col)) {
plays = plays + 1;
if (xturn) {
xturn = false;
} else {
xturn = true;
}
}
if (this.isGameOver() == 'x') {
System.out.println("X won.");
return true;
} else if (this.isGameOver() == 'o') {
System.out.println("O won.");
return true;
} else if (this.isGameOver() == 't') {
System.out.println("Tie");
return true;
} else {
return false;
}
}
public boolean makeMove(int row, int col) {
if (plays == 9) {
System.out.println("The board is already completely filled. ");
return false;
}
if (row < 0 || row > 2 || col < 0 || col > 2) {
System.out.println(" The coordinate (x, y) is out of range.");
return false;
}
if (this.board[row][col] != '-') {
System.out.println("The coordinate (x, y) is already taken.");
return false;
} else {
if (xturn == true) {
this.board[row][col] = 'X';
} else {
this.board[row][col] = 'O';
}
return true;
}
}
public char isGameOver() {
boolean winner = true;
for (i = 0; i < 3; i++) {
if ((this.board[i][0] == this.board[i][1]) == (this.board[i][0] == this.board[i][2])) {
return this.board[i][0];
}
}
for (j = 0; j < 3; j++) {
if ((this.board[0][j] == this.board[1][j]) == (this.board[0][j] == this.board[2][j])) {
return this.board[0][j];
}
}
if ((this.board[0][0] == this.board[1][1]) == (this.board[0][0] == this.board[2][2])) {
return this.board[0][0];
}
if ((this.board[0][2] == this.board[1][1]) == (this.board[0][0] == this.board[2][0])) {
return this.board[0][2];
}
}
public String toString( ){
String ans = " ";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.println( this.board[i][j]);
}
}
return ans;
}
}
公共类TicTacToe{
私人char[][]董事会;
私有字符串层;
私有字符串oPlayer;
私有布尔值;
私人智力游戏;
公共提克塔克托(玩家X,玩家O){
这(“玩家X”,“玩家O”,真);
}
公共TicTacToe(字符串xPlayer、字符串oPlayer、布尔xtorn){
this.xPlayer=xPlayer;
this.oPlayer=oPlayer;
this.xtorn=xtorn;
这是0;
this.board=新字符[3][3];
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
这个.board[i][j]='-';
}
}
}
公共布尔值isXturn(){
返回Xtrn;
}
公共布尔播放(整数行,整数列){
此.makeMove(行、列);
if(此.makeMove(行、列)){
播放次数=播放次数+1;
如果(xtrn){
xtrn=false;
}否则{
xtrn=真;
}
}
if(this.isGameOver()=='x'){
System.out.println(“X元”);
返回true;
}else if(this.isGameOver()=='o'){
System.out.println(“O-won”);
返回true;
}else if(this.isGameOver()=“t”){
系统输出打印号(“Tie”);
返回true;
}否则{
返回false;
}
}
公共布尔makeMove(int行,int列){
如果(播放==9){
System.out.println(“电路板已完全填满”);
返回false;
}
如果(行<0 | |行>2 | |列<0 | |列>2){
System.out.println(“坐标(x,y)超出范围。”);
返回false;
}
如果(此.board[row][col]!='-'){
System.out.println(“坐标(x,y)已经取了。”);
返回false;
}否则{
如果(xtrn==true){
此.board[row][col]='X';
}否则{
此.board[行][col]=“O”;
}
返回true;
}
}
公共字符isGameOver(){
布尔值=真;
对于(i=0;i<3;i++){
if((this.board[i][0]==this.board[i][1])==(this.board[i][0]==this.board[i][2])){
返回此.board[i][0];
}
}
对于(j=0;j<3;j++){
if((this.board[0][j]==this.board[1][j])==(this.board[0][j]==this.board[2][j])){
返回此.board[0][j];
}
}
if((this.board[0][0]==this.board[1][1])==(this.board[0][0]==this.board[2][2])){
返回此.board[0][0];
}
if((this.board[0][2]==this.board[1][1])==(this.board[0][0]==this.board[2][0])){
返回此.board[0][2];
}
}
公共字符串toString(){
字符串ans=“”;
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
System.out.println(此.board[i][j]);
}
}
返回ans;
}
}
还有Tictaoe的一位客户
public class TicTacToeClient {
public static void main(String[] args) {
TicTacToe ttt = new TicTacToe();
System.out.println("Below should be an empty board: ");
System.out.println(ttt);
System.out.println("Testing diagonal win (X should win): ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
boolean retVal = ttt.play(i, j);
if (retVal)
break;
}
}
System.out.println("\n" + ttt);
System.out.println("Testing other diagonal win (X should win): ");
ttt = new TicTacToe();
for (int i = 0; i < 3; i++) {
for (int j = 2; j >= 0; j--) {
boolean retVal = ttt.play(i, j);
if (retVal)
break;
}
}
System.out.println("\n" + ttt);
System.out.println("Testing row 1 win (X should win): ");
ttt = new TicTacToe();
ttt.play(1, 0);
ttt.play(0, 1);
ttt.play(1, 1);
ttt.play(0, 2);
ttt.play(1, 2);
System.out.println(ttt);
System.out.println("Testing row 1 win (O should win): ");
ttt = new TicTacToe("Player X", "Player O", false);
ttt.play(1, 0);
ttt.play(0, 1);
ttt.play(1, 1);
ttt.play(0, 2);
ttt.play(0, 0);
ttt.play(2, 2);
ttt.play(1, 2);
System.out.println(ttt);
System.out.println("Tie game: ");
ttt = new TicTacToe("Arthur", "Ford", false);
ttt.play(1, 1);
ttt.play(2, 2);
ttt.play(1, 2);
ttt.play(1, 0);
ttt.play(2, 0);
ttt.play(0, 2);
ttt.play(0, 1);
ttt.play(2, 1);
ttt.play(0, 0);
System.out.println(ttt);
System.out.println("Testing making a move after 9 plays: ");
ttt.play(0, 1);
System.out.println();
System.out.println("Testing col 1 win (Arthur should win): ");
ttt = new TicTacToe("Arthur", "Ford", true);
ttt.play(1, 1);
ttt.play(2, 2);
ttt.play(0, 1);
ttt.play(2, 0);
ttt.play(2, 1);
System.out.println(ttt);
System.out.println("Testing playing the same position (it should be O's turn again): ");
ttt = new TicTacToe();
ttt.play(0, 0);
ttt.play(0, 0);
if (ttt.isXturn()) {
System.out.println("Incorrect, it should be O's turn.");
} else {
System.out.println("It is O's turn!");
}
System.out.println();
System.out.println("Testing the out of range error (it should be O's turn again): ");
ttt = new TicTacToe();
ttt.play(0, 0);
ttt.play(-1, 0);
ttt.play(0, -1);
ttt.play(3, 0);
ttt.play(0, 3);
if (ttt.isXturn()) {
System.out.println("Incorrect, it should be O's turn.");
} else {
System.out.println("It is O's turn!");
}
}
}
公共类客户端{
公共静态void main(字符串[]args){
TicTacToe ttt=新的TicTacToe();
System.out.println(“下面应该是一个空板:”);
系统输出打印项次(ttt);
System.out.println(“测试对角线赢(X应该赢):”;
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
布尔retVal=ttt.play(i,j);
如果(返回)
打破
}
}
System.out.println(“\n”+ttt);
System.out.println(“测试其他对角线赢(X应该赢):”;
ttt=新的TicTacToe();
对于(int i=0;i<3;i++){
对于(int j=2;j>=0;j--){
布尔retVal=ttt.play(i,j);
如果(返回)
打破
}
}
System.out.println(“\n”+ttt);
System.out.println(“测试行1赢(X应该赢):”;
ttt=新的TicTacToe();
ttt.play(1,0);
ttt.play(0,1);
ttt.play(1,1);
ttt.play(0,2);
ttt.play(1,2);
系统输出打印项次(ttt);
System.out.println(“测试行1赢(O应该赢):”;
ttt=新Tictaoe(“玩家X”,“玩家O”,错误);
ttt.play(1,0);
ttt.play(0,1);
ttt.play(1,1);
ttt.play(0,2);
ttt.play(0,0);
ttt.play(2,2);
ttt.play(1,2);
系统输出打印项次(ttt);
System.out.println(“平局游戏”);
ttt=新Tictaoe(“亚瑟”,“福特”,假);
ttt.play(1,1);
ttt.play(2,2);
ttt.play(1,2);
ttt.play(1,0);
ttt.play(2,0);
ttt.play(0,2);
ttt.play(0,1);
ttt.play(2,1);
ttt.play(0,0);
系统输出打印项次(ttt);
System.out.println(“在9次播放后测试移动:”);
ttt.play(0,1);
System.out.println();
System.out.println(“测试列1赢(Arthur应该赢):”;
ttt=新Tictatoe(“亚瑟”,“福特”,真);
ttt.play(1,1);
ttt.play(2,2);
ttt.play(0,1);
ttt.play(2,0);
ttt.play(2,1);
系统输出打印项次(ttt);
System.out.println(“测试打同一个位置(该轮到O了):”;
ttt=新的TicTacToe();
ttt.play(0,0);
ttt.play(0,0);
if(ttt.isxtorn()){
System.out.println(“不正确,该轮到O了。”);
}否则{
System.out.println(“轮到O了!”);
}
System.out.println();
System.out.println(“测试超出范围的错误(应该再次轮到O):”;
ttt=新的TicTacToe();
ttt.play(0,0);
ttt.play(-1,0);
ttt.play(0,-1);
ttt.play(3,0);
ttt.play(0,3);
if(ttt.isxtorn()){
System.out.println(“不正确,该轮到O了。”);
}否则{
System.out.println(“轮到O了!”);
}
}
}
在TicTacToe类的isGameOver()方法中,您在第一个循环中使用了i,在第二个循环中使用了j,这两个都是在声明之前使用的。解决这个问题,这个问题就会得到解决
public char isGameOver() {
boolean winner = true;
for (int i = 0; i < 3; i++) {
if ((this.board[i][0] == this.board[i][1]) == (this.board[i][0] == this.board[i][2])) {
return this.board[i][0];
}
}
for (int j = 0; j < 3; j++) {
if ((this.board[0][j] == this.board[1][j]) == (this.board[0][j] == this.board[2][j])) {
return this.board[0][j];
}
}
if ((this.board[0][0] == this.board[1][1]) == (this.board[0][0] == this.board[2][2])) {
return this.board[0][0];
}
if ((this.board[0][2] == this.board[1][1]) == (this.board[0][0] == this.board[2][0])) {
return this.board[0][2];
}
public char isGameOver(){
布尔赢家=tr