THIS IS A JAVA FX PROBLEM Create a card game program, pisti. The user will play
ID: 3823799 • Letter: T
Question
THIS IS A JAVA FX PROBLEM
Create a card game program, pisti. The user will play the game with the computer. The rules of the game added below. Requirements: Create classes for appropriate objects such as deck, card, player (bot and person). Use ArrayLists, Arrays, exception handling. Create a GUI that displays the cards in the center, and the player’s cards. At the end of the game, the scores should be calculated and winner should be announced.
Introduction
Piti (pronounced "pishti"), is a popular Turkish card game, using a standard 52 card pack. The direction of play is counter-clockwise. Cards are played to a central pile, which can be captured by matching the previous card played or playing a jack. Points are scored for certain captured cards. The word "piti", which means "burnt", describes a capture of a pile containing only one card, for which extra points are scored. The dealer turns one card of the four in the center of the table face up, to start the discard pile. If it is a jack, the cards should be shuffled and start the game again.
The Play
The player to dealer's right begins, and the turn to play passes counter-clockwise (For our project the user will play, then bot plays). A turn consists of playing one card from your hand face up on top of the discard pile. If rank of the played card matches the rank of the previous card on the pile, the playing team captures the whole pile. The captured cards are stored face down in front of one member of the team. The next player will then start a new discard pile by playing a card face up to the empty table. Playing a jack also captures the whole pile, no matter what card is on top of it. If the played card is not a jack and is not equal to the previous top card of the pile, the played card is simply added to the top of the pile. The team which makes the first capture also gets the cards which were dealt to the center of the table. Both members of the capturing team can look at these cards, but the other team are not allowed to see them. When everyone has played their last four cards, any cards remaining in the discard pile are given to the last team that made a capture. The play of the hand is now over, and the teams score for the cards they have captured.
Piti
If the pile consists of just one card and the next player captures it by playing a matching card (not a jack), the capturing team scores a 10 point bonus for a piti. The capturing card is placed face up in the team's capture pile as a way of remembering the 10 points when scoring. If the pile consists of just a single jack and you capture it with another jack, this counts as a double piti, worth 20 points. A piti can happen at any stage of the game, except that you cannot score a piti for the very first card played by the player to dealer's right (capturing the original center cards) nor for the very last card played by the dealer (just before the hand is scored).
Scoring
Points are scored for particular cards, for the majority of cards, and for each piti as follows:
Each J,Q,K,10 - 1 point
Each ace - 1 -point
2 of clubs - 2 points
10 of diamonds - 3 points
Majority of cards - 3 points
Each piti - 10 points
Each J piti - 20 points
Explanation / Answer
package javapoker;
public 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
}
}
if (ranks[x] > sameCards)
{
if (sameCards != 1) //if sameCards was not the default value
{
sameCards2 = sameCards;
smallGroupRank = x; //changed from smallGroupRank=largeGroupRank;
}
sameCards = ranks[x];
largeGroupRank = x;
}
for (int x=13; x>=1; x--) //x is rank of cards, ranks[x] is number of cards of that rank
{
if (ranks[x] > sameCards)
{
if (sameCards == 1) //if sameCards was not assigned to already
{
largeGroupRank = x;
}
else {
sameCards2 = sameCards; //if sameCards was assigned to, write data from
smallGroupRank = x; //top group to low group
}
sameCards = ranks[x]; //update sameCards to new greatest sameCards value in ranks
} else if (ranks[x] > sameCards2)
{
sameCards2 = ranks[x];
smallGroupRank = x;
}
}
package javapoker;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.