Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING JAVA • The computer will always be the dealer, and will always match all b

ID: 3714403 • Letter: U

Question

USING JAVA • The computer will always be the dealer, and will always match all bets by the player. The dealer will start out with 5 times as much money as the player o (Remember, the house always wins, so they can afford it) • The player will put money on the table to enter the game (an initial bet), called the ante. • Both players are dealt 1 card face down, then one card face up. • Loop until each player has 5 cards – each round gives both players 1 card o Each player can bet, check (a bet of 0), or fold (give up this game) o Once all bets have been made, another card is dealt face up to all players • Once all players have 5 cards, a final bet is made, or the player can fold • All players show all cards • The player with the best hand wins the pot • The dealer collects all cards and shuffles the deck, and another hand is played until the player chooses to quit or one of the two players is out of money. • If a player folds, the dealer wins the pot automatically. For simplicity, our dealer (the computer) will never fold, and will match all bets from the player. • Your client class will use five different objects, which you will have to create (listed above) o You will present a welcome message and a description to the user o Ask the user for their name and other information to create the Player object o Start the loop to control the game ? Create a dealer (this will make a new deck of cards for each hand) ? Create a table ? Add the player and the dealer to the table ? Call table.play() ? Ask if the player wants to play again, or quit if they are out of money o Print a thanks for playing message

Your Card class

o value: Represents a point value of the card ? Note: Aces can be a low or high card in poker

o suit: Represents one of the four card suits in a standard 52 card deck. This will be done with an Enumeration.

o visible: A Boolean. Used to show or hide the first card dealt.

o name: A String, and will be either the number of the card (i.e. 3 or 10), or the actual name if it is a face card (i.e. Queen or Ace)

o show() and hide(): change the visible Boolean

o toString(): Represent the card. It should output the name and suit of the card, like this: 3 of Spades, or Queen of Hearts, or Hidden Card if visible == false

o compareTo(): compare cards to each other based on their value  

Your Deck class

o cards: An ArrayList of Cards, with all 52 cards put in the list when the deck is created

o shuffle(): A method to shuffle the ArrayList (use Collections.shuffle), call this as part of the constructor

o deal(): A method to remove a Card from the deck and return it

Your Player class

o name: a String that holds the player’s name, or the word Dealer

o stash: an Int that holds the money the player can bet with. It must be a positive number

o hand: an ArrayList of cards that have been dealt to the player

o bet(): Get the bet for this round from the player. Return the Int of the bet, or -1 if the bet is invalid (they bet more than they have, or bet a – number)

o fold(): Give up this hand before the hand is done being dealt

o scoreHand(): Return an Integer with a score based on the hand .

o toString(): Return a String showing the player’s name and the cards in their hand, along with their stash of money •

Your Dealer object o Inherits from Player

o deck: A deck of cards

o dealToPlayer(Player player): A method to get a card from the deck and put it in the player’s hand

Your Table object

o Pot: An Int that holds the bets from both players for each hand. Will initially be 0

o Players: A two element array of Player objects. One is the dealer, and the other is the player

o declareWinner(): A method to use each player’s scoreHand() results to pick a winner.

o show(): A method to show all cards face up on the table. Used after the last bet

o getBets(): A method to ask the player for a bet, and if valid, get the same bet from the dealer

o play(): A method to actually play the game

As you develop each class, be sure to write a unit test suite that will be used to test each method and branch of code. Combine all unit tests into a TestDriver class that can be used to show unit testing for each class. A menu shall be provided allowing the tester to select the class to run under test. Exit this menu through selection.

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class poker2 extends JFrame {
private Card deck[]; //this is an array of objects of Card type. See the Card class at the end
private int currentCard; //this keeps track of the number of cards dealt
private JButton dealButton, shuffleButton, testButtton; //GUI stuff, the two buttons
private JTextField displayField; //GUI stuff, the textfield that displays the card dealt
private JLabel statusLabel; //GUI stuff, Displays the number of cards dealt
private JLabel cardback, card1, card2,card3,card4,card5,card6,card7,card8,card9,card10;//GUI stuff The labels that displays the cards
private JTextField hand1TF, hand2TF;
Card dealt; //this is an object of Card type for a dealt card
Icon card = new ImageIcon(".\cards\back2.GIF"); //this is the cardback graphics file
Card hand1[];
Card hand2[];
int handNums[];
int test;
// set up deck of cards and GUI
public poker2()
{
super( "5-Card Stud" ); //Title

String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

deck = new Card[ 52 ];
hand1 = new Card[5];
hand2 = new Card[5];
handNums = new int[13]; //creates array to hold 52 cards
currentCard = -1;
test = 4;

// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] = new Card( faces[ count % 13 ],suits[ count / 13 ] );

// set up GUI and event handling
Container container = getContentPane();
container.setLayout( new FlowLayout() );

//Diplay the cardbacks
card1 = new JLabel();
card1.setIcon(card);
container.add( card1 );

card3 = new JLabel();
card3.setIcon(card);
container.add( card3 );

card5 = new JLabel();
card5.setIcon(card);
container.add( card5 );

card7 = new JLabel();
card7.setIcon(card);
container.add( card7 );

card9 = new JLabel();
card9.setIcon(card);
container.add( card9 );

hand1TF = new JTextField(32);
hand1TF.setEditable( false );
hand1TF.setText( "Here it Is" );
container.add( hand1TF );

card2 = new JLabel();
card2.setIcon(card);
container.add( card2 );

card4 = new JLabel();
card4.setIcon(card);
container.add( card4 );

card6 = new JLabel();
card6.setIcon(card);
container.add( card6 );

card8 = new JLabel();
card8.setIcon(card);
container.add( card8 );

card10 = new JLabel();
card10.setIcon(card);
container.add( card10 );

hand2TF = new JTextField(32);
hand2TF.setEditable( false );
hand2TF.setText( "Here Theirs Are" );
container.add( hand2TF );

// Initialize the "Deal Button"
dealButton = new JButton( "DEAL 'EM" );
dealButton.addActionListener(

new ActionListener() { // anonymous inner class

// deal a card. This listens for the "Deal CArd" button to be clicked
public void actionPerformed( ActionEvent actionEvent )
{
dealt = dealCard();

if ( dealt != null ) {
displayField.setText( dealt.viewcard() );
showcard(currentCard);
statusLabel.setText( "Card #: " + (currentCard + 1) );

}
else {
displayField.setText( "NO MORE CARDS TO DEAL" );
statusLabel.setText( "Shuffle cards to continue" );
}
//This is the right spot1 ???
int i;
i = 0;
if(currentCard % 2 == 0)
{hand2 = dealt;
i++;}
else
hand1[currentCard / 2] = dealt;
}

} // end anonymous inner class

); // end call to addActionListener

container.add( dealButton ); //this adds the button to the GUI
dealButton.setEnabled(false); //this disables the button. Don't deal a card until the deck is shuffled

//initialize the "Shuffle Deck" button
shuffleButton = new JButton( "Shuffle cards" );
shuffleButton.addActionListener(

new ActionListener() { // anonymous inner class

// shuffle deck. This listens for the "Shuffle cards" button to be clicked
public void actionPerformed( ActionEvent actionEvent )
{
displayField.setText( "SHUFFLING ..." );
shuffle();
displayField.setText( "DECK IS SHUFFLED" );

//reset the card graphics to the back every shuffle
card = new ImageIcon(".\cards\back2.GIF");
card1.setIcon(card);
card2.setIcon(card);
card3.setIcon(card);
card4.setIcon(card);
card5.setIcon(card);
card6.setIcon(card);
card7.setIcon(card);
card8.setIcon(card);
card9.setIcon(card);
card10.setIcon(card);
dealButton.setEnabled(true); //enable the dealbutton after every shuffle
}
private void evalHand()
{
int i;
for(i = 0; i < hand1.length;i++)
{
if(hand1.face.equals("Ace"))
handNums[0]++;
}
}

} // end anonymous inner class

); // end call to addActionListener

container.add( shuffleButton );

displayField = new JTextField( 20 );
displayField.setEditable( false );
container.add( displayField );

statusLabel = new JLabel();
container.add( statusLabel );

setSize( 400, 400 ); // set window size
setVisible( true ); // show window
setResizable(false);
}

// shuffle deck of cards with one-pass algorithm
private void shuffle()
{
currentCard = -1;

// for each card, pick another random card and swap them
for ( int first = 0; first < deck.length; first++ ) {
int second = ( int ) ( Math.random() * 52 );
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;
}

//dealButton.setEnabled( true );
}

// deal one card
private Card dealCard()
{
if ( ++currentCard < deck.length )
return deck[ currentCard ];
else {
dealButton.setEnabled( false );
return null;
}
}

// c is the currentcard, just check for the card value and suit and display the appropriate graphics file
private void showcard(int c)
{
// CASES 0,2,4,6,8 Are your Cards
// CASES 1,3,5,7,9 are your opponents cards
card = new ImageIcon(getCard(dealt));
switch (c)
{
case 0: card1.setIcon(card); break;
case 1: card2.setIcon(card); break;
case 2: card3.setIcon(card); break;
case 3: card4.setIcon(card); break;
case 4: card5.setIcon(card); break;
case 5: card6.setIcon(card); break;
case 6: card7.setIcon(card); break;
case 7: card8.setIcon(card); break;
case 8: card9.setIcon(card); break;
case 9: card10.setIcon(card);
dealButton.setEnabled(false);
break;
}//end card switch
}//end showcard


private String getCard(Card myCard)
{
String str_return = ".\cards\";
// Suits Thingy
if (myCard.suit.equals("Hearts")) str_return += "H";
if (myCard.suit.equals("Diamonds")) str_return += "D";
if (myCard.suit.equals("Clubs")) str_return += "C";
if (myCard.suit.equals("Spades")) str_return += "S";

// Face Thingy

if (myCard.face.equals("Ace")) str_return += "14";
if (myCard.face.equals("Deuce")) str_return += "2";
if (myCard.face.equals("Three")) str_return += "3";
if (myCard.face.equals("Four")) str_return += "4";
if (myCard.face.equals("Five")) str_return += "5";
if (myCard.face.equals("Six")) str_return += "6";
if (myCard.face.equals("Seven")) str_return += "7";
if (myCard.face.equals("Eight")) str_return += "8";
if (myCard.face.equals("Nine")) str_return += "9";
if (myCard.face.equals("Ten")) str_return += "10";
if (myCard.face.equals("Jack")) str_return += "11";
if (myCard.face.equals("Queen")) str_return += "12";
if (myCard.face.equals("King")) str_return += "13";

return (str_return += ".gif");
}//end getCard

// execute application
public static void main( String args[] )
{
poker application = new poker();

application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class poker

// class to represent a card
class Card {
public String face;
public String suit;

// constructor to initialize a card
public Card( String cardFace, String cardSuit )
{
face = cardFace;
suit = cardSuit;
}

// return String represenation of Card
public String viewcard()
{
return face + " of " + suit;

}//end viewcard

} // end class Card