我试图建立一个象棋引擎作为一个长期的项目。目前我正在研究一种翻转电路板的方法(比如翻转电路板,但也要改变颜色)。大写字母代表白色,小写字母代表黑色。但是在初始初始化之后,java似乎正在覆盖我的temp变量,尽管我没有给它赋值。如system.out.println所示:“r”<-第一次输出r“<-秒输出
我对java很陌生,我认为问题是在将静态变量的值赋给临时变量时引起的。在我看来,其余的代码应该可以正常工作。
public class chess{
static String chessBoard[][]={
{"r","k","b","q","a","b","k","r"},
{"p","p","p","p","p","p","p","p"},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," ","P"," "},
{"P","P","P","P","P","P"," ","P"},
{"R","K","B","Q","A","B","K","R"}};
}
public static void flipBoard() {
String temp[][]=chessBoard;
System.out.println(temp[0][0]);
for(int i=0;i<64;i++){
int r=i/8, c=i%8;
chessBoard[r][c]=temp[7-r][7-c];
}
System.out.println(temp[0][0]);
}
我期待着:
chessBoard[][]={
{"R","K","B","A","Q","B","K","R"},
{"P"," ","P","P","P","P","P","P"},
{" ","P"," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{"p","p","p","p","p","p","p","p"},
{"r","k","b","a","q","b","k","r"}};
但我得到了:
chessBoard[][]={
{"R","K","B","A","Q","B","K","R"},
{"P"," ","P","P","P","P","P","P"},
{" ","P"," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," "," "," "},
{" "," "," "," "," "," ","P"," "},
{"P","P","P","P","P","P"," ","P"},
{"R","K","B","Q","A","B","K","R"}};
如你所见,所有的碎片现在都是白色的。我真的失去了我的心,任何帮助是非常感谢!
2条答案
按热度按时间zour9fqk1#
String temp[][]=chessBoard;
//将棋盘变量的引用指定给temp。要制作数组的深度副本,请尝试以下操作
rta7y2nd2#
为什么不:
或者您可能希望沿对角线反转: