删除象/后的对角线走法,如果棋子阻挡(Java)。

huangapple 未分类评论47阅读模式
英文:

chess game delete diagonal movements if pieces block for bishops/queens (java)

问题

boolean t = false;
if (hasRectMoves) {
    for (int i = y; i >= 0; i--) {
        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;
                }
                test = false;
            }
        } else {
            res[x + 1][i] = 4;
        }
    }
    test = true;
    for (int i = x; i >= 0; i--) {
        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;
                }
                test = false;
            }
        } else {
            res[i][y + 1] = 4;
        }
    }
    test = true;
    for (int i = x + 2; i < 8; i++) {
        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;
                }
                test = false;
            }
        } else {
            res[i][y + 1] = 4;
        }
    }
    test = true;
    for (int i = y + 2; i < 8; i++) {
        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;
                }
                test = false;
            }
        } else {
            res[x + 1][i] = 4;
        }
    }
}
英文:

I'm making a Chess game in Java.

I did a JFrame that lets me create pieces that is why I have all possibles moves for anypiece (and I'm going to make more pieces than there are in normal chess).

But I have a little problem, its been 2 days I'm trying to delete the movements of bishops, and it not as easy as it seems.

I've an array containing the pieces positon that looks like this :

_______ 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

The function is called when it needs to know the legal moves of a piece and it has detected that the piece has diagonal moves
for this piece legal moves here should look like this

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

Because there are other pieces (white) that blocks the moves

Thank you in advance, if you want (if it helps you to help me) I can give you the function of movements for rooks (its when rectiligne movements are detected so it works also for the queen and whatever piece I create that has this kind of moves).
code for rooks :

boolean t = false;
   if (hasRectMoves) {
    System.out.println(x + &quot; XY &quot; + y);
    System.out.println(&quot;HAS RECT MOVES !&quot;);
    System.out.println(&quot;_______ PIECES&quot;);
    for (int i = 0; i &lt; 8; i++) {
     for (int j = 0; j &lt; 8; j++) {
      System.out.print(pieces[i][j] + &quot; &quot;);
     }
     System.out.println(&quot;&quot;);
    }
    System.out.println(&quot;_______ RES&quot;);
    for (int i = 0; i &lt; 8; i++) {
     //System.out.println(&quot;i = &quot;+ i);
     for (int j = 0; j &lt; 8; j++) {
      System.out.print(res[j][i] + &quot; &quot;);
     }
     System.out.println(&quot;&quot;);
    }
    System.out.println(&quot;My piece : &quot; + (x + 1) + &quot; &quot; + (y + 1));
    boolean test = true;

    for (int i = y; i &gt;= 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 + &quot; &quot;+ pieces[i][y+1] + &quot; &quot;  + (x+1) + &quot;=x y=&quot;+ (y+1)); 
       test = false;
      }
     } else {
      res[x + 1][i] = 4;
     }

    }
    test = true;
    for (int i = x; i &gt;= 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 + &quot; &quot;+ pieces[i][y+1] + &quot; &quot;  + (x+1) + &quot;=x y=&quot;+ (y+1)); 
       test = false;
      }
     } else {
      res[i][y + 1] = 4;
     }

    }
    test = true;
    for (int i = x + 2; i &lt; 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 + &quot; &quot;+ pieces[i][y+1] + &quot; &quot;  + (x+1) + &quot;=x y=&quot;+ (y+1)); 
       test = false;
      }
     } else {
      res[i][y + 1] = 4;
     }

    }
    test = true;
    for (int i = y + 2; i &lt; 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 + &quot; &quot;+ pieces[i][y+1] + &quot; &quot;  + (x+1) + &quot;=x y=&quot;+ (y+1)); 
       test = false;
      }
     } else {
      res[x + 1][i] = 4;
     }
    }

答案1

得分: 0

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

Note: I've only translated the provided Java code into text. The code is related to a chess-like project involving positions and moves. If you have any specific questions or need further assistance, feel free to ask.

英文:

Nice project.

I wrote a little example in a main so you can try it out. Sure there is room for improvement.

My output:

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 

The position of the bishop is labeled with 9.
I add every field a rating starting by 8 counting down regulated how far the field is away form the bishop. (I hope this is English?)

Then you have to split the rectangle into 4 subs. For every sub you have to calculate the legal moves separately.

Please try it out. If you have a question please let me know.

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 + &quot; XY &quot; + y);
        System.out.println(&quot;HAS RECT MOVES !&quot;);
        System.out.println(&quot;_______ PIECES&quot;);
        for (int i = 0; i &lt; 8; i++) {
            for (int j = 0; j &lt; 8; j++) {
                if(i&lt;2){ pieces[i][j] = 1; }
                if(i&gt;5){ pieces[i][j] = 2; }
                
                System.out.print(pieces[i][j] + &quot; &quot;);
            }
            System.out.println(&quot;&quot;);
        }
        System.out.println(&quot;_______ RES&quot;);
        for (int i = 0; i &lt; 8; i++) {
            for (int j = 0; j &lt; 8; j++) {
                res[i][j] = checkField(i, j, x, y);
                System.out.print(res[i][j] + &quot; &quot;);
            }
            System.out.println(&quot;&quot;);
        }
        System.out.println(&quot;My piece : &quot; + (x) + &quot; &quot; + (y));
        System.out.println(&quot;_______ legalmoves&quot;);
        // 1. section:
        System.out.println(&quot;1. section:&quot;);
        calcSection(pieces, res, 0, 0, x, y);
        // 2. section:
        System.out.println(&quot;2. section:&quot;);
        calcSection(pieces, res, 0, y, x, 7);
        // 3. section:
        System.out.println(&quot;3. section:&quot;);
        calcSection(pieces, res, x, 0, 7, y);
        // 4. section:
        System.out.println(&quot;4. section:&quot;);
        calcSection(pieces, res, x, y, 7, 7);

        for (int i = 0; i &lt; 8; i++) {
            for (int j = 0; j &lt; 8; j++) {
                System.out.print(res[i][j] + &quot; &quot;);
            }
            System.out.println(&quot;&quot;);
        }
        
    }
    public static int[][] calcSection(int[][] pieces, int[][] res, int recStartX, int recStartY, int recEndX, int recEndY){
        System.out.println(recStartX+ &quot;:&quot;+ recStartY + &quot;:&quot;+ recEndX +&quot;:&quot; +recEndY);
        System.out.println(&quot;res[i][j] = &quot; + res[5][7]);
        for (int k = 8; k &gt; 0; k--) {
            for (int i = recStartX; i &lt;= recEndX; i++) {
                for (int j = recStartY; j &lt;= recEndY; j++) {
                    if(res[j][i] == k){
                        if(pieces[j][i] &gt; 0){
                            for (int l = recStartX; l &lt;= recEndX; l++) {
                                for (int m = recStartY; m &lt;= recEndY; m++) {
                                    if(res[m][l] &lt;= 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 )&amp;(fieldX == y )){
            return 9;
        }
        else{
            for (int i = 0; i &lt; 8; i++) {
                if((fieldY == x+i )&amp;(fieldX == y+i )){
                    return 9-i;
                } 
                else if((fieldY == x+i )&amp;(fieldX == y-i )){
                    return 9-i;
                } 
                else if((fieldY == x-i )&amp;(fieldX == y+i )){
                    return 9-i;
                }
                else if((fieldY == x-i )&amp;(fieldX == y-i )){
                    return 9-i;
                }
            }
        }
        return 0;
    }
}

huangapple
  • 本文由 发表于 2020年4月4日 23:36:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/61030509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定