Task2 bos

 avatar
unknown
java
a year ago
2.7 kB
6
Indexable
static void task2(){
        //read the input file and the number of test case
        Scanner scan = new Scanner(Main.class.getResourceAsStream("board.txt"));
        int t = scan.nextInt();
        //loop for each test case
        for(int n=0;n<t;n++){
            int xP = 0;
            int yP = 0;
            int xQ = 0;
            int yQ = 0;
            //decalre the needed variable
            int board[][] = new int[8][8];
            //loop to assign the number into 2D array and find the position of queen and Pawn
            for(int i = 0; i < board.length; i++){
                for(int j = 0; j < board[0].length; j++){
                    board[i][j] = scan.nextInt();
                    if(board[i][j] == 1){
                        xQ = i;
                        yQ = j;
                    }
                    else if(board[i][j] == 2){
                        xP = i;
                        yP = j;
                    }
             }
           }
           
        
         //decalare any required variables here
         boolean isThreaten = false;
            
         //check whether the pawn is threatened or not and update board
         if (xP == xQ){
             isThreaten = true;
             if(yP < yQ){
                 for(int i = yP+1; i<yQ; i++){
                     board[xP][i] = 3;
                 }
             } else{
                 for(int i = yQ + 1; i<yP; i++){
                     board[xP][i] = 3;
                 }
             }
         } else if(yP == yQ){
             isThreaten = true;
             if(xP > xQ){
                 for(int i = xQ + 1; i<xP; i++){
                     board[i][yP] = 3;
                 }
             }
             else{
                 for(int i = xP + 1; i < xQ; i++){
                     board[i][yP] = 3;
                 }
             }
         }   else if(Math.abs(xP-xQ) == Math.abs(yP - yQ) ){
             isThreaten = true;
             if(xQ < xP && yQ < yP){
                 for(int i = 1; i< xP - xQ; i++){
                     board[xQ + i][xQ + i] = 3;
                 }
             }
             else if(xQ < xP && yQ > yP){
                 for(int i = 1; i< xP - xQ; i++){
                     board[xQ + i][xQ - i] = 3;
                 }
             }
             else if(xQ > xP && yQ > yP){
                 for(int i = 1; i< xQ - xP; i++){
                     board[xQ - i][xQ - i] = 3;
                 }
             }
             else if(xQ > xP && yQ < yP){
                 for(int i = 1; i< xQ - xP; i++){
                     board[xQ + i][xQ - i] = 3;
                 }
             }
         }
Editor is loading...
Leave a Comment