通过单个阵列处理卡

时间:2014-04-30 23:23:19

标签: java compiler-errors

我必须编写一个程序来玩两个人类之间的扑克游戏。玩家。我有主要的方法(经销商)设置为要求第一个玩家的赌注,但我还没有能够弄清楚如何调用Deck类来处理交易方法。我试过了deck.deal()等,但似乎没有任何事情可以通过。我会发布下面涉及的课程,并且我已经注意到代码应该在经销商类中的牌组中调用的地方

public class Dealer
{
    private Deck deck;
    private Player[] players;
    private String n;

    Scanner scan = new Scanner(System.in);

    public static final int NUMBER_OF_PLAYERS = 2;

    /**
     * Default constructor - creates and populates the Player array and the
     * Deck object.
     */
   public Dealer()
   {
      players = new Player[ NUMBER_OF_PLAYERS ]; //IMPORTANT players is the array

      deck = new Deck();

    // populate the array of players

    } // constructor

    /**
     * Outermost level of abstraction for the poker game
     */
    public void play()
    {
      System.out.println("Welcome to Poker!");

      //Getting First Names
      System.out.println("\nEnter first player's name: ");
      String name = scan.nextLine(); 
      players[0] = new Player(name);
      players[0].getName(name);

      //Getting Second Name
      System.out.println("Enter second player's name: ");
      name = scan.nextLine();
      players[1] = new Player(name);
      players[1].getName(name);

      //First Player Goes
      System.out.println(players[0].getName(n) + "'s Turn\n");

      //Bet
      System.out.println("Enter bet: ");
      int bet = players[0].getBet(); //IMPORTANT players is the array and can call Player method as own

      //Able to bet?
      while(!players[0].bet(bet) || !players[1].bet(bet))
      { 
         bet = 20;
         if(!players[0].bet(bet) || !players[1].bet(bet))
         {
            bet = 1;
         }
      }

      //Draw Cards/Deck
      //*****NEED TO DEAL CARDS HERE*****
      //*****DEAL AN ARRAY OF CARD[5] TO EACH PLAYERS[0] AND [1]******

      //Compare

      //Add to winner

      //Add to loser

      //Winner Goes ^^



    } // method play
}    

public class Deck
{
    private int pointer = 0; // indicates the current position in the deck.
                         // This should begin with 0 (the first call)
                         // and increment every time a card is dealt.

   private Card deck[] = new Card[CARDS_IN_DECK];
   private Card tempDeck[] = new Card[CARDS_IN_DECK];
   private Card Cards[] = new Card[5]; 

    public static final int CARDS_IN_DECK = 52;


    /**
     * Instantiate an array of Cards and populate the array with 52 Card 
     * objects.  The face values of the cards should be between 2 - 14.  
     * Values 2 - 10 represent the number cards.  Values 11 - 14 represent 
     * Jack, Queen, King, and Ace, respectively.  The suits should be as 
     * follows: 100 = Clubs, 200 = Diamonds, 300 = Hearts, and 400 = Spades.  
     * See the Card class for more information.
     *
     * You should both shuffle and cut the cards before this method 
     * concludes.
     */
    public Deck()
    {
      int i = 0;
      for(int a = 1; a <= 5; a++)
      {
         for(int b = 2; b <=14; b++)
         {
            deck[i] = new Card(a,b);
         }
      }

      shuffle();
      cut();

    } // default constructor


    /**
     * Cut the deck.  Choose a point in the deck (this can be either random 
     * or fixed) and re-arrange the cards.  For example, if you choose to 
     * cut at position 26, then the 27th - 52nd cards will be placed in the 
     * 1st - 26th positions.  The 1st - 26th cards will be placed in the 
     * 27th - 52nd positions.
     */
    public void cut()
      {
         int cut = 26;
         int a = 0;
         int b = 0;

         for(int i = 0 ; i<cut; i++)
         {
            tempDeck[i] = new Card(a,b);
            tempDeck[i] = deck[i+26];
            tempDeck[i+26] = deck[i];
         }

         deck = tempDeck; 
      }

 // method cut

    /**
     * Deal 5 cards from the deck. Deal out the next 5 cards and return 
     * these in an array.  You will need to maintain a pointer that lets
     * you know where you are in the deck.  You should make sure also
     * to reshuffle and cut the deck and start over if there are not enough 
     * cards left to deal a hand.
     *
     * @return an array of 5 cards
     */
   public Card[] deal(int[] args)
   {
      int i = 0;
      int a = 0;
      int b = 0;
      Cards[i] = new Card(a,b);

      for(i = 0; i < 1; i++)
      {
         Cards[pointer] = deck[pointer];
         pointer++;
      }               

      return Cards; 
         // this is a stub only - replace this!!!!

    } // method deal


    /**
     * Get a card from the deck
     *
     * @param the position of the card you are retrieving
     * @return the card object
     */
   public Card getCard( int card )
   {
      Card oneCard = deck[pointer];
      deck[pointer] = null;
      pointer +=1;

      return oneCard; // this is a stub only - replace this!!!

   } // method getCard


    /**
     * Shuffle the deck.  Randomly re-arrange the cards in the deck. There
     * are plenty of algorithms for doing this - check out Google!!!
     */
      public void shuffle()
      {
         int i, j, k;
         int n = 15;

         for ( k = 0; k < n; k++ )
         {
          i = (int) ( CARDS_IN_DECK * Math.random() );  // Pick 2 random cards
          j = (int) ( CARDS_IN_DECK * Math.random() );  // in the deck

          tempDeck[j] = deck[i];
          tempDeck[i] = deck[j];
          deck = tempDeck;
       }

       pointer = 0;   // Reset current card to deal

      } // end shuffle

} // class Deck


public class Player
{
    private int bet;
    private int chips;
    private int totalChips = 0;

    private Hand hand;
    private String name;


    public static final int START_CHIPS = 100;
    public static final int WINNING_CHIPS = 200;

    Scanner scan = new Scanner(System.in);

    /**
     * Sets the player's name and the starting number of chips.
     *
     * @param the player's name
     */
   public Player( String n )
   {
         name = n;

         totalChips = START_CHIPS;

   } // constructor


    /**
     * Sets the amount of the bet and decreases the number of chips that 
     * the player has by the number of chips bet. Do not allow bets if 
     * there are not enough chips left.
     *
     * @param the number of chips bet
     * @return true if the bet was successful (there were enough chips)
     */
   public boolean bet( int bet )
   {
      int chipsAB;
      boolean canBet;

      //Get Bet
      getBet();

      //See if player has enough chips for bet
      if(totalChips >= bet)
      {
         canBet = true;
      }
      else
      {
         canBet = false;        
      }

      return canBet; // this is a stub only - replace this!!!!

   } // method bet


    /**
     * Return the number of chips bet
     *
     * @return the number of chips bet
     */              //DONE
    public int getBet()
    {
         int bet;

         bet = scan.nextInt();

         while (bet < 1 || bet > getChips())
         {
            System.out.println("Error. Re-enter bet: ");
            bet = scan.nextInt();
         }

         return bet; // this is a stub only - replace this!!!!

    } // method getBet

    /**
     * Return the number of chips currently held
     *
     * @return the number of chips held
     */
   public int getChips()
   {
      for(int i = 0; i<1; )
      {
         totalChips = START_CHIPS;
      }  


      return totalChips; // this is a stub only - replace this!!!!

   } // method getChips

   public int setChips()
   {
      int playersChips = getChips();
      return playersChips;
   }


    /**
     * Return the player's hand
     *
     * @return the player's hand object
     */
    public Hand getHand()
    {
        return new Hand(); // this is a stub only - replace this!!!!

    } // method getHand


    /**
     * Return the player's name
     *
     * @return the player's name
     */
   public String getName(String name)
   {
      String n = name;          
      return n; // this is a stub only - replace this!!!!

    } // method getName

    /**
     * Indicates whether this player has won
     *
     * @return true if the player has more than the number of winning points
     */
    public boolean hasWon()
    {
         boolean won = false;

         if(chips == 0)
         {
            won = true;
         }

        return won; // this is a stub - replace this!!!

    } // method hasWon


    /**
     * Set the Hand object to the incoming Hand object (this comes from the 
     * Dealer)
     *
     * @param the hand dealt by the dealer
     */
    public void setHand( Hand h )
    {


    } // method setHand


    /**
     * Return the player's name & the number of chips
     *
     * @return the players name & number of chips
     */
    public String toString()
    {
        String nameChips;
        nameChips = (name + totalChips);
        return nameChips; // this is a stub only - replace this!!!

    } // method toString


    /**
     * We won the hand, so increase chips
     *
     * @param the number of chips won
     */
    public int winHand()
    {
      int chipsAB = 0;
      //if(hand.beats(other))
      {
         chipsAB = getChips() + getBet();
      }
      //else
         chipsAB = getChips() - getBet();

      return chipsAB;

    } // method winHand


} // class Player

2 个答案:

答案 0 :(得分:0)

代码为我编译,但我不得不

  • 使用构造函数Card
  • 创建一个名为Card(int a, int b)的类
  • 创建一个名为Hand
  • 的类
  • 忽略Javadoc错误 在@param之后,您应该将参数的名称设为the ...

答案 1 :(得分:0)

主要方法不包括在内,但除此之外,它还会进行一些更改。

我想我可以让你朝着正确的方向前进,但我没有时间做所有的工作。我进一步使用Java集合来澄清/简化一些用途。这里有一堆代码更新。关键点是Suit enum,Collections.shuffle,使用ArrayLists进行卡座和指针,以及切割功能的更改。还有很多机会进行更多错误和边界检查。

在风格上,我建议考虑你的对象布局。经销商是玩家的专业化,洗牌和切割可能更适合经销商的方法(甲板不会对自己做这些事情)。无论如何,对于这次更新,我没有触及那些东西,只是需要注意的事情。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Dealer {
    public static final int NUMBER_OF_PLAYERS = 2;

    private Deck deck;
    private List<Player> players = new ArrayList<Player>(NUMBER_OF_PLAYERS);

    Scanner scan = new Scanner(System.in);

    /**
     * Default constructor - creates and populates the Player array and the Deck object.
     */
    public Dealer() {
        deck = new Deck();
    } // constructor

    /**
     * Outermost level of abstraction for the poker game
     */
    public void play() {
        System.out.println("Welcome to Poker!");

        // Getting First Names
        System.out.println("\nEnter first player's name: ");
        String name = scan.nextLine();
        players.add(new Player(name));

        // Getting Second Name
        System.out.println("Enter second player's name: ");
        name = scan.nextLine();
        players.add(new Player(name));

        // First Player Goes
        System.out.println(players.get(0).getName() + "'s Turn\n");

        // Bet
        System.out.println("Enter bet: ");
        int bet = players.get(0).getBet();

        // Able to bet?
        while (!players.get(0).bet(bet) || !players.get(1).bet(bet)) {
            bet = 20;
            if (!players.get(0).bet(bet) || !players.get(0).bet(bet)) {
                bet = 1;
            }
        }

        // Draw Cards/Deck
        // *****NEED TO DEAL CARDS HERE*****
        // *****DEAL AN ARRAY OF CARD[5] TO EACH PLAYERS[0] AND [1]******
        for (Player player : players) {
            player.setHand(deck.deal(5));
        }

        // Compare

        // Add to winner

        // Add to loser

        // Winner Goes ^^

    } // method play
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Player {
    private int chips;
    private int totalChips = 0;

    private List<Card> hand = new ArrayList<Card>();
    private String name;

    public static final int START_CHIPS = 100;
    public static final int WINNING_CHIPS = 200;

    Scanner scan = new Scanner(System.in);

    /**
     * Sets the player's name and the starting number of chips.
     * 
     * @param the
     *            player's name
     */
    public Player(String n) {
        name = n;
        totalChips = START_CHIPS;
    } // constructor

    /**
     * Sets the amount of the bet and decreases the number of chips that the player has by the number of chips bet. Do not allow
     * bets if there are not enough chips left.
     * 
     * @param the
     *            number of chips bet
     * @return true if the bet was successful (there were enough chips)
     */
    public boolean bet(int bet) {
        boolean canBet;

        // Get Bet
        getBet();

        // See if player has enough chips for bet
        if (totalChips >= bet) {
            canBet = true;
        } else {
            canBet = false;
        }

        return canBet; // this is a stub only - replace this!!!!

    } // method bet

    /**
     * Return the number of chips bet
     * 
     * @return the number of chips bet
     */
    // DONE
    public int getBet() {
        int bet;

        bet = scan.nextInt();

        while (bet < 1 || bet > getChips()) {
            System.out.println("Error. Re-enter bet: ");
            bet = scan.nextInt();
        }

        return bet; // this is a stub only - replace this!!!!

    } // method getBet

    /**
     * Return the number of chips currently held
     * 
     * @return the number of chips held
     */
    public int getChips() {
        for (int i = 0; i < 1;) {
            totalChips = START_CHIPS;
        }

        return totalChips; // this is a stub only - replace this!!!!

    } // method getChips

    public int setChips() {
        int playersChips = getChips();
        return playersChips;
    }

    /**
     * Return the player's hand
     * 
     * @return the player's hand object
     */
    public List<Card> getHand() {
        return hand;
    } // method getHand

    /**
     * Return the player's name
     * 
     * @return the player's name
     */
    public String getName() {
        return name;
    } // method getName

    /**
     * Indicates whether this player has won
     * 
     * @return true if the player has more than the number of winning points
     */
    public boolean hasWon() {
        return (chips > WINNING_CHIPS);
    } // method hasWon

    /**
     * Set the Hand object to the incoming Hand object (this comes from the Dealer)
     * 
     * @param the
     *            hand dealt by the dealer
     */
    public void setHand(List<Card> dealtHand) {
        hand = dealtHand;
    } // method setHand

    /**
     * Return the player's name & the number of chips
     * 
     * @return the players name & number of chips
     */
    public String toString() {
        return "Name: " + name + ", Chips: " + chips;
    } // method toString

    /**
     * We won the hand, so increase chips
     * 
     * @param the
     *            number of chips won
     */
    public int winHand() {
        int chipsAB = 0;
        // if(hand.beats(other))
        {
            chipsAB = getChips() + getBet();
        }
        // else
        chipsAB = getChips() - getBet();

        return chipsAB;

    } // method winHand

} // class Player


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Deck {
    public static final int CARDS_IN_DECK = 52;

    private List<Card> deck = new ArrayList<Card>(CARDS_IN_DECK);


    /**
     * Constructor
     * 
     * Instantiate an array of Cards and populate the array with 52 Card objects. The face values of the cards should be between 2 -
     * 14. Values 2 - 10 represent the number cards. Values 11 - 14 represent Jack, Queen, King, and Ace, respectively.
     * 
     * You should both shuffle and cut the cards before this method concludes.
     */
    public Deck() {
        for (Suit suit : Suit.values()) {
            // I made the Ace equal to 1, but it's just preference
            for (int value = 2; value <= 14; value++) {
                deck.add(new Card(suit, value));
            }
        }

        Collections.shuffle(deck);
        cut();
    } // default constructor

    /**
     * Cut the deck. Choose a point in the deck (this can be either random or fixed) and re-arrange the cards. For example, if you
     * choose to cut at position 26, then the 27th - 52nd cards will be placed in the 1st - 26th positions. The 1st - 26th cards
     * will be placed in the 27th - 52nd positions.
     */
    public void cut() {
        Random rand = new Random();
        int cutPoint = rand.nextInt(deck.size()) + 1;

        List<Card> newDeck = new ArrayList<Card>(deck.size());
        newDeck.addAll(deck.subList(cutPoint, deck.size()));
        newDeck.addAll(deck.subList(0, cutPoint));

        deck = newDeck;
    }

    // method cut

    /**
     * Deal 5 cards from the deck. Deal out the next 5 cards and return these in an array. You will need to maintain a pointer that
     * lets you know where you are in the deck. You should make sure also to reshuffle and cut the deck and start over if there are
     * not enough cards left to deal a hand.
     * 
     * @return an array of 5 cards
     */
    public List<Card> deal(int size) {
        List<Card> hand = new ArrayList<Card>(size);
        hand.add(deck.remove(0));
        return hand;
    } // method deal

    /**
     * Get a card from the deck
     * 
     * @param the
     *            position of the card you are retrieving
     * @return the card object
     */
    public Card getCard(int position) {
        if (position >= 0 && position < deck.size()) {
            return deck.remove(position);
        }
        else {
            return null; // or throw an exception, or print an error or whatever
        }
    } // method getCard
} // class Deck

public class Card {
    private Suit suit;
    private int value;

    public Card(Suit suit, int value) {
        this.suit = suit;
        this.value = value;
    }

    public Suit getSuit() {
        return suit;
    }

    public int getValue() {
        return value;
    }
}

public enum Suit {
    CLUBS, DIAMONDS, HEARTS, SPADES;
}