如何修复 Poker Deck 使其不重新实例化并将其限制为一副扑克牌中的 52 张牌?
How can I fix the Poker Deck not to reinstantiate itself and restrict it to just the 52 cards in a deck?
我正在构建一个程序来 运行 5 轮 5 张抽牌。我有一些代码,主要是我的 PokerHandValue
class.
PokerHandValue
class 唯一的问题是同时评估双手而不是单独评估。由于我测试了各种组件,我认为问题所在的 PokerHandClass
过于臃肿。如何为程序添加一些代码以仅将一只手作为一个对象进行评估?
我的主要class:
public static void main(String[] args) {
Cards deck = new Cards();
//setting loop for playing 5 hands
int playedHands = 5;
do {
//creating the deck and player hands
List<String> playableDeck = new ArrayList<>(deck.ShuffleDeck());
List<String> playerHand = new ArrayList<>(playableDeck.subList(0, 5));
for (int i = 0; i < 5; i++) {
playableDeck.remove(i);
}
List<String> dealerHand = new ArrayList<>(playableDeck.subList(6, 11));
for (int j = 0; j < 5; j++) {
playableDeck.remove(j);
}
//assessing the value of hand dealt to player
PokerHandValue playerValue = new PokerHandValue(playerHand);
int pValue = playerValue.getValue();
String pReason = playerValue.getResult();
int playerHigh = playerValue.getHighCard();
//assessing the value of hand dealt to player
PokerHandValue dealerValue = new PokerHandValue(dealerHand);
int dValue = dealerValue.getValue();
String dReason = dealerValue.getResult();
int dealerHigh = dealerValue.getHighCard();
//displaying the output and value and results of each hands
System.out.print("Black: " + playerHand.toString().replace("[", "").replace("]", ""));
System.out.println(" and White: " + dealerHand.toString().replace("[", "").replace("]", ""));
//determining the winner
if (dValue < pValue) {
System.out.println("White wins. - with a " + pReason + "\n");
} else if (pValue < dValue) {
System.out.println("Black wins. - with a " + dReason + "\n");
} else if (playerHigh == dealerHigh) {
if (playerHigh < dealerHigh) {
String stringDHighCard = determineFace(Integer.toString(dealerHigh));
System.out.println("Black wins. with a " + stringDHighCard + "\n");
} else {
String stringPHighCard = determineFace(Integer.toString(playerHigh));
System.out.println("White wins. - with a " + stringPHighCard + "\n");
}
} else System.out.println("Tie.\n");
playedHands--;
//Removing the deck and hands to reshuffle deck and re-deal
playableDeck.removeAll(playableDeck);
playerHand.removeAll(playerHand);
dealerHand.removeAll(dealerHand);
}
while (playedHands > 0);
}
static String determineFace(String faceValue){
String face=faceValue;
if (face.equals("11")) {
face = "J";
}
else if(face.equals("12")) {
face = "Q";
}
else if (face.equals("13")) {
face = "K";
}
else face = "A";
return face;
}
我的PokerHandValue
class评价个人牌局:
import java.util.*;
class PokerHandValue {
private int highCard;
private int value;
private String result;
private List<String>thisHand;
PokerHandValue(List<String> newHand) {
this.thisHand=newHand;
Collections.sort(thisHand);
int i = 0;
String card1 = thisHand.get(0);
String card1num = Character.toString(card1.charAt(0));
if (isJack(card1num)){
card1num = "11";
}
if (isQueen(card1num)){
card1num = "12";
}
if (isKing(card1num)){
card1num = "13";
}
if (isAce(card1num)){
card1num = "14";
}
int card1val = Integer.parseInt(card1num);
String card1face = Character.toString(card1.charAt(1));
String card2 = newHand.get(1);
String card2num = Character.toString(card2.charAt(0));
if (isJack(card2num)){
card2num = "11";
}
if (isQueen(card2num)){
card2num = "12";
}
if (isKing(card2num)){
card2num = "13";
}
if (isAce(card2num)){
card2num = "14";
}
int card2val = Integer.parseInt(card2num);
String card2face = Character.toString(card2.charAt(1));
String card3 = newHand.get(2);
String card3num = Character.toString(card3.charAt(0));
if (isJack(card3num)){
card3num = "11";
}
if (isQueen(card3num)){
card3num = "12";
}
if (isKing(card3num)){
card3num = "13";
}
if (isAce(card3num)){
card3num = "14";
}
int card3val = Integer.parseInt(card3num);
String card3face = Character.toString(card3.charAt(1));
String card4 = newHand.get(3);
String card4num = Character.toString(card4.charAt(0));
if (isJack(card4num)){
card4num = "11";
}
if (isQueen(card4num)){
card4num = "12";
}
if (isKing(card4num)){
card4num = "13";
}
if (isAce(card4num)){
card4num = "14";
}
int card4val = Integer.parseInt(card4num);
String card4face = Character.toString(card4.charAt(1));
String card5 = newHand.get(4);
String card5num = Character.toString(card5.charAt(0));
if (isJack(card5num)){
card5num = "11";
}
if (isQueen(card5num)){
card5num = "12";
}
if (isKing(card5num)){
card5num = "13";
}
if (isAce(card5num)){
card5num = "14";
}
int card5val = Integer.parseInt(card5num);
String card5face = Character.toString(card5.charAt(1));
String[] sortedHand = {card1, card2, card3, card4, card5};
if ((card1face.equals(card2face) && card1face.equals(card3face) && card1face.equals(card4face)
&& card1face.equals(card5face)) && (card1.equals(sortedHand[i]) && card2num.equals(sortedHand[i + 1])
&& card3num.equals(sortedHand[i + 2]) && card4num.equals(sortedHand[i + 3]) &&
card4num.equals(sortedHand[i + 4]))) {
this.value = 9;
this.result = "Straight Flush";
} else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1)))&&
(card1.charAt(1)==(card4.charAt(1))))||(((card2.charAt(1)==(card3.charAt(1)))&&(card2.charAt(1)==(card4.charAt(1)))&&
(card2.charAt(1)==(card5.charAt(1)))&&(card1.charAt(1)==(card5.charAt(1)))))){
this.value = 8;
this.result = "Four of a kind";
} else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1)))&&
((card4.charAt(1)==(card5.charAt(1)))))||((card1.charAt(1)==(card2.charAt(1)))
&&(card3.charAt(1)==(card4.charAt(1)))&&((card3.charAt(1)==(card5.charAt(1)))))){
this.value = 7;
this.result = "Full House";
} else if (card1face.equals(card2face) && card1face.equals(card3face) &&
card1face.equals(card4face) && card1face.equals(card5face)) {
this.value = 6;
this.result = "Flush";
} else if (card1.equals(sortedHand[i]) && card2num.equals(sortedHand[i + 1]) &&
card3num.equals(sortedHand[i + 2]) && card4num.equals(sortedHand[i + 3])
&& card4num.equals(sortedHand[i + 4])) {
this.value = 5;
this.result = "Straight";
} else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1))))||(
card3.charAt(0)==card4.charAt(0)&&card3.charAt(0)==card5.charAt(0))
) {
this.value = 4;
this.result = "Three of a kind";
} else if (((card1val == card2val || card1val == card3val || card1val == card4val || card1val == card5val) ||
(card2val == card3val || card2val == card4val || card2val == card5val) || (card3val == card4val ||
card3val == card5val) || (card4val == card5val)) && ((card1val == card2val || card1val == card3val ||
card1val == card4val || card1val == card5val) || (card2val == card3val || card2val == card4val ||
card2val == card5val) || (card3val == card4val || card3val == card5val) || (card4val == card5val))) {
this.value = 3;
this.result = "Two pair";
} else if ((card1val == card2val || card1val == card3val || card1val == card4val || card1val == card5val)
|| (card2val == card3val || card2val == card4val || card2val == card5val) ||
(card3val == card4val || card3val == card5val) || (card4val == card5val)) {
this.value = 2;
this.result = "Pair";
} else {
this.highCard=card1val;
if (card1val < card2val || card1val < card3val || card1val < card4val) {
this.highCard = card2val;
if (card2val < card3val || card2val < card4val || card2val < card5val) {
this.highCard = card3val;
if (card3val < card4val || card3val < card5val) {
this.highCard = card4val;
if (card4val < card5val) {
this.highCard = card5val;
}
}
}
this.value = 1;
this.result = "High Card of " + highCard;
}
}
}
int getValue(){
return value;
}
String getResult(){
return result;
}
int getHighCard() {
return highCard;
}
private boolean isJack(String card) {
if (card.equals("J")){
return true;
}
else return false;
}
private boolean isQueen(String card) {
if (card.equals("Q")){
return true;
}
else return false;
}
private boolean isKing(String card) {
if (card.equals("K")){
return true;
}
else return false;
}
private boolean isAce(String card) {
if (card.equals("A")){
return true;
}
else return false;
}}
My Cards class.
//Creating the cards and suits arrays
private int card,suit;
private static String[] CARDS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private static String[] SUITS = {"♠", "♣", "♦", "♥"};
private List<String> inOrderDeck=new ArrayList<>();
private List<String> shuffledDeck;
List <String> ShuffleDeck(){
//creating a deck that is in order of C2-CA,D2-DA,H2-HA,S2-SA
int n=52;
int cnt=0;
String[] deck = new String[n];
for (String CARD : CARDS) {
for (String SUIT : SUITS) {
deck[cnt] = (CARD + SUIT);
cnt++;
}
}
//Adding set Arrays to List
inOrderDeck.addAll(Arrays.asList(deck).subList(0, n));
//Shuffling the deck
List<String> shuffledDeck=new ArrayList<>(inOrderDeck);
inOrderDeck.removeAll(inOrderDeck);
Collections.shuffle(shuffledDeck);
return shuffledDeck;
}
}
每次调用 ShuffleDeck
时,都会向 inOrderDeck
添加 52 张卡片。这在第一手很好,但对于随后的每一手,牌的数量将增加 52,这将导致到最后一手开始时,牌组中大约有 270 张牌。
我看你有3个选择:
- 在 return 已洗牌的牌组之前清除未洗牌的牌组
ShuffledDeck
。
- 不要使用静态列表来存储你的未洗牌
每次调用
ShuffleDeck
- 分开
将未洗牌的牌组创建为
createDeck
方法,以及
在 ShuffleDeck
方法中只洗牌。
我会选择最后一个。
我正在构建一个程序来 运行 5 轮 5 张抽牌。我有一些代码,主要是我的 PokerHandValue
class.
PokerHandValue
class 唯一的问题是同时评估双手而不是单独评估。由于我测试了各种组件,我认为问题所在的 PokerHandClass
过于臃肿。如何为程序添加一些代码以仅将一只手作为一个对象进行评估?
我的主要class:
public static void main(String[] args) {
Cards deck = new Cards();
//setting loop for playing 5 hands
int playedHands = 5;
do {
//creating the deck and player hands
List<String> playableDeck = new ArrayList<>(deck.ShuffleDeck());
List<String> playerHand = new ArrayList<>(playableDeck.subList(0, 5));
for (int i = 0; i < 5; i++) {
playableDeck.remove(i);
}
List<String> dealerHand = new ArrayList<>(playableDeck.subList(6, 11));
for (int j = 0; j < 5; j++) {
playableDeck.remove(j);
}
//assessing the value of hand dealt to player
PokerHandValue playerValue = new PokerHandValue(playerHand);
int pValue = playerValue.getValue();
String pReason = playerValue.getResult();
int playerHigh = playerValue.getHighCard();
//assessing the value of hand dealt to player
PokerHandValue dealerValue = new PokerHandValue(dealerHand);
int dValue = dealerValue.getValue();
String dReason = dealerValue.getResult();
int dealerHigh = dealerValue.getHighCard();
//displaying the output and value and results of each hands
System.out.print("Black: " + playerHand.toString().replace("[", "").replace("]", ""));
System.out.println(" and White: " + dealerHand.toString().replace("[", "").replace("]", ""));
//determining the winner
if (dValue < pValue) {
System.out.println("White wins. - with a " + pReason + "\n");
} else if (pValue < dValue) {
System.out.println("Black wins. - with a " + dReason + "\n");
} else if (playerHigh == dealerHigh) {
if (playerHigh < dealerHigh) {
String stringDHighCard = determineFace(Integer.toString(dealerHigh));
System.out.println("Black wins. with a " + stringDHighCard + "\n");
} else {
String stringPHighCard = determineFace(Integer.toString(playerHigh));
System.out.println("White wins. - with a " + stringPHighCard + "\n");
}
} else System.out.println("Tie.\n");
playedHands--;
//Removing the deck and hands to reshuffle deck and re-deal
playableDeck.removeAll(playableDeck);
playerHand.removeAll(playerHand);
dealerHand.removeAll(dealerHand);
}
while (playedHands > 0);
}
static String determineFace(String faceValue){
String face=faceValue;
if (face.equals("11")) {
face = "J";
}
else if(face.equals("12")) {
face = "Q";
}
else if (face.equals("13")) {
face = "K";
}
else face = "A";
return face;
}
我的PokerHandValue
class评价个人牌局:
import java.util.*;
class PokerHandValue {
private int highCard;
private int value;
private String result;
private List<String>thisHand;
PokerHandValue(List<String> newHand) {
this.thisHand=newHand;
Collections.sort(thisHand);
int i = 0;
String card1 = thisHand.get(0);
String card1num = Character.toString(card1.charAt(0));
if (isJack(card1num)){
card1num = "11";
}
if (isQueen(card1num)){
card1num = "12";
}
if (isKing(card1num)){
card1num = "13";
}
if (isAce(card1num)){
card1num = "14";
}
int card1val = Integer.parseInt(card1num);
String card1face = Character.toString(card1.charAt(1));
String card2 = newHand.get(1);
String card2num = Character.toString(card2.charAt(0));
if (isJack(card2num)){
card2num = "11";
}
if (isQueen(card2num)){
card2num = "12";
}
if (isKing(card2num)){
card2num = "13";
}
if (isAce(card2num)){
card2num = "14";
}
int card2val = Integer.parseInt(card2num);
String card2face = Character.toString(card2.charAt(1));
String card3 = newHand.get(2);
String card3num = Character.toString(card3.charAt(0));
if (isJack(card3num)){
card3num = "11";
}
if (isQueen(card3num)){
card3num = "12";
}
if (isKing(card3num)){
card3num = "13";
}
if (isAce(card3num)){
card3num = "14";
}
int card3val = Integer.parseInt(card3num);
String card3face = Character.toString(card3.charAt(1));
String card4 = newHand.get(3);
String card4num = Character.toString(card4.charAt(0));
if (isJack(card4num)){
card4num = "11";
}
if (isQueen(card4num)){
card4num = "12";
}
if (isKing(card4num)){
card4num = "13";
}
if (isAce(card4num)){
card4num = "14";
}
int card4val = Integer.parseInt(card4num);
String card4face = Character.toString(card4.charAt(1));
String card5 = newHand.get(4);
String card5num = Character.toString(card5.charAt(0));
if (isJack(card5num)){
card5num = "11";
}
if (isQueen(card5num)){
card5num = "12";
}
if (isKing(card5num)){
card5num = "13";
}
if (isAce(card5num)){
card5num = "14";
}
int card5val = Integer.parseInt(card5num);
String card5face = Character.toString(card5.charAt(1));
String[] sortedHand = {card1, card2, card3, card4, card5};
if ((card1face.equals(card2face) && card1face.equals(card3face) && card1face.equals(card4face)
&& card1face.equals(card5face)) && (card1.equals(sortedHand[i]) && card2num.equals(sortedHand[i + 1])
&& card3num.equals(sortedHand[i + 2]) && card4num.equals(sortedHand[i + 3]) &&
card4num.equals(sortedHand[i + 4]))) {
this.value = 9;
this.result = "Straight Flush";
} else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1)))&&
(card1.charAt(1)==(card4.charAt(1))))||(((card2.charAt(1)==(card3.charAt(1)))&&(card2.charAt(1)==(card4.charAt(1)))&&
(card2.charAt(1)==(card5.charAt(1)))&&(card1.charAt(1)==(card5.charAt(1)))))){
this.value = 8;
this.result = "Four of a kind";
} else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1)))&&
((card4.charAt(1)==(card5.charAt(1)))))||((card1.charAt(1)==(card2.charAt(1)))
&&(card3.charAt(1)==(card4.charAt(1)))&&((card3.charAt(1)==(card5.charAt(1)))))){
this.value = 7;
this.result = "Full House";
} else if (card1face.equals(card2face) && card1face.equals(card3face) &&
card1face.equals(card4face) && card1face.equals(card5face)) {
this.value = 6;
this.result = "Flush";
} else if (card1.equals(sortedHand[i]) && card2num.equals(sortedHand[i + 1]) &&
card3num.equals(sortedHand[i + 2]) && card4num.equals(sortedHand[i + 3])
&& card4num.equals(sortedHand[i + 4])) {
this.value = 5;
this.result = "Straight";
} else if (((card1.charAt(1)==(card2.charAt(1)))&&(card1.charAt(1)==(card3.charAt(1))))||(
card3.charAt(0)==card4.charAt(0)&&card3.charAt(0)==card5.charAt(0))
) {
this.value = 4;
this.result = "Three of a kind";
} else if (((card1val == card2val || card1val == card3val || card1val == card4val || card1val == card5val) ||
(card2val == card3val || card2val == card4val || card2val == card5val) || (card3val == card4val ||
card3val == card5val) || (card4val == card5val)) && ((card1val == card2val || card1val == card3val ||
card1val == card4val || card1val == card5val) || (card2val == card3val || card2val == card4val ||
card2val == card5val) || (card3val == card4val || card3val == card5val) || (card4val == card5val))) {
this.value = 3;
this.result = "Two pair";
} else if ((card1val == card2val || card1val == card3val || card1val == card4val || card1val == card5val)
|| (card2val == card3val || card2val == card4val || card2val == card5val) ||
(card3val == card4val || card3val == card5val) || (card4val == card5val)) {
this.value = 2;
this.result = "Pair";
} else {
this.highCard=card1val;
if (card1val < card2val || card1val < card3val || card1val < card4val) {
this.highCard = card2val;
if (card2val < card3val || card2val < card4val || card2val < card5val) {
this.highCard = card3val;
if (card3val < card4val || card3val < card5val) {
this.highCard = card4val;
if (card4val < card5val) {
this.highCard = card5val;
}
}
}
this.value = 1;
this.result = "High Card of " + highCard;
}
}
}
int getValue(){
return value;
}
String getResult(){
return result;
}
int getHighCard() {
return highCard;
}
private boolean isJack(String card) {
if (card.equals("J")){
return true;
}
else return false;
}
private boolean isQueen(String card) {
if (card.equals("Q")){
return true;
}
else return false;
}
private boolean isKing(String card) {
if (card.equals("K")){
return true;
}
else return false;
}
private boolean isAce(String card) {
if (card.equals("A")){
return true;
}
else return false;
}}
My Cards class.
//Creating the cards and suits arrays
private int card,suit;
private static String[] CARDS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private static String[] SUITS = {"♠", "♣", "♦", "♥"};
private List<String> inOrderDeck=new ArrayList<>();
private List<String> shuffledDeck;
List <String> ShuffleDeck(){
//creating a deck that is in order of C2-CA,D2-DA,H2-HA,S2-SA
int n=52;
int cnt=0;
String[] deck = new String[n];
for (String CARD : CARDS) {
for (String SUIT : SUITS) {
deck[cnt] = (CARD + SUIT);
cnt++;
}
}
//Adding set Arrays to List
inOrderDeck.addAll(Arrays.asList(deck).subList(0, n));
//Shuffling the deck
List<String> shuffledDeck=new ArrayList<>(inOrderDeck);
inOrderDeck.removeAll(inOrderDeck);
Collections.shuffle(shuffledDeck);
return shuffledDeck;
}
}
每次调用 ShuffleDeck
时,都会向 inOrderDeck
添加 52 张卡片。这在第一手很好,但对于随后的每一手,牌的数量将增加 52,这将导致到最后一手开始时,牌组中大约有 270 张牌。
我看你有3个选择:
- 在 return 已洗牌的牌组之前清除未洗牌的牌组
ShuffledDeck
。 - 不要使用静态列表来存储你的未洗牌
每次调用
ShuffleDeck
- 分开
将未洗牌的牌组创建为
createDeck
方法,以及 在ShuffleDeck
方法中只洗牌。
我会选择最后一个。