Java-扑克游戏玩家

时间:2016-04-30 03:44:44

标签: java

我有一个创建扑克游戏的任务,询问有多少人正在玩(1-7)以及他们是否想要玩5张牌或7.没有投注图等等。它只是一只手看到你有什么游戏。我需要让方法问题为gameType和numPlayers返回一个值。 5卡片组和7卡片组取决于从gameType变量返回的值。我不知道如何实现这一点......任何想法?

我还必须以某种方式将玩家数量整合到甲板上。

    import java.util.Scanner;

public class pokerGame {

    public static void main(String[] args) {
        Questions();
         if (Questions == 5){
            Deck5();}
            else{
              Deck7();}
    }

        public static int Questions() {
            int numPlayers, gameType;
            Scanner sc = new Scanner(System.in);
            System.out.print("How many people do we have playing poker today?  " + "Enter a number between 1 and 7: \n");
            numPlayers = sc.nextInt();
            System.out.println("Are you playing 7 car stud or 5 card stud?");
            gameType = sc.nextInt();
            return gameType;
        }

public static String Deck5() {
        int[] deck = new int[52];
        String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
        String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
        // Initialize cards
        for (int i = 0; i < deck.length; i++){
            deck[i] = i;
            String suit = suits[deck[i] / 13];
            String rank = ranks[deck[i] % 13];

        //show the deck in the original order
        System.out.println(deck[i] + ": " + rank + " of " + suit + "\n");
        }
        // Shuffle the cards
        for (int i = 0; i < deck.length; i++) {

            // Generate an index randomly
            int index = (int) (Math.random() * deck.length);
            int temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }

        // Display the 5 cards
        for (int i = 0; i < 5; i++) {
            String suit = suits[deck[i] / 13];
            String rank = ranks[deck[i] % 13];
            System.out.println(rank + " of " + suit);
        }

  public static String Deck7();
        int[] deck = new int[52];
        String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
        String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
        // Initialize cards
        for (int i = 0; i < deck.length; i++)
            deck[i] = i;

        // Shuffle the cards
        for (int i = 0; i < deck.length; i++) { 
            // Generate an index randomly
            int index = (int) (Math.random() * deck.length);
            int temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }

        // Display the 5 cards
        for (int i = 0; i < 7; i++) {
            String suit = suits[deck[i] / 13]; 
            String rank = ranks[deck[i] % 13];
            System.out.println(rank + " of " + suit);
        } 
    }
}

1 个答案:

答案 0 :(得分:1)

如果问题是问题类如何将两个整数返回到主类,那么你将无法做到。如果您唯一的问题是找到返回两个整数的方法,那么您可以为其创建一个单独的类,其中包含两个int变量,用于玩家数量和套牌大小。我真的不明白你的代码是如何工作的。您不要在主类中调用Deck5或Deck7类。

编辑: 您可以完全摆脱问题类,只需将问题移到主类。这样你就不用担心返回任何东西了。如果你真的需要问题类,那么就像我之前说的那样,用变量numPlayers和gameType创建一个单独的类“游戏”并将其返回给主类。

就玩家数量问题而言,我会将玩家数量作为参数传递给你的牌组方法。然后在deck方法中创建一个for循环,运行这段代码:

for (int i = 0; i < 7; i++) {
        String suit = suits[deck[i] / 13]; 
        String rank = ranks[deck[i] % 13];
        System.out.println(rank + " of " + suit);}

每位玩家一次。

此外,您在甲板上使用的代码效率不高。没有必要洗牌。相反,在生成用户卡时,只需在那里生成一个随机整数。