Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please write down both java and output. Thank you! Chip - colour String Chip (e:

ID: 3855972 • Letter: P

Question

please write down both java and output. Thank you!

Chip - colour String Chip (e: String): + get/set methods for all the attributes + equals (e : Chip) : boolean + Chip (c: String): + toString () : String Board - rows: int - cols: int - boardllI: Chip +Board (r: int, c: int): + get methods for all the attributes + isEmpty (r : int, c: int): boolean + add (r : int, c : int, chip : Chip): boolean Connect4Board +Connect4Board (O: + add (c: int, colour : String) : boolean + winType (): String + winner (): boolean +toString ) : String

Explanation / Answer

Here is the code for the question with output. In case of any issues, post a comment. If happy with the answer, please rate it. Thank you

Chip.java

public class Chip {

   public static final String RED = "RED";

   public static final String YELLOW = "YELLOW";

   private String colour;

   public Chip(String c)

   {

       colour = c;

   }

   public boolean equals(Chip c)

   {

       if(c == null)

           return false;

       if(c.colour.equalsIgnoreCase(colour))

           return true;

       else

           return false;

      

   }

  

   public void setColour(String c)

   {

       colour = c;

   }

  

   public String getColour()

   {

       return colour;

   }

  

   public String toString()

   {

       return colour.charAt(0)+"";

   }

}

Board.java

public class Board {

   private int rows;

   private int cols;

   protected Chip[][] board;

  

   public Board(int r, int c)

   {

       rows = r;

       cols = c;

       board = new Chip[rows][cols];

   }

   public int getRows()

   {

       return rows;

   }

  

   public int getCols()

   {

       return cols;

   }

  

   public boolean isEmpty(int r, int c)

   {

       return (board[r][c] == null);

   }

  

   public boolean add(int r, int c, Chip chip)

   {

       if(isEmpty(r, c))

       {

           board[r][c] = chip;

           return true;

       }

       else

           return false;

   }

}

Connect4Board.java

public class Connect4Board extends Board {

  

   private Chip winner;

   public Connect4Board()

   {

       super(6, 7);

   }

  

   public boolean add(int c, String colour)

   {

       //check for empty row from bottom in the column c

       for(int r = 5; r >= 0; r--)

       {

           if(isEmpty(r, c))

           {

               add(r, c, new Chip(colour));

               return true;

           }

       }

      

       return false;

   }

  

  

   public String winType()

   {

       int total = 0;

         

       for( int r = 0; r < getRows(); r++)

       {

          

           for(int c = 0; c < getCols(); c++)

           {

               if(board[r][c] == null) continue;

               winner = board[r][c];

               //for each of the current location of r,c , check 8 different directions to see if

               //4 were connected in that direction

              

               //check +ve x horizontal direction

               total = 0;

               for(int i = 0; i < 4 && c+i < getCols(); i++)

               {

                   if(board[r][c].equals(board[r][c+i]) )

                       total++;

               }

              

               if(total == 4)

                   return "Horizontal";

              

               //check -ve horizontal direction

               total = 0;

               for(int i = 0; i < 4 && c-i >= 0; i++)

               {

                   if(board[r][c].equals(board[r][c-i] ))

                       total++;

               }

              

               if(total == 4)

                   return "Horizontal";

              

               //check +ve vertical direction

               total = 0;

               for(int i = 0; i < 4 && r-i >= 0; i++)

               {

                   if(board[r][c].equals(board[r - i][c] ))

                       total++;

               }

              

               if(total == 4)

                   return "Vertical";

              

               //check -ve vertical direction

               total = 0;

               for(int i = 0; i < 4 && r+i < getRows(); i++)

               {

                   if(board[r][c].equals(board[r + i][c]) )

                       total++;

               }

              

               if(total == 4)

                   return "Vertical";

              

               //check diagonal 1 quadrant

               total = 0;

               for(int i = 0; i < 4 && r-i >= 0 && c+i < getCols(); i++)

               {

                   if(board[r][c].equals(board[r - i][c + i] ))

                       total++;

               }

              

               if(total == 4)

                   return "Diagonal";

              

               //check diagonal 2nd quadrant

               total = 0;

               for(int i = 0; i < 4 && r - i >= 0 && c - i >= 0; i++)

               {

                   if(board[r][c].equals(board[r - i][c - i] ))

                       total++;

               }

              

               if(total == 4)

                   return "Diagonal";

              

               //check diagonal 3rd quadrant

               total = 0;

               for(int i = 0; i < 4 && r + i < getRows() && c - i >= 0; i++)

               {

                   if(board[r][c].equals(board[r + i][c - i] ))

                       total++;

               }

              

               if(total == 4)

                   return "Diagonal";

              

               //check diagonal 4th quadrant

               total = 0;

               for(int i = 0; i < 4 && r + i < getRows() && c + i < getCols(); i++)

               {

                   if(board[r][c].equals(board[r + i][c + i] ))

                       total++;

               }

              

               if(total == 4)

                   return "Diagonal";

              

           }

       }

      

       winner = null;

       return null;

   }

  

   public boolean winner()

   {

       if(winType() == null)

           return false;

       else

           return true;

   }

  

   public boolean isFull()

   {

       for(int i = 0; i < getRows(); i++)

       {

           for(int j = 0; j < getCols(); j++)

               if(board[i][j] == null)

                   return false;

       }

      

       return true;

   }

   public Chip getWinningChip()

   {

       return winner;

   }

  

   public String toString()

   {

       String str =" ";

       for(int i = 1 ; i <= getCols(); i ++)

           str += " " + i;

      

       for(int r = 0; r < getRows(); r++)

       {

           str += " " + (r+1) ;

           for(int c = 0; c < getCols(); c++)

           {

               str += " " ;

               if(board[r][c] == null)

                   str += " ";

               else

                   str += board[r][c];

           }

           str += " ";

       }

      

       return str;

   }

}

Play.java

import java.util.Scanner;

public class Play {

   public static void main(String[] args) {

       Scanner keybd = new Scanner(System.in);

       String ans;

       do

       {

           String player1, player2;

           int totalTurns = 0;

           int currTurn = 0;

          

           System.out.println("Welcome to Connect 4. Please enter your names.");

           System.out.print("Player 1 name: ");

           player1 = keybd.nextLine().trim();

           System.out.print("Player 2 name: ");

           player2 = keybd.nextLine().trim();

          

           System.out.println(player1 + " - You have red chips "R" and you go first");

           System.out.println(player2 + " - You have yellow chips "Y" and you go second");

          

           Connect4Board board = new Connect4Board();

           String currChip = Chip.RED;

           int col;

           while(!board.isFull() && board.winner() == false)

           {

               System.out.println(board);

              

               do{

                   System.out.print(" "+ currChip + " - Please input a column # between 1-7: ");

                   col = keybd.nextInt();

               }while(col < 1 || col > 7);

              

               if(board.add(col - 1, currChip))

               {

                   totalTurns++;

                  

                   //change turn and colours

                   if(currChip.equals(Chip.RED))

                       currChip = Chip.YELLOW;

                   else

                       currChip = Chip.RED;      

               }

               else

                   System.out.println("Chip could not be added in the column. Try another column!");

           }

           Chip winner = board.getWinningChip();

           if(winner == null)

               System.out.println("There is no winner");

           else

           {

               if(winner.getColour() == Chip.RED)

                   System.out.println("RED - Connect 4 ! Congratulations " + player1 + "! You win in " + totalTurns + " turns");

               else

                   System.out.println("YELLOW - Connect 4 ! Congratulations " + player2 + "! You win in " + totalTurns + " turns");

              

           }

          

           System.out.print(" Do you want to play again ? y/n: ");

           ans = keybd.next().trim();

       }while(ans.equalsIgnoreCase("y"));

   }

}

output

Welcome to Connect 4. Please enter your names.

Player 1 name: Bob

Player 2 name: Jane

Bob - You have red chips "R" and you go first

Jane - You have yellow chips "Y" and you go second

       1   2   3   4   5   6   7

1                             

2                             

3                             

4                             

5                             

6                             

RED - Please input a column # between 1-7: 1

       1   2   3   4   5   6   7

1                             

2                             

3                             

4                             

5                             

6   R                        

YELLOW - Please input a column # between 1-7: 2

       1   2   3   4   5   6   7

1                             

2                             

3                             

4                             

5                             

6   R   Y                     

RED - Please input a column # between 1-7: 1

       1   2   3   4   5   6   7

1                             

2                             

3                             

4                             

5   R                        

6   R   Y                     

YELLOW - Please input a column # between 1-7: 1

       1   2   3   4   5   6   7

1                             

2                             

3                             

4   Y                        

5   R                        

6   R   Y                     

RED - Please input a column # between 1-7: 3

       1   2   3   4   5   6   7

1                             

2                             

3                             

4   Y                        

5   R                        

6   R   Y   R                

YELLOW - Please input a column # between 1-7: 2

       1   2   3   4   5   6   7

1                             

2                             

3                             

4   Y                        

5   R   Y                     

6   R   Y   R                

RED - Please input a column # between 1-7: 1

       1   2   3   4   5   6   7

1                             

2                             

3   R                        

4   Y                        

5   R   Y                     

6   R   Y   R                

YELLOW - Please input a column # between 1-7: 2

       1   2   3   4   5   6   7

1                             

2                             

3   R                        

4   Y   Y                     

5   R   Y                     

6   R   Y   R                

RED - Please input a column # between 1-7: 4

       1   2   3   4   5   6   7

1                             

2                             

3   R                        

4   Y   Y                     

5   R   Y                     

6   R   Y   R   R             

YELLOW - Please input a column # between 1-7: 2

YELLOW - Connect 4 ! Congratulations Jane! You win in 10 turns

Do you want to play again ? y/n: n