Can you help me fix my program so it can run properly? I can\'t seem to get it t
ID: 3581573 • Letter: C
Question
Can you help me fix my program so it can run properly? I can't seem to get it to run and theres no errors. Heres my three java files with TestVideoPoker.java being the main test file
//PlayingCard.java
import java.util.*;
//=================================================================================
/** class PlayingCardException: It is used for errors related to Card and Deck objects
* Do not modify this class!
*/
public class PlayingCard extends Exception {
/* Constructor to create a PlayingCardException object */
PlayingCard (){
super ();
}
PlayingCard ( String reason ){
super ( reason );
}
}
//=================================================================================
/** class Card : for creating playing card objects
* it is an immutable class.
* Rank - valid values are 1 to 13
* Suit - valid values are 0 to 3
* Do not modify this class!
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int suit, int rank) throws PlayingCard {
if ((rank < 1) || (rank > 13))
throw new PlayingCard("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
throw new PlayingCard("Invalid suit:"+suit);
else
cardSuit = suit;
}
/* Accessor and toString */
/* You may implement equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(3,1); // A Spades
System.out.println(c1);
c1 = new Card(0,10); // 10 Clubs
System.out.println(c1);
c1 = new Card(5,10); // generate exception here
}
catch (PlayingCard e)
{
System.out.println("PlayingCard: "+e.getMessage());
}
}
}
//=================================================================================
/** class Decks represents : n decks of 52 playing cards
* Use class Card to construct n * 52 playing cards!
*
* Do not add new data fields!
* Do not modify any methods
* You may add private methods
*/
class Decks {
/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;
/* this starts with copying cards from originalDecks */
/* it is used to play the card game */
/* see reset(): resets gameDecks to originalDecks */
private List<Card> gameDecks;
/* number of decks in this object */
private int numberDecks;
/**
* Constructor: Creates default one deck of 52 playing cards in originalDecks and
* copy them to gameDecks.
* initialize numberDecks=1
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & gameDecks
*/
public Decks()
{
this(1);// implement this method!
}
/**
* Constructor: Creates n decks (52 cards each deck) of playing cards in
* originalDecks and copy them to gameDecks.
* initialize numberDecks=n
* Note: You need to catch PlayingCardException from Card constructor
* Use ArrayList for both originalDecks & gameDecks
*/
public Decks(int n)
{
originalDecks = new ArrayList<>();
numberDecks = n;
for(int i = 0; i < numberDecks; i++){
for(int j = 0; j < 4; j++){
for(int h = 1; h <=13; h++){
try{
originalDecks.add(new Card(h, j));
}
catch(PlayingCard e){
System.out.println(e);
}
}
}
}
gameDecks = originalDecks;
}
/**
* Task: Shuffles cards in gameDecks.
* Hint: Look at java.util.Collections
*/
public void shuffle()
{
Collections.shuffle(gameDecks);
}
/**
* Task: Deals cards from the gameDecks.
*
* @param numberCards number of cards to deal
* @return a list containing cards that were dealt
* @throw PlayingCardException if numberCard > number of remaining cards
*
* Note: You need to create ArrayList to stored dealt cards
* and should removed dealt cards from gameDecks
*
*/
public List<Card> deal(int numberCards) throws PlayingCard
{
List<Card> temp = new ArrayList<>();
if(numberCards > remainSize())
throw new PlayingCard("You can't deal more than the number of cards left.");
for(int i = 0; i < numberCards; i++){
temp.add(gameDecks.remove(0));
}
return temp;
}
/**
* Task: Resets gameDecks by getting all cards from the originalDecks.
*/
public void reset()
{
gameDecks = originalDecks;
}
/**
* Task: Return number of remaining cards in gameDecks.
*/
public int remainSize()
{
return gameDecks.size();
}
/**
* Task: Returns a string representing cards in the gameDecks
*/
public String toString()
{
return ""+gameDecks;
}
/* Quick test */
/* */
/* Do not modify these tests */
/* Generate 2 decks of cards */
/* Loop 2 times: */
/* Deal 30 cards for 4 times */
/* Expect exception last time */
/* reset() */
public static void main(String args[]) {
System.out.println("******* Create 2 decks of cards ********* ");
Decks decks = new Decks(2);
for (int j=0; j < 2; j++)
{
System.out.println(" ************************************************ ");
System.out.println("Loop # " + j + " ");
System.out.println("Before shuffle:"+decks.remainSize()+" cards");
System.out.println(" "+decks);
System.out.println(" ============================================== ");
int numHands = 4;
int cardsPerHand = 30;
for (int i=0; i < numHands; i++)
{
decks.shuffle();
System.out.println("After shuffle:"+decks.remainSize()+" cards");
System.out.println(" "+decks);
try {
System.out.println(" Hand "+i+":"+cardsPerHand+" cards");
System.out.println(" "+decks.deal(cardsPerHand));
System.out.println(" Remain:"+decks.remainSize()+" cards");
System.out.println(" "+decks);
System.out.println(" ============================================== ");
}
catch (PlayingCard e)
{
System.out.println("*** In catch block:PlayingCardException:Error Msg: "+e.getMessage());
}
}
decks.reset();
}
}
}
//TestVideoPoker.java (This is the program that I'm suppose to run)
public class TestVideoPoker {
public static void main(String args[])
{
VideoPoker pokergame;
if (args.length > 0)
pokergame = new VideoPoker(Integer.parseInt(args[0]));
else
pokergame = new VideoPoker();
pokergame.play();
}
}
//VideoPoker.java
import java.util.*;
/*
* Ref: http://en.wikipedia.org/wiki/Video_poker
* http://www.freeslots.com/poker.htm
*
*
* Short Description and Poker rules:
*
* Video poker is also known as draw poker.
* The dealer uses a 52-card deck, which is played fresh after each playerHand.
* The player is dealt one five-card poker playerHand.
* After the first draw, which is automatic, you may hold any of the cards and draw
* again to replace the cards that you haven't chosen to hold.
* Your cards are compared to a table of winning combinations.
* The object is to get the best possible combination so that you earn the highest
* payout on the bet you placed.
*
* Winning Combinations
*
* 1. Jacks or Better: a pair pays out only if the cards in the pair are Jacks,
* Queens, Kings, or Aces. Lower pairs do not pay out.
* 2. Two Pair: two sets of pairs of the same card denomination.
* 3. Three of a Kind: three cards of the same denomination.
* 4. Straight: five consecutive denomination cards of different suit.
* 5. Flush: five non-consecutive denomination cards of the same suit.
* 6. Full House: a set of three cards of the same denomination plus
* a set of two cards of the same denomination.
* 7. Four of a kind: four cards of the same denomination.
* 8. Straight Flush: five consecutive denomination cards of the same suit.
* 9. Royal Flush: five consecutive denomination cards of the same suit,
* starting from 10 and ending with an ace
*
*/
/* This is the video poker game class.
* It uses Decks and Card objects to implement video poker game.
* Please do not modify any data fields or defined methods
* You may add new data fields and methods
* Note: You must implement defined methods
*/
public class VideoPoker {
// default constant values
private static final int startingBalance=100;
private static final int numberOfCards=5;
// default constant payout value and playerHand types
private static final int[] multipliers={1,2,3,5,6,9,25,50,250};
private static final String[] goodHandTypes={
"Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush ",
"Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };
// must use only one deck
private static final Decks Decks(1);
// holding current poker 5-card hand, balance, bet
private List<Card> playerHand;
private int playerBalance;
private int playerBet;
/** default constructor, set balance = startingBalance */
public VideoPoker()
{
this(startingBalance);
}
/** constructor, set given balance */
public VideoPoker(int playerBalance)
{
this.playerBalance= playerBalance;
}
/** This display the payout table based on multipliers and goodHandTypes arrays */
private void showPayoutTable()
{
System.out.println(" ");
System.out.println("Payout Table Multiplier ");
System.out.println("=======================================");
int size = multipliers.length;
for (int i=size-1; i >= 0; i--) {
System.out.println(goodHandTypes[i]+" | "+multipliers[i]);
}
System.out.println(" ");
}
/** Check current playerHand using multipliers and goodHandTypes arrays
* Must print yourHandType (default is "Sorry, you lost") at the end of function.
* This can be checked by testCheckHands() and main() method.
*/
private void checkHands()
{
int combination = checkCombination();
switch(combination){
case 1:
System.out.println(goodHandTypes[0]);
playerBalance = playerBalance + ( playerBet * multipliers[0] );
break;
case 2:
System.out.println(goodHandTypes[1]);
playerBalance = playerBalance + ( playerBet * multipliers[1] );
break;
case 3:
System.out.println(goodHandTypes[2]);
playerBalance = playerBalance + ( playerBet * multipliers[2] );
break;
case 4:
System.out.println(goodHandTypes[3]);
playerBalance = playerBalance + ( playerBet * multipliers[3] );
break;
case 5:
System.out.println(goodHandTypes[4]);
playerBalance = playerBalance + ( playerBet * multipliers[4] );
break;
case 6:
System.out.println(goodHandTypes[5]);
playerBalance = playerBalance + ( playerBet * multipliers[5] );
break;
case 7:
System.out.println(goodHandTypes[6]);
playerBalance = playerBalance + ( playerBet * multipliers[6] );
break;
case 8:
System.out.println(goodHandTypes[7]);
playerBalance = playerBalance + ( playerBet * multipliers[7] );
break;
case 9:
System.out.println(goodHandTypes[8]);
playerBalance = playerBalance + ( playerBet * multipliers[8] );
break;
default:
System.out.println("Sorry, you lost");
break;
}
}
/*************************************************
* add additional private methods here ....
*
*************************************************/
private int checkCombination(){
int combination = 0;
if( checkRoyalPair() )
combination = 1;
if( checkTwoPair() )
combination = 2;
if( checkThreeOfAKind() )
combination = 3;
if( checkStraight() )
combination = 4;
if( checkFlush() )
combination = 5;
if( checkFullHouse() )
combination = 6;
if( checkFourOfAKind() )
combination = 7;
if( checkStraightFlush() )
combination = 8;
if( checkRoyalFlush() )
combination = 9;
return combination;
}
//check for royal pair
private boolean checkRoyalPair(){
Card[] array = sortRank();
int countPair = 0;
for( int i = 0; i < array.length - 1; i++ ){
if( array[i].getRank() == array[i+1].getRank() ){
if( array[i].getRank() > 10 && array[i].getRank() <= 13 ){
countPair++;
}
}
if( countPair == 1 )
return true;
}
return false;
}
//check for two pair
private boolean checkTwoPair(){
Card[] array = sortRank();
int countPair = 0;
for( int i = 0; i < array.length - 1; i++ ){
if( array[i].getRank() == array[i+1].getRank() ){
countPair++;
i++;
}
if( countPair == 2 )
return true;
}
return false;
}
//check for three of a kind
private boolean checkThreeOfAKind(){
Card[] array = sortRank();
for( int i = 0; i < array.length - 2; i++ ){
if( array[i].getRank() == array[i+1].getRank() && array[i].getRank() == array[i+2].getRank() ){
return true;
}
}
return false;
}
//check for straight
private boolean checkStraight(){
Card[] array = sortRank();
for( int i = 0; i < array.length - 1; i++ ){
if( array[i + 1].getRank() != array[i].getRank() + 1 ){
return false;
}
}
return true;
}
//check for flush
private boolean checkFlush(){
for(int i = 0; i < playerHand.size() - 1; i++){
if(playerHand.get(i).getSuit() != playerHand.get(i+1).getSuit())
return false;
}
return true;
}
//check for full house
private boolean checkFullHouse(){
Card[] array = sortRank();
int rank1 = 1;
int rank2 = 1;
for( int i = 0; i < array.length - 1; i++ ){
if( array[i].getRank() == array[0].getRank() )
rank1++;
if( array[i].getRank() == array[array.length - 1].getRank() )
rank2++;
}
if( rank1 == 3 && rank2 == 2 )
return true;
if( rank1 == 2 && rank2 == 3 )
return true;
return false;
}
//check for four of a kind
private boolean checkFourOfAKind(){
Card[] array = sortRank();
if( array[0].getRank() == array[array.length - 2 ].getRank() )
return true;
if( array[1].getRank() == array[array.length - 1 ].getRank() )
return true;
return false;
}
//check straight flush
private boolean checkStraightFlush(){
if( checkFlush() && checkStraight() )
return true;
return false;
}
//check royal flush
private boolean checkRoyalFlush(){
Card[] array = sortRank();
int counter = 0;
int rank = array[1].getRank();
if( checkFlush() && array[0].getRank() == 1 && array[1].getRank() == 10 ){
for( int i = 2; i < array.length; i++ ){
if( array[i].getRank() == rank + 1)
counter++;
rank++;
}
if( counter == 3 )
return true;
}
return false;
}
//sort deck by rank
private Card[] sortRank(){
Card[] array = toArray();
for( int i = 0; i < array.length; i++ )
{
int min = i;
for( int j = i + 1; j < array.length; j++ )
{
if( array[ j ].getRank() < array[ min ].getRank() )
{
min = j;
}
}
Card temp = array[ min ];
array[ min ] = array[ i ];
array[ i ] = temp;
}
return array;
}
//list to array
private Card[] toArray(){
return playerHand.toArray( new Card[0] );
}
public void play()
{
//Show the payout table
showPayoutTable();
boolean running = true;
while (running){
//Show balance and how much you want to bet
System.out.println("Balance: " + playerBalance);
Scanner scanner = new Scanner(System.in);
System.out.println("Bet: ");
playerBet = scanner.nextInt();
//Validate bet amount
while(playerBet > playerBalance){
System.out.println("Enter valid bet: ");
playerBet = scanner.nextInt();
}
//Update bet and deal cards
playerBalance = playerBalance - playerBet;
oneDeck.reset();
oneDeck.shuffle();
try{
playerHand = oneDeck.deal(5);
}
catch(PlayingCard e){
System.out.println("Error" + e);
running = false;
}
//Hold chosen cards and deal new cards
List<String> keepList = new ArrayList<>();
List<Card> newHand = new ArrayList<>();
System.out.println("Hand: " + playerHand.toString() + " What cards you want to hold? (e.g. 1 3 4)" );
scanner = new Scanner(System.in);
keepList = Arrays.asList(scanner.nextLine().split(" "));
int card_replace = numberOfCards - keepList.size();
try{
newHand = oneDeck.deal(card_replace);
}
catch(PlayingCard e){
System.out.println("Error " + e);
}
for(int i = 0; i < keepList.size(); i++){
newHand.add(playerHand.get(Integer.parseInt(keepList.get(i))-1));
}
playerHand = newHand;
System.out.println(playerHand.toString());
//Check new hand
checkHands();
//Ask if the player wants to play one more time or exit the game bacause the balance is zero.
Scanner answer = new Scanner(System.in);
String answerGame;
if(playerBalance == 0){
System.out.print("Your balance is " + playerBalance + " Thank you for playing! Come back soon!");
running = false;
}
else{
System.out.println("Your balance: " + playerBalance + " Do you want to play a new game? (y or n)");
answerGame = answer.nextLine();
if("n".equals(answerGame)){
System.out.print("Thank you for playing! Come back soon!");
running = false;
}
else{
System.out.println("Do you want to see playout table? (y or n)");
answerGame = answer.nextLine();
if("y".equals(answerGame)){
showPayoutTable();
}
}
}
}
}
/*************************************************
* Do not modify methods below
/*************************************************
/** testCheckHands() is used to test checkHands() method
* checkHands() should print your current hand type
*/
public void testCheckHands()
{
try {
playerHand = new ArrayList<Card>();
// set Royal Flush
playerHand.add(new Card(3,1));
playerHand.add(new Card(3,10));
playerHand.add(new Card(3,12));
playerHand.add(new Card(3,11));
playerHand.add(new Card(3,13));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight Flush
playerHand.set(0,new Card(3,9));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight
playerHand.set(4, new Card(1,8));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Flush
playerHand.set(4, new Card(3,5));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush ",
// "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };
// set Four of a Kind
playerHand.clear();
playerHand.add(new Card(3,8));
playerHand.add(new Card(0,8));
playerHand.add(new Card(3,12));
playerHand.add(new Card(1,8));
playerHand.add(new Card(2,8));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Three of a Kind
playerHand.set(4, new Card(3,11));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Full House
playerHand.set(2, new Card(1,11));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Two Pairs
playerHand.set(1, new Card(1,9));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Royal Pair
playerHand.set(0, new Card(1,3));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// non Royal Pair
playerHand.set(2, new Card(3,3));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
/* Quick testCheckHands() */
public static void main(String args[])
{
VideoPoker pokergame = new VideoPoker();
pokergame.testCheckHands();
}
}
Explanation / Answer
Okay, here are some things you need to fix to run it properly:
Remember, one java file can only have one public class.