Using Dr Java
Objective:
Create a game of video poker with a graphical user interface (GUI). The player should start with $100, and then the system deals out 5 playing cards. After the player sees the cards they are then asked to wager $10, $20, $50, or $100. Next the user picks which cards they wish to throw away, and then the system deals more cards in their place. Once this has concluded money are awarded by these criteria:
Nothing – Player loses their wager
Pair – Player gets back their wager
Two Pair – Player wins wager x 2
3 of a kind – Payer wins wager x 3
Full House – Player wins wager x 6
Flush – Player wins wager x 10
Straight – Player wins wager x 15
4 of a kind – Player wins wager x 20
Royal Flush – Player wins wager x 100
If the player’s cash is less than or equal to 0 the game is over, and they restart with $100. Also once they close the program it should store the score in a file that once they restart it they begin with the score they once had.
/**
*
*/
package com.abc.test;
import java.util.ArrayList;
import java.util.Random;
/**
* @author NAP0911
*
*/
class Card
{
private short rank, suit;
private static String[] suits = { "hearts",
"spades", "diamonds", "clubs" };
private static String[] ranks = { "Ace", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
public static String rankAsString( int __rank )
{
return ranks[__rank];
}
Card(short suit, short rank)
{
this.rank=rank;
this.suit=suit;
}
public @Override String toString()
{
return ranks[rank] + " of " +
suits[suit];
}
public short getRank() {
return rank;
}
public short getSuit() {
return suit;
}
}
class Deck {
private ArrayList<Card> cards;
Deck()
{
cards = new
ArrayList<Card>();
int index_1, index_2;
Random generator = new
Random();
Card temp;
for (short a=0; a<=3;
a++)
{
for (short b=0;
b<=12; b++)
{
cards.add( new
Card(a,b) );
}
}
int size = cards.size() -1;
for (short i=0; i<100;
i++)
{
index_1 =
generator.nextInt( size );
index_2 =
generator.nextInt( size );
temp = (Card)
cards.get( index_2 );
cards.set(
index_2 , cards.get( index_1 ) );
cards.set(
index_1, temp );
}
}
public Card drawFromDeck()
{
return cards.remove( cards.size()-1
);
}
public int getTotalCards()
{
return cards.size(); //we could use
this method when making a complete poker game to see if we needed a
new deck
}
}
class Hand {
private Card[] cards;
private int[] value;
Hand(Deck d)
{
value = new
int[6];
cards = new
Card[5];
for (int x=0;
x<5; x++)
{
cards[x] = d.drawFromDeck();
}
int[] ranks =
new int[14];
int[]
orderedRanks = new int[5]; //miscellaneous cards that
are not otherwise significant
boolean
flush=true, straight=false;
int
sameCards=1,sameCards2=1;
int
largeGroupRank=0,smallGroupRank=0;
int
index=0;
int
topStraightValue=0;
for (int x=0;
x<=13; x++)
{
ranks[x]=0;
}
for (int x=0;
x<=4; x++)
{
ranks[ cards[x].getRank() ]++;
}
for (int x=0;
x<4; x++) {
if ( cards[x].getSuit() != cards[x+1].getSuit()
)
flush=false;
}
for (int x=13;
x>=1; x--)
{
if (ranks[x] >
sameCards)
{
if
(sameCards != 1) //if sameCards was not the default value
{
sameCards2 = sameCards;
smallGroupRank = largeGroupRank;
}
sameCards
= ranks[x];
largeGroupRank = x;
} else if (ranks[x] >
sameCards2)
{
sameCards2
= ranks[x];
smallGroupRank = x;
}
}
if (ranks[1]==1)
//if ace, run this before because ace is highest card
{
orderedRanks[index]=14;
index++;
}
for (int
x=13; x>=2; x--)
{
if (ranks[x]==1)
{
orderedRanks[index]=x; //if
ace
index++;
}
}
for (int x=1;
x<=9; x++) //can't have straight with lowest value of more than
10
{
if (ranks[x]==1 && ranks[x+1]==1
&& ranks[x+2]==1 && ranks[x+3]==1 &&
ranks[x+4]==1)
{
straight=true;
topStraightValue=x+4; //4
above bottom value
break;
}
}
if
(ranks[10]==1 && ranks[11]==1 && ranks[12]==1
&& ranks[13]==1 && ranks[1]==1) //ace high
{
straight=true;
topStraightValue=14; //higher than king
}
for (int x=0;
x<=5; x++)
{
value[x]=0;
}
//start hand
evaluation
if (
sameCards==1 ) {
value[0]=1;
value[1]=orderedRanks[0];
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}
if
(sameCards==2 && sameCards2==1)
{
value[0]=2;
value[1]=largeGroupRank; //rank of pair
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
value[4]=orderedRanks[2];
}
if
(sameCards==2 && sameCards2==2) //two pair
{
value[0]=3;
value[1]= largeGroupRank>smallGroupRank ?
largeGroupRank : smallGroupRank; //rank of greater pair
value[2]= largeGroupRank<smallGroupRank ?
largeGroupRank : smallGroupRank;
value[3]=orderedRanks[0]; //extra card
}
if
(sameCards==3 && sameCards2!=2)
{
value[0]=4;
value[1]= largeGroupRank;
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
}
if (straight
&& !flush)
{
value[0]=5;
value[1]=topStraightValue;
}
if (flush
&& !straight)
{
value[0]=6;
value[1]=orderedRanks[0]; //tie determined by
ranks of cards
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}
if
(sameCards==3 && sameCards2==2)
{
value[0]=7;
value[1]=largeGroupRank;
value[2]=smallGroupRank;
}
if
(sameCards==4)
{
value[0]=8;
value[1]=largeGroupRank;
value[2]=orderedRanks[0];
}
if (straight
&& flush)
{
value[0]=9;
value[1]=topStraightValue;
}
}
void display()
{
String s;
switch( value[0]
)
{
case 1:
s="high card";
break;
case 2:
s="pair of " +
Card.rankAsString(value[1]) + "\'s";
break;
case 3:
s="two pair " +
Card.rankAsString(value[1]) + " " +
Card.rankAsString(value[2]);
break;
case 4:
s="three of a kind " +
Card.rankAsString(value[1]) + "\'s";
break;
case 5:
s=Card.rankAsString(value[1])
+ " high straight";
break;
case 6:
s="flush";
break;
case 7:
s="full house " +
Card.rankAsString(value[1]) + " over " +
Card.rankAsString(value[2]);
break;
case 8:
s="four of a kind " +
Card.rankAsString(value[1]);
break;
case 9:
s="straight flush " +
Card.rankAsString(value[1]) + " high";
break;
default:
s="error in Hand.display:
value[0] contains invalid value";
}
s =
"
" + s;
System.out.println(s);
}
void displayAll()
{
for (int x=0;
x<5; x++)
System.out.println(cards[x]);
}
int compareTo(Hand that)
{
for (int x=0;
x<6; x++)
{
if (this.value[x]>that.value[x])
return 1;
else if (this.value[x]<that.value[x])
return -1;
}
return 0; //if
hands are equal
}
}
public class ABC {
public static void main(String[]
args) {
for (int i=0;
i<20000; i++)
{
Deck deck= new
Deck();
Hand hand= new
Hand(deck);
Hand hand2= new
Hand(deck);
hand.display();
hand.displayAll();
hand2.display();
hand2.displayAll();
System.out.println(hand.compareTo(hand2));
}
}
}
Using Dr Java Objective: Create a game of video poker with a graphical user interface (GUI)....
You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...
1. Problem Description Language: JAVA The game of Poker Dice is a bit like standard poker but played with dice instead of cards. In this game, five fair dice are rolled. We will recognize one of seven different hands, in order of increasing value: None alike: Five distinct die values occur. Example: 1, 3, 4, 5, 6 One Pair: Four distinct die values occur; one die value occurs twice and the other three die values occur once each. Example: 1,...
Using Java You are helping a corporation create a new system for keeping track of casinos and customers. The system will be able to record and modify customer and casino information. It will also be able to simulate games in the casino. Customer-specific requirements You can create new customers All new customers have a new customerID assigned to them, starting with 1 for the first, 2 for the second, 3 for the third, ect. New customers have names and monetary...
This task involves constructing a Java program that supports interaction via a graphical user interface.The object is to implement a Scrabble graphical game. The basis for the game is quite simple. Presented with a 6x6 grid of Scrabble tiles, the challenge is for the player(s) to form as many high scoring words as possible. Words may only be formed from sequences of adjacent tiles. Two tiles are adjacent if their edges or corners meet. A tile may...