JAVA Poker Simulator Project! Help :)

NeXuS1

Fully Optimized
Messages
2,960
Well here are the instructions

Poker Simulator:

In this assignment, you will implement a simulation of a popular casino game usually called video poker. The card deck contains 52 cards, 13 of each suit. At the beginning of the game, the deck is shuffled. You need to devise a fair method for shuffling. (It does not have to be efficient.) Then the top five cards of the deck are presented to the player. The player can reject none, some, or all of the cards. The rejected cards are replaced from the top of the deck. Now the hand is score. At the very minimum, your project should have at least a Card class and a driver class. Your program should pronounce it to be one of the following:

* No pair - The lowest hand, containing five separate cards that do not match up to create any of the hands below.
* One pair - Two cards of the same value, for example two queens.
* Two pairs - Two pairs, for example two queens and two 5's.
* Three of a kind - Three cards of the same value, for example three queens.
* Straight - Five cards with consecutive values, not necessarily of the same suit, such as 4, 5, 6, 7, and 8. The ace can either precede a 2 or follow a king.
* Flush - Five cards, not necessarily in order, of the same suit.
* Full House - Three of a kind and a pair, for example three queens and two 5's
* Four of a Kind - Four cards of the same value, such as four queens
* Straight Flush - A straight and a flush: Five cards with consecutive values of the same suit
* Royal Flush - The best possible hand in a poker. A 10, jack, queen, king, and ace, all of the same suit.

If you are so inclined, you can implement a wager. The player pays a JavaDollar for each game, and wins according to the
following payout chart:
Hand Payout

Royal Flush 250
Straight Flush 50
Four of a Kind 25
Full House 6
Flush 5
Straight 4
Three of a kind 3
Two Pair 2
Pair of Jacks 1
or Better

Code located at my last post
V
V
V
 
I wrote a similar program to this, and mine looked similar to yours but I realised something. In your example, it won't be a fair deal because, theoretically, you could be dealt the same card over and over, you could get 6 3 of hearts because the inital array of values is not being modified. You may want to consider a multi-dimensional array instead of two separate "suit" and "rank" array's. However, after working through it, this is the most efficient way I can think to do it, and at least for the first part, you have to use a multi-dimensional array. It doesn't play yet, but you should be able to make the game playable with a workable deck of cards. Hope this helps.
Code:
import java.util.Random;
import java.util.Scanner;
public class cardsGame {
 private String deck[][] = new String[13][4]; // basic deck of cards
 // in this array, each of the 13 cards will have it's own suit
 private String suits[][] = new String[13][4]; // this is used to output suits
 private String shuffledDeck[] = new String[52]; // this is the deck that is dealt from
 private String shuffledSuit[] = new String[52]; // this is used to keep track of suits
 private String playerCards[] = new String[5]; // this is used to keep track of player's cards
 private String playerSuits[] = new String[5]; // this is used to keep track of player's card's suits
 private static final int BANK = 50;
 private int myBank;
 private int card;
 private int suit;
 private int wager = 0;
 public cardsGame() {
  play();
 } // end default no-argument constructor
 public void populateDeck() {
  for (int x = 0; x <13; x++) {
   for (int y = 0; y<4; y++) {
	switch(x) {
	 case 9:
	  deck[x][y] = "J";
	  break;
	 case 10:
	  deck[x][y] = "Q";
	  break;
	 case 11:
	  deck[x][y] = "K";
	  break;
	 case 12:
	  deck[x][y] = "A";
	  break;
	 default:
	  deck[x][y] = "" + (x+2);
	  break;
	} // end switch
	switch(y) {
	 case 0:
	  suits[x][y] = "Spades";
	  break;
	 case 1:
	  suits[x][y] = "Clubs";
	  break;
	 case 2:
	  suits[x][y] = "Diamonds";
	  break;
	 case 3:
	  suits[x][y] = "Hearts";
	  break;
	} // end switch
   } // end for
  } // end for
 } // end method populateDeck
 public void shuffle() {
  for (int x = 0; x<52; x++) {
   do {
	card = (int)(13*Math.random());
	suit = (int)(4*Math.random());
   } while(deck[card][suit].equals("0"));
   //playerDeck[card][suit]
   shuffledDeck[x] = deck[card][suit];
   shuffledSuit[x] = suits[card][suit];
   deck[card][suit] = "0";
  } // end for
 } // end method shuffle
 public void play() {
  Scanner input = new Scanner(System.in);
  myBank = BANK;
  populateDeck();
  shuffle();
  System.out.printf("\n\t%s\n\n%s\n\n%s\n","Welcome to Java Poker!", "You can press <ctrl> z at any time to exit the program.","Please enter your wager:");
  deal(); // deal cards
  System.out.print("You have " + myBank + " credits.\n");
  System.out.printf("Amount:");
  while((getWager() <=10 && getWager() >0) && getWager() <= myBank) {
   wager = input.nextInt();
  }

 } // end method play
 public int getWager() {
  return wager;
 } // end method getWager
 public void deal() {
  System.out.printf("\t%s\n%s\n","Your Hand","------------------------");
  for (int x = 0; x<5; x++) {
   do {
	playerCards[x] = shuffledDeck[x];
   } while(shuffledDeck[x] == "0");
   shuffledDeck[x] = "0";
   do {
	playerSuits[x] = shuffledSuit[x];
   } while(shuffledSuit[x] == "0");
   shuffledSuit[x] = "0";
   System.out.print(playerCards[x] + " of " + playerSuits[x] + ".\t" + x + "\n");
  } // end for loop
  System.out.print("------------------------\n");
 } // end method deal
 public static void main(String args[]) {
  cardsGame cg = new cardsGame();
 } // end method main

}// end class cardsGame
 
Thanks but the thing is that havent seen multi dimensional arrays, so it would illegal if I use them. If you use my two classes the SimpleDeck class creates a deck when initDeck method and you can set ranks and the suits methods. But I still cant get my deck to shuffle the cards.
 
Well, it's not the way I would do it... But you can do it that way i suppose. Keep in mind though that it is more work for the system running the application because it has to devote more memory to an array of class objects as opposed to an array of strings. You might try creating a new class called shuffledDeck, or something like that. And basically just follow my logic, but using classes. Specifically this portion of my code:
Code:
public void shuffle() {
for (int x = 0; x<52; x++) {
do {
	card = (int)(13*Math.random());
	suit = (int)(4*Math.random());
} while(deck[card][suit].equals("0"));
//playerDeck[card][suit]
shuffledDeck[x] = deck[card][suit];
shuffledSuit[x] = suits[card][suit];
deck[card][suit] = "0";
} // end for
} // end method shuffle

Only instead of using my array, use your array of classes:
Code:
// declare this variable as a class-wide so that shuffle() has access to it
private int myRank, mySuit; // variables that will be assigned random numbers
private ShuffledDeck[] sd; // new array of shuffledDeck objects
public void shuffle() {
sd = new shuffledDeck[52];
for (int x = 0; x<52; x++) {
do {
myRank = (int)(13*Math.random());
mySuit = (int)(4*Math.random());
} while(theDeck[x].getRank().equals("0"));
/* the purpose for the above code is to continuously generate random numbers while the value of the element of the array of classes is 0, which in a regular deck of cards can't happen */
sd[x] = new shuffledDeck([theDeck[x].getRank().Value()], theDeck[x].getSuit().Value()]);
theDeck[x] = "0"; // initalize the value that was grabbed to 0
} // end for
} // end method shuffle

I didn't compile and test that, because I didn't re-enter your program, the syntax isn't 100% correct, but you can sort of follow where I am going I hope.
 
I finally got all the deck working all shuffled each start im starting to do the game, but im having trouble assigning the first 5 cards of the deck to an array called hand that holds 5 card. Now I have to get the game working ill post the code later!
 
Ok here is the code I am having a problem assining the first cards of the deck to my hand. All the classes reference each other SimpleCard and SimpleDeck are working I just need to make the Game class work. Help me I get the error array required, but SimpleDeck found.

SimpleCard Class

public class SimpleCard
{
private String rank;
private String suit;
public SimpleCard(String r, String s)
{
rank = r;
suit = s;
}
public SimpleCard()
{
rank = " ";
suit = " ";
}

public void setSuit(String s)
{
suit = s;
}
public void setRank(String s)
{
rank = s;
}
public String getSuit()
{
return suit;
}
public String getRank()
{
return rank;
}
public String toString()
{
String str;
str = "Card " + rank + " of " + suit;
return str;
}
public void swapCards(SimpleCard otherCard)
{
SimpleCard temp= new SimpleCard(this.rank, this.suit);

this.rank = otherCard.rank;
this.suit = otherCard.suit;

otherCard.rank = temp.rank;
otherCard.suit = temp.suit;
}

}


SimpleDeck Class

import java.util.Random;
public class SimpleDeck
{
private SimpleCard[] theDeck;
Random randomNumbers = new Random();
public SimpleDeck()
{
theDeck = new SimpleCard[52];
initDeck();
setSuits();
setRanks();
shuffle();
}
public void initDeck()
{
for(int i = 0; i < theDeck.length; i++)
theDeck = new SimpleCard();
}
public void setSuits()
{
String[] suitName = {"Hearts", "Spades", "Diamonds", "Clubs"};
for (int i = 0; i < theDeck.length; i++)
{
theDeck.setSuit(suitName[i / 13]);
}
}
public void setRanks()
{
String[] rankName = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (int i = 0; i < theDeck.length; i++)
{
theDeck.setRank(rankName[i % 13]);
}
}
public void printDeck()
{
for (int i = 0; i < theDeck.length; i++)
{
System.out.println(theDeck.getRank() + " of " + theDeck.getSuit());
}
}

public void shuffle()
{
for (int i = 0; i < theDeck.length; i++)
{
theDeck.swapCards(theDeck[randomNumbers.nextInt(52)]);
}

}

}

Game Class

public class Game
{
public static void main(String[] args)
{
SimpleDeck gameDeck = new SimpleDeck();
String[] hand = new String[5];
System.out.println("Welcome to Video Poker");
System.out.println("Your hand");
int j = 0;
for (int i = 0; i < 5; i++)
{
hand = gameDeck[j];
j++;
}

}
}
 
In the game class, when you create an object of the SimpleDeck class named gameDeck, it's not declared as an array. then later, you attempt to assign a value to the array that you never created.
I didn't test it, but changing the code to :
SimpleDeck gameDeck[] = new SimpleDeck();
 
I tried I get:
Incompatible types- found SimpleDeck but expected SimpleDeck[]
 
Back
Top Bottom