Advertisement
Guest User

AHAHAHAH

a guest
Jan 23rd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. //Mo Tabet
  2. //AP Comp Sci
  3. //1/23/20
  4. // Card class represents a playing card
  5.  
  6. public class Card
  7. {
  8. private String face; // face of card ie..."Ace","Deuce", "King"..etc
  9. private String suit; // suit of card ie "Heart", "Diamond"
  10. private int num; //Number of the card
  11.  
  12. // two argument card constructor
  13. public Card( String cardFace, String cardSuit )
  14. {
  15. face = cardFace;
  16. suit = cardSuit;
  17. if(face=="Ace")
  18. num=14;
  19. if(face=="Deuce")
  20. num=2;
  21. if(face=="Three")
  22. num=3;
  23. if(face=="Four")
  24. num=4;
  25. if(face=="Five")
  26. num=5;
  27. if(face=="Six")
  28. num=6;
  29. if(face=="Seven")
  30. num=7;
  31. if(face=="Eight")
  32. num=8;
  33. if(face=="Nine")
  34. num=9;
  35. if(face=="Ten")
  36. num=10;
  37. if(face=="Jack")
  38. num=11;
  39. if(face=="Queen")
  40. num=12;
  41. if(face=="King")
  42. num=13;
  43. }
  44. public int getValue()
  45. {
  46. return num;
  47. }
  48. public String getSuit()
  49. {
  50. return suit;
  51. }
  52.  
  53. public String toString()
  54. {
  55. return face + " of " + suit;
  56. }
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. //Mo Tabet
  67. //AP Comp Sci
  68. //1/23/20
  69. import java.util.Random;
  70.  
  71. public class DeckOfCards
  72. {
  73. private Card deck[]; // array of Card objects
  74. private int currentCard; // index of next Card to be dealt
  75. private final int NUMBER_OF_CARDS = 52;
  76.  
  77. // Constructor fills the deck of Cards
  78. public DeckOfCards()
  79. {
  80. String faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
  81. "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
  82.  
  83. String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
  84.  
  85. deck = new Card[NUMBER_OF_CARDS]; // create our array of card objects
  86.  
  87. currentCard = 0; //set currentCard so firstCard dealt is deck[0]
  88.  
  89. // populate the deck with card objects
  90.  
  91. for (int count=0; count<deck.length; count++)
  92. {
  93. deck[count] = new Card( faces[count % 13], suits[count/13] );
  94. }
  95. }
  96. private static int findMinimum(Card []a, int first)
  97. {
  98. int minIndex=first;
  99. for(int i =first+1;i<a.length;i++)
  100. {
  101. if(a[i].getValue()<a[minIndex].getValue())
  102. {
  103. minIndex=i;
  104. }
  105. }
  106. return minIndex;
  107. }
  108. public static void organize(Card[]hand)
  109. {
  110. Card temp;
  111. for(int i=0;i<hand.length-1;i++)
  112. {
  113. int minIndex=findMinimum(hand,i);
  114. if(minIndex!=i)
  115. {
  116. //swap the cards
  117. temp=hand[i];
  118. hand[i]=hand[minIndex];
  119. hand[minIndex]=temp;
  120. }
  121. }
  122. }
  123. public void shuffle()
  124. {
  125. Random generator = new Random();
  126.  
  127. // for each Card, pick another random Card and swap them
  128. for ( int first=0; first < deck.length; first++)
  129. {
  130. // select a random number between 0 and 51
  131. int second = generator.nextInt(NUMBER_OF_CARDS);
  132.  
  133. // swap current Card with randomly selected Card
  134.  
  135. Card temp = deck[first];
  136. deck[first] = deck[second];
  137. deck[second] = temp;
  138. }
  139. currentCard = 0;
  140. }
  141.  
  142.  
  143. public Card dealCard()
  144. {
  145. if (currentCard < deck.length)
  146. {
  147. return deck[currentCard++];
  148. }
  149. else
  150. {
  151. return null;
  152. }
  153.  
  154.  
  155. }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. //Mo Tabet
  163. //AP Comp Sci
  164. //1/23/20
  165. public class Poker
  166. {
  167. public static void main(String args[])
  168. {
  169. System.out.println();
  170. int p1score=0;
  171. int rp1score=0;
  172. int p2score=0;
  173. //Pair = 100
  174. //2 Pair = 200
  175. //3 of a Kind = 300
  176. //Straight = 400
  177. //Flush = 500
  178. DeckOfCards deck=new DeckOfCards();
  179. System.out.println("*****************");
  180. System.out.println("WELCOME TO POKER");
  181. System.out.println("*****************");
  182. System.out.println();
  183.  
  184. deck.shuffle();
  185. Card[] p1=new Card[5];
  186. Card[] rp1=new Card[5];
  187. Card[] p2=new Card[5];
  188.  
  189. for(int i=0;i<5;i++)
  190. {
  191. p1[i]=deck.dealCard();
  192. }
  193. for(int i=0;i<5;i++)
  194. {
  195. p2[i]=deck.dealCard();
  196. }
  197. System.out.println("Player 1 Has Cards: ");
  198. deck.organize(p1);
  199. for(int i=0;i<5;i++)
  200. {
  201. System.out.println(" " + p1[i].toString());
  202. }
  203. rp1=p1;
  204. System.out.println();
  205.  
  206. System.out.println("Player 2 Has Cards: ");
  207. deck.organize(p2);
  208. for(int i=0;i<5;i++)
  209. {
  210. System.out.println(" " + p2[i].toString());
  211. }
  212. System.out.println();
  213.  
  214. for(int i=0;i<2;i++)
  215. {
  216. rp1score=p1score;
  217. p1score=0;
  218. //WINNING SITUATIONS
  219. int five=p1[4].getValue();
  220. int four=p1[3].getValue();
  221. int three=p1[2].getValue();
  222. int two=p1[1].getValue();
  223. int one=p1[0].getValue();
  224. //FLUSH
  225. if(p1[0].getSuit().equals(p1[1].getSuit())&&p1[1].getSuit().equals(p1[2].getSuit())&&p1[2].getSuit().equals(p1[3].getSuit())&&p1[3].getSuit().equals(p1[4].getSuit()))
  226. {
  227. p1score=p1score+500+p1[4].getValue();
  228. }
  229. //STRAIGHT
  230. else if((five--)==four&&(four--)==three&&(three--)==two&&(two--)==one)
  231. {
  232. p1score=p1score+400+p1[4].getValue();
  233. }
  234. //THREE OF A KIND
  235. else if((p1[0].getValue()==p1[1].getValue()&&p1[0].getValue()==p1[2].getValue()))
  236. {
  237. p1score=p1score+300+p1[0].getValue();
  238. }
  239. else if((p1[1].getValue()==p1[2].getValue()&&p1[1].getValue()==p1[3].getValue()))
  240. {
  241. p1score=p1score+300+p1[1].getValue();
  242. }
  243. else if((p1[2].getValue()==p1[3].getValue()&&p1[2].getValue()==p1[4].getValue()))
  244. {
  245. p1score=p1score+300+p1[2].getValue();
  246. }
  247. //TWO PAIR
  248. else if(p1[0].getValue()==p1[1].getValue()&&p1[2].getValue()==p1[3].getValue())
  249. {
  250. p1score=p1score+200+p1[2].getValue();
  251. }
  252. else if(p1[0].getValue()==p1[1].getValue()&&p1[3].getValue()==p1[4].getValue())
  253. {
  254. p1score=p1score+200+p1[3].getValue();
  255. }
  256. else if(p1[1].getValue()==p1[2].getValue()&&p1[3].getValue()==p1[4].getValue())
  257. {
  258. p1score=p1score+200+p1[3].getValue();
  259. }
  260. //PAIR
  261. else if(p1[0].getValue()==p1[1].getValue())
  262. {
  263. p1score=p1score+100+p1[1].getValue();
  264. }
  265. else if(p1[1].getValue()==p1[2].getValue())
  266. {
  267. p1score=p1score+100+p1[2].getValue();
  268. }
  269. else if(p1[2].getValue()==p1[3].getValue())
  270. {
  271. p1score=p1score+100+p1[3].getValue();
  272. }
  273. else if(p1[3].getValue()==p1[4].getValue())
  274. {
  275. p1score=p1score+100+p1[4].getValue();
  276. }
  277. //HIGH CARD
  278. else
  279. {
  280. p1score=p1score+p1[4].getValue();
  281. }
  282. p1=p2;
  283. p2score=p1score;
  284. }
  285. p1score=rp1score;
  286. if(p1score>p2score)
  287. {
  288. System.out.println("Player 1 wins");
  289.  
  290. if(p1score>=500)
  291. {
  292. System.out.println("With a Flush");
  293. }
  294. else if(p1score>=400&&p1score<500)
  295. {
  296. System.out.println("With a Straight");
  297. }
  298. else if(p1score>=300&&p1score<400)
  299. {
  300. System.out.println("With a Three of a Kind");
  301. }
  302. else if(p1score>=200&&p1score<300)
  303. {
  304. System.out.println("With a Two Pair");
  305. }
  306. else if(p1score>=100&&p1score<200)
  307. {
  308. System.out.println("With a Pair");
  309. }
  310. else if(p1score>=0&&p1score<100)
  311. {
  312. System.out.println("With a High Card");
  313. }
  314. }
  315. if(p2score>p1score)
  316. {
  317. System.out.println("Player 2 wins");
  318. if(p2score>=500)
  319. {
  320. System.out.println("With a Flush");
  321. }
  322. else if(p2score>=400&&p1score<500)
  323. {
  324. System.out.println("With a Straight");
  325. }
  326. else if(p2score>=300&&p1score<400)
  327. {
  328. System.out.println("With a Three of a Kind");
  329. }
  330. else if(p2score>=200&&p1score<300)
  331. {
  332. System.out.println("With a Two Pair");
  333. }
  334. else if(p2score>=100&&p1score<200)
  335. {
  336. System.out.println("With a Pair");
  337. }
  338. else if(p2score>=0&&p1score<100)
  339. {
  340. System.out.println("With a High Card");
  341. }
  342. }
  343. if(p1score==p2score)
  344. {
  345. System.out.println("Tis a tie and no one wins!");
  346. }
  347. }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement