Goals for this assignment are to learn about:
Carefully read all of Chapter 6 before starting this assignment.
You will need to enter the following code from the text: List.java, ListIterator.java, ListNode.java, LinkedList.java, and Predecessor.java. I suggest that you create a new project and copy over your "lists" package from the last lab. Add the linked list classes to it (it should already contain List.java). As you enter the code, look through it carefully so you understand what it is doing. Draw pictures if necessary to understand how things are linked together. Test the code by creating a linked list and applying the various functions.
Write a reverse() method in your LinkedList.java class that reverses the order of the items in your list. No ListNodes should be created or destroyed in the process. Everything should be done by modifying references.
Test that it works. This exercise is not to turn in but you should know how to do it! You will need your linked list classes for the next part of the lab.
In this part of the lab you are to begin the implementation of the following game: Two players (you and the computer) are each dealt five cards. The remaining cards are placed face down in the center of the table. At each turn, a player picks two cards from the deck and discards two cards from their hand, trying to get five cards of the same suit in numeric sequence. The game continues with the players drawing and discarding until a player finally gets five the sequence. If the deck is used up and there is no winner, then the game is a tie (alternatively, you may take the discard pile and reshuffle it to continue with the game. This repeats until someone wins.)
Getting Started: Create a new project and copy your "lists" package into it. Also copy over your "card" package containing the Card and Deck classes from the previous lab.
Updating the Card class: As the cards are dealt to a player, the player must be able to insert the new card into the existing hand so that the cards are always sorted first by suit (shdc) and then by rank (A23456789TJQK). To do this, you need to define an ordering on the cards. That is, given any two cards, it should make sense to ask the question: Is (card1 < card2)? To do this, your Card class needs to implement the Comparable<Card> interface. Look up this interface in the Javadoc to see what method(s) you need to add to your Card class.
insertSorted(Card c)
:
When a card is dealt during the game, you will insert the
card into the hand. It must be placed into the linked list at proper location. The insertSorted method does this
by "walking down" the list. Assume the list you are inserting into is always sorted. Given that, figure out where
you need to insert the new card.
discard(Card c)
:
It is easy to remove a card from the hand if you know the card; you just need
to use the existing remove method in the LinkedList class. This method is used by the player after
the player has decided which card to discard. Note, two cards are discarded so this just gets called twice.
hasSequence()
:
This method checks to see if a five card sequence is in the hand (thus, this hand
wins the game).
toString()
:
You might need to add a new toString() method that overrides the one in the LinkedList class.
Set up some type of interface to show that your classes and their methods all work properly. For example, create a frame with 1) a textarea , 2) button to deal a card, 3) a comboBox to choose a card to discard when a second button is pressed. The program begins by creating, shuffling and printing the deck.
Complete the implementation of the game. Among other things, you will need to do the following:
By midnight on Thursday October 18, zip together the entire Card Game project and email the code as an attachment to gorr@willamette.edu. Make sure a working jar file is included. Place CS241 Lab 6 in the subject line.
By Tuesday October 23, demonstrate the code to the instructor.