Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Mo Tabet
- //AP Comp Sci
- //1/23/20
- // Card class represents a playing card
- public class Card
- {
- private String face; // face of card ie..."Ace","Deuce", "King"..etc
- private String suit; // suit of card ie "Heart", "Diamond"
- private int num; //Number of the card
- // two argument card constructor
- public Card( String cardFace, String cardSuit )
- {
- face = cardFace;
- suit = cardSuit;
- if(face=="Ace")
- num=14;
- if(face=="Deuce")
- num=2;
- if(face=="Three")
- num=3;
- if(face=="Four")
- num=4;
- if(face=="Five")
- num=5;
- if(face=="Six")
- num=6;
- if(face=="Seven")
- num=7;
- if(face=="Eight")
- num=8;
- if(face=="Nine")
- num=9;
- if(face=="Ten")
- num=10;
- if(face=="Jack")
- num=11;
- if(face=="Queen")
- num=12;
- if(face=="King")
- num=13;
- }
- public int getValue()
- {
- return num;
- }
- public String getSuit()
- {
- return suit;
- }
- public String toString()
- {
- return face + " of " + suit;
- }
- }
- //Mo Tabet
- //AP Comp Sci
- //1/23/20
- import java.util.Random;
- public class DeckOfCards
- {
- private Card deck[]; // array of Card objects
- private int currentCard; // index of next Card to be dealt
- private final int NUMBER_OF_CARDS = 52;
- // Constructor fills the deck of Cards
- public DeckOfCards()
- {
- String faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
- "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
- String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
- deck = new Card[NUMBER_OF_CARDS]; // create our array of card objects
- currentCard = 0; //set currentCard so firstCard dealt is deck[0]
- // populate the deck with card objects
- for (int count=0; count<deck.length; count++)
- {
- deck[count] = new Card( faces[count % 13], suits[count/13] );
- }
- }
- private static int findMinimum(Card []a, int first)
- {
- int minIndex=first;
- for(int i =first+1;i<a.length;i++)
- {
- if(a[i].getValue()<a[minIndex].getValue())
- {
- minIndex=i;
- }
- }
- return minIndex;
- }
- public static void organize(Card[]hand)
- {
- Card temp;
- for(int i=0;i<hand.length-1;i++)
- {
- int minIndex=findMinimum(hand,i);
- if(minIndex!=i)
- {
- //swap the cards
- temp=hand[i];
- hand[i]=hand[minIndex];
- hand[minIndex]=temp;
- }
- }
- }
- public void shuffle()
- {
- Random generator = new Random();
- // for each Card, pick another random Card and swap them
- for ( int first=0; first < deck.length; first++)
- {
- // select a random number between 0 and 51
- int second = generator.nextInt(NUMBER_OF_CARDS);
- // swap current Card with randomly selected Card
- Card temp = deck[first];
- deck[first] = deck[second];
- deck[second] = temp;
- }
- currentCard = 0;
- }
- public Card dealCard()
- {
- if (currentCard < deck.length)
- {
- return deck[currentCard++];
- }
- else
- {
- return null;
- }
- }
- }
- //Mo Tabet
- //AP Comp Sci
- //1/23/20
- public class Poker
- {
- public static void main(String args[])
- {
- System.out.println();
- int p1score=0;
- int rp1score=0;
- int p2score=0;
- //Pair = 100
- //2 Pair = 200
- //3 of a Kind = 300
- //Straight = 400
- //Flush = 500
- DeckOfCards deck=new DeckOfCards();
- System.out.println("*****************");
- System.out.println("WELCOME TO POKER");
- System.out.println("*****************");
- System.out.println();
- deck.shuffle();
- Card[] p1=new Card[5];
- Card[] rp1=new Card[5];
- Card[] p2=new Card[5];
- for(int i=0;i<5;i++)
- {
- p1[i]=deck.dealCard();
- }
- for(int i=0;i<5;i++)
- {
- p2[i]=deck.dealCard();
- }
- System.out.println("Player 1 Has Cards: ");
- deck.organize(p1);
- for(int i=0;i<5;i++)
- {
- System.out.println(" " + p1[i].toString());
- }
- rp1=p1;
- System.out.println();
- System.out.println("Player 2 Has Cards: ");
- deck.organize(p2);
- for(int i=0;i<5;i++)
- {
- System.out.println(" " + p2[i].toString());
- }
- System.out.println();
- for(int i=0;i<2;i++)
- {
- rp1score=p1score;
- p1score=0;
- //WINNING SITUATIONS
- int five=p1[4].getValue();
- int four=p1[3].getValue();
- int three=p1[2].getValue();
- int two=p1[1].getValue();
- int one=p1[0].getValue();
- //FLUSH
- 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()))
- {
- p1score=p1score+500+p1[4].getValue();
- }
- //STRAIGHT
- else if((five--)==four&&(four--)==three&&(three--)==two&&(two--)==one)
- {
- p1score=p1score+400+p1[4].getValue();
- }
- //THREE OF A KIND
- else if((p1[0].getValue()==p1[1].getValue()&&p1[0].getValue()==p1[2].getValue()))
- {
- p1score=p1score+300+p1[0].getValue();
- }
- else if((p1[1].getValue()==p1[2].getValue()&&p1[1].getValue()==p1[3].getValue()))
- {
- p1score=p1score+300+p1[1].getValue();
- }
- else if((p1[2].getValue()==p1[3].getValue()&&p1[2].getValue()==p1[4].getValue()))
- {
- p1score=p1score+300+p1[2].getValue();
- }
- //TWO PAIR
- else if(p1[0].getValue()==p1[1].getValue()&&p1[2].getValue()==p1[3].getValue())
- {
- p1score=p1score+200+p1[2].getValue();
- }
- else if(p1[0].getValue()==p1[1].getValue()&&p1[3].getValue()==p1[4].getValue())
- {
- p1score=p1score+200+p1[3].getValue();
- }
- else if(p1[1].getValue()==p1[2].getValue()&&p1[3].getValue()==p1[4].getValue())
- {
- p1score=p1score+200+p1[3].getValue();
- }
- //PAIR
- else if(p1[0].getValue()==p1[1].getValue())
- {
- p1score=p1score+100+p1[1].getValue();
- }
- else if(p1[1].getValue()==p1[2].getValue())
- {
- p1score=p1score+100+p1[2].getValue();
- }
- else if(p1[2].getValue()==p1[3].getValue())
- {
- p1score=p1score+100+p1[3].getValue();
- }
- else if(p1[3].getValue()==p1[4].getValue())
- {
- p1score=p1score+100+p1[4].getValue();
- }
- //HIGH CARD
- else
- {
- p1score=p1score+p1[4].getValue();
- }
- p1=p2;
- p2score=p1score;
- }
- p1score=rp1score;
- if(p1score>p2score)
- {
- System.out.println("Player 1 wins");
- if(p1score>=500)
- {
- System.out.println("With a Flush");
- }
- else if(p1score>=400&&p1score<500)
- {
- System.out.println("With a Straight");
- }
- else if(p1score>=300&&p1score<400)
- {
- System.out.println("With a Three of a Kind");
- }
- else if(p1score>=200&&p1score<300)
- {
- System.out.println("With a Two Pair");
- }
- else if(p1score>=100&&p1score<200)
- {
- System.out.println("With a Pair");
- }
- else if(p1score>=0&&p1score<100)
- {
- System.out.println("With a High Card");
- }
- }
- if(p2score>p1score)
- {
- System.out.println("Player 2 wins");
- if(p2score>=500)
- {
- System.out.println("With a Flush");
- }
- else if(p2score>=400&&p1score<500)
- {
- System.out.println("With a Straight");
- }
- else if(p2score>=300&&p1score<400)
- {
- System.out.println("With a Three of a Kind");
- }
- else if(p2score>=200&&p1score<300)
- {
- System.out.println("With a Two Pair");
- }
- else if(p2score>=100&&p1score<200)
- {
- System.out.println("With a Pair");
- }
- else if(p2score>=0&&p1score<100)
- {
- System.out.println("With a High Card");
- }
- }
- if(p1score==p2score)
- {
- System.out.println("Tis a tie and no one wins!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement