gpt4 book ai didi

java - 国际象棋游戏如果棋子阻挡了主教/皇后,则删除对角线移动(java)

转载 作者:行者123 更新时间:2023-12-01 17:36:26 29 4
gpt4 key购买 nike

我正在用 Java 制作国际象棋游戏。

我做了一个 JFrame,它可以让我创建棋子,这就是为什么我对任何棋子都有所有可能的走法(并且我将制作比正常国际象棋中更多的棋子)。

但是我有一个小问题,我已经尝试删除主教的移动两天了,但这并不像看起来那么容易。

我有一个包含片段位置的数组,如下所示:

_______ 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

当需要知道棋子的合法走法并且检测到该棋子有对角走法时,调用该函数对于这一部分,这里的合法 Action 应该是这样的

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

因为还有其他棋子(白色)阻挡移动

提前谢谢您,如果您愿意(如果它可以帮助您帮助我),我可以为您提供车的运动功能(当检测到直线运动时,它也适用于女王以及我创建的任何作品)有这样的 Action )。车代码:

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;
}
}

最佳答案

不错的项目。

我在 main 中写了一个小例子,所以你可以尝试一下。当然还有改进的空间。

我的输出:

3 XY 4
HAS RECT MOVES !
_______ PIECES
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
_______ RES
0 0 0 0 0 0 0 5
6 0 0 0 0 0 6 0
0 7 0 0 0 7 0 0
0 0 8 0 8 0 0 0
0 0 0 9 0 0 0 0
0 0 8 0 8 0 0 0
0 7 0 0 0 7 0 0
6 0 0 0 0 0 6 0
My piece : 3 4
_______ legalmoves
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 7 0 0 0 7 0 0
0 0 8 0 8 0 0 0
0 0 0 9 0 0 0 0
0 0 8 0 8 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

主教的位置标记为9。我为每个字段添加了一个从 8 开始倒数的评级,规定了该字段距主教的距离。 (我希望这是英文?)

然后你必须将矩形分成 4 个子矩形。对于每个替补,你必须单独计算合法的移动。

请尝试一下。如果您有疑问,请告诉我。

public static void main(String[] args) throws Exception{

int pieces[][] = new int[8][8];
int res[][] = new int[8][8];

int x = 3;
int y = 4;

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++) {
if(i<2){ pieces[i][j] = 1; }
if(i>5){ pieces[i][j] = 2; }

System.out.print(pieces[i][j] + " ");
}
System.out.println("");
}
System.out.println("_______ RES");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
res[i][j] = checkField(i, j, x, y);
System.out.print(res[i][j] + " ");
}
System.out.println("");
}
System.out.println("My piece : " + (x) + " " + (y));
System.out.println("_______ legalmoves");
// 1. section:
System.out.println("1. section:");
calcSection(pieces, res, 0, 0, x, y);
// 2. section:
System.out.println("2. section:");
calcSection(pieces, res, 0, y, x, 7);
// 3. section:
System.out.println("3. section:");
calcSection(pieces, res, x, 0, 7, y);
// 4. section:
System.out.println("4. section:");
calcSection(pieces, res, x, y, 7, 7);

for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println("");
}

}
public static int[][] calcSection(int[][] pieces, int[][] res, int recStartX, int recStartY, int recEndX, int recEndY){
System.out.println(recStartX+ ":"+ recStartY + ":"+ recEndX +":" +recEndY);
System.out.println("res[i][j] = " + res[5][7]);
for (int k = 8; k > 0; k--) {
for (int i = recStartX; i <= recEndX; i++) {
for (int j = recStartY; j <= recEndY; j++) {
if(res[j][i] == k){
if(pieces[j][i] > 0){
for (int l = recStartX; l <= recEndX; l++) {
for (int m = recStartY; m <= recEndY; m++) {
if(res[m][l] <= k){
res[m][l] = 0;
}
}
}
return res;
}
}
}
}
}
return res;
}

public static int checkField(int fieldX, int fieldY, int x, int y){
//x = 3;
//y = 2;
if((fieldY == x )&(fieldX == y )){
return 9;
}
else{
for (int i = 0; i < 8; i++) {
if((fieldY == x+i )&(fieldX == y+i )){
return 9-i;
}
else if((fieldY == x+i )&(fieldX == y-i )){
return 9-i;
}
else if((fieldY == x-i )&(fieldX == y+i )){
return 9-i;
}
else if((fieldY == x-i )&(fieldX == y-i )){
return 9-i;
}
}
}
return 0;
}
}

关于java - 国际象棋游戏如果棋子阻挡了主教/皇后,则删除对角线移动(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61030509/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com