QuestionQuestion

Assignment
Goals
Learn how to work with multidimensional arrays and associated array logic. Become accustom to more complex logic such as managing the game logic/flow in this assignment.
Task
Create a program to play Connect-N. Connect-N is similar to Connect-4 except that the game can be played on a game board of any size and have a different number of chips in a row to win.This program will allow two people to play the game of Connect-N via the command line.
The game will start on player ones’ turn. The state of the game board will be outputted to the command line at the start of every player’s turn. Each player will be asked to input their chip into a column. Players should be asked for valid input until they give valid input. Players also have the option to quit the game at any time by typing -1. Once a valid column number is given the chip should be inserted into the correct location in the game and the array holding the game state. If there are no more open spots for the given column, the user should be notified and asked to input a different column number. The game ends when a player has achieved n-in-a-row horizontally, vertically, or diagonally. The game also ends when there are no more spots left to insert chips. When the game ends the winner of the game should be outputted as well as the final game state. The game should then ask the user if he/she wishes to play again. If the user chooses to play again, a new game will start otherwise the main loop of the program should end as well as the program.
You must use the template provided without adding, removing, or changing any function signatures or variable declarations. Win detection should be implemented using the last position where a chip was inserted or no credit will be given for the assignment. This program will be partially implemented in Assignment 2 and then finished in Assignment 3.
The following is a list of functions and which functions should be implemented for Assignment 2 and which functions should be implemented for Assignment 2.
Function List
Function Signature
public static void main(String args[])
public ConnectN()
public ConnectN(int rows, int columns, int winningNumber)
public void playGame()
public void outputGameState()
public boolean insertChip(int playerNumber, int columnNumber)
private boolean isGameFull()
public void resetGame()
public int detectWin()
private int detectWinColumns()
private int detectWinRows()
private int detectWinDiagonals()

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

public class ConnectN {
private int gameState[][];
private int rows;
private int columns;
private int winningNumber;
private int lastRow, lastColumn;
private static Scanner scanner = new Scanner(System.in);

public ConnectN() {
    // initialize
    // initialize class variables to default parameters...
    // rows = 6
    //columns = 7
    //winning number = 4
    //initialize game state
    //initialize lastRow and lastColumn
    rows = 6;
    columns = 7;
    winningNumber = 4;
    gameState = new int[6][7];
    lastRow = -1;
    lastColumn = -1;
}

public ConnectN(int rows, int columns, int winningNumber) {
    // initialize
    // initialize class variables based on input parameters
    //initialize
    // game state
    //initialize lastRow and lastColumn
    this.rows = rows;
    this.columns = columns;
    this.winningNumber = winningNumber;
    lastRow = -1;
    lastColumn = -1;
    gameState = new int[rows][columns];
}

public void resetGame() {
    // set lastRow and lastColumn to -1
    lastRow = -1;
    lastColumn = -1;
    // set all positions to empty
    for (int i = 0; i < gameState.length; i++) {
      for (int j = 0; j < gameState[i].length; j++) {
       gameState[i][j] = 0;
      }
    }
}

public boolean insertChip(int playerNumber, int columnNumber) {
    boolean inserted = false;
    //try to insert
    //start at row 0 (the top row)
    //loop through the rows until a non-empty position is found
    //set game state position to the player number
    //insert = true //set last row and last column to the inserted position
    if (gameState[0][columnNumber-1] == 0)
    {
      for (int i = gameState.length -1; i >= 0; i--)
      {
       if (gameState[i][columnNumber-1]==0)
       {
          gameState[i][columnNumber-1] = playerNumber;
          inserted = true;
          lastRow = i;
          lastColumn = columnNumber-1;
          i = -1;
       }
      }
    }
    return inserted;
}


public void outputGameState() {
    // create a string builder
    // loop through all rows
    // loop through all columns
    // append gameState[row][column] to the output string
    // builder
    // output the game state string
    String result = "";
    for (int row = 0; row < gameState.length; row++) {
      for (int col = 0; col < gameState[row].length; col++) {
       result += String.valueOf(gameState[row][col]) + " ";
      }
      result += "\n";
    }
    System.out.print(result);
}

private boolean isGameFull() {
    // check if game board is full
    //loop through all columns
    // if the top position is empty then the game is not full
    for (int j = gameState.length - 1; j >= 0; j--) {
      if (gameState[0][j]==0) {
       return false;
      }
    }
    return true;
}

public int detectWin() {
    int winner = -1;
    // detect win rows
    // detect win columns
    // detect win diagonals
    winner = detectWinRows();
    if (winner != 0) {
      return winner;
    }
    winner = detectWinColumns();
    if (winner != 0) {
      return winner;
    }
    winner = detectWinDiagonals();
    if (winner != 0) {
      return winner;
    }
    return winner;
}

private int detectWinColumns() {
    int lastPlayer = gameState[lastRow][lastColumn];
    int count = 1;
    // return the player who won or 0 if no player has won int winColumns =
    // 0;
    // start at last position
    // loop down the column as far as needed //set winColumn if count of
    // number in a row is greater than winning
    // number
    // return winColumns;
    for (int i = lastRow+1; i < rows; i++) {
      if (gameState[i][lastColumn] == lastPlayer) {
       count++;
      }
    }
    if (count >= winningNumber) {
      return lastPlayer;
    } else {
      return 0;
    }
}

private int detectWinRows() {
    int lastPlayer = gameState[lastRow][lastColumn];
    int count = 1;
    // count of chips in a row boolean stopLeft = false;
    // stop searching to the left boolean stopRight = false;
    // stop searching to the right
    // left size
    // loop starting at lastColumn-1 until out of bounds or stopLeft =
    // false
    // if current position == player
    // increment count
    // else
    // stopLeft = true
    // right side (similar to left side)
    // if count >= winningNumber set winRows to the last player
    for (int i = lastColumn-1; i >= 0; i--) {
      if (gameState[lastRow][i] == lastPlayer) {
       count++;
      } else {
       i = -1;
      }
    }
    for (int i = lastColumn+1; i < columns; i++) {
      if (gameState[lastRow][i] == lastPlayer) {
       count++;
      } else {
       i = columns;
      }
    }
    if (count >= winningNumber) {
      return lastPlayer;
    } else {
      return 0;
    }
}

private int detectWinDiagonals() {
    int winDiagonals...

By purchasing this solution you'll be able to access the following files:
Solution.PNG and Solution.java.

$50.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats