这是我的甲板课程:
import java.util.Random;
public class Deck
{
private Card[] myCards;
private int numCards;
// This constructor builds a deck of 52 cards.
public Deck()
{
int c = 0;
this.numCards = 52;
this.myCards = new Card[this.numCards];
for(int i = 0; i < 4; i++){
for(int j = 1; j <= 13; j++){
this.myCards[c] = new Card (i, j);
c++;
}
}
}
public Card deal()
{
Card top = this.myCards[0];
for (int c=1; c<this.numCards; c++){
this.myCards[c-1]=this.myCards[c];
}
this.myCards[this.numCards-1]=null;
this.numCards--;
return top;
}
public boolean isEmpty()
{
if (numCards == 0 ){
return true;
} else {
return false;
}
}
public void shuffle(){
Random rng = new Random();
Card temp;
int j;
for(int i = 0; i < this.numCards; i++){
j = rng.nextInt(this.numCards);
temp = this.myCards[i];
this.myCards[i]= this.myCards[j];
this.myCards[j] = temp;
}
}
public void printDeck(int numToPrint){
for (int c = 0; c<numToPrint; c++){
System.out.printf("% 3d/%d %s \n", c+1 ,this.numCards, this.myCards[c].toString());
}
System.out.printf("\t\t[%d other]", this.numCards- numToPrint);
}
}
这是我的卡类
// This class represents one playing card.
public class Card{
// Card suits (provided for your convenience - use is optional)
public static final int SPADES = 0;
public static final int HEARTS = 1;
public static final int CLUBS = 2;
public static final int DIAMONDS = 3;
// Card faces (provided for your convenience - use is optional)
public static final int ACE = 1;
public static final int TWO = 2;
public static final int THREE = 3;
public static final int FOUR = 4;
public static final int FIVE = 5;
public static final int SIX = 6;
public static final int SEVEN = 7;
public static final int EIGHT = 8;
public static final int NINE = 9;
public static final int TEN = 10;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
// define fields here
private int suit;
private int face;
private Suit mySuit;
private int cardValue;
public Card(int cardSuit, int cardFace)
{
this.suit = cardSuit;
this.face = cardFace;
}
// This method retrieves the suit (spades, hearts, etc.) of this card.
public int getSuit()
{
return suit;
}
// This method retrieves the face (ace through king) of this card.
public int getFace()
{
return face;
}
public int getValue()
{
if (face>=2 && face<=10){
return face;
} else if (face>= 11 && face<=13){
return 10;
} else {
return 1;
}
}
public String toString(){
String str = "Error";
switch(this.face){
case 1:
str = " Ace";
break;
case 2:
str = "Two";
break;
case 3:
str = "Three";
break;
case 4:
str = " Four";
break;
case 5:
str = "Five";
break;
case 6:
str = "Six";
break;
case 7:
str = " Seven";
break;
case 8:
str = "Eight";
break;
case 9:
str = "Nine";
break;
case 10:
str = " Ten";
break;
case 11:
str = "Jack";
break;
case 12:
str = "Queen";
break;
case 13:
str = " King";
break;
}
return str + " of " + this.suitToString(suit);
}
public String suitToString(int a ){
String b;
if(a == 0){
b = "Spades";
return b;
}else if (a == 1){
b = "Hearts";
return b;
}else if (a == 2){
b = "Clubs";
return b;
}else {
b = "Diamonds";
return b;
}
}
}
这是我的Player类:
public class Player {
public String name;
private double bankRoll;
private Card[] hand;
private double currentBet;
private int numCards = 0;
public double getCurrentBet() {
return currentBet;
}
public void setCurrentBet(double currentBet) {
this.currentBet = currentBet;
}
public double getBankRoll() {
return bankRoll;
}
public Card[] getHand() {
return hand;
}
public Player(String name, double amount){
this.name = name;
this.bankRoll = amount;
this.currentBet = 0;
this.hand = new Card[11];
}
public void resetHand(){
this.hand = new Card[11];
this.numCards = 0;
}
public String toString(){
return this.name + " - $" + this.bankRoll + printHand();
}
public void addCard2(Card c){
this.hand[this.numCards] = c;
numCards ++;
}
public String printHand(){
String handString = "";
for (int i = 0; i<this.numCards; i++){
handString += this.hand[i] + ";" ;
}
return handString;
}
public boolean addCard(Card aCard){
if(this.numCards ==10){
System.err.printf("%s's hand already has 10 cards\n",
this.name );
System.exit(1);
}
this.hand[this.numCards]=aCard;
this.numCards++;
return(this.getHandSum()<=21);
}
public int getHandSum(){
int handSum = 0;
int cardNum;
int numAces = 0;
for (int c=0; c<this.numCards; c++){
cardNum = this.hand[c].getValue();
if(cardNum == 1){
numAces++;
handSum+=11;
} else if (cardNum > 10){
handSum+=10;
}else {
handSum += cardNum;
}
}
while (handSum >21 && numAces > 0){
handSum -= 10;
numAces --;
}
return handSum;
}
public void showHand(boolean showFirstCard){
System.out.printf("%s's cards:", this.name);
System.out.println();
for(int c = 0; c<this.numCards; c++){
if(c==0 && !showFirstCard){
System.out.println(" [hidden]");
} else {
System.out.printf(" %s", this.hand[c].toString());
System.out.println();
}
}
}
private int face;
public int getFace()
{
this.face = face;
return face;
}
}
这是我的主要游戏循环:
import java.util.Scanner;
public class Blackjack {
public static void main(String[] args) {
Deck myDeck = new Deck();
myDeck.shuffle();
System.out.print("Enter number of Players (Max 6): ");
int numPlayers = IO.readInt();
double[] playersBank = new double[numPlayers];
if (numPlayers<0 || numPlayers>6){
IO.reportBadInput();
}
Player[] player = new Player[numPlayers];
Player dealer = new Player ("Dealer", 0);
for (int i =0; i<numPlayers; i++){
System.out.print("Enter player's name: ");
String playerName = IO.readString();
System.out.println();
System.out.print("Enter player's starting balance:");
double startingBalance = IO.readDouble();
System.out.println();
playersBank[i]= startingBalance;
Player a;
a = new Player(playerName, startingBalance);
player[i] = a;
System.out.print(player[i]+ " ");
System.out.println();
}
double currentBet;
double[] bets = new double[player.length];
for (int i=0;i<player.length; i++){
System.out.println(player[i]+" "+"enter current bet:");
currentBet = IO.readDouble();
if(currentBet> player[i].getBankRoll()){
System.out.println("That is greater than the amount in your account");
return;
}
bets[i]= currentBet;
}
for(int i = 0;i<player.length; i++){
player[i].addCard(myDeck.deal());
player[i].addCard(myDeck.deal());
player[i].showHand(true);
System.out.println(player[i].getHandSum());
}
for(int i=0; i<1;i++){
dealer.addCard(myDeck.deal());
dealer.addCard(myDeck.deal());
dealer.showHand(false);
}
for(int i=0; i<player.length; i++){
dealer.showHand(false);
if(dealer.getHandSum() - 10 == 11){
player[i].showHand(true);
System.out.println("Would you like insurance? (y) or (n)");
String ans = IO.readString();
ans.toLowerCase();
if(ans.equalsIgnoreCase("y")){
System.out.println("How much would you like to bet?");
int answer = IO.readInt();
if(dealer.getHandSum() == 21){
playersBank[i] -= bets[i];
}
}
}
}
for(int i=0; i<player.length;i++){
int numCards = 2;
boolean playerDone = false;
boolean dealerDone = true;
String ans = "";
boolean doubleDown;
if(player[i].getBankRoll() - bets[i] > 0){
System.out.println(player[i].name+" Your hand value is: "+ player[i].getHandSum());
System.out.println("Would you like to double down? (Y or N)");
ans = IO.readString();
ans.toLowerCase();
if(ans.equalsIgnoreCase("y") ){
player[i].addCard(myDeck.deal());
bets[i]= bets[i]*2;
doubleDown = true;
player[i].showHand(true);
playerDone = true;
}}else {break;}
System.out.println(player[i].name+" Your hand value is: "+ player[i].getHandSum());
System.out.println();
System.out.println("Enter 1 to hit 2 to stay");
System.out.println(player[i].getHandSum());
int answer = IO.readInt();
while(answer == 1 && player[i].getHandSum()<21){
player[i].addCard(myDeck.deal());
player[i].showHand(true);
if(player[i].getHandSum() > 21){
System.out.println("You busted");
playerDone = true;
break;
}
System.out.println();
System.out.println(player[i].name + "Your hand value is: " + player[i].getHandSum());
System.out.println();
System.out.println("Enter 1 to hit 2 to stay");
answer = IO.readInt();
if(player[i].getHandSum() > 21){
System.out.println("You busted");
playerDone = true;
}else if (answer == 2){
playerDone = true;
dealerDone = true;
continue;
}
}
boolean allPlayersDone = true;
dealerDone = false;
if(allPlayersDone && !dealerDone){
dealer.showHand(true);
System.out.println();
if(dealer.getHandSum()==21 && dealer.getHand().length == 2){
System.out.println("Dealer has blackjack");
dealerDone=true;
}else while(dealer.getHandSum()<17){
dealer.addCard(myDeck.deal());
dealerDone = false;
}}else if(dealer.getHandSum()>=17 && dealer.getHandSum()<22){
System.out.println("Dealer Stays.");
System.out.print(dealer + "Hand value is:" + dealer.getHandSum());
dealerDone = true;
}
}
boolean winCheck = true;
if(winCheck == true){
for (int i=0; i<player.length;i++){
int playerValue = player[i].getHandSum();
int dealerValue = dealer.getHandSum();
player[i].showHand(true);
dealer.showHand(true);
System.out.println();
if(playerValue > 21){
System.out.println(player[i].name + " You Busted You Lose " + bets[i] + " will be deducted from your account." + " $"+ playersBank[i] + " is left in your account");
double temp = playersBank[i] - bets[i];
playersBank[i] = temp;
}
if(dealerValue > playerValue){
System.out.println( player[i].name + " You lose "+ bets[i]+ " Will be deducted from your account" + " $"+ playersBank[i] + " is left in your account");
double temp = playersBank[i]-bets[i];
playersBank[i] = temp;
}else if(dealerValue > 21 && playerValue <= 21 ){
System.out.println(player[i].name + " You win "+ bets[i]+ " Will be added to your account" + " $"+ playersBank[i] + " is left in your account");
double temp = playersBank[i]+bets[i];
playersBank[i] = temp;
}else if(playerValue==dealerValue){
System.out.println(player[i] + " Push: Bet is unchanged"+ " $"+playersBank[i] + " is left in your account");
}
}
}
}
}
我必须采用在大多数赌场二十一点游戏中常见的Split手部功能。我遇到的问题是调用并比较玩家手中的两张牌。每当我尝试调用作为Player对象一部分的Card数组的元素时,它就会给我带来错误。该计划也不会扣除或添加投注到玩家的余额。
我还需要在每个玩家玩游戏时为其添加一个基本的统计和提示系统。
任何帮助我如何使这个更有效或任何你看到的错误将不胜感激。
答案 0 :(得分:0)
直到我放弃的地方,这些是我的笔记。我放弃了,因为糟糕的格式化正在破坏我阅读代码的努力。
另外,在我看来,格式较差的地区也是包含最荒谬的代码的地区,所以我开始怀疑有一些可疑的东西,比如可能涉及两个程序员,一个人不明白蹲下试图完成另一个可能理解了几件事的人的工作。
我建议使用ArrayList<Card>
而不是Card[]
,但如果您的作业要求数组,那么很好,坚持使用数组。
我建议你一直停止使用this.
,但如果你的教授要求你,那么哦,你能做什么。
您的整个isEmpty()
方法可以编码为{ return numCards == 0; }
,但您所拥有的并没有错,这只是愚蠢。
你的西装和面孔应该是enum
而不是大的常数组。这不是一个错误,但你的面部常量从1而不是0开始的事实可能会导致问题。
您的suitToString()
应该使用switch
语句实现。 (不是错误。)
你的Card.mySuit和Card.cardValue会员毫无意义,但他们似乎并没有被使用。
Card.mySuit似乎属于Suit
类型。那么,最后,您是否有Suit
枚举?你在向我们隐瞒什么吗?
在printHand()
中你真的应该使用StringBuilder
。但是,不是错误。
如果您遇到错误, 不 System.err.printf()
和exit()
。而是抛出异常。但是,不是错误。
以下代码没有意义:
private int face;
public int getFace()
{
this.face = face;
return face;
}
和 对于玩家来说,它甚至没有任何意义。 幸运的是,它永远不会被使用。摆脱它。
就这样你知道,这个:
Player a;
a = new Player(playerName, startingBalance);
player[i] = a;
简化为:
Player a = new Player(playerName, startingBalance);
player[i] = a;
简化为:
player[i] = new Player(playerName, startingBalance);
请不要浪费时间强迫我们阅读无意识的代码行。
在main()
中你声明了一些double[] playersBank
,但每个玩家都有自己的bankRoll
,这对我没有任何意义。我可能错了,但我认为其中一个需要去。我建议摆脱playersBank
。这可能是一个错误。
是的,当然程序没有扣除或添加投注到玩家的余额,你还没有编写代码来做到这一点。但对于已经编写了如此多代码的人来说,这应该是微不足道的。您有没有机会从其他人那里复制此代码,而不知道它是如何工作的以及它想要做什么?
无论如何,在bets[i]= currentBet;
附近,你需要从玩家的余额中减去currentBet
。这是一个错误。
另外,我建议丢失bets[]
数组并将currentBet
移到Player
类。但那不是错误。
对于分手功能,您可能需要将Card[] splitHand
成员添加到Player
,如果非空,则表示玩家已分手。而且您需要编写要求它的代码,然后执行它。我们在这里发现人们遇到的问题&#39;代码和建议解决方案,我们通常不会写人。代码。