Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.67 KB | None | 0 0
  1. Computer Science 182 Data Structures and Program Design
  2. Programming Project #3 A Linked List Video Poker Card Game (15 points)
  3.  
  4.  
  5. One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them.
  6.  
  7. Your mission, is to start with this code and build a Video card game. Obviously the program you write will not just display all the cards on the screen, it will not reveal the cards until dealt.
  8.  
  9.  
  10. The cards you see on the screen will be part of a Linked List. When you deal a hand of cards, you must create your own Linked List of cards and move the dealt card(s) from the deck's linked list to the hands linked list.
  11.  
  12.  
  13. Make sure you pay attention to the following guidelines for this project:
  14.  
  15. Design First: 3 points of this project will be based solely on your design, do it before you start coding this project. Sketch on paper what you want the screen to look like. Draw it at the start of the game (empty card hands), after initial deal and during the game. Draw your linked lists. Draw pictures of what the linked lists will look like. Draw the lists at the start of the game (empty card hand), after initial deal and during the game. Finally write some pseudo code for the deal, the transfer of cards from the deck to the player/computer hand.
  16. Use at least 3 linked lists in your design. For Video Poker it would be a deck, players hand, discard list. Show ALL 3 lists on the screen.
  17. Design upgrades to the Card class for use in scoring. The card class must have member variable(s) that allow you to determine the rank/suit of the card and determine poker hands (pairs, 3 of a kind, flush, straight etc.).
  18. Use the linked lists when displaying cards. When you show cards in a linked list use the following pseudo code to do it:
  19. current card = list.first
  20.  
  21. while (current card is NOT null)
  22. display current card on the screen
  23. current card = current card.next
  24.  
  25. Here are some hints that make this project easier, implement it step by step (or button by button):
  26.  
  27. Start with the 'New Deck' button. Most of it has been done for you, just need to display the deck across the top row of the screen, rather then over the entire screen. I would display the cards closer together so you can see at least half the card and tell what it is. Don't worry if the end of the deck goes off the right side of the screen, when cards are dealt the deck gets smaller and the cards at the end come in to view. At the start show the cards face up!
  28. The 'Shuffle' button is pretty much done. At the start don't shuffle the cards! It is much easier to debug the linked lists, knowing exactly which cards will be coming off the deck and moving into the hand(s). When the program is working, then start shuffling the cards.
  29. The 'Deal Poker' button. Write the code to have the deal button create a new card list, remove 5 cards from the deck, insert them into the new card list. Have the paintComponent display the new list in the middle row below the deck. In other words, don't start with a full version of poker, write a simple program that creates an empty hand, deals 5 cards from the deck to the hand and displays those 5 cards. Space the cards out, do not overlap them, the 5 cards dealt to the player should clearly be shown as a SEPARATE linked list (hand) from the deck. Each time the 'Deal Poker' button is pressed start a new hand and take 5 MORE cards from the deck. After the first 'Deal Poker' there will be 5 cards in the hand, and 47 left in the deck, after the second 'Deal Poker' there will be 5 new cards in the hand, and 42 left in the deck, and so on. While the user can choose to start a 'New Deck' any time they want, they DO NOT have to until the deck has less than 10 cards left in it. By design 'Deal Poker' can be pressed many times in a row. Display a message in the text box or on the screen to press 'New Deck' and STOP dealing cards when there are less than 10 cards in the deck.
  30. Partial credit warning! Do the steps in order! Turn in the most advanced step you can get working for partial credit. Don't mess with additional steps until the basic 'Deal Poker' is working.
  31. Now turn to the buttons on the bottom of the screen. Draw Poker requires discards or throwing away cards in the hand. You will need 5 discard buttons (add more buttons), and the initial function is quite simple. Toggle the card face up/face down. A card left face up will be kept in the hand and a card face down will be thrown away (discarded). Players often change their mind so the buttons MUST flip the cards back and forth. A player can discard all or none of the cards in the hand.
  32. When the player makes up their mind the 'Draw' button will remove the discards (if any) from the players hand and add them to a third linked list for the discards. New cards MUST be taken from the deck to replace the discards with cards from the deck, restoring the hand to the final 5 cards. It is not a typical feature of Video Poker but show the discards in a third row below the hand just like the deck is shown above the hand. Display the discards closer together so you can see at least half the card and tell what it is. I want you to 'see' the card objects move from the deck linked list, to the hand linked list to the discard linked list.
  33. It is HELPFUL to start the game in cheat mode (show all deck cards face up) when testing the game. It is so much easier to decide what to discard if you can see the deck. And easier to test specific scoring hands (see below) when you know what is coming off the deck. Now you can implement the 'Cheat' button, it simply toggles the deck linked lists face up or face down. If those cards are currently face up press 'Cheat' and they turn face down. Press 'Cheat' again and they toggle face up. The 'Cheat' button has no affect on the cards in the hand linked list or the discard list.
  34. Scoring after 'Draw' is pressed! Do not even think about doing this until the basic game play above is working. Google 'Poker Hands' a low hand contains a pair ( two 4's), moving up there is two pairs (two Jacks, two 6's), three of a kind, etc. A very good hand named a straight flush is easy to test if the cards are NOT shuffled. Report in a clear and obvious message the best poker hand that can be made from the final 5 cards in the hand after the 'Draw' button is pressed.
  35. A player can simply press 'Deal Poker' to start a new game. The final 5 cards in the previous hand should move to the discard list when a new deal occurs. Pressing 'New Deck' is an option, but NOT required until the deck has less than 10 cards. The discard link list should be cleared when 'New Deck' is pressed. It would be nice to start with the buttons on the bottom disabled. Enable the buttons on the bottom AFTER 'Deal Poker' is pressed and disable them again AFTER 'Draw' is pressed. In other words, don't let them use the discard and draw buttons unless a hand is active.
  36. Optional, do NOT attempt unless full game is working. Make it pretty, A green background or a Poker Table graphic below the cards...
  37.  
  38.  
  39. You will need to download and unzip the images zip file into the same directory as your project in order to load card images and display them.
  40.  
  41.  
  42. Here is the code you should start with.
  43.  
  44. /***************************************************************
  45. Project Number 3 - Comp Sci 182 - Data Structures (w/ Swing)
  46. Start Code - Build your program starting with this code
  47. Card Game
  48. Copyright 2005-2016 Christopher C. Ferguson
  49. This code may only be used with the permission of Christopher C. Ferguson
  50. ***************************************************************/
  51. package project3; // must match your project
  52. import javax.swing.*;
  53. import java.awt.*;
  54. import java.awt.event.*;
  55. import java.io.*;
  56.  
  57. public class Project3 extends JFrame implements ActionListener {
  58.  
  59. private static int winxpos=0,winypos=0; // place window here
  60.  
  61.  
  62. private JButton dealButton, shuffleButton,exitButton, newButton, cheatButton;
  63. private JButton discard1Button, drawButton;
  64. private JTextField inputBox;
  65. private CardList theDeck = null;
  66. private JPanel northPanel;
  67. private JPanel southPanel;
  68. private MyPanel centerPanel;
  69. private static JFrame myFrame = null;
  70.  
  71. //////////// MAIN ////////////////////////
  72. public static void main(String[] args) {
  73. Project3 tpo = new Project3();
  74. }
  75.  
  76. //////////// CONSTRUCTOR /////////////////////
  77. public Project3 ()
  78. {
  79. myFrame = this; // need a static variable reference to a JFrame object
  80. northPanel = new JPanel();
  81. northPanel.setBackground(Color.white);
  82. inputBox = new JTextField("5 Card Draw Video Poker",20);
  83. northPanel.add(inputBox);
  84. dealButton = new JButton("Deal Poker");
  85. northPanel.add(dealButton);
  86. dealButton.addActionListener(this);
  87. shuffleButton = new JButton("Shuffle");
  88. northPanel.add(shuffleButton);
  89. shuffleButton.addActionListener(this);
  90. newButton = new JButton("New Deck");
  91. northPanel.add(newButton);
  92. newButton.addActionListener(this);
  93. cheatButton = new JButton("Cheat");
  94. northPanel.add(cheatButton);
  95. cheatButton.addActionListener(this);
  96. exitButton = new JButton("Exit");
  97. northPanel.add(exitButton);
  98. exitButton.addActionListener(this);
  99. getContentPane().add("North",northPanel);
  100.  
  101. southPanel = new JPanel();
  102. southPanel.setBackground(Color.white);
  103. discard1Button = new JButton("Discard 1");
  104. southPanel.add(discard1Button);
  105. discard1Button.addActionListener(this);
  106. southPanel.add(new JLabel(" More buttons here?"));
  107. drawButton = new JButton("Draw");
  108. southPanel.add(drawButton);
  109. drawButton.addActionListener(this);
  110. getContentPane().add("South",southPanel);
  111.  
  112. centerPanel = new MyPanel();
  113. getContentPane().add("Center",centerPanel);
  114.  
  115. theDeck = new CardList(52);
  116. Card.setBackImage(52);
  117.  
  118. setSize(800,800);
  119. setLocation(winxpos,winypos);
  120.  
  121. // Exit when the window is closed. i.e. when top right X box pressed
  122. addWindowListener(new WindowAdapter() {
  123. public void windowClosing(WindowEvent e) {
  124. System.exit(0);
  125. }
  126. });
  127.  
  128. setVisible(true);
  129. }
  130.  
  131.  
  132. //////////// BUTTON CLICKS ///////////////////////////
  133. public void actionPerformed(ActionEvent e) {
  134.  
  135. if (e.getSource()== exitButton) {
  136. dispose(); System.exit(0);
  137. }
  138. if (e.getSource()== dealButton) {
  139. String inputmsg = inputBox.getText(); // note you CAN use this box for input
  140. inputBox.setText("DEAL 5 Cards from deck");
  141. repaint();
  142. }
  143. if (e.getSource()== shuffleButton) {
  144. String inputmsg = inputBox.getText();
  145. inputBox.setText("Shuffle");
  146. theDeck.shuffle();
  147. repaint();
  148. }
  149. if (e.getSource()== cheatButton) {
  150. inputBox.setText("Turn Cheat ON/OFF (show/hide)");
  151. Card current = theDeck.getFirstCard();
  152. current.setShowHide();
  153. repaint();
  154. }
  155. if (e.getSource()== newButton) {
  156. inputBox.setText("New Deck");
  157. theDeck = new CardList(52);
  158. repaint();
  159. }
  160. }
  161.  
  162.  
  163. // This routine will load an image into memory
  164. //
  165. public static Image load_picture(String fname)
  166. {
  167. // Create a MediaTracker to inform us when the image has
  168. // been completely loaded.
  169. Image image;
  170. MediaTracker tracker = new MediaTracker(myFrame);
  171.  
  172.  
  173. // getImage() returns immediately. The image is not
  174. // actually loaded until it is first used. We use a
  175. // MediaTracker to make sure the image is loaded
  176. // before we try to display it.
  177.  
  178. image = myFrame.getToolkit().getImage(fname);
  179.  
  180. // Add the image to the MediaTracker so that we can wait
  181. // for it.
  182. tracker.addImage(image, 0);
  183. try { tracker.waitForID(0); }
  184. catch ( InterruptedException e) { System.err.println(e); }
  185.  
  186. if (tracker.isErrorID(0)) { image=null;}
  187. return image;
  188. }
  189. // -------------- end of load_picture ---------------------------
  190.  
  191. class MyPanel extends JPanel {
  192.  
  193. //////////// PAINT ////////////////////////////////
  194. public void paintComponent (Graphics g) {
  195. //
  196. int xpos = 25, ypos = 25;
  197. if (theDeck == null) return;
  198. g.drawString("This is ALL 52 cards in the Deck!", xpos, ypos-10);
  199. Card current = theDeck.getFirstCard();
  200. while (current!=null) {
  201. Image tempimage = current.getCardImage();
  202. g.drawImage(tempimage, xpos, ypos, this);
  203. // note: tempimage member variable must be set BEFORE paint is called
  204. xpos += 80;
  205. if (xpos > 700) {
  206. xpos = 25; ypos += 105;
  207. }
  208. current = current.getNextCard();
  209. } //while
  210. }
  211. }
  212.  
  213. } // End Of class Project3
  214.  
  215. /*****************************************************************
  216. Class Link, the base class for a link list of playing cards
  217. May be placed in a file named Link.java
  218.  
  219. ******************************************************************/
  220. class Link {
  221. protected Link next;
  222.  
  223. public Link getNext() { return next; }
  224. public void setNext(Link newnext) { next = newnext; }
  225.  
  226. } // end class Link
  227.  
  228. /*****************************************************************
  229. Class Card, the derived class each card is one object of type Card
  230. May be placed in a file named Card.java
  231. ******************************************************************/
  232.  
  233. class Card extends Link {
  234. private static Image backimage = null; //why static?
  235. private Image cardimage; //why not static?
  236. private boolean showhide = true;
  237.  
  238. public Card (int cardnum) {
  239. cardimage = Project3.load_picture("images/gbCard" + cardnum + ".gif");
  240. // code ASSUMES there is an images sub-dir in your project folder
  241. if (cardimage == null) {
  242. System.out.println("Error - image failed to load: images/gbCard" + cardnum + ".gif");
  243. System.exit(-1);
  244. }
  245.  
  246. }
  247. public Card getNextCard() {
  248. return (Card)next;
  249. }
  250. public Image getCardImage() {
  251. if (showhide)
  252. return cardimage;
  253. else
  254. return backimage;
  255. }
  256.  
  257. public void setShowHide() {
  258. showhide=!showhide;
  259. }
  260. public static void setBackImage(int cardnum) { //why static?
  261. backimage = Project3.load_picture("images/gbCard" + cardnum + ".gif");
  262. }
  263. public static Image getBackImage() { //why static?
  264. return backimage;
  265. }
  266. } //end class Card
  267.  
  268. /*****************************************************************
  269. Class CardList, A Linked list of playing cards
  270. May be placed in a file named CardList.java
  271.  
  272. Note : This class can be used to create a 'hand' of cards
  273. Just Create another CardList object, and delete cards from
  274. 'theDeck' and insert the into the new CardList object
  275.  
  276. ******************************************************************/
  277.  
  278. class CardList {
  279. private Card firstcard = null;
  280. private int numcards=0;
  281.  
  282. public CardList(int num) {
  283. numcards = num; //set numcards in the deck
  284. for (int i = 0; i < num; i++) { // load the cards
  285. Card temp = new Card(i);
  286. if (firstcard != null) {
  287. temp.setNext(firstcard);
  288. }
  289. firstcard = temp;
  290. }
  291. }
  292.  
  293. public Card getFirstCard() {
  294. return firstcard;
  295. }
  296.  
  297. public Card deleteCard(int cardnum) {
  298. Card target, targetprevious;
  299.  
  300. if (cardnum > numcards)
  301. return null; // not enough cards to delete that one
  302. else
  303. numcards--;
  304.  
  305. target = firstcard;
  306. targetprevious = null;
  307. while (cardnum-- > 0) {
  308. targetprevious = target;
  309. target = target.getNextCard();
  310. if (target == null) return null; // error, card not found
  311. }
  312. if (targetprevious != null)
  313. targetprevious.setNext(target.getNextCard());
  314. else
  315. firstcard = target.getNextCard();
  316. return target;
  317. }
  318.  
  319. public void insertCard(Card target) {
  320. numcards++;
  321. if (firstcard != null)
  322. target.setNext(firstcard);
  323. else
  324. target.setNext(null);
  325. firstcard = target;
  326. }
  327.  
  328. public void shuffle() {
  329. for ( int i = 0; i < 300; i++) {
  330. int rand = (int)(Math.random() * 100) % numcards;
  331. Card temp = deleteCard(rand);
  332. if (temp != null) insertCard(temp);
  333. } // end for loop
  334. } // end shuffle
  335.  
  336.  
  337. } // end class CardList
  338.  
  339.  
  340. Here is the image of a card back, it is gbCard52.gif.
  341.  
  342. Here are 52 card images, they are gbCard0.gif to gbCard51.gif
  343. You can download them individual images with a right click on the image (using the zip file is better):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement