CS 124, Spring 2013
Lab 14: Poker Solitaire with Two-dimensional Array
This final lab is a short exercise in using a two-dimensional array. You will complete the implementation of a simple "Poker Solitaire" game by adding an array to hold the data required in the game.
You do not need to start a new project for this lab. You can add the file for today's lab to your lab13 project. To begin the lab, you should add the file /classes/cs124/PokerSolitaire.java to the src folder in your lab13 project. (Note that this program requires the same support files as the Video Poker program from Lab 13.)
Lab 13 and Lab 14 are due together at noon, this Saturday, May 4.
Poker Solitaire
The version of Poker Solitaire in this lab is a very simple game in which the user lays out cards in a five-by-five grid. The user is dealt one card at a time and must place that card on the grid. The user places the card by clicking on an empty position in the grid. Once the card has been placed, it cannot be moved or replaced. The goal of the game is to try to get high-value poker hands in each row, in each column, and along the two diagonals. There is no notion of "winning" the game, though it could be scored. A complete version of the game can be found in the runnable jar file /classes/cs124/PokerSolitaireComplete.jar. (A version with scoring -- which you are not required to do for this lab -- is in /classes/cs124/PokerSolitaireWithScore.jar. In this version, poker hands are scored using the table of Video Poker payouts from the previous lab.)
You will work in the file PokerSolitaire.java. As it stands, this program already shows a grid of empty places where cards can be placed. It shows the "Next Card", which is the card that is available to be placed on the grid. It includes the code for figuring out which grid position the user has clicked and for starting a new game once the grid is full. What it does not have is an array for holding the grid of cards or any code for using that array. Your job is to add the array and the programming to make it work.
Start by adding private PokerCard[][] cards as an instance variable in the PokerSolitaire class. The array itself can be created in the startGame() method (it is easiest to create a new array for each game). It should be a 5-by-5 array of PokerCards. Remember that when you create an array of objects, the array is initially filled with null values. In this program, a null value in the array is a signal that there is no card in that position.
Next, add code to the paintComponent() method to draw the cards. For help on how to do this and where to draw the cards, look at how the paintComponent() method already draws empty boxes where the card should go. Remember that some elements of the array of cards can be null. Of course, you should only draw the non-null elements of the array.
Finally, you need to say what happens when the user clicks a position in the grid. The code for this should be added to the clicked() method. (This method is called by mousePressed() when the user clicks a spot on the grid. The mousePressed() method figures out which grid position was clicked, and it calls the clicked() method to respond to that click; you do not have to do any work on mousePressed().) The clicked() method needs to test whether the grid position is empty -- if it is not empty, the click should be ignored since the user is not allowed to replace a card that has already been played; if the grid position is empty, then you have to put nextCard in that position, get another card from the deck to be nextCard, and increment the variable cardsPlayed, which keeps track of the number of grid positions that have been filled.
With those changes, you should have a working program. You do not need to add any comments or change any of the comments that are already there. You should finish this exercise in lab and have extra time to work on your Video Poker program. (If you have extra time and would like to add scoring to the Poker Solitaire game, ask about how to do it. It's not particularly difficult, but it is sort of tedious.)