Prog 3: Pop Out
Write a program to play the game Pop Out, itself a variant of the game Connect 4. It is similar to tic-tac-toe, only you have to get 4 in a row. Furthermore, imagine that the board is "standing up," so that as you play each piece, it falls down as far as possible in the column in which it was played. The game is played on a board with 6 rows and 7 columns. If a player has the bottom-most piece in a particular column, then instead of playing a new piece the player can select a column and remove their bottom piece, resulting in all the other pieces in that column sliding down one position.
You may work with a partner on this program. If you choose to do this you must:
Read the description of Pair Programming given in the syllabus.
Email the TA at least one week before the deadline indicating you will be working with a partner.
Submit a single program with both your names on it.
Running your program will look like the following, except you should have your own name and TA information. The boldfaced text is something that you type in.
Author: Dale Reed Assignment: #3, Pop Out TA: Edmund Hillary, Sat 1:00-1:05 Welcome to the game of Pop Out. Choose a column in which to move and try to get four pieces in a row, either horizontally, diagonally, or vertically. At any point you may enter 'x' to exit the program. Your moves will be marked by an 'X', and the computer's moves by an 'O'. If a player has a piece in the bottom row for a column, entering a letter 'A'..'G' removes that bottom piece, sliding the remaining pieces in that column down by one for a player. --------------- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | --------------- 1 2 3 4 5 6 7 <- place piece A B C D E F G <- remove piece 1. Enter column for X to play -> 4 --------------- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | --------------- 1 2 3 4 5 6 7 <- place piece A B C D E F G <- remove piece 2. Enter column for O to play -> 4 --------------- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O| | | | | | | |X| | | | --------------- 1 2 3 4 5 6 7 <- place piece A B C D E F G <- remove piece 3. Enter column for X to play -> 3 --------------- | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |O| | | | | | |X|X| | | | --------------- 1 2 3 4 5 6 7 <- place piece A B C D E F G <- remove piece 4. Enter column for O to play -> 5 . . . --------------- | | | | | | | | | | | | | | | | | | | |O| | | | | | | |X| | | | |X|X|X|O|O| | | |O|X|X|X|O|O|O| --------------- 1 2 3 4 5 6 7 <- place piece A B C D E F G <- remove piece 15. Enter column for X to play -> A Sorry, you can only remove your own piece. Please retry. 15. Enter column for X to play -> D --------------- | | | | | | | | | | | | | | | | | | | | | | | | | | | |O| | | | |X|X|X|X|O| | | |O|X|X|O|O|O|O| --------------- Four in a row, X wins!
Exiting program...
You need to know the following concepts in order to write this program:
Everything from previous programs; reading input as a string; methods (including return values and parameters)
Details & Hints:
To run a version of Connect 4 online as an applet, browse over to http://www.bodo.com/Applets/Connect4/
Note that your program will different than the one shown in the above applet. The above program does not allow removing a piece at the bottom of a stack, like our program will allow. Our program does not play against the computer, but rather facilitates two humans playing against each other.
You must represent the board using 42 char variables called p0 all the way up to p41 as follows:
You may not use arrays (or simulated arrays using Strings), but you should create a method setValue() which is used to store a char ('X','O', or ' ') into a particular location (0..41), where both the char value and the location are parameters. You should also create another method getValue() which returns the char value at a particular location.
When an invalid move is entered, give an appropriate message and allow retrying the move. Moves should be numbered. After each move give an appropriate message when a player wins.
If removing a piece results in a win for both players, then the win goes to the player whose turn it is.
You should write this program in stages as follows, where the point values add up to the 55 points for correct execution:
(0 points) Display instructions and identifying information. Declare the 42 variables for the board and use them in a method to display the board. Prompt for user input and read it in as a String, changing it to all upper-case. Extract the first char of the user input. Prompt for user input
(15 points) Use the user input to make moves on the board, sliding each piece down to the lowest available row in each column. As the move number goes from odd to even, the piece to be placed should go from 'X' to 'O'. You should make use of the setValue() and getValue() methods to manipulate the variables p0..p41, otherwise you will end up writing 42 if-else statements throughout your program every time you want to store a value into a piece or retrieve a value from a piece.
(25 points) Implement "popping out" the lowest piece in a column. Ensure this move can only be done when the player to move has the bottom-most piece in a column. If a column is full then a blank should be brought into the top-most square which becomes empty.
(15 points) After each move is made check to see if there are 4 in a row. If so display a message for the winning player and exit the program.
Turn in your program into the Program 3 assignment in Blackboard.
Extra Credit (up to 10 points):
Be sure to keep a backup copy of the human vs. human version of your program before starting to work on the extra credit version.
(4 points) The extra credit version must be represented graphically, using the Canvas, Square and Circle classes given here.
(6 points) For the extra credit have the human player play against a "smart" computer opponent. To get the computer to play "smart" you must use the idea of potentially winning "coalitions" (or groups of 4 squares) to which each square belongs. There are horizontal coalitions, vertical coalitions, and coalitions on both left and right diagonals. A square is part of potentially winning coalition from the computer's point of view if there are no human's pieces (X's) in that coalition. A coalition can have 0, 1, 2, 3, or 4 pieces in it. A coalition of 4 means that a player has won the game (has "connected 4"). A coalition of 3 means that 3 out of the 4 squares in the coalition belong to O and the remaining square is blank. This is defined similarly for coalitions of 2 and 1. Note that any coalition with a combination of both X's and O's will never lead to a win.
Coalitions are assigned point values based on the number of pieces in that coalition. Coalitions of 4 are worth 10000 points, coalitions of 3 are worth 27 points, coalitions of 2 are worth 7 points, and coalitions of 1 are worth 2 points each. The square selected for the computer's move is the square yielding the highest sum of coalition point values. In case of a tie, choose the lowest-numbered column (e.g. column 3 is numbered lower than column 5).
In addition, when the next move does not yield a win for the computer, the computer must block the human's move if the human has 3 in a row and will win on the next move