更换牌的循环,日食中的扑克游戏

时间:2014-05-24 12:29:23

标签: java eclipse

这是我编写的代码,我正在尝试创建一个循环来使用多个卡片。我试图这样做,但总是有错误。我尝试在changeCard中插入循环(每次应该更换卡片的方法,直到玩家按0,例如)以及原则类。希望可以有人帮帮我。感谢

以下是我正在使用的课程。该代码仅适用于更换卡

package javapoker;
import java.util.Scanner;

public class Test {

public Test(String[] args)
{

    Deck deck= new Deck();
    Card C;

    System.out.println( deck.getTotalCards() );

   while (deck.getTotalCards()!=0 )
   {
       C = deck.drawFromDeck();
       System.out.println( C.toString() );
   }
} 

/**
 * @param args
 */
  public static void main(String[] args)  {
  //   for (int amount1=100; amount1>=0; amount1--){
  /*   {
      Deck deck= new Deck();
      Hand hand= new Hand(deck);
      hand.display(); //show the summary of the hand, e.g. "full house"
      hand.displayAll(); //look at all the individual cards in the hand         
  }*/
  /**
   *                CREAZIONE DI ELEMENTI
   */
Deck deck = new Deck();
Scanner user1InputScanner = new Scanner(System.in);
Scanner user2InputScanner = new Scanner(System.in);
Scanner bet1Scanner = new Scanner(System.in);
Scanner player2Input = new Scanner(System.in);
System.out.println("There are " + deck.getTotalCards() + " cards in the deck.\n");

/**
 *          FIELDS
 */
Chips chips = new Chips();
int amount1 = chips.getValue();
int amount2 = amount1;
int lose;
int victory;
int victory2;
Hand hand = new Hand(deck);

/**
 *          CREAZIONE DELLA PRIMA MANO
 */
System.out.println("Player1 cards: --> \n");
hand.display();
hand.displayAll();

/**
 *          BET DEL PRIMO GIOCATORE
 */
if(amount1 > 0) {
System.out.println("\nPlayer1, you have " + chips + " $");
System.out.println("\nPlayer1, how much do you want to bet? ");
int betPlayer1 = bet1Scanner.nextInt();
int afterBet = amount1 - betPlayer1;
int afterBet2 = -afterBet + afterBet; //Nel caso in cui sono state puntate troppe chip rispetto a quelle che si hanno gia
/**
 *          NEL CASO IN CUI VENGONO PUNTATI 0$ o meno
 */

if(afterBet < 0){
    System.out.println("You cannot bet so much!");
    System.out.println("\nPlayer1, now you have " + afterBet2 + " $!");


}else{ //When afterBet > 0
    if(afterBet == amount1){ //Se non sono state scommesse chips
        System.out.println("Player 1 decides to check");}
        System.out.println("\nPlayer1, now you have " + afterBet + " $!");

    if(afterBet == amount1){
        System.out.println("Player 1 decides to check");}
    }

    /**
     *      TENTATIVO DI LOOP PER SCOMMETTERE NEL CASO IN CUI IL PLAYER SCOMMETTA PIU DEL NORMALE 
     */



/**
 *          CAMBIO DI CARTA
 */
System.out.print("\nPlayer1, which card do you want to change? ");

/**
 *          INPUT DELLA CARTA DA CAMBIARE NELLA PRIMA MANO E CAMBIO
 */

int numberOfCard1 = user1InputScanner.nextInt();
    hand.changeCard(numberOfCard1, deck);

/**
 *          CREAZIONE SECONDA MANO
 */

Hand hand2 = new Hand(deck);    
System.out.println("\nPlayer2 cards are: --> \n");
hand2.display();
hand2.displayAll();
System.out.println("\nPlayer2 you have " + chips + " $\n");

/**
 *          DECISIONE DEL SECONDO GIOCATORE DI PASSARE O GIOCARE LA MANO
 */
System.out.println("If you want to play write \"call\" and bet " + amount1 + "$. If you want to pass write \"pass\" ");
String input = player2Input.nextLine();

/**
 *          NEL CASO IN CUI IL GIOCATORE DECIDE DI PASSARE LA MANO...
 */
if(input.equals("pass")){
    System.out.println("\nPlayer2 pass the hand then Player1 won!\n");
    //Se il giocatore 2 passa la mano, i due giocatori non perdono e non ricevono nulla in cambio
    System.out.println("Player1 has " + (amount1 + 5) + "$ and Player2 has " + amount2 + "$");

}

/**
 *          NEL CASO IN CUI IL GIOCATORE DECIDE DI GIOCARE LA MANO
 */

else if(input.equals("call")){
    if(afterBet <0){
    System.out.println("\nPlayer2, now you have " + afterBet2 + " $\n");}
    else{
        System.out.println("\nPlayer2, now you have " + afterBet + " $\n");
    }
    System.out.print("\nPlayer 2, which card do you want to change? ");

    /**
     *      SCELTA DELLA CARTA DA CAMBIARE E CAMBIO 
     */

    int numberOfCard2 = user2InputScanner.nextInt();

    hand2.changeCard(numberOfCard2, deck);
    hand2.display();
    /**
     *      MANI CAMBIATE 
     */

System.out.println("\nThe Player1 cards are: --> \n");
hand.display();
hand.displayAll();
System.out.println("\nThe Player2 cards are: --> \n");
hand2.display();
hand2.displayAll();
System.out.println("\nIn the deck there are " + deck.getTotalCards()+" cards\n");

    /**
     *      EVALUATE E VINCITA DEI GIOCATORI, VITTORIA DEL GIOCATORE 1 E SCOMMESSA VINTA
     */
if(afterBet < 0){
  if (hand.compareTo(hand2) == -1) {
    System.out.println("Player1 won the game!\n");
    victory = amount1 + betPlayer1 + afterBet;
    lose = 0;
    System.out.println("Player 1 has " + victory + "$");
    System.out.println("Player 2 has " + lose + "$");
  }

  /**
   *        VITTORIA DEL GIOCATORE 2 CON PREMIO PER IL GIOCATORE 2
   */

  else if (hand.compareTo(hand2) == 1) {
    System.out.println("Player2 won the game!\n");
    lose = 0;
    victory = amount1 + betPlayer1 + afterBet;
    System.out.println("Player 1 has " + lose + "$");
    System.out.println("Player 2 has " + victory + "$");

  }

  /**
   *        PAREGGIO (NEL CASO DI MANI UGUALI) CON DIVISIONE DEL PIATTO
   */
  else if (hand.compareTo(hand2) == 0){
      System.out.println("Player1 and Player2 won both, the bet will be split!\n");
      System.out.println("Player 1 has " + amount1 + "$");
      System.out.println("Player 2 has " + amount2 + "$");

  }
}

/**
 *          SE IL GIOCATORE 1 HA GIOCATO UNA SCOMMESSA MINORE DELLE SUE CHIPS 
 */
if(afterBet > 0){
    if (hand.compareTo(hand2) == -1) {
        System.out.println("Player1 won!");
        victory = amount1 + betPlayer1;
        lose = amount2 - betPlayer1;
        System.out.println("Player 1 has " + victory + "$");
        System.out.println("Player 2 has " + lose + "$");
        amount1 = victory;
        amount2 = lose;
      }

      /**
       *        VITTORIA DEL GIOCATORE 2 CON PREMIO PER IL GIOCATORE 2
       */

      else if (hand.compareTo(hand2) == 1) {
        System.out.println("Player2 won!");
        lose = amount1 - betPlayer1;
        victory = amount2 + betPlayer1;
        System.out.println("Player 1 has " + lose + "$");
        System.out.println("Player 2 has " + victory + "$");
        amount1 = lose;
        amount2 = victory;
      }

      /**
       *        PAREGGIO (NEL CASO DI MANI UGUALI) CON DIVISIONE DEL PIATTO
       */
      else if (hand.compareTo(hand2) == 0){
          System.out.println("Player1 and Player2 won, the bet will be split!\n");
          int split = betPlayer1 / 2;
          victory = amount1 + split;
          victory2 = amount2 + split;
          System.out.println("Player 1 has " + victory + "$");
          System.out.println("Player 2 has " + victory2 + "$");
          amount1 = victory;
          amount2 = victory2;
}
}
}
}
}
}

如果评论是我的语言,我很抱歉。我发布的课程的原则是,此时在eclipse中作为命令行工作。 Hand类(包含方法changeCard的类)如下:

package javapoker;
public class Hand {
private Card[] cards;
protected int[] value;

Hand(Deck d)
{
    value = new int[6];
    setCards(new Card[5]);
    for (int x=0; x<5; x++)
    {
        getCards()[x] = d.drawFromDeck();
    }

    int[] ranks = new int[14];
    //miscellaneous cards that are not otherwise significant
    int[] orderedRanks = new int[5];
    boolean flush=true, straight=false;
    int sameCards=1,sameCards2=1;
    int largeGroupRank=0;
    int smallGroupRank=0;
    int index=0;
    for (int x=0; x<=13; x++)
    {
        ranks[x]=0; 
    }
    for (int x=0; x<=4; x++)
    {
        ranks[ getCards()[x].getRank() ]++;
    }
    for (int x=0; x<4; x++) {
        if ( getCards()[x].getSuit() != getCards()[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;
            break;
        }
    }

    if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && 
        ranks[13]==1 && ranks[1]==1) //ace high
    {
        straight=true;
    }

    for (int x=0; x<=5; x++)
    {
        value[x]=0;
    }


    //start hand evaluation

    if ( sameCards==1 ) {     //if we have no pair...
        value[0]=1;         //this is the lowest type of hand, so it gets the lowest value
        value[1]=orderedRanks[0];    //the first determining factor is the highest card,
        value[2]=orderedRanks[1];    //then the next highest card,
        value[3]=orderedRanks[2];
        value[4]=orderedRanks[3];
        value[5]=orderedRanks[4];
    }

    if (sameCards==2 && sameCards2==1)       //if 1 pair
    {
        value[0]=2;              //pair ranked higher than high card
        value[1]=largeGroupRank; //rank of pair
        value[2]=orderedRanks[0];   //next highest cards.
        value[3]=orderedRanks[1];
        value[4]=orderedRanks[2];
    }

    if (sameCards==2 && sameCards2==2) //two pair
    {
        value[0]=3;
        //rank of greater pair
        value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
        value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
        value[3]=orderedRanks[0];  //extra card
    }

    if (sameCards==3 && sameCards2!=2)
    {   //three of a kind (not full house)
        value[0]=4;
        value[1]= largeGroupRank;
        value[2]=orderedRanks[0];
        value[3]=orderedRanks[1];
        //if we have two straights, 
        //the one with the highest top cards wins
    }

    if (straight && !flush)
    {
        value[0]=5;
        value[1]= orderedRanks[0]; //CONTROLLA    //VICKY
    }

    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)  //full house
    {
        value[0]=7;
        value[1]=largeGroupRank;
        value[2]=smallGroupRank;
    }

    if (sameCards==4)       //poker
    {
        value[0]=8;
        value[1]=largeGroupRank;
        value[2]=orderedRanks[0];
    }

    if (straight && flush)
    {
        value[0]=9;
        value[1]= largeGroupRank; //CONTROLLA    //VICKY
    }

}

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]) + " and " + 
                            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;
    //this just moves the output over a little in the console 
    //so its easier to see when viewing the output
    System.out.println(s);
}

void displayAll()
{
    for (int x=0; x<5; x++)
        System.out.println(getCards()[x]);
}

int compareTo(Hand that)
{
    for (int x=0; x<5; x++)
    {
        if (this.value[x]>that.value[x])
            return -1;
        else if (this.value[x]<that.value[x])
            return 1;
        else if (this.value[x] == that.value[x])
            return 0;
    }
    return 0; //if hands are equal
}

void changeCard(int i, Deck d){
    {
        if(i==0){
            System.out.println("\nThe player decides to not change cards");         
        }
        if (i > 5) {
            System.out.print("\n Card not found! \n");
        }else{
            getCards() [i] = d.drawFromDeck();
        }
    }
}

public Card[] getCards() {
    return cards;
}

public void setCards(Card[] cards) {
    this.cards = cards;
}
  }

看起来像这样,代码可以工作,但每个玩家只需一次更改。我想我需要一个循环,所以Player1和Player2可以更改一张牌(例如,更换一张牌 - &gt;显示新手牌 - &gt;决定改变或停在那里并传递给下一位牌手或到比较)。希望一切都清楚正确。感谢

我还插入另一段代码:

    /**
     *          INPUT DELLA CARTA DA CAMBIARE NELLA PRIMA MANO E CAMBIO
     */
    boolean change = true;

    if(change == true){

        int numberOfCard1 = user1InputScanner.nextInt();
        hand.changeCard(numberOfCard1, deck);
        hand.displayAll();
        System.out.print("\nPlayer1, which card do you want to change? ");
        if(numberOfCard1 != 0){
            change = true;
        }else{
            change = false;
        }
    }else{


    /**
     *          CREAZIONE SECONDA MANO
     */

换卡。当我尝试插入一个数字时,它会更改卡片,但随后会转到程序的末尾

0 个答案:

没有答案