CPSC 124, Fall 2001 Lab 13: A Two-Dimensional Array

For the final lab of the course, you will be working with a two-dimensional array. The array will be used in an applet that implements a simple solitaire card game.

You should finish this lab during class today and give me a printout before you leave. You will get 10 points for finishing the assignment (less if you don't finish it).

You will find the files you need for this lab in the directory /home/cs124/lab14. You should copy this directory into your account, as usual.

Poker Solitaire

In this simple game, the user is dealt one card at a time and the user places the card in any empty position in a five-by-five grid. After twenty-five cards have been played, the grid is full and the game ends. The object of the game is to obtain the best possible poker hands in each of the five rows, five columns, and two diagonals of the grid. The applet, however, makes no attempt to compute a score for the game. You will start with the following almost-working version of the applet:

The source code for this version of the applet is in the file PokerSolitaire.java.

This version works, provided that the user never tries to place a card on a position that is already occupied and provided that the applet does not have to be redrawn because it has been covered and then uncovered. The problem is that the applet does not keep track of what cards are on the board. When the user clicks on a space in the grid, it has no way of knowing whether that space is already occupied. If it is, the previous card just gets overwritten. When the applet needs to be redrawn, the applet has no way of knowing what cards it should draw in the grid. The solution is to use a five-by-five two-dimensional array of Cards to hold the cards that have been played in the grid. An empty space is represented by a null in this array. A non-null value is a card that the user has played. Here is a version of the applet that uses an array in this way. Try clicking on a grid position after a card has already been played in that position:

Your assignment is to add a two-dimensional array to PokerSolitaire.java and use it to keep track of the cards that have been played. You need to make the following modifications to the source code:

1. Declare a new instance variable of type Card[][] to represent the grid of cards.
2. Modify the startGame() routine so that it creates a new 5-by-5 array of Cards and assigns it to that instance variable. (The array starts out filled with null's, which is the correct state for the beginning of the game, so you just need to add one line to startGame() to create the array.)
3. Change the playCard() method so that it uses your array. In this method, you know that the user has clicked on one of the grid positions. Use your array to check whether this position is already occupied. If so, you should not allow the user to play there. If the position is not occupied, you should put nextCard into the array (and do all the other stuff that is already done in the method). Note that in my completed version of the game, the message always changes in the playCard() method. You can do the same if you want.
4. Make the small change in the paint() routine that will make it draw the cards from your array, instead of simply drawing a null card in each row and column of the grid. You just have to modify a part of one line.

You should finish this exercise during lab. When you have finished, give me a printout of your applet, and let me try out your applet on your computer.

David Eck, 6 December 2001