我的代码如下。问题是我输入的任何值(0或1)[xxx] .takenSquare [row] [col] = true; (xxx所在的位置)该方法没有正确设置,因此drawSquare
方法总是返回“O”,即使玩家1已经占据了正方形。
static boolean [][]takenSquare= new boolean [3][3];
int [][]playerTable; //gia tin niki
static tictac[] player = {
new tictac( "PlayerName1" ),
new tictac( "PlayerName2" ),
};
static private int row; //<---
static private int col; //""
private static int activePlayer;
public tictac(String name){
this.name=name;
playerTable=new int[3][3];
}
public static boolean isValidMove(int row,int col){
boolean valid=false;
if( ( row >= 0 && row<3 ) && ( col >=0 &&col < 3 ) ) {
if ( takenSquare[ row ][ col ] == false ) {
valid = true;
}
}
return valid;
} //isValidmove
public static void setMove (int nrow, int ncol,int active){
col=ncol-1;
row=nrow-1;
/* StringTokenizer tokenizer=new StringTokenizer(move);
this.row= Integer.parseInt(tokenizer.nextToken());
this.col= Integer.parseInt(tokenizer.nextToken()); */
if (isValidMove(row,col)){
player[xxx].takenSquare[row][col]=true;
player[xxx].playerTable[row][col]=table[row][col];
}
}
public static String drawTable(){
if ( ( row >= 0 && row<3 ) && ( col >=0 &&col < 3 ) ){
String a="";
a+=(drawSquare(0,0)+"|");
a+=(drawSquare(0,1)+"|");
a+=(drawSquare(0,2)+'\n');
a+=("-----------")+'\n';
a+=drawSquare(1,0)+"|";
a+=drawSquare(1,1)+"|";
a+=(drawSquare(1,2)+'\n');
a+=("-----------")+'\n';
a+=drawSquare(2,0)+"|";
a+=drawSquare(2,1)+"|";
a+=(drawSquare(2,2));
return a;
} else return "error";
}
public static String drawSquare(int x,int y){
if (player[0].isTaken(x,y)) {
return " O ";
} else if (player[1].isTaken(x,y)) return " X ";
else return " ";
}
public boolean isTaken(int x,int y) {
return takenSquare[x][y];
}
答案 0 :(得分:0)
播放器[0] .takenSquare [row] [col] 与播放器[1] .takenSquare [row] [col] 相同,与< i> ClassName.takenSquare [row] [col] 其中'player'是ClassName类的对象。
你允许引用静态字段的方式,但不是你打算用它做什么!
你的两个玩家都分享这个资源,这就是为什么对于任何玩家来说都是一样的!请重做您的代码,并确保不对非共享资源使用静态来避免此问题。