SX514LEFV

simplified poker prog 1

Dec 8th, 2024 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.94 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3. //importing tools
  4. public class Poker {
  5.     public static int[] buildDeck() {
  6.         int[] fullDeck = new int[40];
  7.         //we're building a new array of 40 elements
  8.         for (int cardvalue=1; cardvalue<=10; cardvalue++) {
  9.             //this for loop assigns a value to my elements
  10.             //because the values only range from 1-10, the initial value is 1, and the last one is 10.
  11.             for (int cardoftype=0; cardoftype<4; cardoftype++) {
  12.                 //this loop is for counting how many times a given value has been given.
  13.                 //because each value has to be repeated 4 times in this array, the loop stops when the int cardoftype is no longer lesser than 4
  14.                 int indexvalue = (cardvalue-1) * 4 +cardoftype;
  15.                 //this int is for keeping track of the index of the element we are giving a value.
  16.                 //because the index starts at 0, while cardvalue starts at 1, we need to always subtract 1 from the cardvalue
  17.                 //next, it multiplies the cardvalue -1 by 4, because we are moving from one value to the next in increments of 4 in our index.
  18.                 //for instance, if we are on cardvalue 2, that means fullDeck[0] through fullDeck[3] have already been filled.
  19.                 //therefore, we are on fullDeck[4], which is what index would be equal to if you did (2-1) * 4
  20.                 //then we add +cardoftype, which tells us how many times our current value has been given to an element already.
  21.                 //if we are to give the value 2 to our second card, after the earlier example, we would be on fullDeck[5], which can be obtained by doing (2-1) * 4 +1
  22.                 fullDeck[indexvalue] = cardvalue;
  23.                 //give current cardvalue to the determined index element
  24.             }
  25.         }
  26.         return fullDeck;
  27.     }
  28.     public static void shuffleDeck(int[] cardArray) {
  29.         //we're shuffling our deck by swapping random elements
  30.         java.util.Random random = new java.util.Random();
  31.         int swap = 0;
  32.         int randomnum = 0;
  33.         final int LENGTH = cardArray.length;
  34.         for (int shuffle=0; shuffle<LENGTH; shuffle++){
  35.             //loop for the full length of the array
  36.             swap = cardArray[shuffle];
  37.             //the value of the current index element is temporarily stored in the swap variable
  38.             randomnum = random.nextInt(cardArray.length - 1);
  39.             //picking a random number from 0 to the index number of the last element in our array, which is its length - 1
  40.             cardArray[shuffle] = cardArray[randomnum];
  41.             //giving the value of that random array element to the current index element
  42.             cardArray[randomnum] = swap;
  43.             //giving the random element the value of the current index value, which was stored in our swap variable
  44.         }
  45.     }
  46.     public static int dealCard (int[]cardArray) {
  47.         //this method picks one element and sets the value of that element in the array to 0
  48.         int card = 0;
  49.         final int LENGTH = cardArray.length;
  50.         for (int deal=0; deal<LENGTH; deal++) {
  51.             if (cardArray[deal]!=0) {
  52.                 card = cardArray[deal];
  53.                 //as long as the element's value isn't 0, we pull it out of the array
  54.                 cardArray[deal] = 0;
  55.                 //then we set that element's value to zero to make sure we don't pull the same element twice
  56.                 break;
  57.                 //we can break the loop right away after doing this, since we only need to return this one card
  58.             }
  59.         }
  60.         return card;
  61.     }
  62.     public static int[] dealHand (int[]cardArray) {
  63.         //this method creates an array that is populated by calling dealCard 5 times
  64.         int[] hand = new int[5];
  65.         final int LENGTH = hand.length;
  66.         for (int dealh=0; dealh < LENGTH; dealh++) {
  67.             hand[dealh] = dealCard(cardArray);
  68.     }
  69.         return hand;
  70.     }
  71.     public static void redraw(int[]hand, int[]deck, int position) {
  72.         hand[position] = dealCard(deck);
  73.         //if a redraw is triggered, this method deals a card from the deck and returns it to the specified position in the hand
  74.     }
  75.     public static int countOccurrences(int[] cardArray, int value) {
  76.         //counts the number of times a given value is repeated in an array
  77.         int numoccurrences = 0;
  78.         final int LENGTH = cardArray.length;
  79.         //starts at 0
  80.         for (int search=0; search<LENGTH; search++) {
  81.             //search refers to the element we are checking against the value in parameters
  82.             if (cardArray[search] == value) {
  83.                 numoccurrences++;
  84.                 //if the index element is equal to the value checked, increase the number of occurrences by 1
  85.             }
  86.         }
  87.         return numoccurrences;
  88.     }
  89.     public static boolean isFourOfAKind(int[] cardArray) {
  90.         final int LENGTH = cardArray.length;
  91.         for (int fourofk=0; fourofk<LENGTH; fourofk++) {
  92.         if (countOccurrences(cardArray, cardArray[fourofk]) == 4) {
  93.             return true;
  94.             //calls the occurrence check method for every value in our array, returns true if any of them show up 4 times
  95.         }
  96.         }
  97.         return false;
  98.     }
  99.     public static boolean isThreeOfAKind (int[] cardArray) {
  100.         final int LENGTH = cardArray.length;
  101.         for (int threeofk=0; threeofk<LENGTH; threeofk++) {
  102.         if (countOccurrences(cardArray, cardArray[threeofk]) == 3) {
  103.             return true;
  104.             //checks occurrences for every value in the array, return true if any of them show up 3 times
  105.         }
  106.         }
  107.         return false;
  108.     }
  109.     public static boolean isPair(int[] cardArray) {
  110.     final int LENGTH = cardArray.length;
  111.     for (int pair=0; pair<LENGTH; pair++) {
  112.         if (countOccurrences(cardArray, cardArray[pair]) == 2) {
  113.             return true;
  114.             //check occurrences for every value in the array, return true if any of them show up 2 times
  115.         }
  116.         }
  117.         return false;
  118.     }
  119.     public static boolean isFullHouse(int[] cardArray) {
  120.         if (isPair(cardArray) && isThreeOfAKind(cardArray)) {
  121.             return true;
  122.             //checks if there is both a pair and a three of a kind
  123.         }
  124.         else {
  125.             return false;
  126.         }
  127.     }
  128.     public static boolean isTwoPair (int[]cardArray) {
  129.         int pairs =0;
  130.         for (int paircheck=0; paircheck<=10; paircheck++) {
  131.             //checks each possible value 1-10
  132.             if (countOccurrences(cardArray, paircheck) == 2) {
  133.                 //basically recreates isPair but checks for individual values instead of just repeated array elements
  134.                 pairs++;
  135.             }
  136.         }
  137.         if (pairs == 2) {
  138.             return true;
  139.             //returns true if two different values have pairs
  140.         }
  141.         return false;
  142.     }
  143.     public static boolean isStraight (int[] cardArray) {
  144.         int swap = 0;
  145.         final int LENGTH = cardArray.length;
  146.         for (int supersort=0; supersort<LENGTH-1; supersort++) {
  147.             //i have to run this sorter once for each element in the array, since it has to compare each element against all the others
  148.             //if I didn't do this, in a scenario where the fifth element is the smallest one, I would fail to place it in the first position
  149.             //the following sorter only compares each element with its adjacent element, so i need to make sure it makes this comparison upon each new sort until they're all in place
  150.         for (int sort=0; sort<LENGTH - 1; sort++) {
  151.             if (cardArray[sort]>cardArray[sort + 1]) {
  152.                 swap = cardArray[sort];
  153.                 //if the current element is greater than the following element, we put its value into the swap variable
  154.                 cardArray[sort] = cardArray[sort+1];
  155.                 //then we put the value of the smaller adjacent element into our current element
  156.                 cardArray[sort+1] = swap;
  157.                 //and we put the larger value, stored in swap, into the following element
  158.             }  
  159.         }
  160.         }
  161.         for (int checkstraight=0; checkstraight<LENGTH-1; checkstraight++) {
  162.             //now that we've sorted the elements of the array in ascending order, we'll simply check if each of them is 1 integer away from the next
  163.             if (cardArray[checkstraight] != cardArray[checkstraight+1] - 1) {
  164.             return false;
  165.             //if any given element is not equal to the following element - 1, we don't have a straight, and must return false
  166.             }
  167.         }
  168.         return true;
  169.     }
  170.     public static int assessHand(int[] cardArray) {
  171.         //this method assigns points by checking each possible hand in order from highest to lowest amount of points
  172.         //so that a full house ONLY gives points for the hand being a full house, not for three of a kind and pair
  173.         int points = 0;
  174.         if (isFourOfAKind(cardArray)) {
  175.             points = 4500;
  176.         }
  177.         else if (isFullHouse(cardArray)) {
  178.             points = 700;
  179.         }
  180.         else if (isStraight(cardArray)) {
  181.             points = 300;
  182.         }
  183.         else if (isThreeOfAKind(cardArray)) {
  184.             points = 50;
  185.         }
  186.         else if (isTwoPair(cardArray)) {
  187.             points = 20;
  188.         }
  189.         else if (isPair(cardArray)) {
  190.             points = 1;
  191.         }
  192.         return points;
  193.     }
  194.     public static int runRound() {
  195.         Scanner reader = new Scanner(System.in);
  196.         int[] cardArray = buildDeck();
  197.         //calls build deck to build deck
  198.         shuffleDeck(cardArray);
  199.         //shuffles the deck, retaining the same array address
  200.         int[] hand = dealHand(cardArray);
  201.         final int LENGTH = hand.length;
  202.         //deals a hand from the shuffled deck
  203.         System.out.println("Here are your cards: ");
  204.             for (int handprint=0; handprint<LENGTH; handprint++) {
  205.                 System.out.print(hand[handprint] +" ");
  206.                 //shows the player his deck
  207.             }
  208.         System.out.println(" ");
  209.         for (int redrawloop=0; redrawloop<3; redrawloop++) {
  210.             System.out.println("Would you like to redraw? If yes, specify the position of the card you would like to redraw.");
  211.             System.out.println("If no, type -1");
  212.             int position = reader.nextInt();
  213.             if (position == -1) {
  214.                 break;
  215.                 //immediately begins the round if the player doesn't want to redraw
  216.             }
  217.             else {
  218.                 hand[position] = dealCard(cardArray);
  219.                 System.out.println("Your new card is: " +hand[position]);
  220.                 //draws new card to replace the card in whatever position the user requests
  221.             }
  222.         }
  223.         System.out.println("Your final hand is: ");
  224.         for (int p=0; p<LENGTH; p++) {
  225.             System.out.print(hand[p] +" ");
  226.         }
  227.         System.out.println("You have gained: " +assessHand(hand) +" points");
  228.         return assessHand(hand);
  229.         //prints number of points gained this round and returns it
  230.     }
  231.     public static void runGame() {
  232.         int totalpoints = 0;
  233.         for (int games=0; games<10; games++) {
  234.             totalpoints = totalpoints + runRound();
  235.             //runs a round and adds the number of points gained in this round to the total
  236.         }
  237.         System.out.println("Your total number of points is: " +totalpoints);
  238.         //prints total number of points gained across 10 rounds
  239.     }
  240.     public static void main(String[]args) {
  241.         runGame();
  242.     }
  243. }
Add Comment
Please, Sign In to add comment