Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- //importing tools
- public class Poker {
- public static int[] buildDeck() {
- int[] fullDeck = new int[40];
- //we're building a new array of 40 elements
- for (int cardvalue=1; cardvalue<=10; cardvalue++) {
- //this for loop assigns a value to my elements
- //because the values only range from 1-10, the initial value is 1, and the last one is 10.
- for (int cardoftype=0; cardoftype<4; cardoftype++) {
- //this loop is for counting how many times a given value has been given.
- //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
- int indexvalue = (cardvalue-1) * 4 +cardoftype;
- //this int is for keeping track of the index of the element we are giving a value.
- //because the index starts at 0, while cardvalue starts at 1, we need to always subtract 1 from the cardvalue
- //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.
- //for instance, if we are on cardvalue 2, that means fullDeck[0] through fullDeck[3] have already been filled.
- //therefore, we are on fullDeck[4], which is what index would be equal to if you did (2-1) * 4
- //then we add +cardoftype, which tells us how many times our current value has been given to an element already.
- //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
- fullDeck[indexvalue] = cardvalue;
- //give current cardvalue to the determined index element
- }
- }
- return fullDeck;
- }
- public static void shuffleDeck(int[] cardArray) {
- //we're shuffling our deck by swapping random elements
- java.util.Random random = new java.util.Random();
- int swap = 0;
- int randomnum = 0;
- final int LENGTH = cardArray.length;
- for (int shuffle=0; shuffle<LENGTH; shuffle++){
- //loop for the full length of the array
- swap = cardArray[shuffle];
- //the value of the current index element is temporarily stored in the swap variable
- randomnum = random.nextInt(cardArray.length - 1);
- //picking a random number from 0 to the index number of the last element in our array, which is its length - 1
- cardArray[shuffle] = cardArray[randomnum];
- //giving the value of that random array element to the current index element
- cardArray[randomnum] = swap;
- //giving the random element the value of the current index value, which was stored in our swap variable
- }
- }
- public static int dealCard (int[]cardArray) {
- //this method picks one element and sets the value of that element in the array to 0
- int card = 0;
- final int LENGTH = cardArray.length;
- for (int deal=0; deal<LENGTH; deal++) {
- if (cardArray[deal]!=0) {
- card = cardArray[deal];
- //as long as the element's value isn't 0, we pull it out of the array
- cardArray[deal] = 0;
- //then we set that element's value to zero to make sure we don't pull the same element twice
- break;
- //we can break the loop right away after doing this, since we only need to return this one card
- }
- }
- return card;
- }
- public static int[] dealHand (int[]cardArray) {
- //this method creates an array that is populated by calling dealCard 5 times
- int[] hand = new int[5];
- final int LENGTH = hand.length;
- for (int dealh=0; dealh < LENGTH; dealh++) {
- hand[dealh] = dealCard(cardArray);
- }
- return hand;
- }
- public static void redraw(int[]hand, int[]deck, int position) {
- hand[position] = dealCard(deck);
- //if a redraw is triggered, this method deals a card from the deck and returns it to the specified position in the hand
- }
- public static int countOccurrences(int[] cardArray, int value) {
- //counts the number of times a given value is repeated in an array
- int numoccurrences = 0;
- final int LENGTH = cardArray.length;
- //starts at 0
- for (int search=0; search<LENGTH; search++) {
- //search refers to the element we are checking against the value in parameters
- if (cardArray[search] == value) {
- numoccurrences++;
- //if the index element is equal to the value checked, increase the number of occurrences by 1
- }
- }
- return numoccurrences;
- }
- public static boolean isFourOfAKind(int[] cardArray) {
- final int LENGTH = cardArray.length;
- for (int fourofk=0; fourofk<LENGTH; fourofk++) {
- if (countOccurrences(cardArray, cardArray[fourofk]) == 4) {
- return true;
- //calls the occurrence check method for every value in our array, returns true if any of them show up 4 times
- }
- }
- return false;
- }
- public static boolean isThreeOfAKind (int[] cardArray) {
- final int LENGTH = cardArray.length;
- for (int threeofk=0; threeofk<LENGTH; threeofk++) {
- if (countOccurrences(cardArray, cardArray[threeofk]) == 3) {
- return true;
- //checks occurrences for every value in the array, return true if any of them show up 3 times
- }
- }
- return false;
- }
- public static boolean isPair(int[] cardArray) {
- final int LENGTH = cardArray.length;
- for (int pair=0; pair<LENGTH; pair++) {
- if (countOccurrences(cardArray, cardArray[pair]) == 2) {
- return true;
- //check occurrences for every value in the array, return true if any of them show up 2 times
- }
- }
- return false;
- }
- public static boolean isFullHouse(int[] cardArray) {
- if (isPair(cardArray) && isThreeOfAKind(cardArray)) {
- return true;
- //checks if there is both a pair and a three of a kind
- }
- else {
- return false;
- }
- }
- public static boolean isTwoPair (int[]cardArray) {
- int pairs =0;
- for (int paircheck=0; paircheck<=10; paircheck++) {
- //checks each possible value 1-10
- if (countOccurrences(cardArray, paircheck) == 2) {
- //basically recreates isPair but checks for individual values instead of just repeated array elements
- pairs++;
- }
- }
- if (pairs == 2) {
- return true;
- //returns true if two different values have pairs
- }
- return false;
- }
- public static boolean isStraight (int[] cardArray) {
- int swap = 0;
- final int LENGTH = cardArray.length;
- for (int supersort=0; supersort<LENGTH-1; supersort++) {
- //i have to run this sorter once for each element in the array, since it has to compare each element against all the others
- //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
- //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
- for (int sort=0; sort<LENGTH - 1; sort++) {
- if (cardArray[sort]>cardArray[sort + 1]) {
- swap = cardArray[sort];
- //if the current element is greater than the following element, we put its value into the swap variable
- cardArray[sort] = cardArray[sort+1];
- //then we put the value of the smaller adjacent element into our current element
- cardArray[sort+1] = swap;
- //and we put the larger value, stored in swap, into the following element
- }
- }
- }
- for (int checkstraight=0; checkstraight<LENGTH-1; checkstraight++) {
- //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
- if (cardArray[checkstraight] != cardArray[checkstraight+1] - 1) {
- return false;
- //if any given element is not equal to the following element - 1, we don't have a straight, and must return false
- }
- }
- return true;
- }
- public static int assessHand(int[] cardArray) {
- //this method assigns points by checking each possible hand in order from highest to lowest amount of points
- //so that a full house ONLY gives points for the hand being a full house, not for three of a kind and pair
- int points = 0;
- if (isFourOfAKind(cardArray)) {
- points = 4500;
- }
- else if (isFullHouse(cardArray)) {
- points = 700;
- }
- else if (isStraight(cardArray)) {
- points = 300;
- }
- else if (isThreeOfAKind(cardArray)) {
- points = 50;
- }
- else if (isTwoPair(cardArray)) {
- points = 20;
- }
- else if (isPair(cardArray)) {
- points = 1;
- }
- return points;
- }
- public static int runRound() {
- Scanner reader = new Scanner(System.in);
- int[] cardArray = buildDeck();
- //calls build deck to build deck
- shuffleDeck(cardArray);
- //shuffles the deck, retaining the same array address
- int[] hand = dealHand(cardArray);
- final int LENGTH = hand.length;
- //deals a hand from the shuffled deck
- System.out.println("Here are your cards: ");
- for (int handprint=0; handprint<LENGTH; handprint++) {
- System.out.print(hand[handprint] +" ");
- //shows the player his deck
- }
- System.out.println(" ");
- for (int redrawloop=0; redrawloop<3; redrawloop++) {
- System.out.println("Would you like to redraw? If yes, specify the position of the card you would like to redraw.");
- System.out.println("If no, type -1");
- int position = reader.nextInt();
- if (position == -1) {
- break;
- //immediately begins the round if the player doesn't want to redraw
- }
- else {
- hand[position] = dealCard(cardArray);
- System.out.println("Your new card is: " +hand[position]);
- //draws new card to replace the card in whatever position the user requests
- }
- }
- System.out.println("Your final hand is: ");
- for (int p=0; p<LENGTH; p++) {
- System.out.print(hand[p] +" ");
- }
- System.out.println("You have gained: " +assessHand(hand) +" points");
- return assessHand(hand);
- //prints number of points gained this round and returns it
- }
- public static void runGame() {
- int totalpoints = 0;
- for (int games=0; games<10; games++) {
- totalpoints = totalpoints + runRound();
- //runs a round and adds the number of points gained in this round to the total
- }
- System.out.println("Your total number of points is: " +totalpoints);
- //prints total number of points gained across 10 rounds
- }
- public static void main(String[]args) {
- runGame();
- }
- }
Add Comment
Please, Sign In to add comment