
note - Mon, 19 Feb 2007 22:34:23 GMT
CS 211 Programming Assignment #1
Crazy Eights
Due: Sunday, Feb 25, 2007 by 10:30 PM
Initial Due Date: Sunday, Sept 18, 2005 by 10:30 PM
Background
For this project, you will be creating a program to play a card game known as "Crazy Eights." If you are not familiar with this game, here are the rules:- Initial Setup:The deck of cards is shuffled and a five card hand is dealt to each player. The rest of the cards are placed face down in the "Draw" pile. The top card of the draw pile is removed and placed face up. This becomes the first card of the "Discard" pile.
- How to Win: The object of the game is for a player to remove all the cards from his or her hand by placing them on the discard pile before the other player has gotten rid of his/her cards in the same way. The first player to get rid of all his/her cards wins the game.
- How to Play: The players alternate turns, and during his/her turn, each player must make one of the following moves:
- Discard a card from his/her hand that matches either the suit or the value of the top (face-up) card on the Discard pile
- Discard an Eight (8) of any suit from his/her hand to the discard pile. As the name of the game implies, the Eights are special "wild cards" that can always be discarded. In addition, the player is allowed to name the "Crazy Eight" to be of any suit, regardless of the actual suit of the Eight. Thereafter, for the next move, the opposing player must either play an Eight him/herself, or play a card of the named suit (Spades, Hearts, Diamonds or Clubs).
- Draw cards from the draw pile until s/he is able to perform either I or 2 above. (Note: a player need not be unable to perform I or 2 in order to draw cards from the draw pile. A player may draw cards as long as s/he chooses, before discarding a card according to I or 2 above. Of course,the more cards that s/he draws, the more cards s/he must discard before winning the game. Only one card may be discarded per turn.)
Project Description
This project requires that you complete the design of a program that plays Crazy Eights between two players: a living person (the user) and the computer. At the beginning of each game, the full deck of cards is shuffled, and five cards dealt to each player. The user's cards are displayed on the screen. The program also displays the number of cards the computer player currently holds, but does not display the computer's cards. The top card of the Discard pile is also visable, since this is the card that must be matched by the next player.The play begins by allowing the user to discard one of the cards from her/his hand, or to draw cards until a legal discard card is available. A player may draw cards from the draw pile even if there is a legal discard card in the hand, so basically, the user may draw cards as long as s/he likes. Once the user has discarded a card, he may not play again until the computer has taken its turn. Your program should not allow a card to be played that is not valid. If either player plays a "Crazy Eight", the suit that player names to be played next by the opponent, must be displayed.
If the draw pile ever runs out of cards, then the top card is removed from the discard pile, and the remainder of the discard pile is shuffled and then used for the new draw pile. The top card of the discard pile then becomes the one card remaining in the discard pile.
The game continues until either the human or computer player discards all his/her cards. The first player to have an empty hand wins! Therefore, if either player's hand ever becomes empty, and all discards are valid, then your program should declare that player to be the winner.
Computer's Strategy
For this game, you may use a very simple strategy for the computer's play. If the computer has a card in its hand that can be played, it will play from its hand. It should first try to discard a card that match the card value or the card suit of the top card of the discard pile. If more than one card matches either suit or value, you may choose which card to play any way you like. If no card has an exact match of suit or value, but the computer holds an 8 in its hand, then the computer should play the 8 and name the new suit to be the suit of which it currently holds the most cards. Ties for number of cards in a suit may be resolved in any manner you wish.If the computer cannot play any card in its hand, then it must draw cards from the draw pile one by one until it draws one that it can legally play. Note: If you wish, you may experiment to see if you can devise a better-working strategy than the above, though this is not required. Use the above strategy unless you have reason to believe another is better. If you do change the strategy, you should clearly document in your README file why and how your new strategy works better than the above.
Implementation Requirements
You should have already downloaded the jar file containing the image directory, Card.java and Deck.java. A nice alternative implementaion for a Deck can be found at http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html. This makes use of enumerated types and the new for-loop. However, this implementation doesn't provide a way for the discard pile to become the deck, a process this project requires.You will need to download the other files contained here. Look at game.GIF to get an idea of how the screen will look. CrazyEights.java contains the main() method and produces the GUI.
Before you can run the GUI, you will need to complete the other classes. You should complete
- Card.java according to instructions in Proj1-Part1
- Deck.java -- is given. You should compile and test.
- Player.html is the API for the class Player. It was created via
JavaDoc and should be viewed using your browser. In the Crazy Eights
application, the computer will be an instance of Player. You must create
this class according to the specifications given in the file and stated below. You may not add any
additional public members, but are free to add any additional private
data or methods you desire.
Since this project's focus in on using arrays, the Player's hand must be implemented as an expandable array of Cards. The "add" method therefore must call a private method to expand the array if adding a card would exceed the capacity. See page 228 of your text.
The "play" method that takes a card as a parameter, must return a card in the hand that matches either the suit or the value of the parameter, or an eight. If no such card exist, it must return null. The other "play" method must play a card having a suit matching the parameter; if none exist, it must return null. The "nameSuit" method must return the character representing one of the longest suits in the hand.
You must add a main() method or supply a separate tester for this class. It should test all methods. Your tester will be part of your submission and you will lose points if all methods have not been sufficiently tested.
- CrazyEights.java -- has main() method that produces the GUI and controls
the running or the program. You are required to make some modifications
and are free to make changes in the layout, but cannot add new
buttons, fields, except those specified.
As given, when the window opens, cards have been dealt and the top of the pile is visible. It is the user's turn. When it is the user's turn, the user will push one of the buttons on the top right -- "Play Card" or "Take Card". The former will play the currently selected card. Clicking on another card changes the selection. When the "Take Card" button is pushed, a card is taken from the deck and added to the hand.
"AddButtonListener" responds to "Take Card", resulting in another card going into the user's hand. You may need to scroll to see all the cards. The card taken from the deck will appear at the bottom. You need to modify the given codeso that you don't try to play a card from an empty deck. If the deck is empty, it must be replaced with the discard pile (discardDeck). [Note that the model of the hand is the DefaultListModel. Note that the user is not an instance of the class Player -- computer is the only instance of Player in the game. The view is the JList.]
"DeleteButtonListener" responds to "Play Card" and handles the playing of the card. The selected card will go to the top of the Discard pile. You need to modify the given code so that it only allows the selection of a valid card (match to top of pile). You can simply ignore an invalid choice or signal an error with a pop-up window or beep.
If the card chosen is a "Crazy Eight", the chosen suit in the form "Spades", "Hearts", "Diamonds" or "Clubs" should be typed into the "Named Suit" field by the user before it becomes the computer's turn.The computer's play button must be pressed to cause the computer to try to play before the user is allowed to take another turn. You will need to complete the code for the PlayButtonListenerwhich listens for the computer's "Play" button to be pressed. Remember computer is an instance of class Player. When the method ends the computer should have played a valid card, drawing as many cards as necessary. If a card has been played or both the deck and discard pile are empty, then it should become the user's turn. Note that you must call the methods in the Player class when implementing this listener.
Submission
You should create a README file in which you state what you did and did not complete. If there are only portions that work correctly, you should include code that the TA can run to test those methods.You can modify the existing jar file, by using
jar uf jar-file input-file(s)where you replace input-file(s) with a list of files to be added/replaced. See Jar Basics for details. If you prefer, you can recreate it.
Submit your jar file using the upload procedure.
From:
file:///C:/DOCUME~1/Tae/LOCALS~1/Temp/Project1.html


