R. Brown R. Brown - 2 months ago 7
Java Question

Java- Poker Gamer

I have an assignment to create a poker game, that asks how many people are playing (1-7) and if they want to play 5 card stud or 7. Theres no betting drawing, etc. Its a simple one hand see what you have game. I need to have method Questions return a value for both gameType, and numPlayers. The 5 card deck and the 7 card deck depend on the value that is returned from the gameType variable. I don't know how to make this happen... any ideas?

I also have to somehow integrate number of players into the decks as well.

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);
}
}
}

Answer

If you're problem is how the Questions class would return two ints to your main class, you wouldn't be able to. If you're only problem is finding a way to return two ints, you could make a separate class for that with two int variables for the number of players and deck size. I don't really understand how your code is working though. You don't call either the Deck5 or Deck7 classes in the main class.

EDIT: You can get rid of the questions class altogether, and just move the questions to the main class. That way you don't need to worry about returning anything. If you really need the questions class, then like I said before, create a separate class "game" with the variables numPlayers and gameType and return it to the main class.

In terms of the number of players problem, I would pass the number of players as a parameter into your deck methods. Then create a for loop within the deck method that runs this bit of code:

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);}

once for each player.

Additionally, the code you used for the decks is not very efficient. There's no need to shuffle the deck. Instead, when generating the users cards, simply generate a random integer there.