Guest User

Untitled

a guest
Mar 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class TexasHoldEm {
  6.  
  7. public static void main(String[] args) throws Exception {
  8. // variables
  9. Deck holdemDeck = new Deck();
  10. int numPlayers = 0;
  11. int cardCounter = 0;
  12. int burnCounter = 0;
  13. int boardCounter = 0;
  14. Board board = new Board();
  15.  
  16. // initializations
  17. numPlayers = getNumberOfPlayers();
  18. Player[] player = new Player[numPlayers];
  19.  
  20. /* 3 shuffles just like in real life. */
  21. for(int i=0;i<3;i++){
  22. holdemDeck.shuffle();
  23. }
  24.  
  25. // Cut Deck
  26. holdemDeck.cutDeck();
  27.  
  28. // Initialize players
  29. for (int i=0;i<numPlayers;i++){
  30. player[i] = new Player();
  31. }
  32.  
  33. // Main processing
  34. // Deal hole cards to players
  35. for (int i=0;i<2;i++){
  36. for (int j=0;j<numPlayers;j++){
  37. player[j].setCard(holdemDeck.getCard(cardCounter++), i);
  38. }
  39. }
  40.  
  41. // Start dealing board
  42.  
  43. // Burn one card before flop
  44. board.setBurnCard(holdemDeck.getCard(cardCounter++), burnCounter++);
  45.  
  46. // deal flop
  47. for (int i=0; i<3;i++){
  48. board.setBoardCard(holdemDeck.getCard(cardCounter++), boardCounter++);
  49. }
  50.  
  51. // Burn one card before turn
  52. board.setBurnCard(holdemDeck.getCard(cardCounter++), burnCounter++);
  53.  
  54. // deal turn
  55. board.setBoardCard(holdemDeck.getCard(cardCounter++), boardCounter++);
  56.  
  57. // Burn one card before river
  58. board.setBurnCard(holdemDeck.getCard(cardCounter++), burnCounter++);
  59.  
  60. // deal river
  61. board.setBoardCard(holdemDeck.getCard(cardCounter++), boardCounter++);
  62.  
  63. //------------------------
  64. // end dealing board
  65. //------------------------
  66.  
  67. System.out.println("The hand is complete...n");
  68.  
  69. // print deck
  70. holdemDeck.printDeck();
  71.  
  72. //print board
  73. board.printBoard();
  74.  
  75. // print player cards
  76. System.out.println("The player cards are the following:n");
  77. for (int i=0;i<numPlayers;i++){
  78. player[i].printPlayerCards(i);
  79. }
  80.  
  81. // print burn cards
  82. board.printBurnCards();
  83.  
  84. //------------------------
  85. // Begin hand comparison
  86. //------------------------
  87. for (int i=0;i<numPlayers;i++){
  88. HandEval handToEval = new HandEval();
  89.  
  90. // populate with player cards
  91. for (int j=0;j<player[i].holeCardsSize();j++){
  92. handToEval.addCard(player[i].getCard(j),j);
  93. }
  94.  
  95. //populate with board cards
  96. for (int j=player[i].holeCardsSize();j<(player[i].holeCardsSize()+board.boardSize());j++){
  97. handToEval.addCard(board.getBoardCard(j-player[i].holeCardsSize()),j);
  98. }
  99.  
  100. System.out.println("Player " + (i+1) + " hand value: " + handToEval.evaluateHand());
  101. }
  102. }
  103.  
  104. protected static int getNumberOfPlayers() throws Exception{
  105. int intPlayers = 0;
  106. String userInput = "";
  107.  
  108. // Get number of players from user.
  109. System.out.println("Enter number of players (1-9):");
  110. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  111.  
  112. try {
  113. userInput = br.readLine();
  114. } catch (IOException ioe) {
  115. System.out.println("Error: IO error trying to read input!");
  116. System.exit(1);
  117. }
  118.  
  119. // convert user input to an integer
  120. try {
  121. intPlayers = Integer.parseInt(userInput);
  122. } catch (NumberFormatException nfe) {
  123. System.out.println("Error: Input provided is not a valid Integer!");
  124. System.exit(1);
  125. }
  126.  
  127. if ((intPlayers<1) || (intPlayers>9)){
  128. throw new Exception("Error: Number of players must be an integer between 1 and 9");
  129. }
  130.  
  131. return intPlayers;
  132. }
  133. }
  134.  
  135. public class Player {
  136. private Card[] holeCards = new Card[2];
  137.  
  138. //constructor
  139. public Player(){
  140. }
  141.  
  142. public Player(Card card1, Card card2){
  143. holeCards[0] = card1;
  144. holeCards[1] = card2;
  145. }
  146.  
  147. //methods
  148. protected void setCard(Card card, int cardNum){
  149. holeCards[cardNum] = card;
  150. }
  151.  
  152. protected Card getCard(int cardNum){
  153. return holeCards[cardNum];
  154. }
  155.  
  156. protected int holeCardsSize(){
  157. return holeCards.length;
  158. }
  159.  
  160. protected void printPlayerCards(int playerNumber){
  161. System.out.println("Player " + (playerNumber+1) + " hole cards:");
  162. for (int i=0;i<2;i++){
  163. System.out.println(holeCards[i].printCard());
  164. }
  165. System.out.println("n");
  166. }
  167. }
  168.  
  169. import java.util.Arrays;
  170.  
  171. public class HandEval {
  172. private Card[] availableCards = new Card[7];
  173.  
  174. private final static short ONE = 1;
  175. private final static short TWO = 2;
  176. private final static short THREE = 3;
  177. private final static short FOUR = 4;
  178.  
  179. // Constructor
  180. public HandEval(){
  181. }
  182.  
  183. //methods
  184. protected void addCard(Card card, int i){
  185. availableCards[i] = card;
  186. }
  187.  
  188. protected Card getCard(int i){
  189. return availableCards[i];
  190. }
  191.  
  192. protected int numCards(){
  193. return availableCards.length;
  194. }
  195.  
  196. protected void sortByRank(){
  197. Arrays.sort(availableCards, new rankComparator());
  198. }
  199.  
  200. protected void sortBySuit(){
  201. Arrays.sort(availableCards, new suitComparator());
  202. }
  203.  
  204. protected void sortBySuitThenRank(){
  205. Arrays.sort(availableCards, new suitComparator());
  206. Arrays.sort(availableCards, new rankComparator());
  207. }
  208.  
  209. protected void sortByRankThenSuit(){
  210. Arrays.sort(availableCards, new rankComparator());
  211. Arrays.sort(availableCards, new suitComparator());
  212. }
  213.  
  214. protected String evaluateHand(){
  215. String handResult = new String();
  216. short[] rankCounter = new short[13];
  217. short[] suitCounter = new short[4];
  218.  
  219. // initializations
  220. for (int i=0;i<rankCounter.length;i++){
  221. rankCounter[i] =0;
  222. }
  223.  
  224. for (int i=4;i<suitCounter.length;i++){
  225. suitCounter[i] = 0;
  226. }
  227.  
  228. // Loop through sorted cards and total ranks
  229. for(int i=0; i<availableCards.length;i++){
  230. rankCounter[ availableCards[i].getRank() ]++;
  231. suitCounter[ availableCards[i].getSuit() ]++;
  232. }
  233.  
  234. //sort cards for evaluation
  235. this.sortByRankThenSuit();
  236.  
  237. // hands are already sorted by rank and suit for royal and straight flush checks.
  238. // check for royal flush
  239. handResult = evaluateRoyal(rankCounter, suitCounter);
  240.  
  241. // check for straight flush
  242. if (handResult == null || handResult.length() == 0){
  243. handResult = evaluateStraightFlush(rankCounter, suitCounter);
  244. }
  245.  
  246. // check for four of a kind
  247. if (handResult == null || handResult.length() == 0){
  248. handResult = evaluateFourOfAKind(rankCounter);
  249. }
  250.  
  251. // check for full house
  252. if (handResult == null || handResult.length() == 0){
  253. handResult = evaluateFullHouse(rankCounter);
  254. }
  255.  
  256. // check for flush
  257. if (handResult == null || handResult.length() == 0){
  258. handResult = evaluateFlush(rankCounter, suitCounter);
  259. }
  260.  
  261. // check for straight
  262. if (handResult == null || handResult.length() == 0){
  263. // re-sort by rank, up to this point we had sorted by rank and suit
  264. // but a straight is suit independent.
  265. this.sortByRank();
  266. handResult = evaluateStraight(rankCounter);
  267. }
  268.  
  269. // check for three of a kind
  270. if (handResult == null || handResult.length() == 0){
  271. handResult = evaluateThreeOfAKind(rankCounter);
  272. }
  273.  
  274. // check for two pair
  275. if (handResult == null || handResult.length() == 0){
  276. handResult = evaluateTwoPair(rankCounter);
  277. }
  278.  
  279. // check for one pair
  280. if (handResult == null || handResult.length() == 0){
  281. handResult = evaluateOnePair(rankCounter);
  282. }
  283.  
  284. // check for highCard
  285. if (handResult == null || handResult.length() == 0){
  286. handResult = evaluateHighCard(rankCounter);
  287. }
  288.  
  289.  
  290. return handResult;
  291. }
  292.  
  293. private String evaluateRoyal(short[] rankCounter, short[] suitCounter){
  294. String result = "";
  295.  
  296. // Check for Royal Flush (10 - Ace of the same suit).
  297. // check if there are 5 of one suit, if not royal is impossible
  298. if ((rankCounter[9] >= 1 && /* 10 */
  299. rankCounter[10] >= 1 && /* Jack */
  300. rankCounter[11] >= 1 && /* Queen */
  301. rankCounter[12] >= 1 && /* King */
  302. rankCounter[0] >= 1) /* Ace */
  303. && (suitCounter[0] > 4 || suitCounter[1] > 4 ||
  304. suitCounter[2] > 4 || suitCounter[3] > 4)){
  305.  
  306. // min. requirements for a royal flush have been met,
  307. // now loop through records for an ace and check subsequent cards.
  308. // Loop through the aces first since they are the first card to
  309. // appear in the sorted array of 7 cards.
  310. royalSearch:
  311. for (int i=0;i<3;i++){
  312. // Check if first card is the ace.
  313. // Ace must be in position 0, 1 or 2
  314. if (availableCards[i].getRank() == 0){
  315. // because the ace could be the first card in the array
  316. // but the remaining 4 cards could start at position 1,
  317. // 2 or 3 loop through checking each possibility.
  318. for (int j=1;j<4-i;j++){
  319. if ((availableCards[i+j].getRank() == 9 &&
  320. availableCards[i+j+1].getRank() == 10 &&
  321. availableCards[i+j+2].getRank() == 11 &&
  322. availableCards[i+j+3].getRank() == 12)
  323. &&
  324. (availableCards[i].getSuit() == availableCards[i+j].getSuit() &&
  325. availableCards[i].getSuit() == availableCards[i+j+1].getSuit() &&
  326. availableCards[i].getSuit() == availableCards[i+j+2].getSuit() &&
  327. availableCards[i].getSuit() == availableCards[i+j+3].getSuit())){
  328. // Found royal flush, break and return.
  329. result = "Royal Flush!! Suit: " + Card.suitAsString(availableCards[i].getSuit());
  330. break royalSearch;
  331. }
  332. }
  333. }
  334. }
  335. }
  336. return result;
  337. }
  338.  
  339. // Straight flush is 5 consecutive cards of the same suit.
  340. private String evaluateStraightFlush(short[] rankCounter, short[] suitCounter){
  341. String result = "";
  342.  
  343. if (suitCounter[0] > 4 || suitCounter[1] > 4 ||
  344. suitCounter[2] > 4 || suitCounter[3] > 4){
  345.  
  346. // min. requirements for a straight flush have been met.
  347. // Loop through available cards looking for 5 consecutive cards of the same suit,
  348. // start in reverse to get the highest value straight flush
  349. for (int i=availableCards.length-1;i>3;i--){
  350. if ((availableCards[i].getRank()-ONE == availableCards[i-ONE].getRank() &&
  351. availableCards[i].getRank()-TWO == availableCards[i-TWO].getRank() &&
  352. availableCards[i].getRank()-THREE == availableCards[i-THREE].getRank() &&
  353. availableCards[i].getRank()-FOUR == availableCards[i-FOUR].getRank())
  354. &&
  355. (availableCards[i].getSuit() == availableCards[i-ONE].getSuit() &&
  356. availableCards[i].getSuit() == availableCards[i-TWO].getSuit() &&
  357. availableCards[i].getSuit() == availableCards[i-THREE].getSuit() &&
  358. availableCards[i].getSuit() == availableCards[i-FOUR].getSuit())){
  359. // Found royal flush, break and return.
  360. result = "Straight Flush!! " + Card.rankAsString(availableCards[i].getRank()) + " high of " + Card.suitAsString(availableCards[i].getSuit());
  361. break;
  362. }
  363. }
  364. }
  365. return result;
  366. }
  367.  
  368. // Four of a kind is 4 cards with the same rank: 2-2-2-2, 3-3-3-3, etc...
  369. private String evaluateFourOfAKind(short[] rankCounter){
  370. String result = "";
  371.  
  372. for (int i=0;i<rankCounter.length;i++){
  373. if (rankCounter[i] == FOUR){
  374. result = "Four of a Kind, " + Card.rankAsString(i) +"'s";
  375. break;
  376. }
  377. }
  378. return result;
  379. }
  380.  
  381. // Full house is having 3 of a kind of one rank, and two of a kind of
  382. // a second rank. EX: J-J-J-3-3
  383. private String evaluateFullHouse(short[] rankCounter){
  384. String result = "";
  385. short threeOfKindRank = -1;
  386. short twoOfKindRank = -1;
  387.  
  388. for (int i=rankCounter.length;i>0;i--){
  389. if ((threeOfKindRank < (short)0) || (twoOfKindRank < (short)0)){
  390. if ((rankCounter[i-ONE]) > 2){
  391. threeOfKindRank = (short) (i-ONE);
  392. }
  393. else if ((rankCounter[i-ONE]) > 1){
  394. twoOfKindRank = (short)(i-ONE);
  395. }
  396. }
  397. else
  398. {
  399. break;
  400. }
  401. }
  402.  
  403. if ((threeOfKindRank >= (short)0) && (twoOfKindRank >= (short)0)){
  404. result = "Full House: " + Card.rankAsString(threeOfKindRank) + "'s full of " + Card.rankAsString(twoOfKindRank) + "'s";
  405. }
  406.  
  407. return result;
  408. }
  409.  
  410. // Flush is 5 cards of the same suit.
  411. private String evaluateFlush(short[] rankCounter, short[] suitCounter){
  412. String result = "";
  413.  
  414. // verify at least 1 suit has 5 cards or more.
  415. if (suitCounter[0] > 4 || suitCounter[1] > 4 ||
  416. suitCounter[2] > 4 || suitCounter[3] > 4){
  417.  
  418. for (int i=availableCards.length-1;i>3;i--){
  419. if (availableCards[i].getSuit() == availableCards[i-ONE].getSuit() &&
  420. availableCards[i].getSuit() == availableCards[i-TWO].getSuit() &&
  421. availableCards[i].getSuit() == availableCards[i-THREE].getSuit() &&
  422. availableCards[i].getSuit() == availableCards[i-FOUR].getSuit()){
  423. // Found royal flush, break and return.
  424. result = "Flush!! " + Card.rankAsString(availableCards[i].getRank()) + " high of " + Card.suitAsString(availableCards[i].getSuit());
  425. break;
  426. }
  427. }
  428. }
  429.  
  430.  
  431. return result;
  432. }
  433.  
  434. // Straight is 5 consecutive cards, regardless of suit.
  435. private String evaluateStraight(short[] rankCounter){
  436. String result = "";
  437.  
  438. // loop through rank array to check for 5 consecutive
  439. // index with a value greater than zero
  440. for (int i=rankCounter.length;i>4;i--){
  441. if ((rankCounter[i-1] > 0) &&
  442. (rankCounter[i-2] > 0) &&
  443. (rankCounter[i-3] > 0) &&
  444. (rankCounter[i-4] > 0) &&
  445. (rankCounter[i-5] > 0)){
  446. result = "Straight " + Card.rankAsString(i-1) + " high";
  447. break;
  448. }
  449. }
  450. return result;
  451. }
  452.  
  453. // Three of a kind is 3 cards of the same rank.
  454. private String evaluateThreeOfAKind(short[] rankCounter){
  455. String result = "";
  456.  
  457. // loop through rank array to check for 5 consecutive
  458. // index with a value greater than zero
  459. for (int i=rankCounter.length;i>0;i--){
  460. if (rankCounter[i-1] > 2){
  461. result = "Three of a Kind " + Card.rankAsString(i-1) + "'s";
  462. break;
  463. }
  464. }
  465. return result;
  466. }
  467.  
  468. // Two pair is having 2 cards of the same rank, and two
  469. // different cards of the same rank. EX: 3-3-7-7-A
  470. private String evaluateTwoPair(short[] rankCounter){
  471. String result = "";
  472. short firstPairRank = -1;
  473. short secondPairRank = -1;
  474.  
  475. for (int i=rankCounter.length;i>0;i--){
  476. if ((firstPairRank < (short)0) || (secondPairRank < (short)0)){
  477. if (((rankCounter[i-ONE]) > 1) && (firstPairRank < (short)0)){
  478. firstPairRank = (short) (i-ONE);
  479. }
  480. else if ((rankCounter[i-ONE]) > 1){
  481. secondPairRank = (short)(i-ONE);
  482. }
  483. }
  484. else
  485. {
  486. // two pair found, break loop.
  487. break;
  488. }
  489. }
  490.  
  491. // populate output
  492. if ((firstPairRank >= (short)0) && (secondPairRank >= (short)0)){
  493. if (secondPairRank == (short)0){
  494. // Aces serve as top rank but are at the bottom of the rank array
  495. // swap places so aces show first as highest pair
  496. result = "Two Pair: " + Card.rankAsString(secondPairRank) + "'s and " + Card.rankAsString(firstPairRank) + "'s";
  497. }
  498. else
  499. {
  500. result = "Two Pair: " + Card.rankAsString(firstPairRank) + "'s and " + Card.rankAsString(secondPairRank) + "'s";
  501. }
  502. }
  503.  
  504. return result;
  505. }
  506.  
  507. // One is is two cards of the same rank.
  508. private String evaluateOnePair(short[] rankCounter){
  509. String result = "";
  510.  
  511. for (int i=rankCounter.length;i>0;i--){
  512. if((rankCounter[i-ONE]) > 1){
  513. result = "One Pair: " + Card.rankAsString(i-ONE) + "'s";
  514. break;
  515. }
  516. }
  517. return result;
  518. }
  519.  
  520. // high card is the highest card out of the 7 possible cards to be used.
  521. private String evaluateHighCard(short[] rankCounter){
  522. String result = "";
  523.  
  524. for (int i=rankCounter.length;i>0;i--){
  525. if((rankCounter[i-ONE]) > 0){
  526. result = "High Card: " + Card.rankAsString(i-ONE);
  527. break;
  528. }
  529. }
  530. return result;
  531. }
  532.  
  533. }
  534.  
  535. import java.util.Random;
  536.  
  537. public class Deck{
  538. private Card[] cards = new Card[52];
  539.  
  540. //Constructor
  541. public Deck(){
  542. int i = 0;
  543. for (short j=0; j<4; j++){
  544. for (short k=0; k<13;k++){
  545. cards[i++] = new Card(k, j);
  546. }
  547. }
  548. }
  549.  
  550. // Print entire deck in order
  551. protected void printDeck(){
  552. for(int i=0; i<cards.length;i++){
  553. System.out.println(i+1 + ": " + cards[i].printCard());
  554. }
  555. System.out.println("n");
  556. }
  557.  
  558. // Find card in deck in a linear fashion
  559. // Use this method if deck is shuffled/random
  560. protected int findCard(Card card){
  561. for (int i=0;i<52;i++){
  562. if (Card.sameCard(cards[i], card)){
  563. return i;
  564. }
  565. }
  566. return -1;
  567. }
  568.  
  569. //return specified card from deck
  570. protected Card getCard(int cardNum){
  571. return cards[cardNum];
  572. }
  573.  
  574. protected void shuffle(){
  575. int length = cards.length;
  576. Random random = new Random();
  577. //random.nextInt();
  578. for (int i=0;i<length;i++){
  579. int change = i + random.nextInt(length-i);
  580. swapCards(i, change);
  581. }
  582. }
  583.  
  584. protected void cutDeck(){
  585. Deck tempDeck = new Deck();
  586. Random random = new Random();
  587. int cutNum = random.nextInt(52);
  588. for (int i=0;i<cutNum;i++){
  589. tempDeck.cards[i] = this.cards[52-cutNum+i];
  590. }
  591. for (int j=0;j<52-cutNum;j++){
  592. tempDeck.cards[j+cutNum] = this.cards[j];
  593. }
  594. this.cards = tempDeck.cards;
  595. }
  596.  
  597. // Swap cards in array to 'shuffle' the deck.
  598. private void swapCards(int i, int change){
  599. Card temp = cards[i];
  600. cards[i] = cards[change];
  601. cards[change] = temp;
  602. }
  603. }
  604.  
  605. import java.util.*;
  606.  
  607. public class Card{
  608. private short rank, suit;
  609.  
  610. private static String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
  611. private static String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
  612.  
  613. //Constructor
  614. public Card(short rank, short suit){
  615. this.rank = rank;
  616. this.suit = suit;
  617. }
  618.  
  619. // Getter and Setters
  620. public short getSuit(){
  621. return suit;
  622. }
  623.  
  624. public short getRank(){
  625. return rank;
  626. }
  627.  
  628. protected void setSuit(short suit){
  629. this.suit = suit;
  630. }
  631.  
  632. protected void setRank(short rank){
  633. this.rank = rank;
  634. }
  635.  
  636. // methods
  637. public static String rankAsString(int __rank){
  638. return ranks[__rank];
  639. }
  640.  
  641. public static String suitAsString(int __suit){
  642. return suits[__suit];
  643. }
  644.  
  645. public @Override String toString(){
  646. return rank + " of " + suit;
  647. }
  648.  
  649. // Print card to string
  650. protected String printCard(){
  651. return ranks[rank] + " of " + suits[suit];
  652. }
  653.  
  654. // Determine if two cards are the same (Ace of Diamonds == Ace of Diamonds)
  655. public static boolean sameCard(Card card1, Card card2){
  656. return (card1.rank == card2.rank && card1.suit == card2.suit);
  657. }
  658.  
  659. }
  660.  
  661. class rankComparator implements Comparator<Object>{
  662. public int compare(Object card1, Object card2) throws ClassCastException{
  663. // verify two Card objects are passed in
  664. if (!((card1 instanceof Card) && (card2 instanceof Card))){
  665. throw new ClassCastException("A Card object was expeected. Parameter 1 class: " + card1.getClass()
  666. + " Parameter 2 class: " + card2.getClass());
  667. }
  668.  
  669. short rank1 = ((Card)card1).getRank();
  670. short rank2 = ((Card)card2).getRank();
  671.  
  672. return rank1 - rank2;
  673. }
  674. }
  675.  
  676. class suitComparator implements Comparator<Object>{
  677. public int compare(Object card1, Object card2) throws ClassCastException{
  678. // verify two Card objects are passed in
  679. if (!((card1 instanceof Card) && (card2 instanceof Card))){
  680. throw new ClassCastException("A Card object was expeected. Parameter 1 class: " + card1.getClass()
  681. + " Parameter 2 class: " + card2.getClass());
  682. }
  683.  
  684. short suit1 = ((Card)card1).getSuit();
  685. short suit2 = ((Card)card2).getSuit();
  686.  
  687. return suit1 - suit2;
  688. }
  689. }
  690.  
  691. public class Board {
  692. private Card[] board = new Card[5];
  693. private Card[] burnCards = new Card[3];
  694.  
  695. //constructor
  696. public Board(){
  697. }
  698.  
  699. //methods
  700. protected void setBoardCard(Card card, int cardNum){
  701. this.board[cardNum] = card;
  702. }
  703.  
  704. protected Card getBoardCard(int cardNum){
  705. return this.board[cardNum];
  706. }
  707.  
  708. protected void setBurnCard(Card card, int cardNum){
  709. this.burnCards[cardNum] = card;
  710. }
  711.  
  712. protected Card getBurnCard(int cardNum){
  713. return this.burnCards[cardNum];
  714. }
  715.  
  716. protected int boardSize(){
  717. return board.length;
  718. }
  719.  
  720. protected void printBoard(){
  721. System.out.println("The board contains the following cards:");
  722. for(int i =0; i<board.length;i++){
  723. System.out.println(i+1 + ": " + getBoardCard(i).printCard());
  724. }
  725. System.out.println("n");
  726. }
  727.  
  728. protected void printBurnCards(){
  729. System.out.println("The burn cards are:");
  730. for(int i =0; i<burnCards.length;i++){
  731. System.out.println(i+1 + ": " + getBurnCard(i).printCard());
  732. }
  733. System.out.println("n");
  734. }
  735.  
  736. }
  737.  
  738. import java.util.*;
  739.  
  740. public class Card{
  741. private short rank, suit;
  742.  
  743. protected void setSuit(short suit){
  744. this.suit = suit;
  745. }
  746.  
  747. protected void setRank(short rank){
  748. this.rank = rank;
  749. }
  750.  
  751. public static String rankAsString(int __rank){
  752. return ranks[__rank];
  753. }
  754.  
  755. public static String suitAsString(int __suit){
  756. return suits[__suit];
  757. }
  758.  
  759. public @Override String toString(){
  760. return rank + " of " + suit;
  761. }
  762.  
  763. // Print card to string
  764. protected String printCard(){
  765. return ranks[rank] + " of " + suits[suit];
  766. }
  767.  
  768. public static boolean sameCard(Card card1, Card card2){
  769. return (card1.rank == card2.rank && card1.suit == card2.suit);
  770. }
  771.  
  772. @Override public boolean equals(Object that) {
  773. return this == that;
  774. }
  775.  
  776. public class Board { //abbreviated
  777.  
  778. private Card[] board = new Card[5];
  779. private Card[] burnCards = new Card[3];
  780.  
  781. private int boardIndex = 0;
  782. private int burnCardIndex = 0;
  783.  
  784. //methods
  785. protected void addBoardCard(Card card){
  786. this.board[boardIndex++] = card;
  787. }
  788.  
  789. protected void addBurnCard(Card card){
  790. this.burnCards[burnCardIndex++] = card;
  791. }
  792. }
  793.  
  794. protected void cutDeck(){
  795. Card[] temp = new Card[52];
  796. Random random = new Random();
  797. int cutNum = random.nextInt(52);
  798.  
  799. System.arraycopy(this.cards, 0, temp, 52-cutNum, cutNum);
  800. System.arraycopy(this.cards, cutNum, temp, 0, 52-cutNum);
  801.  
  802. this.cards = temp;
  803. }
  804.  
  805. try {
  806. userInput = br.readLine();
  807. } catch (IOException ioe) {
  808. throw new HoldemIOException(ioe);
  809. }
  810.  
  811. try {
  812. intPlayers = Integer.parseInt(userInput);
  813. } catch (NumberFormatException nfe) {
  814. throw new HoldemUserException("Error: Not an integer entered for number of players", nfe);
  815. }
  816.  
  817. if ((intPlayers<1) || (intPlayers>9)){
  818. throw new HoldemUserException("Error: Number of players must be an integer between 1 and 9");
  819. }
  820.  
  821. class HoldemException extends Exception {}
  822.  
  823. do {
  824. try {
  825. numPlayers = getNumberOfPlayers();
  826. } catch (HoldemUserException e) {
  827. System.out.println(e.getMessage());
  828. // "Only" a user error, keep trying
  829. } catch (HoldemIOException e) {
  830. System.err.println("Error: IO error trying to read input!");
  831. System.exit(1);
  832. }
  833. } while (numPlayers = 0);
Add Comment
Please, Sign In to add comment