Problem with chess exercise in java

2

The objective of the program is to print a chessboard, with squares B (white) and N (black), ask for a row and a column and establish a bishop there.

So far so good, but the problem comes when you have to modify the board with asterisks in the boxes where you can move the bishop (diagonally):

        B * B N B * B N
        N B * B * B N B
        B N B A B N B N
        N B * B * B N B
        B * B N B * B N
        * B N B N B * B
        B N B N B N B *
        N B N B N B N B

The professor has skipped this problem, so I turn to this forum. How would you raise it? Code:

int posicion=8*(fila-1)+columna;

  for(int i=1;i<=64;i++) {

        if(i%2!=0){
           if(i==posicion){
              System.out.print("A ");
              continue;
           }
           System.out.print("B ");
        }else{
           if(i==posicion){
              System.out.print("A ");
              continue;
           }
           System.out.print("N ");
        }

     if(i%8==0){
        System.out.println();
     }

  }

I have not given matrices yet. Thanks

[EDIT] Only valid movements are valid, according to the actual movement of the bishop.

    
asked by MigherdCas 08.11.2016 в 19:48
source

2 answers

2

If you want your bishop to be placed in the [x1 ; y1] position, then you must complete with asterisks all [x2 ; y2] positions that meet the condition that:

  • X1 != X2 y Y1 != Y2
  • | X1 - X2 | = | Y1 - Y2 |
  • I think the following code can help you:

    public static int mod(int x) { return (x < 0)?-x:x; } 
    
    int x, y; //Posicion del alfil
    
    for(int i=1;i<=8;i++) {
        for(int j=1;j<=8;j++)
            if(i == x && j == y)
                System.out.print("A ");
            else if(mod(i - x) == mod(j - y))
                System.out.print("* ");
            else if((i + j) % 2 == 0)
                System.out.println("B ");
            else
                System.out.println("N ");
        System.out.println();
    }
    
        
    answered by 16.11.2016 / 04:49
    source
    1
    class Ok {
        public static void main(String[] args) {
            dbjt(2, 3);
        }
    
        public static void dbjt(int f, int c) {
            int a = f - c;
            int b = f + c;
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    System.out.print(" ");
                    if (i == f && j == c) {
                        System.out.print("A");
                    } else if (i - j == a || i + j == b) {
                        System.out.print("*");
                    } else {
                        System.out.print((i + j) % 2 == 0 ? "B" : "N");
                    }
                }
                System.out.println();
            }
        }
    }
    

    / *

    It reminded me of the problem of the 8 queens and since a bishop moves partially like a queen, the formula you occupy is similar to the one needed to represent the first problem. You can see the section "Problem Statement" in the link to the wiki that I leave with you; basically one position is diagonal with another if the result of row less column or row plus column is the same in both. Greetings from Mexico. link

    * /

        
    answered by 08.11.2016 в 21:00