Partner Work 7 - 2D Arrays - Connect Four
[an error occurred while processing this directive]Directions
- Start by importing ConnectFour2013.zip into Eclipse.
- 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...
- Complete the following problems
- (24 points) Implement the
hasWinnermethod 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). - (6 points) Create a new computer player. When implemented correctly, this player will play automatically (more details below).
- (24 points) Implement the
hasWinner Method Implementation
The GameBoard.java file models the Connect 4 Game Board. It has some key data members.
-
OPEN,RED, andBLACK: these are integers used to represent individual game board squares (which can be empty, have a red checker, or have a black checker). -
board: this is a 2D array of integers. Each value in the array isOPENorREDorBLACK. It models the game board. For example, if the array valueboard[0][0]has a value ofREDthen there is a red checker in the upper-left corner of the board.
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.
-
getRowAmt()returns an integer indicating the number of (horizontal) rows in the board. -
getColAmt()returns an integer indicating the number of (vertical) columns in the board.
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.
- GUIPlayer.java: models a human player using the mouse to click where to drop a checker
- SequentialPlayer.java: a computer player that just plays in the lowest (left-most) available column
- BadDropPlayer.java: a computer player that intentionally tries to drop a checker in a full column
- ExceptionPlayer.java: a computer player that intentionally tries to drop a checker in a non-existent board area
Here are a few things to notice about the players.
- They all have the following elements in common
- They all start with
public class XPlayer extends Player(with X replaced with their unique names) - They all have a method named
public XPlayer()(with X replaced with their unique names) - They all have a method named
public int ai(GameBoard gb). This is the heart of the computer players because it returns in which column to next drop a checker. - SequentialPlayer has a call
to
gb.canPlay(spot), which returns a boolean indicating whether a player is currently allowed to drop a checker into columnspot. This is a nice way to figure out whether a column is full before dropping a checker there (the computer cannot see the board, so the computer must use thegbobject to figure out what's on the board). - GUIPlayer has an extra data element to help it operate. Any player may introduce it's own data to help it play the game.
- They all start with
You are to create a new computer player
called RevSequentialPlayer by following these directions:
- Single-click (do not double-click) on SequentialPlayer.java in the Project Explorer Window
- Use keyboard shortcuts Ctrl-c and Ctrl-v to copy and paste the file
- Use the name
RevSequentialPlayerfor the copy - You should now see RevSequentialPlayer.java in the Project Explorer Window. Double-click it to open it.
- 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.
- Change the name of the player to Rev. C. Quenchell
- Change the code in the
aimethod so that the player plays from the right-hand side of the board instead of from the left-hand side of the board. - Open the ConnectFour2013.java file and locate
the
setupTournamentmethod. - 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.
- Brainstorm with your partner. Although this is a computer class, use pencil and paper!
- Use class notes and sample projects from this week about decision code
- Use the general resources on the CSC 130 Main Page
- Use the CSC 130 tutors
- Visit the instuctor during office hours or by appointment
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.
- The
hasWinnermethod (header and implementation) - All code in the RevSequentialPlayer.java file