Individual Work 7 - Connect Four
[an error occurred while processing this directive]Description
On the last day of class we will have a Connect Four tournament. You will write code to implement your own computer player. I will assemble all of the computer players into a single tournament and we'll watch them play off against each other. The tournament winner may select a question to skip on the final examination.
Directions
If you and your partner from PW-7 created only one version of your code, and you don't have it, have your partner give you a copy.
Open the code you created in PW-7. We will now do something very similar to what we did in the second problem of PW-7.
- 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
XPlayerfor the copy but replaceXwith your first name (for example, if the instructor was completing this assignment, the copy would be namedHutchingsPlayer) - Open the new file
- Update the code to use your name instead of C. Quenchell.
- Also, re-locate the
setupTournamentmethod in the ConnectFour2013.java file and add your player to the tournament similar to the way you did forRevSequentialPlayerin PW-7.
It's a good idea to run the code now to make sure your new player is operating in the tournament (your player has your name but currently plays just like SequentialPlayer does). At this point only, you may seek guidance from your PW-7 partner, a classmate, or a tutor, to get your player are up and running, but do not seek any additional guidance from these sources on any other aspects of the assignment.
Now that your player is playing, you should now change the code in
your player's ai method to make your player as good as
possible. You won't change the header of the method, just the
implementation. Also, just like GUIPlayer does, you may add data
elements and methods to your player if it aids game play.
Helpful Notes
The ai method has an input parameter
called gb that is a GameBoard object. It
models the game board at the time of your turn. The ai
method must return a column in which to play. The gb
object has several methods that will help you create a smarter
player.
-
gb.hasWinner()returns a boolean letting you know if the board has a winner. You implemented this in PW-7. -
gb.getBoard2D()returns a 2D array of integers representing the game board. The value of each element in the array isGameBoard.EMPTY,GameBoard.BLACK, orGameBoard.RED -
gb.getRowAmt()returns an integer indicating how many (horizontal) rows are on the board. -
gb.getColAmt()returns an integer indicating how many (vertical) columns are on the board. -
gb.getCopy()returns a GameBoard object that is a complete copy of the original board. This could be helpful if you want to "try out" a move by making a copy first. -
gb.play(int col, int mark)allows you to simulate dropping amark-colored checker into columncol. As an example,gb.play(0, myColor)would simulate you dropping one of your checkers into the leftmost column on the board.gb.play(1, GameBoard.BLACK)would simulate dropping a black checker in the next column over. The method returns false if you are not allowed to drop a checker in the indicated column. -
gb.canPlay(int col)returns a boolean indicating if playing in columncolis allowed. A play might not be allowed because a column is full or because the column number does not exist on the game board. For examplegb.canPlay(0)indicates whether the leftmost column is open for play (true) or full (false). -
gb.prevColumn()returns an integer indicating the last column that was played. This essentially tells you where the opponent most recently dropped a checker.
Here's a quick example. Suppose you are thinking about making a play in column 1. This code would copy the board, drop a checker in column 1 of the copy, and indicate if you won.
GameBoard myCopy = gb.getCopy(); // copy the board
boolean canPlay = myCopy.play(1, myColor);
boolean hasWinner = myCopy.hasWinner();
if (canPlay && hasWinner) {
// I won!
}
else {
// I did not win yet
}
Here's another helpful example. You'd like to know what color you are...
if (myColor == GameBoard.RED) {
System.out.println("I'm Red!");
}
else if (myColor == GameBoard.BLACK) {
System.out.println("I'm Black!");
}
Testing Your Player
If you followed the directions in the first part of this assignment, you should have a 2-player tournament of your player against you (as the GUI Player). This is a good way to see how your player reacts to "human" play.
You may also play your player against other students' players in the class, but you must take protections so that you do not expose the code to other students. If you can't figure out how to do this, or you don't trust the other student, don't use this approach.
Grading Guidelines
Grading in this assignment works in a tiered approach... as you meet stronger requirements, you qualify to accumulate more points.
- Level 0 (10 points max): player has appropriate name (both class name and name displayed on screen) but otherwise plays in the sequential default.
- Level 1 (20 points max): player always chooses a random column. For full credit, the column must be a legal move (don't play in a full column and don't play in a column that isn't there).
- Level 2 (30 points max): player wins when possible, then defaults to a random move.
- Level 3 (40 points max): player wins when possible, blocks when possible, then defaults to a random move.
- Level 4 (50 points max): player wins when possible, blocks when possible, then does anything smarter than a random move.
Tournament Notes
As mentioned at the outset of the assignment, everyone's player will play a tournament on the last day of class. The tournament winner gets to skip a question on the final exam. To try to account for all possibilities that might arise to call into question the tournament result, here are the tournament rules and guidelines.
- Any player's code that causes a Java exception or returns an invalid column automatically loses the current game.
- The first player in any game must win outright. The second player can win by either winning outright or dropping the last possible checker on the board (see rule 1).
- Players must make moves in a reasonable time frame. Code that has an infinite loop or runs excessively long will result in disqualification of the player and a reset of the entire tournament (i.e. all players eliminated through normal game play are re-entered and we start over without the offending player).
- Any external causes of tournament disruption (power failure, network failure, etc.) void the tournament. If possible, the tournament will be reset with any players that were not disqualified via rule 3.
- In other situtaions not accounted for by these rules, the instructor will attempt to make the fairest possible ruling. Instructor decisions are final.
Resources
The resources below are the only resources you should use in completing this assignment, except as explicitly noted in the directions. In particular, do not use the CSC 130 tutors and do not search any electronic resources such as the Web for sample code. You may visit the tutors for general guidance, but they have been instructed not to help with the specific assignment questions.
- Use pencil and paper to sketch ideas
- Use your class notes and the class Web site
- Use the general resources on the CSC 130 Main Page
- Use the textbook
- Feel free to search the Web for general resources on playing Connect 4, but do not look for nor copy existing code.
- Visit the instuctor during office hours or by appointment
Grading
Recall that individual work assignments are worth 50 points each. Refer to the syllabus for more course grading information.
Submission
Submit by taking one action: upload a java file. Details are below.
- Upload your player's java file to Moodle no later than 11:30am on the final day of class. People uploading later than this time run this risk of not being entered in the tournament.