14. CardGame
This program "deals" a poker hand, and checks for pairs, three of kind, and four a kind.
Description: This program "deals" a poker hand, and checks for pairs, three of kind, and four a kind. Just the beginning of what could be a fund poker-playing game.
Code
package asu.cardgame;
import java.util.Scanner;
public class CardGame {
public static int[] hand = new int[5];
public static int[] deck = new int[52];
public static void main(String[] args) {
//lab4_execise_1();
lab4_exercise_4();
}
private static int randomInteger(int a, int b) {
return (int) ((b - a) * Math.random() + a);
}
private static void lab4_execise_1() {
//generate two integers
//for timing:
long start, stop;
start = System.currentTimeMillis();
System.out.println("start time =" + start);
int n1, n2;
int n_digits = 2;
int a = (int) Math.pow(10, n_digits - 1);
int b = (int) Math.pow(10, n_digits);
n1 = randomInteger(a, b);
n2 = randomInteger(a, b);
System.out.println(" " + n1);
System.out.println(" " + n2);
System.out.println("+___");
int correct = n1 + n2;
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
while (answer != correct) {
System.out.println("Wrong, try again:");
answer = input.nextInt();
}
System.out.println("Got it");
stop = System.currentTimeMillis();
System.out.println("It took you " + (stop - start) / 1000.0 + " seconds.");
}
private static void lab4_exercise_4() {
// choose a random card
// model the deck as 52 cards, indexed from 0 to 51.
// print the whole deck:
//printDeck();
initializeDeck();
shuffleDeck();
dealPokerHand();
sortHand();
displayPokerHand();
if (hasPair() >= 0) {
System.out.println("Has a pair of " + getRank(hasPair() % 13) + "'s");
}
if(hasThree() >= 0) {
System.out.println("Has three " + getRank(hasThree() % 13) + "'s");
}
}
private static int getSuit(int suit) {
switch (suit) {
case 0:
return 9829; //hearts
case 1:
return 9830; //diamonds
case 2:
return 9824; //spades
case 3:
return 9827; //clubs
default:
return 9837; //flat
}
}
private static char getRank(int rank) {
if (rank > 0 && rank < 9) {
return (char) (49 + rank);
}
switch (rank) {
case 0:
return 'A';
case 9:
return 'T';
case 10:
return 'J';
case 11:
return 'Q';
case 12:
return 'K';
}
return (char) rank;
}
private static void printDeck() {
for (int i = 0; i < 52; i++) {
int rank = i % 13;
int suit = i / 13;
System.out.println(getRank(rank) + " " + (char) getSuit(suit));
}
}
private static void initializeDeck() {
for (int i = 0; i < 52; i++) {
deck[i] = i;
}
}
private static void shuffleDeck() {
for(int i = 0; i < 10000; i++) {
int card1 = randomInteger(0, 52);
int card2 = randomInteger(0, 52);
//swap card1 with card2
int temp = deck[card1];
deck[card1] = deck[card2];
deck[card2] = temp;
}
}
private static void dealPokerHand() {
for(int i = 0; i < 5; i++) {
//hand[i] = randomInteger(0, 52);
hand[i] = deck[i];
}
}
private static void displayPokerHand(){
for (int i = 0; i < 5; i++) {
char cardRank = getRank(hand[i] % 13);
char cardSuit = (char) getSuit(hand[i] / 13);
System.out.print(cardRank + "" + cardSuit);
System.out.print(" ");
}
System.out.println("");
}
private static int hasPair() {
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j > i && j < 5; j++) {
if (hand[i] % 13 == hand[j] % 13) {
return hand[i];
}
}
}
return -1;
}
private static int hasThree() {
//assumes the hand is sorted by rank
int[] rank = new int[5];
for (int i = 0; i < 5; i++) {
rank[i] = hand[i] % 13;
}
for (int i = 0; i < 3; i++) {
if (rank[i] == rank[i+1] && rank[i] == rank[i+2]) {
return hand[i];
}
}
return -1;
}
private static void sortHand() {
int[] rank = new int[5];
for (int i = 0; i < 5; i++) {
rank[i] = hand[i] % 13;
}
boolean flag = true;
for (int i = 0; i < 4; i++) {
if (rank[i] > rank[i + 1]) {
int temp = rank[i];
rank[i] = rank[i + 1];
int temp2 = hand[i];
hand[i] = hand[i + 1];
hand[i + 1] = temp2;
flag = true;
}
}
}
}
Output
4♣ 4♦ 9♥ 9♠ T♥
Has a pair of 4's
Last updated
Was this helpful?