COMPSCI 200 University of Wisconsin Madison Debug & Implement Test Cases Project
Question Description
I did all the code refactoring and added all the header comments I just need help debugging and finishing the test cases. I have all needed information and code below thank you.
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 < numPlayers && i < players.length; i++) {
players[i] = HUMAN_PLAYER;
}
boolean gameOn = true;
System.out.println(“Welcome to Connect Token Game. nOn 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 < board.length; 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 < 1 || 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 < BOARD_WIDTH; 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 < board.length; 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 < BOARD_WIDTH; 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 < 0 || col >= BOARD_WIDTH || col < 0 || board[row][col] != -1 || (row < BOARD_HEIGHT – 1 && board[row + 1][col] != -1) || (row > 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 < BOARD_WIDTH; 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 < BOARD_HEIGHT; 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 < BOARD_WIDTH; 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 < BOARD_HEIGHT; i++){
for(int j = col + 1; j < BOARD_WIDTH; 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 < BOARD_WIDTH; i++) {
System.out.print(“–“);
}
System.out.println(“-“);
for(int i = board.length – 1; i >= 0; i–) {
System.out.print(“|”);
for(int j = 0; j < BOARD_WIDTH; j++) {
System.out.print(getToken(board[i][j]) + “|”);
}
System.out.println();
}
for(int i = 0; i < BOARD_WIDTH; i++) {
System.out.print(“–“);
}
System.out.println(“-“);
System.out.print(” “);
for(int i = 0; i < BOARD_WIDTH; 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
//Note: I already have I jest need help fixing them.
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);
}
}
}
"Place your order now for a similar assignment and have exceptional work written by our team of experts, guaranteeing you "A" results."