【发布时间】:2020-04-04 15:36:11
【问题描述】:
我正在用 Java 制作国际象棋游戏。
我做了一个 JFrame,它可以让我创建棋子,这就是为什么我对任何棋子都有所有可能的移动(而且我将做出比普通国际象棋更多的棋子)。
但是我有一个小问题,我已经2天了我试图删除主教的动作,它并不像看起来那么容易。
我有一个包含片段位置的数组,如下所示:
_______ PIECES[x][y] //1 is black 0 is no piece 2 is white
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2
_______ legalmoves[y][x] //Containing legal moves 1 is move/attack 0 cannot move there
// ( (x,y) (its y x in this tab)
// is reverse because it needs to be somewhere else in the code this is not a big deal)
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 1
0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0
My piece X,Y : 5 7
当它需要知道棋子的合法走法并且检测到棋子有对角线走法时调用该函数 对于这件作品,这里的合法动作应该是这样的
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
因为还有其他棋子(白色)阻碍了移动
提前谢谢你,如果你愿意(如果它帮助你帮助我)我可以给你车的运动功能(当检测到直线运动时,它也适用于女王和我创造的任何作品有这种动作)。 车代码:
boolean t = false;
if (hasRectMoves) {
System.out.println(x + " XY " + y);
System.out.println("HAS RECT MOVES !");
System.out.println("_______ PIECES");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(pieces[i][j] + " ");
}
System.out.println("");
}
System.out.println("_______ RES");
for (int i = 0; i < 8; i++) {
//System.out.println("i = "+ i);
for (int j = 0; j < 8; j++) {
System.out.print(res[j][i] + " ");
}
System.out.println("");
}
System.out.println("My piece : " + (x + 1) + " " + (y + 1));
boolean test = true;
for (int i = y; i >= 0; i--) {
//res[x+1][i]=0;
if (test) {
if (pieces[i][x + 1] != 0) {
if (this.isBlack) {
if (pieces[i][x + 1] == 1)
res[x + 1][i] = 4;//4 to seen in the array where it makes move illegal
}
if (!this.isBlack) {
if (pieces[i][x + 1] == 2)
res[x + 1][i] = 4;
}
// System.out.println(i + " "+ pieces[i][y+1] + " " + (x+1) + "=x y="+ (y+1));
test = false;
}
} else {
res[x + 1][i] = 4;
}
}
test = true;
for (int i = x; i >= 0; i--) {
//res[x+1][i]=0;
if (test) {
if (pieces[y + 1][i] != 0) {
if (this.isBlack) {
if (pieces[y + 1][i] == 1)
res[i][y + 1] = 4;
}
if (!this.isBlack) {
if (pieces[y + 1][i] == 2)
res[i][y + 1] = 4;
}
// System.out.println(i + " "+ pieces[i][y+1] + " " + (x+1) + "=x y="+ (y+1));
test = false;
}
} else {
res[i][y + 1] = 4;
}
}
test = true;
for (int i = x + 2; i < 8; i++) {
//res[x+1][i]=0;
if (test) {
if (pieces[y + 1][i] != 0) {
if (this.isBlack) {
if (pieces[y + 1][i] == 1)
res[i][y + 1] = 4;
}
if (!this.isBlack) {
if (pieces[y + 1][i] == 2)
res[i][y + 1] = 4;
}
// System.out.println(i + " "+ pieces[i][y+1] + " " + (x+1) + "=x y="+ (y+1));
test = false;
}
} else {
res[i][y + 1] = 4;
}
}
test = true;
for (int i = y + 2; i < 8; i++) {
//res[x+1][i]=0;
if (test) {
if (pieces[i][x + 1] != 0) {
if (this.isBlack) {
if (pieces[i][x + 1] == 1)
res[x + 1][i] = 4;
}
if (!this.isBlack) {
if (pieces[i][x + 1] == 2)
res[x + 1][i] = 4;
}
// System.out.println(i + " "+ pieces[i][y+1] + " " + (x+1) + "=x y="+ (y+1));
test = false;
}
} else {
res[x + 1][i] = 4;
}
}
【问题讨论】:
标签: java arrays chess diagonal