[an error occurred while processing this directive]

Partner Work 7 - 2D Arrays - Connect Four

[an error occurred while processing this directive]

Directions

  1. Start by importing ConnectFour2013.zip into Eclipse.
  2. Run the code. You will see a Connect 4 board. Here is some information on game play.
    • Click on the blue box to hide the message.
    • Click the green triangle to start the game. Click the pause button to pause it.
    • When it's your turn, click in any column to play a checker there.
    • When it's C. Quenchell's turn (a computer player) the move will happen quickly. A triangle at the bottom of the game board will help you see where the computer made the last play. If you want to slow down or speed up the computer player, position your mouse in the light gray area at the bottom of the window.
    • You'll notice that the game keeps going even after someone has won the game. That's because you haven't solved the first problem yet...
  3. Complete the following problems
    1. (24 points) Implement the hasWinner method in the GameBoard.java file (more details below). When implemented correctly, the game will stop when one of the players has won (by placing 4 checkers in a horizontal, vertical, or diagonal line).
    2. (6 points) Create a new computer player. When implemented correctly, this player will play automatically (more details below).

hasWinner Method Implementation

The GameBoard.java file models the Connect 4 Game Board. It has some key data members.

The hasWinner method should analyze the board to check whether there are four checkers of the same color in a row (i.e. 4 RED or 4 BLACK). hasWinner does not need to determine which color it is, just simply whether there is a winner. Further, the board itself already exists... you should not be creating a new board nor should you be changing any board values.

A couple of methods in this file will help you analyze the board. Although our initial game board has 6 rows and 7 columns, this project allows for boards of any size, so you should not assume that there will always be 6 rows and 7 columns.

As an example of the methods and data members described above, this code would determine the value of the checker in the lower-right corner of the board and print it in the Console window.

int nRows = getRowAmt();
int nCols = getColAmt();
int lorhc = board[nRows-1][nCols-1];

if (lorhc == OPEN) {
  System.out.println("Lower-Right is empty");
}
else if (lorhc == BLACK) {
  System.out.println("Lower-Right has a black checker");
}
else if (lorhc == RED) {
  System.out.println("Lower-Right has a red checker");
}

Your previous work in 2D arrays should prove quite helpful in completing this part of the assignment.

Since the C. Quenchell player is so bad, you should be able to pretty easily exploit it to test out your code by putting wins in various spots around the board. Pay particular attention to wins that use the first or last columns or rows. Also make sure to try both types of diagonals.

A New Computer Player

The types of players that currently can play in the Connect 4 tournament are in the following files. Take a moment to look at each one.

Here are a few things to notice about the players.

You are to create a new computer player called RevSequentialPlayer by following these directions:

  1. Single-click (do not double-click) on SequentialPlayer.java in the Project Explorer Window
  2. Use keyboard shortcuts Ctrl-c and Ctrl-v to copy and paste the file
  3. Use the name RevSequentialPlayer for the copy
  4. You should now see RevSequentialPlayer.java in the Project Explorer Window. Double-click it to open it.
  5. More directions are below. Follow them.

The code should look like this:

package connectfour2013;

public class RevSequentialPlayer extends Player {

  public RevSequentialPlayer() {
    myName = "C. Quenchell";
  }

  public int ai(GameBoard gb) {
    int spot = 0;
    while(gb.canPlay(spot) == false) {
      spot++;
    }
    return spot;
  }
}

Now follow these remaining directions.

  1. Change the name of the player to Rev. C. Quenchell
  2. Change the code in the ai method so that the player plays from the right-hand side of the board instead of from the left-hand side of the board.
  3. Open the ConnectFour2013.java file and locate the setupTournament method.
  4. Update the code there so it looks like the code seen below. Run the code and test whether the name appears properly and the player plays from the right-hand side of the board.
players.add(new GUIPlayer());
// players.add(new SequentialPlayer());
players.add(new RevSequentialPlayer());

Resources

Do not use any resources other than the ones listed below. In particular, do not search the Web for code samples. The point of these exercises is to create code on your own, not to find an answer that has already been created.

Grading

Recall that partner work assignments are worth 30 points each (breakdown outlined above). Refer to the syllabus for more course grading information.

Submission

Read submission directions carefully!

Each pair will submit 1 file by taking one action: sending one total email message. Copy and paste only the following pieces of code into the body of an email message. Ensure to cc the partner.

[an error occurred while processing this directive]