Question: During the process of writing code, the programmer is deeply immersed in the problem, the algorithms and code to solve it and has that I

During the process of writing code, the programmer is deeply immersed in the problem, the algorithms and code to solve it and has that context when naming methods, variables, and writing comments. Looking at someone else's  code, or even your own code 6 months later can be challenging since the names and comments weren't necessarily written to guide someone new 

I need help to finish debugging the isWinningCoord method and finish the test cases for the TestBenchConnectFour class. I already did all the code refactoring I just need help with these two things. I have all the code I did for both files below. 


import java.util.Random;

import java.util.Scanner;

import java.util.Arrays;


public class ConnectFour {


   final static int BOARD_WIDTH = 7;

   final static int BOARD_HEIGHT = 6;

   final static int CONNECT_WIN = 4;

   final static int COMPUTER_PLAYER = 0;

   final static int HUMAN_PLAYER = 1;


   public enum argState {NONE, PLAYERS, TEST};


   /**

    * This is the main method that introduces the users to the program and prompts the users to enter a input representative of a a column within the game.

    * The main method is also responsible for determining if the player input or move is selected on a column that is not filled, if it is the program will continue until desired input is fulfilled.

    * The main method utilizes class constant variables to set the parameters of the board height and width along with parameters for the win conditions and the different player types.    

    * @param args

    */

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       int numPlayers = 2;

       boolean testMode = false;

       boolean seedInput = false;

       long seed = 0;


       argState cmdFlag = argState.NONE;

       for(String arg: args) {

           switch(arg) {

           case "-t":

               cmdFlag = argState.TEST;

               break;

           case "-p":

               cmdFlag = argState.PLAYERS;

               break;

           default:

               if(cmdFlag == argState.TEST) {

                   seed = Long.parseLong(arg);

                   seedInput = true;

               } else if(cmdFlag == argState.PLAYERS) {

                   numPlayers = Integer.parseInt(arg);

               }

               cmdFlag = argState.NONE;

               break;

           }

       }

       Random rand;

       if(seedInput) {

           rand = new Random(seed);

       } else {

           rand = new Random();

       }

       int[] players = new int[]{COMPUTER_PLAYER, COMPUTER_PLAYER};

       for(int i = 0; i

           players[i] = HUMAN_PLAYER;

       }

       boolean gameOn = true;

       System.out.println("Welcome to Connect Token Game. On your turn, please select a column from 1 to " +

                          BOARD_WIDTH + " to drop your token.");

       int[][] board = new int[BOARD_HEIGHT][BOARD_WIDTH];

       for(int i = 0; i

           Arrays.fill(board[i], -1);

       }

       int player = 0;

       playerGameBoard(board);

       while(gameOn) {

           System.out.println("Player " + (player + 1) + " your move:");

           if(players[player] == HUMAN_PLAYER) {

               while(!input.hasNextInt()){

                   input.next();

               }

               int move = input.nextInt();

               if(move BOARD_WIDTH || board[BOARD_HEIGHT - 1][move - 1] != -1) {

                   System.out.println("Invalid column: " + move + ". Please select a (non-full) column from 1 to " +

                                      BOARD_WIDTH + ".");

                   continue;

               }

               gameOn = !isWinningCol(move - 1, board, player);

               dropToken(move - 1, board, player);

           } else {

               gameOn = !comp(board, player, rand);

           }

           playerGameBoard(board);

           if(!gameOn) {

               System.out.println("Player " + (player + 1) + " won!");

           }

           player = (player + 1) % 2;

           if(gameOn && checkFullBoard(board)) {

               System.out.println("Game over. We have a draw!");

               gameOn = false;

           }

       }

       System.out.println("Thank you for playing!");

   }


   /**

    * Checks if the game board is full, i.e., if no more tokens can be added. 

    *

    * A game board is not full if any of the top most cells contain the value -1.

    *

    * @param board The game board to verify. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    * @return true if the game board is not full, false otherwise.

    */

   public static boolean checkFullBoard(int[][] board)

   {

       for(int i = 0; i

           {

               if(board[BOARD_HEIGHT - 1][i] == -1)

                   return false;

           }

       return true;

   }


   /**

    * Maps the player index to a character.

    *

    * @param player The integer index to map to a character.

    * @return Returns the mapped character:

    *        - 0 is mapped to 'X'

    *        - 1 is mapped to 'O'

    *        - Every other index is mapped to ' '

    */

   public static char getToken(int player) {

       return (player == 0 ? 'X' : (player == 1 ? 'O' : ' '));

   }


   /**

    * Drops a token into the game board at a specified column, col. The token is place at the lowest

    * unfilled cell (value -1) of column col. Specifically, the lowest unfilled cell is set to the player 

    * index.

    *

    * @param col The column where the token is dropped.

    * @param board The game board into which the token is dropped. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    * @param player The player index. 

    * @return Returns false if the column if full, i.e., the maximum index is not -1. Otherwisem returns true.

    */

   public static boolean dropToken(int col, int[][] board, int player) {

       if(board[BOARD_HEIGHT - 1][col] != -1) {

           return false;

       }

       for(int i = 0; i

           if(board[i][col] == -1) {

               board[i][col] = player;

               break;

           }

       }

       return true;

   }


   /**

    * Checks each column to see if dropping a token at that column would result in a win for 

    * the specified player index.

    *

    * @param board The game board into which the token is dropped. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    * @param player The player index. 

    * @returns The lowest column index for which the specified player would win by dropping a token. If there is

    *         no such column, -1 is returned.

    */

   public static int findWinningMove(int[][] board, int player) {

       for(int col = 0; col

           if(isWinningCol(col, board, player)) {

               return col;

           }

       }

       return -1;

   }


   /**

    * Checks if dropping a token at the specified column for the specified player would result in 

    * a win.

    *

    * @param col The column where the token is dropped.

    * @param board The game board into which the token is dropped. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    * @param player The player index. 

    * @return true if col is a winning column for player. Otherwise, returns false. 

    */

   public static boolean isWinningCol(int col, int[][] board, int player) {

       for(int i = BOARD_HEIGHT - 1; i >= 0; i--)

           {

               if(isWinningCoord(i, col, board, player)) { return true; }

           }

       return false;

   }


//Buggy method: add test vectors to test bench in order to debug and fix the code.

   /**

    * This method sets the win conditions for the game and will return them to the program based on what condition is met.

    * @param row The row where the token is dropped.

    * @param col The column where the token is dropped.

    * @param board The game board into which the token is dropped. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    * @param player The player index

    * @return true if the row and column is a winning set for the player if vertical horizontal, or diagonal. Otherwise, returns false. 

    */


   public static boolean isWinningCoord(int row, int col, int[][] board, int player) {

       if(row >= BOARD_HEIGHT || row = BOARD_WIDTH || col 0 && board[row - 1][col] == -1)) {

           return false;

       }

       { // Vertical

           int count = 0;

           for(int i = row - 1; i >= 0; i--) {

               if(board[i][col] != player) {

                   break;

               } else {

                   count++;

               }

           }

           if(count >= CONNECT_WIN - 1) {

               return true;

           }

       }

       { // Horizontal

           int count = 0;

           for(int i = col - 1; i >= 0; i--) {

               if(board[row][i] != player) {

                   break;

               } else {

                   count++;

               }

           }

           for(int i = col + 1; i

               if(board[row][i] != player) {

                   break;

               } else {

                   count++;

               }

           }           

           if(count >= CONNECT_WIN - 1) {

               return true;

           }

       }

       { // Diagonals

           int countNegSlope = 0;

           for(int i = row + 1; i

               for(int j = col - 1; j >= 0; j--){

                   if(board[i][j] != player) {

                       break;

                   } else {

                       countNegSlope++;

                   }

               }

           }

           for(int i = row - 1; i >= 0; i--){

               for(int j = col + 1; j

                   if(board[i][j] != player) {

                       break;

                   } else {

                       countNegSlope++;

                   }

               }

           }

           if(countNegSlope >= CONNECT_WIN - 1) {

               return true;

           }

           int countPosSlope = 0;

           for(int i = row + 1; i

               for(int j = col + 1; j

                   if(board[i][j] != player) {

                       break;

                   } else {

                       countPosSlope++;

                   }

               }

           }

           for(int i = row - 1; i >= 0; i--){

               for(int j = col - 1; j >= 0; j--){

                   if(board[i][j] != player) {

                       break;

                   } else {

                       countPosSlope++;

                   }

               }

           }

           if(countPosSlope >= CONNECT_WIN - 1) {

               return true;

           }


       }

       return false;

   }


   /**

    * 

    * @param board The game board into which the token is dropped by the computer player. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    * @param player the player index.

    * @param rand the number generator that helps the computer decide where to place the token in the column.

    * @return

    */

   public static boolean comp(int[][] board, int player, Random rand) {

       int col = findWinningMove(board, player);

       if(col != -1) {

           dropToken(col, board, player);

           return true;

       }

       col = findWinningMove(board, (player + 1) % 2);

       if(col != -1) {

           dropToken(col, board, player);

           return false;

       }

       do {

           col = rand.nextInt(BOARD_WIDTH);

       } while(board[BOARD_HEIGHT - 1][col] != -1);

       dropToken(col, board, player);

       return false;

   }


   /**

    * This method decides how the outline of the gameBoard the players will used will be printed using the static variable of the Board_Width to decide how many "-" and "--" and "|" are printed. 

    * @param board The game board into which the token is dropped. It must be of size BOARD_WIDTH * BOARD_HEIGHT.

    */

   public static void playerGameBoard(int[][] board) {

       for(int i = 0; i

           System.out.print("--");

       }

       System.out.println("-");

       for(int i = board.length - 1; i >= 0; i--) {

           System.out.print("|");

           for(int j = 0; j

               System.out.print(getToken(board[i][j]) + "|");

           }

           System.out.println();

       }

       for(int i = 0; i

           System.out.print("--");

       }

       System.out.println("-");

       System.out.print(" ");

       for(int i = 0; i

           System.out.print((i + 1) + " ");

       }

       System.out.println();

   }


}

public class TestBenchConnectFour {


   public static void main(String[] args) {

       testIsWinningCord();

   }


   // Add at least 4 more clearly distinct test vectors to help debug the bugs in isWinningCoord

   public static void testIsWinningCord() {

       {//Vertical

           int[][] board = {

               {0,-1,-1,-1,-1,-1,-1},

               {0,-1,-1,-1,-1,-1,-1},

               {0,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1}

           };

           System.out.println("Vertical winning move test (Row: 3, Col: 0): " +

                              ConnectFour.isWinningCoord(3, 0, board, 0));

           ConnectFour.playerGameBoard(board);

       }

       {//Vertical Negative 1

           int[][] board = {

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1}

           };

           System.out.println("Vertical (neg) winning move test (Row: 3, Col: 0): " +

                              ConnectFour.isWinningCoord(3, 0, board, 0));

           ConnectFour.playerGameBoard(board);

       }

       {//Diagonal 

           int[][] board = {

               { 1, 1, 1, 0,-1,-1,-1},

               { 1, 1, 0,-1,-1,-1,-1},

               { 1, 0,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1}

           };

           System.out.println("Diagonal winning move test (Row: 3, Col: 0): " +

                              ConnectFour.isWinningCoord(3, 0, board, 0));

           ConnectFour.playerGameBoard(board);

       }

       {//Diagonal Negative 1

           int[][] board = {

               { 1, 1, 1, 0,-1,-1,-1},

               { 1, 1, 0,-1,-1,-1,-1},

               { 1, 0,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1}

           };

           System.out.println("Diagonal (neg) winning move test (Row: 3, Col: 0): " +

                              ConnectFour.isWinningCoord(3, 0, board, 0));

           ConnectFour.playerGameBoard(board);

       }


       {//Horizontal 

           int[][] board = {

               {0,0,0,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1},

               {-1,-1,-1,-1,-1,-1,-1}

           };

           System.out.println("Horizontal winning move test (Row: 1, Col: 0): " +

                              ConnectFour.isWinningCoord(1, 0, board, 0));

           ConnectFour.playerGameBoard(board);

       }

       {//Horizontal Negative 1

           int[][] board = {

                 {-1,-1,-1,-1,-1,-1,-1},

                   {-1,-1,-1,-1,-1,-1,-1},

                   {-1,-1,-1,-1,-1,-1,-1},

                   {-1,-1,-1,-1,-1,-1,-1},

                   {-1,-1,-1,-1,-1,-1,-1},

                   {-1,-1,-1,-1,-1,-1,-1}

           };

           System.out.println("Horizontal (neg) winning move test (Row: 3, Col: 0): " +

                              ConnectFour.isWinningCoord(3, 0, board, 0));

           ConnectFour.playerGameBoard(board);

       }

   }


}

context when naming methods, variables, and writing comments. Looking at someone else's code, or even your own code 6 months later can be challenging since the names and comments weren't necessarily written to guide someone new to the problem and code. This assignment's code works, but some methods have poorly chosen names, formatting, and comments. The objective is to format it, reduce redundant code, improve the naming and commenting to help guide someone like yourself, that is new to the code and problem, through it. Play the game, add additional print statements to learn about the contents of the variables, and use the debugger to systematically step through the code. This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded, see the grading rubric below. Getting Started Set up a Connect Four project in your IDE (Eclipse or intelliJ) Download ConnectFour.java and Test Bench ConnectFour.java Make sure both files compile and run. Note: The provided code compiles and runs, but there is a bug in the method is WinningCoord. The goal is to fix this bug, and refactor, improving the code and ensuring that it is working. Add a file header comment to both files. Turn in both files to zyBooks to make sure all the tests pass. Style and Format each of the methods Format the method code to follow course standards. The IDEs can be helpful, but review to make sure the standards are followed. In Eclipse, select code, go to the Source menu, select Format, and then select Correct Indentation (Alternatively: Ctrl + Shift + F) o In IntelliJ, select code (in editor, Ctrl + A), go to Code menu, select Reformat Code (alternatively: Ctrl + Alt + L) Verify your program is still working. o Turn into zyBooks to verify your program is still working. Turning in also saves snapshots of your code to return to at a later time if necessary. If at any time your program is not working, then back up to a point it was working and try again.

Step by Step Solution

3.43 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!