Posted By

ehrenb on 06/14/11


Tagged

game card


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

TShehorn


Skeleton Code for a Card Game


 / Published in: Java
 

Work in progress...

Some "Skeleton code" for a card game. Deck class (arraylist) that consists of 52 "card" objects. A shuffle function is included, which will randomly swap each card in the deck. The basic functionality is done; IE - the playGame() function will deal out 5 cards to the player (human) and computer, show the player his/her hand. I am leaning towards creating a poker game function first; this will parse the user and computers hand, giving a value to each (ie: pair < royal flush), depending on how good their hand is. The code is broken into 3 files, each file is seperated by "----". A future implementation will introduce a GUI and some basic 2D images.

  1. package cardgames;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4. /**
  5.  *
  6.  * @author Branden
  7.  */
  8. public class deck {
  9. //private int size;
  10. ArrayList<card> cardDeck;// = new ArrayList<card>();
  11.  
  12. public deck() {
  13. // this.size = size;
  14. cardDeck = new ArrayList<card>();
  15. //this.cardDeck = cardDeck;
  16. card ace1 = new card("club","A",1);
  17. card ace2 = new card("diamond","A",1);
  18. card ace3 = new card("heart","A",1);
  19. card ace4 = new card("spade","A",1);
  20.  
  21. card king1 = new card("club","K",10);//face values are all 10, except Ace
  22. card king2 = new card("diamond","K",10);
  23. card king3 = new card("heart","K",10);
  24. card king4 = new card("spade","K",10);
  25.  
  26. card queen1 = new card("club","Q",10);
  27. card queen2 = new card("diamond","Q",10);
  28. card queen3 = new card("heart","Q",10);
  29. card queen4 = new card("spade","Q",10);
  30.  
  31. card jack1 = new card("diamond", "J", 10);
  32. card jack2 = new card("club","J",10);
  33. card jack3 = new card("spade","J",10);
  34. card jack4 = new card("heart","J",10);
  35.  
  36. //
  37. // card two1 = new card("club",'2',2);
  38. // card two2 = new card("diamond",'2',2);
  39. //
  40. // card seven1 = new card("heart", '7', 7);
  41. // card seven2 = new card("diamond", '7', 7);
  42. // card seven3 = new card("spade", '7', 7);
  43. // card seven4 = new card("club", '7', 7);
  44. card tempCardC;
  45. card tempCardD;
  46. card tempCardS;
  47. card tempCardH;
  48.  
  49.  
  50. //this is must faster than manually entering these cards
  51. for (int i=2;i<=10;i++)//start at two, because Ace takes care of 1 value
  52. {
  53.  
  54. tempCardC = new card("club",""+i,i);//type case i into str
  55. tempCardD = new card("diamond",""+i,i);//type case i into astr
  56. tempCardS = new card("spade",""+i,i);//type case i into str
  57. tempCardH = new card("club",""+i,i);//type case i into str
  58.  
  59. //System.out.println(tempCardC.getCardVal());
  60. cardDeck.add(tempCardC);
  61. cardDeck.add(tempCardD);
  62. cardDeck.add(tempCardS);
  63. cardDeck.add(tempCardH);
  64. //add all the different number cards
  65. }
  66.  
  67.  
  68. cardDeck.add(ace1);
  69. cardDeck.add(ace2);
  70. cardDeck.add(ace3);
  71. cardDeck.add(ace4);
  72.  
  73. cardDeck.add(king1);
  74. cardDeck.add(king2);
  75. cardDeck.add(king3);
  76. cardDeck.add(king4);
  77.  
  78. cardDeck.add(queen1);
  79. cardDeck.add(queen2);
  80. cardDeck.add(queen3);
  81. cardDeck.add(queen4);
  82.  
  83.  
  84. cardDeck.add(jack1);
  85. cardDeck.add(jack2);
  86. cardDeck.add(jack3);
  87. cardDeck.add(jack4);
  88.  
  89. //cardDeck.add(seven1);
  90. //cardDeck.add(seven2);
  91. //cardDeck.add(seven3);
  92. //cardDeck.add(seven4);
  93.  
  94. }
  95.  
  96.  
  97. public void pokerGame(){
  98. //
  99. // Random drawCard = new Random();
  100.  
  101. // int randomNumber = drawCard.nextInt(cardDeck.size() - 1) + 1;
  102.  
  103.  
  104.  
  105.  
  106. // //code for PLAYER
  107. //deck is pre-shuffled*
  108. ArrayList<card> playerHand = new ArrayList<card>();//player's hand
  109. for(int i=1;i<=5;i++)//give the player 5 cards from the top of the shuffled deck
  110. {
  111. playerHand.add(cardDeck.get(i));//add the card to the hand
  112. cardDeck.remove(i);//remove the card from the deck so that it cannot be picked again (for the computer's sake
  113. }
  114. System.out.println(playerHand.toString());
  115. //display the user's hand
  116. for (int i=0;i<5;i++){
  117. System.out.println("Card: "+playerHand.get(i).getCardVal()+" * "+playerHand.get(i).getSuit()+"\n");//player hand print
  118. }
  119. //code for COMPUTER
  120. ArrayList<card> compHand = new ArrayList<card>();
  121. for(int i=1;i<=5;i++)//give the comp 5 cards from the top of the shuffled deck
  122. {
  123. compHand.add(cardDeck.get(i));//add the card to the hand
  124. cardDeck.remove(i);//remove the card from the deck so that it cannot be picked again (for the computer's sake
  125. }
  126. }
  127.  
  128. // public boolean checkPair(){
  129. // for(int i=)
  130. //
  131. // return false;
  132. // }
  133.  
  134.  
  135. @Override
  136. public String toString() {
  137. return "deck{" + "size=" + "cardDeck=" + cardDeck + '}';
  138. }
  139.  
  140. public void shuffleDeck()//do this so that we can retain the entire deck instead of removing used cards.
  141. {
  142.  
  143.  
  144. System.out.println(cardDeck.size());
  145. for (int i=0;i<cardDeck.size();i++)
  146. {
  147. Random rand = new Random();
  148. int randomNumber = rand.nextInt(cardDeck.size() - 1) + 1;
  149. System.out.println("Rand num: "+randomNumber);
  150. //So : go through each item of the deck, in incrementing order, and replace the i'th position with a random position in the deck
  151. card tempStore = cardDeck.get(i);
  152. card tempStore2 = cardDeck.get(randomNumber);
  153.  
  154. cardDeck.remove(randomNumber);//remove the item at the random number generated position, and replace it with the item in the i position
  155. cardDeck.add(randomNumber, tempStore);
  156.  
  157. cardDeck.remove(i);//remove the item at cardDeck i position, and replace it with the item retrieved from the random number generator
  158. cardDeck.add(i, tempStore2);
  159.  
  160. }
  161.  
  162. }
  163.  
  164. // @Override
  165. // public String toString() {
  166. // return "deck{" + "size=" + size + '}';
  167. // }
  168.  
  169.  
  170. }
  171.  
  172.  
  173. -------------------------------------------------------
  174.  
  175. /*
  176.  * To change this template, choose Tools | Templates
  177.  * and open the template in the editor.
  178.  */
  179.  
  180. package cardgames;
  181.  
  182. /**
  183.  *
  184.  * @author Branden
  185.  */
  186.  
  187. //a card will have a suit..a value ( ie: K,Q,J,A,1,2,3,4,etc), and a value (1,2,3....11)
  188. public class card {
  189. private String suit;
  190. private String cardVal;
  191. private int worth;
  192.  
  193. public card(String suit, String cardVal, int worth) {
  194. this.suit = suit;
  195. this.cardVal = cardVal;
  196. this.worth = worth;
  197. }
  198.  
  199. public String getCardVal() {
  200. return cardVal;
  201. }
  202.  
  203. public void setCardVal(String cardVal) {
  204. this.cardVal = cardVal;
  205. }
  206.  
  207. public String getSuit() {
  208. return suit;
  209. }
  210.  
  211. public void setSuit(String suit) {
  212. this.suit = suit;
  213. }
  214.  
  215. public int getWorth() {
  216. return worth;
  217. }
  218.  
  219. public void setWorth(int worth) {
  220. this.worth = worth;
  221. }
  222.  
  223. @Override
  224. public String toString() {
  225. return "card{" + "suit=" + suit + "cardVal=" + cardVal + "worth=" + worth + '}';
  226. }
  227.  
  228.  
  229.  
  230.  
  231. }
  232.  
  233. ------------------------------
  234.  
  235. /*
  236.  * To change this template, choose Tools | Templates
  237.  * and open the template in the editor.
  238.  */
  239.  
  240. package cardgames;
  241. import java.util.ArrayList;
  242. import java.util.Scanner;
  243. /**
  244.  *
  245.  * @author Branden
  246.  */
  247. public class Main {
  248.  
  249. /**
  250.   * @param args the command line arguments
  251.   */
  252. public static void main(String[] args) {
  253. // TODO code application logic here
  254. //*** need seperate func that will create a traditional 52 card deck...shuffling works fine
  255. //for shuffling, we should stick w/ doing a shuffle method bc > drawing a random card will not reorder the deck, a user could simply call on that position again to manipulate it(the card)
  256. //once I decide on an interaction, I need to figure out whether the card should be removed from the deck after pulled, or should it be replaced (and reshuffled)
  257. // card ace1 = new card("club",'A',1);
  258. // card ace2 = new card("diamond",'A',1);
  259. // card ace3 = new card("heart",'A',1);
  260. // card ace4 = new card("spade",'A',1);
  261. // card jack1 = new card("diamond", 'J', 10);
  262. // card jack2 = new card("club",'J',10);
  263. // card jack3 = new card("spade",'J',10);
  264. // card jack4 = new card("heart",'J',10);
  265. // card seven1 = new card("heart", '7', 7);
  266. // card seven2 = new card("diamond", '7', 7);
  267. // card seven3 = new card("spade", '7', 7);
  268. // card seven4 = new card("club", '7', 7);
  269.  
  270. //ArrayList<card> cards = new ArrayList<card>();
  271. //
  272. //
  273. //cards.add(ace1);
  274. //cards.add(ace2);
  275. //cards.add(ace3);
  276. //cards.add(ace4);
  277. //cards.add(jack1);
  278. //cards.add(jack2);
  279. //cards.add(jack3);
  280. //cards.add(jack4);
  281. //cards.add(seven1);
  282. //cards.add(seven2);
  283. //cards.add(seven3);
  284. //cards.add(seven4);
  285.  
  286. deck playDeck = new deck();//new implemention...the deck is created by simply calling deck()..it is preset to 52 cards. the arraylist is initialized and filled within the constructor
  287. //for a card game,there shouldnt be any need for a dynamic deck size.
  288.  
  289.  
  290. // System.out.println(playDeck.toString());
  291.  
  292.  
  293. playDeck.shuffleDeck();
  294. // System.out.println(playDeck.toString());
  295.  
  296.  
  297. //initial game setup
  298.  
  299. boolean wantToPlay=true;
  300.  
  301.  
  302. playDeck.pokerGame();
  303.  
  304. //do you want to continue playing?
  305. while (wantToPlay==true)
  306. {
  307. Scanner inputWantToPlay = new Scanner(System.in);
  308. System.out.println("Do you want to play poker again? \n");
  309. String decision2 = inputWantToPlay.next();
  310. if (decision2.equals("n"))
  311. {
  312. wantToPlay = false;
  313. System.exit(1);
  314. }
  315. else if (decision2.equals("y"))
  316. {
  317. playDeck = new deck();
  318. playDeck.shuffleDeck();
  319. wantToPlay = true;
  320. playDeck.pokerGame();
  321. }
  322. else
  323. System.out.println("Error with input");
  324. }
  325. //*while* user wants to play (wantToPlay == true)
  326. //shuffle deck
  327. //game logic method(s)here (include prompts and stuff)
  328. //prompt user if they want to play again..wantToPlay = true/false <<re-enter or exit the loop
  329. //if prompts are not made..then the game will go crazy
  330.  
  331. }
  332.  
  333. //**note for next testing ... fix the worth of face cards..its not right
  334.  
  335.  
  336.  
  337. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: TShehorn on February 25, 2013

Any suggestions on how I could rework this so that it deals to ai and player from their own individual decks?

Posted By: on March 1, 2013

Any suggestions on how I could rework this so that it deals to ai and player from their own individual decks?

You need to login to post a comment.