A configuration of the game is a 6 times 7 integer array boa

A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j] = = 2 indicate that a black chip occupies position (i, j); and board[i][j] = = 0 indicates that position(i, j) is empty. The configuration that models the picture of Figure 7.44 is 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 2 2 1 0 2 0 0 1 2 1 1 1 0 Write a method that accepts a game configuration and determines whether or not a player has won. returning \'G\' for green. \'B\' for black, or \'N\' for neither. Test your method in a program that reads a board configuration and reports the winner, if there is one. Write a method int makeMove(int[][] configuration, int column, char color) such that configuration is a board configuration, column is an integer in the range 0 to 6. and color is either \'G\' or \'B\'. The method make Move(...) updates the board configuration by placing a chip of the specified color in the appropriate column. If a column is full, then the method simply returns 0, otherwise the method performs the update and returns 1, indicating a successful move. Write a method that plays the game interactively against the computer Your method must check for illegal moves and report when the game is over The computer can play by choosing a random column and retrying if the move is illegal. However, to make the game more interesting, you might devise \"strategy\" for the computer\'s move. You need to check if the game ends in a tie Include your methods in a program that plays the game.

Solution


import java.util.*;


class Connect4{

   private int nrows;
   private int ncols;
   private int[][] board;

   public Connect4(int[][] _board){
       nrows = 6;
       ncols = 7;
       board = new int[nrows][ncols];

       for (int i = 0; i < nrows; i ++ ) {
           for (int j = 0 ; j < ncols ; j ++ ) {

               board[i][j] = _board[i][j];
              
           }
       }
   }

   public char decode(int a){
       if( a == 1) return \'G\';
       if( a == 1) return \'B\';
       return \'N\';

   }

   public char winner(){
       // will start from 0

       // will only check right, down
       for (int i = 0; i < nrows; i ++ ) {
           for (int j = 0 ; j < ncols ; j ++ ) {

               if(board[i][j] != 0){
                   // check down
                   if(i+4 < nrows){
                       if(board[i][j] == board[i+1][j] && board[i][j] == board[i+2][j] && board[i][j] == board[i+3][j] && board[i][j] == board[i+4][j] ){
                           return decode(board[i][j]);
                       }
                   }

                   if(j+4 < ncols){
                       if(board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] == board[i][j+3] && board[i][j] == board[i][j+4] ){
                           return decode(board[i][j]);
                       }
                   }

               }
              
           }
       }

       return \'N\';
   }


   public static void main(String[] args) {


       int[][] multi = new int[][]{
       { 0, 0, 0, 0, 0, 0, 0},
       { 0, 0, 0, 0, 0, 0, 0},
       { 0, 0, 0, 0, 0, 0, 0},
       { 0, 0, 2, 0, 0, 0, 0},
       { 0, 2, 2, 1, 0, 2, 0},
       { 0, 1, 2, 1, 1, 1, 0}
      
       };


       Connect4 conn = new Connect4(multi);
       System.out.println(conn.winner() + \" \");
   }
  
}

 A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j]
 A configuration of the game is a 6 times 7 integer array, board, such that board[i][j] = = 1 indicates that a green chip occupies position (i, j); board[i][j]

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site