I try to grograme a game called video poker. It's all most done, but Royal Flush seems to never appears.
The card deck contains 52 cards, 13 of each suit. At the beginning of the game, the deck is shuffled. The player pays a token for each game. Then the top five cards of the deck are presented to the player. The player can reject none, some, or all of the cards. The rejected cards are replaced from the top of the deck. Now the hand is scored. Your program should pronounce it to be one of the following:
No pair — The lowest hand, containing five separate cards that do not match up to create any of the hands below.
One pair—Two cards of the same value, for example two queens. Payout: 1
Two pairs—Two pairs, for example two queens and two 5’s. Payout: 2
Three of a kind — Three cards of the same value, for example three queens. Payout: 3
Straight — Five cards with consecutive values, not necessarily of the same suit, such as 4, 5, 6, 7, and 8. The ace can either precede a 2 or follow a king. Payout: 4
Flush—Five cards, not necessarily in order, of the same suit. Payout: 5
Full House — Three of a kind and a pair, for example three queens and two 5’s. Payout: 6
Four of a Kind — Four cards of the same value, such as four queens. Payout: 25
Straight Flush — A straight and a flush: Five cards with consecutive values of the same suit. Payout: 50
Royal Flush — The best possible hand in poker. A 10, jack, queen, king, and ace, all of the same suit. Payout: 250
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class VideoPoker {
public static void main(String[] args) {
// TODO Auto-generated method stub
//SPADES = 0; // Codes for the 4 suits, plus Joker.
//HEARTS = 1;
//DIAMONDS = 2;
//CLUBS = 3;
//ACE = 1; // Codes for the non-numeric cards.
//JACK = 11; // Cards 2 through 10 have their
//QUEEN = 12; // numerical values for their codes.
//KING = 0;
final int MAX_CARD = 52; //The card deck contains 52 cards, 13 of each suit.
Scanner scanner = new Scanner(System.in);
int[] hand = new int[5]; //Five cards to play
int matchFace = 0;
boolean straight = false;
boolean threeOfKind = false;
boolean fourOfKind = false;
boolean flush = false;
boolean straightFlush = false;
boolean fullHouse = false;
boolean royalFlush = false;
String str;
int pair = 0;
ArrayList<Integer> arrayList = new ArrayList<>();
for (int i=0;i<MAX_CARD;i++) arrayList.add(i); //Fill the list
Collections.shuffle(arrayList);
//Deal 5 cards
for (int i=0;i<hand.length;i++) {
hand[i] = arrayList.get(0);
arrayList.remove(0);
}
for (int i=0;i<hand.length;i++) {
System.out.println(valueToString(hand[i]));
}
//The player can reject none, some,or all of the cards.
//The rejected cards are replaced from the top of the deck.
for (int i=0;i<hand.length;i++){
System.out.printf("Do you want to reject card number %d ? ",i+1);
if (scanner.next().toUpperCase().charAt(0)=='Y'){
arrayList.add(hand[i]); //Return to the end of deck
hand[i] = arrayList.get(0);
arrayList.remove(0);
}
}
System.out.println();
Arrays.sort(hand);
for (int i=0;i<hand.length;i++) {
System.out.println(valueToString(hand[i]));
}
ArrayList<String> strList = new ArrayList<>();
for (int i=0;i<hand.length;i++) strList.add(valueToString(hand[i]));
do{
str = strList.get(0);
strList.remove(0);
for (int j=0;j<strList.size();j++){
if (str.regionMatches(0, strList.get(j), 0, 2)){ //Compare the first two char
matchFace++;
strList.remove(j);
j--;
}
}
if (matchFace ==3) {// Four of a kind—Four cards of the same value, such as four queens.
fourOfKind = true;
threeOfKind = false; //Reset
pair = 0; //Reset
}
else if (matchFace ==2) { // Three of a kind—Three cards of the same value, for example three queens.
threeOfKind = true;
pair = 0; //Reset
}else if (matchFace ==1 ) {
pair++;
}
matchFace = 0; //Reset for each loop
}while (!strList.isEmpty());
ArrayList<Integer> intList = new ArrayList<>();
//Straight—Five cards with consecutive values, not necessarily of the same
//suit, such as 4, 5, 6, 7, and 8. The ace can either precede a 2 or follow a king.
//Case 1 where King to value 13 and Ace set to value 14
for (int i=0;i<hand.length;i++) {
if (hand[i]%13==0){
intList.add(13); // set King to value 13
}else if (hand[i]%13==1){
intList.add(14); // set Ace to value 14
}
else intList.add((hand[i]%13));
}
Collections.sort(intList);
if (intList.get(0)+1==intList.get(1)&&intList.get(1)+1==intList.get(2)
&&intList.get(3)==intList.get(2)+1&&intList.get(4)==intList.get(3)+1){
straight = true;
}
//Case 2 where Ace set to value 1
for (int i=0;i<intList.size();i++) if (intList.get(i)==14)intList.set(i, 1); // Set Ace to value 1
Collections.sort(intList);
if (intList.get(0)+1==intList.get(1)&&intList.get(1)+1==intList.get(2)
&&intList.get(3)==intList.get(2)+1&&intList.get(4)==intList.get(3)+1){
straight = true;
}
//Flush — Five cards, not necessarily in order, of the same suit.
if ((hand[0]/13==hand[1]/13)&& (hand[1]/13==hand[2]/13)&& (hand[2]/13==hand[3]/13)&& (hand[3]/13==hand[4]/13) && (hand[4]/13==hand[0]/13)){
flush = true;
}
////Full House — Three of a kind and a pair, for example three queens and two 5’s.
if (threeOfKind && pair==1) fullHouse = true;
// Straight Flush—A straight and a flush: Five cards with consecutive values of
//the same suit. Payout: 50
if(straight && flush ) straightFlush = true;
//Royal Flush—The best possible hand in poker. A 10, jack, queen, king, and ace,
//all of the same suit.
if ((hand[0]%13==10&&hand[1]%13==11&&hand[2]%13==12&&hand[3]%13==0&&hand[4]%13==1) &&
(hand[0]/13==hand[1]/13)&& (hand[1]/13==hand[2]/13)&& (hand[2]/13==hand[3]/13)&& (hand[3]/13==hand[4]/13) && (hand[4]/13==hand[0]/13) ) {
royalFlush = true;
}
if (royalFlush) {
System.out.println("Royal Flush — The best possible hand in poker. A 10, jack, queen, king, and ace,all of the same suit. Payout: 250");
} else
//Straight Flush—A straight and a flush: Five cards with consecutive values of
//the same suit. Payout: 50
if(straightFlush){
System.out.println("Straight Flush — A straight and a flush: Five cards with consecutive values of the same suit. Payout: 50");
}else
//Four of a Kind—Four cards of the same value, such as four queens. Payout: 25
if (fourOfKind){
System.out.println("Four of a Kind — Four cards of the same value, such as four queens. Payout: 25");
}
else
//Full House — Three of a kind and a pair, for example three queens and two 5’s.
//Payout: 6
if (fullHouse){
System.out.println("Full House — Three of a kind and a pair, for example three queens and two 5’s.Payout: 6");
}
else
//Flush — Five cards, not necessarily in order, of the same suit. Payout: 5
if (flush){
System.out.println("Flush — Five cards, not necessarily in order, of the same suit. Payout: 5");
}
//Straight—Five cards with consecutive values, not necessarily of the same
//suit, such as 4, 5, 6, 7, and 8. The ace can either precede a 2 or follow a king.
//Payout: 4
else
if (straight) {
System.out.println("Straight — Five cards with consecutive values, not necessarily of the same suit, such as 4, 5, 6, 7, and 8. The ace can either precede a 2 or follow a king. Payout: 4");
}
//Three of a kind—Three cards of the same value, for example three queens.
//Payout: 3
else if (threeOfKind) {
System.out.println("Three of a kind — Three cards of the same value. Payout: 3");
}
// Two pairs—Two pairs, for example two queens and two 5’s. Payout: 2
else if (pair==2) {
System.out.println("Two pairs — Two pairs, for example two queens and two 5’s. Payout: 2");
}
// One pair—Two cards of the same value, for example two queens. Payout: 1
else if (pair==1) {
System.out.println("One pair — Two cards of the same value, for example two queens. Payout: 1");
}
else {
System.out.println("No pair — The lowest hand, containing five separate cards that do not match up");
}
scanner.close();
}
public static String valueToString(int value){
String faces;
String suits;
if (value%13 ==0) faces = "King ";
else if (value%13 ==1) faces = "Ace ";
else if (value%13 ==2) faces = "Deuce ";
else if (value%13 ==3) faces = "Three ";
else if (value%13 ==4) faces = "Four ";
else if (value%13 ==5) faces = "Five ";
else if (value%13 ==6) faces = "Six ";
else if (value%13 ==7) faces = "Seven ";
else if (value%13 ==8) faces = "Eight ";
else if (value%13 ==9) faces = "Nine ";
else if (value%13 ==10) faces = "Ten ";
else if (value%13 ==11) faces = "Jack ";
else if (value%13 ==12) faces = "Queen ";
else faces ="ERROR";
if (value/13 ==0) suits = "Spades";
else if (value/13 ==1) suits = "Hearts";
else if (value/13 ==2) suits = "Diamonds ";
else if (value/13 ==3) suits = "Clubs";
else suits = "ERROR";
return faces +"of " + suits;
}
}