Question
I need help with this assigment I am totally lost. Full Game Integration In this assignment, you finish implementing your Card , Deck , and
I need help with this assigment I am totally lost.
Full Game Integration
In this assignment, you finish implementing your Card, Deck, and Hand class and properly execute the game in PGame. To get the full game running you will need to add methods from Card and Deck that were previously unneeded. You will also need to use methods in PGame to change the default colors used by the UI elements along with the default font. As a reminder, you cannot change the files PCard, PDeck, PHand, or PGame.
Grading Rubric
You will receive points for tasks that are Complete, Partial, or Failed. Complete tasks are worth 3 points, partial tasks are worth 2 points, and failed tasks are worth 1 point. Your final score is the actual points for the assignment normalized (usually 100).
-Code Compiles and Runs
-Code is Documented Correctly
-Game runs (at least mostly)
-Dealers first card is hidden
-Cards font color is not light gray
-Cards face color is not white
-Cards background color is not white
-Cards border color is not light gray
-Cards Stripe color is not light gray (while hidden)
-Games background color is not white
-Games banner color is not light gray
-Games banner text color is not white
-Games status text color is not light gray
-Games button color is not light gray
-Games button highlight color is not dark gray
-Games button text color is not white
-Games font is not Sans Serif
Given code:
//////////////////////////////////////////////////////////////////////////////////////////////////////
package plumb;
import java.awt.Color;
/**
* Base class used to create a playing card. This class is extended by
another
* class that overrides the necessary methods to create usability and
visual
* appeal.
*
* @author Jared N. Plumb
* @version 1.0
* @since 2018-05-25
*/
public abstract class PCard {
/**
* Returns a string containing both the rank and suit of the card.
This text is
* used for visual display and may include Unicode characters.
*/
public abstract String getText(); // Return a value such as "\u2605"
/** Sets the card to the face up state. */
public void showCard() {
}
/** Sets the card to the face down state. */
public void hideCard() {
}
/**
* Finds if the card if face-down or face-up
*
* @return true if the card if face down.
*/
public boolean isHidden() {
return true;
}
/** Returns the color of the font used for the card text. */
public Color getFontColor() {
return Color.LIGHT_GRAY;
}
/** Returns the color of the cards face. */
public Color getFaceColor() {
return Color.WHITE;
}
/** Returns the color of the cards background. */
public Color getBackColor() {
return Color.WHITE;
}
/** Returns the color of an 8 pixel border around the edge of the
card. */
public Color getBorderColor() {
return Color.LIGHT_GRAY;
}
/** Returns the alternative color used on the cards background. */
public Color getStripeColor() {
return Color.LIGHT_GRAY;
}
}
///////////////////////////////////////////////////////////////////////
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import plumb.PCard;
/**
* A simple playing card test. This test displays the given card in a window
* using Swing. The only method that needs to be called is run. The
* remaining methods and variables can be ignored.
*
* @author Jared N. Plumb
* @version 1.0
* @since 2018-05-25
*/
public final class CardTest extends JPanel {
/**
* Display a playing card in a window.
*
* @param card
* The card to display.
*/
public static void run(PCard card) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Card");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.add(new CardTest(card));
frame.pack();
frame.setVisible(true);
}
});
}
// **********************************************************************
// Private Data
// (You do not need to know anything below this point!)
// **********************************************************************
private CardTest(PCard card) {
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
this.setOpaque(true);
this.setBackground(Color.WHITE);
this.card = card;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(card.getBorderColor());
g2.fillRoundRect(CARD_X, CARD_Y, CARD_WIDTH, CARD_HEIGHT, 16, 16);
g2.setColor(card.getFaceColor());
g2.fillRoundRect(CARD_X + 4, CARD_Y + 4, CARD_WIDTH - 8, CARD_HEIGHT - 8, 12, 12);
g2.setFont(CARD_FONT);
g2.setColor(card.getFontColor());
FontMetrics metrics = g2.getFontMetrics();
String text = card.getText();
Rectangle2D rect = metrics.getStringBounds(text, g2);
g2.drawString(text, CARD_X + CARD_WIDTH / 2 - (int) rect.getWidth() / 2,
CARD_Y + CARD_HEIGHT / 2 + metrics.getHeight() / 2 - metrics.getDescent());
}
private static final long serialVersionUID = 447779152059287596L;
private static final int GAME_WIDTH = 150;
private static final int GAME_HEIGHT = 200;
private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 150;
private static final int CARD_X = (GAME_WIDTH - CARD_WIDTH) / 2;
private static final int CARD_Y = (GAME_HEIGHT - CARD_HEIGHT) / 2;
private static final Font CARD_FONT = new Font("SansSerif", Font.BOLD, 40);
private final PCard card;
}
///////////////////////////////////////////////////////////////////////
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import plumb.PCard;
import plumb.PDeck;
/**
* A simple playing card deck test. This test will create a window and display
* the full contents of the playing card deck.
*
* @author Jared N. Plumb
* @version 1.1
* @since 2018-06-06
*/
public class DeckTest extends JPanel {
/**
* Display a deck of cards in a window.
*
* @param card
* The deck of cards to display.
*/
public static void run(PDeck deck) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Deck Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.add(new DeckTest(deck));
frame.pack();
frame.setVisible(true);
}
});
}
// **********************************************************************
// Private Data
// (You do not need to know anything below this point!)
// **********************************************************************
public DeckTest(PDeck deck) { /**changed to public for Junit Test**/
this.getFontMetrics(CARD_FONT); // This is a hack to fix a Java font bug
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
this.setOpaque(true);
this.setBackground(Color.WHITE);
this.deck = deck;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int row = deck.cardCount() / 4;
for (int i = 0; deck.cardCount() > 0; i++) {
PCard card = deck.dealCard();
int x = CARD_WIDTH * (i % row);
int y = CARD_HEIGHT * (i / row);
g2.setColor(card.getBorderColor());
g2.fillRoundRect(x, y, CARD_WIDTH, CARD_HEIGHT, 16, 16);
g2.setColor(card.getFaceColor());
g2.fillRoundRect(x + 4, y + 4, CARD_WIDTH - 8, CARD_HEIGHT - 8, 12, 12);
g2.setFont(CARD_FONT);
g2.setColor(card.getFontColor());
FontMetrics metrics = g2.getFontMetrics();
String text = card.getText();
Rectangle2D rect = metrics.getStringBounds(text, g2);
g2.drawString(text, x + CARD_WIDTH / 2 - (int) rect.getWidth() / 2,
y + CARD_HEIGHT / 2 + metrics.getHeight() / 2 - metrics.getDescent());
}
}
private static final long serialVersionUID = 447779152059287596L;
private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 150;
private static final int GAME_WIDTH = CARD_WIDTH * 14;
private static final int GAME_HEIGHT = CARD_HEIGHT * 4;
private static final Font CARD_FONT = new Font("SansSerif", Font.BOLD, 40);
private final PDeck deck;
}
///////////////////////////////////////////////////////////////////////
package plumb;
/**
* Base interface used to manage a collection of cards.
*
* @author Jared N. Plumb
* @version 1.0
* @since 2018-05-25
*/
public interface PDeck {
/** Randomizes the deck. */
public void shuffle();
/** Adds a card to the end of the deck. */
public void addCard(PCard card);
/** Removes a card from the end of the deck. */
public PCard dealCard();
/** Removes a card from the end of the deck and marks it as hidden.
*/
public PCard dealHiddenCard();
/** Returns the number of cards in the deck. */
public int cardCount();
}
//////////////////////////////////////////////////////////////////////////////////////
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import plumb.PCard;
import plumb.PDeck;
import plumb.PHand;
/**
* A simple playing card hand test. This test will attempt a number of hand
* arrangements to validate they add up to the correct points. A final hand that
* equals 21 points will be displayed.
*
* @author Jared N. Plumb
* @version 1.1
* @since 2018-06-12
*/
public class HandTest extends JPanel {
/**
* Display a deck of cards in a window.
*
* @param card
* The deck of cards to display.
*/
public static void run(PDeck deck, PHand hand) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Hand Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.setContentPane(new HandTest(deck, hand));
frame.pack();
frame.setVisible(true);
}
});
}
// **********************************************************************
// Private Data
// (You do not need to know anything below this point!)
// **********************************************************************
private HandTest(PDeck deck, PHand hand) {
this.getFontMetrics(CARD_FONT); // This is a hack to fix a Java font bug
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
this.setOpaque(true);
this.setBackground(Color.WHITE);
this.deck = deck;
this.hand = hand;
for (int i = 0; i < 4; i++)
do {
if (this.hand.getSize() > i)
this.deck.addCard(this.hand.removeCard(i));
deck.shuffle();
this.hand.addCard(this.deck.dealCard());
} while (this.hand.getValue() != 11 + i);
for (int i = 4; i < 6; i++)
do {
if (this.hand.getSize() > i)
this.deck.addCard(this.hand.removeCard(i));
deck.shuffle();
this.hand.addCard(this.deck.dealCard());
} while (this.hand.getValue() != 14 + (i - 3) * 2);
do {
if (this.hand.getSize() > 6)
this.deck.addCard(this.hand.removeCard(6));
deck.shuffle();
this.hand.addCard(this.deck.dealCard());
} while (this.hand.getValue() != 21);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (int i = 0; i < hand.getSize(); i++) {
PCard card = hand.getCard(i);
int x = CARD_WIDTH / 2 + CARD_WIDTH * i;
int y = CARD_HEIGHT / 2;
g2.setColor(card.getBorderColor());
g2.fillRoundRect(x, y, CARD_WIDTH, CARD_HEIGHT, 16, 16);
g2.setColor(card.getFaceColor());
g2.fillRoundRect(x + 4, y + 4, CARD_WIDTH - 8, CARD_HEIGHT - 8, 12, 12);
g2.setFont(CARD_FONT);
g2.setColor(card.getFontColor());
FontMetrics metrics = g2.getFontMetrics();
String text = card.getText();
Rectangle2D rect = metrics.getStringBounds(text, g2);
g2.drawString(text, x + CARD_WIDTH / 2 - (int) rect.getWidth() / 2,
y + CARD_HEIGHT / 2 + metrics.getHeight() / 2 - metrics.getDescent());
}
g2.setFont(CARD_FONT);
g2.setColor(Color.GRAY);
FontMetrics metrics = g2.getFontMetrics();
g2.drawString(String.format("= %d", hand.getValue()), CARD_WIDTH / 2 + CARD_WIDTH * hand.getSize() + 12,
CARD_HEIGHT + metrics.getHeight() / 2 - metrics.getDescent());
}
private static final long serialVersionUID = 3514525153815595242L;
private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 150;
private static final int GAME_WIDTH = CARD_WIDTH * 9;
private static final int GAME_HEIGHT = CARD_HEIGHT * 2;
private static final Font CARD_FONT = new Font("SansSerif", Font.BOLD, 40);
private final PDeck deck;
private final PHand hand;
}
/////////////////////////////////////////////////////////////////////////////////////
package plumb;
/**
* Base interface used to manage cards held by a player.
*
* @author Jared N. Plumb
* @version 1.0
* @since 2018-05-25
*/
public interface PHand {
/** Returns the number of cards in the hand. */
public int getSize();
/** Adds a card to the end of the hand. */
public void addCard(PCard card);
/** Returns a card from the hand (but not removed). */
public PCard getCard(int index);
/** Removes and returns the card */
public PCard removeCard(int index);
/** Returns the point value of the current hand. */
public int getValue();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
package plumb;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Base interface used to manage a collection of cards.
*
* @author Jared N. Plumb
* @version 1.0
* @since 2018-07-09
*/
public final class PGame extends JPanel {
/** Sets the background color for the window */
public static void setBackgroundColor(Color color) {
BACKGROUND_COLOR = color;
}
/** Sets the background color of the banners */
public static void setBannerColor(Color color) {
BANNER_COLOR = color;
}
/** Sets the color of the banner text */
public static void setBannerTextColor(Color color) {
BANNER_TEXT_COLOR = color;
}
/** Sets the color of the status text */
public static void setStatusTextColor(Color color) {
STATUS_TEXT_COLOR = color;
}
/** Sets the background color of the buttons */
public static void setButtonColor(Color color) {
BUTTON_COLOR = color;
}
/** Sets the selected color of the buttons */
public static void setButtonHighlightColor(Color color) {
BUTTON_HIGHLIGHT = color;
}
/** Sets the button text color */
public static void setButtonTextColor(Color color) {
BUTTON_TEXT_COLOR = color;
}
/** Sets the font used throughout the game */
public static void setFont(String name) {
GAME_FONT = name;
}
/**
* Run the game using a deck, a hand for the dealer, and a hand for
the player
*/
public static void run(PDeck deck, PHand dealer, PHand player) {
final PGame game = new PGame(deck, dealer, player);
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.add(game);
frame.pack();
frame.setVisible(true);
}
});
}
//
**********************************************************************
// Private Data
// (You do not need to know anything below this point!)
//
**********************************************************************
private static final long serialVersionUID = -6901243923241700564L;
private static final int GAME_WIDTH = 640;
private static final int GAME_HEIGHT = 480;
private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 150;
private static final int CARD_SPACING = 8;
private static Color BACKGROUND_COLOR = Color.WHITE;
private static Color BANNER_COLOR = Color.LIGHT_GRAY;
private static Color BANNER_TEXT_COLOR = Color.WHITE;
private static Color STATUS_TEXT_COLOR = Color.LIGHT_GRAY;
private static Color BUTTON_COLOR = Color.LIGHT_GRAY;
private static Color BUTTON_HIGHLIGHT = Color.DARK_GRAY;
private static Color BUTTON_TEXT_COLOR = Color.WHITE;
private static String GAME_FONT = "SansSerif";
private PDeck deck;
private PHand dealer;
private PHand player;
private Status status;
private Button hitButton;
private Button standButton;;
private Button restartButton;
private PGame(PDeck deck, PHand dealer, PHand player) {
this.deck = deck;
this.dealer = dealer;
this.player = player;
this.setPreferredSize(new Dimension(GAME_WIDTH,
GAME_HEIGHT));
this.setOpaque(true);
this.setBackground(BACKGROUND_COLOR);
this.add(new Banner("Dealer"));
this.add(new PlayArea(dealer));
this.add(new Banner("Player"));
this.add(new PlayArea(player));
this.status = new Status();
this.hitButton = new HitButton();
this.standButton = new StandButton();
this.restartButton = new RestartButton();
this.add(status);
this.add(hitButton);
this.add(standButton);
this.add(restartButton);
newGame();
}
private void newGame() {
this.deck.shuffle();
this.dealer.addCard(deck.dealHiddenCard());
this.player.addCard(deck.dealCard());
this.dealer.addCard(deck.dealCard());
this.player.addCard(deck.dealCard());
paintGame();
checkGame();
}
private void checkGame() {
if (dealer.getValue() >= 21 || player.getValue() >= 21)
finishGame();
}
private void dealCard() {
player.addCard(deck.dealCard());
checkGame();
paintGame();
}
private void finishGame() {
if (dealer.getSize() > 0)
dealer.getCard(0).showCard();
while (dealer.getValue() <= 17)
dealer.addCard(deck.dealCard());
if (dealer.getSize() == 2 && dealer.getValue() == 21)
status.setText("Dealer Blackjack! You
Lose!");
else if (player.getSize() == 2 && player.getValue() ==
21)
status.setText("Player Blackjack! You
Win!");
else if (dealer.getValue() > 21 && player.getValue() >
21)
status.setText("Dealer Bust! Player Bust!
You Lose!");
else if (dealer.getValue() <= 21 && player.getValue() >
21)
status.setText("Player Bust! You Lose!");
else if (dealer.getValue() > 21 && player.getValue() <=
21)
status.setText("Dealer Bust! You Win!");
else if (dealer.getValue() < player.getValue())
status.setText("You Win!");
else if (dealer.getValue() > player.getValue())
status.setText("You Lose!");
else if (dealer.getValue() == player.getValue())
status.setText("Tie! You Lose!");
else
status.setText("ERROR: Unhandled
Conclusion");
hitButton.setVisible(false);
standButton.setVisible(false);
restartButton.setVisible(true);
paintGame();
}
private void restartGame() {
while (dealer.getSize() > 0)
deck.addCard(dealer.removeCard(0));
while (player.getSize() > 0)
deck.addCard(player.removeCard(0));
status.setText(null);
hitButton.setVisible(true);
standButton.setVisible(true);
restartButton.setVisible(false);
newGame();
}
private void paintGame() {
this.repaint();
}
private class Banner extends JPanel {
private static final long serialVersionUID = -
8125170738840399404L;
private final Font FONT = new Font(GAME_FONT, Font.BOLD,
16);
private String text;
private Banner(String text) {
this.getFontMetrics(FONT); // This is a hack
to fix a Java font bug
this.text = text;
this.setPreferredSize(new Dimension(600,
30));
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(BANNER_COLOR);
g2.fillRoundRect(0, 0, this.getWidth(),
this.getHeight(), 16, 16);
g2.setFont(FONT);
g2.setColor(BANNER_TEXT_COLOR);
FontMetrics metrics = g2.getFontMetrics();
g2.drawString(text, 10, this.getHeight() / 2
+ metrics.getHeight() / 2 - metrics.getDescent());
}
}
private class Status extends JPanel {
private static final long serialVersionUID =
8869322120214666782L;
private final Font FONT = new Font(GAME_FONT, Font.BOLD,
20);
private String text;
private Status() {
this.setPreferredSize(new Dimension(600,
34));
this.setOpaque(false);
}
private void setText(String text) {
this.text = text;
paintGame();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (text != null) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(FONT);
g2.setColor(STATUS_TEXT_COLOR);
FontMetrics metrics =
g2.getFontMetrics();
Rectangle2D rect =
metrics.getStringBounds(text, g2);
g2.drawString(text, this.getWidth() / 2
- (int) rect.getWidth() / 2,
this.getHeight()
/ 2 + metrics.getHeight() / 2 - metrics.getDescent());
}
}
}
private class PlayArea extends JPanel {
private static final long serialVersionUID = -
2576568959049193937L;
private final Font FONT = new Font(GAME_FONT, Font.BOLD,
40);
private PHand hand;
private PlayArea(PHand hand) {
this.hand = hand;
this.setPreferredSize(new Dimension(600,
155));
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int spacing = CARD_SPACING;
if (hand.getSize() > 5)
spacing = 600 / hand.getSize() -
CARD_WIDTH - (hand.getSize() - 5);
for (int i = 0; i < hand.getSize(); i++) {
PCard card = hand.getCard(i);
if (card == null)
continue;
g2.setColor(card.getBorderColor());
g2.fillRoundRect(i * (CARD_WIDTH +
spacing), 0, CARD_WIDTH, CARD_HEIGHT, 16, 16);
g2.setColor(card.getFaceColor());
g2.fillRoundRect(i * (CARD_WIDTH +
spacing) + 4, 4, CARD_WIDTH - 8, CARD_HEIGHT - 8, 12, 12);
int x = i * (CARD_WIDTH + spacing);
if (card.isHidden()) {
for (int s = 0; s < 11; s++)
{
g2.setColor(s % 2
== 0 ? card.getStripeColor() : card.getBackColor());
g2.fillRect(x + 8
+ 4 * s, 8 + 4 * s, CARD_WIDTH - 16 - 8 * s, CARD_HEIGHT - 16 - 8 * s);
}
} else {
g2.setFont(FONT);
g2.setColor(card.getFontColor());
FontMetrics metrics =
g2.getFontMetrics();
String text =
card.getText();
Rectangle2D rect =
metrics.getStringBounds(text, g2);
g2.drawString(text, x +
CARD_WIDTH / 2 - (int) rect.getWidth() / 2,
CARD_HEIGHT
/ 2 + metrics.getHeight() / 2 - metrics.getDescent());
}
}
}
}
private class Button extends JPanel implements MouseListener {
private static final long serialVersionUID =
2562514311212476569L;
private final Font FONT = new Font(GAME_FONT, Font.BOLD,
16);
private String text;
private boolean down;
public Button(String text) {
this.text = text;
this.down = false;
this.setPreferredSize(new Dimension(100,
30));
this.setOpaque(false);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(down ? BUTTON_HIGHLIGHT :
BUTTON_COLOR);
g2.fillRoundRect(0, 0, this.getWidth(),
this.getHeight(), 16, 16);
g2.setFont(FONT);
g2.setColor(BUTTON_TEXT_COLOR);
FontMetrics metrics = g2.getFontMetrics();
Rectangle2D rect =
metrics.getStringBounds(text, g2);
g2.drawString(text, this.getWidth() / 2 -
(int) rect.getWidth() / 2,
this.getHeight() / 2 +
metrics.getHeight() / 2 - metrics.getDescent());
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
down = true;
this.repaint();
}
public void mouseReleased(MouseEvent e) {
down = false;
this.repaint();
}
}
private class HitButton extends Button {
private static final long serialVersionUID =
8142888208655021092L;
public HitButton() {
super("Hit");
}
public void mouseClicked(MouseEvent e) {
dealCard();
}
}
private class StandButton extends Button {
private static final long serialVersionUID =
4290860168347972255L;
public StandButton() {
super("Stand");
}
public void mouseClicked(MouseEvent e) {
finishGame();
}
}
private class RestartButton extends Button {
private static final long serialVersionUID =
6468817203841566611L;
public RestartButton() {
super("Restart");
this.setVisible(false);
}
public void mouseClicked(MouseEvent e) {
restartGame();
}
}
}
//////////////////////////////////////////////////////////
End of given code
This is what I have
///////////////////////////////////////////////
package maingame;
import plumb.PCard;
//import test.CardTest;
public class CreateCard extends PCard{
/** Cards and its values **/
public static final int ACE = 1;
public static final int TWO = 2;
public static final int THREE = 3;
public static final int FOUR = 4;
public static final int FIVE = 5;
public static final int SIX = 6;
public static final int SEVEN = 7;
public static final int EIGHT = 8;
public static final int NINE = 9;
public static final int TEN = 10;
public static final int JACK = 11;
public static final int KNIGHT = 12;
public static final int QUEEN = 13;
public static final int KING = 14;
/** Suits and its value **/
public static final int SPADE = 1;
public static final int HEART = 2;
public static final int DIAMOND = 3;
public static final int CLUB = 4;
private final int rank;
private final int suit;
//private boolean hidden;
/**Constructor. Get rank a suit.**/
public CreateCard(int rank, int suit) {
this.rank = rank;
this.suit = suit;
//this.hidden = false;
}
/** gets the rank **/
public int getRank() {
return rank;
}
/** gets the suit **/
public int getSuit() {
return suit;
}
/** hides the card **/
//public void hideCard() {
// hidden = true;
//}
/**
* Returns a string containing both the rank and suit of the card. This text is
* used for visual display and may include Unicode characters.
*/
public String getText() {
String text = "";
switch (getRank()) {
case TWO:
text += "2";
break;
case THREE:
text += "3";
break;
case FOUR:
text += "4";
break;
case FIVE:
text += "5";
break;
case SIX:
text += "6";
break;
case SEVEN:
text += "7";
break;
case EIGHT:
text += "8";
break;
case NINE:
text += "9";
break;
case TEN:
text += "10";
break;
case ACE:
text += "A";
break;
case JACK:
text += "J";
break;
case KNIGHT:
text += "C";
break;
case QUEEN:
text += "Q";
break;
case KING:
text += "K";
break;
default:
text += getRank();
break;
}
switch (getSuit()) {
case SPADE:
text += "\u2660";
break;
case HEART:
text += "\u2665";
break;
case DIAMOND:
text += "\u2666";
break;
case CLUB:
text += "\u2663";
break;
}
return text;
}
/**public static void main(String[] args) {
CardTest.run(new CreateCard(CreateCard.ACE, CreateCard.CLUB));
//CardTest.run(new CreateCard(CreateCard.NINE, CreateCard.DIAMOND));
}**/
}
////////////////////////////////////////////////////////////////////////
package maingame;
import java.util.ArrayList;
import java.util.Collections;
import plumb.PCard;
import plumb.PDeck;
//import test.DeckTest;
/** CreateDeck class implements PDeck **/
public class CreateDeck implements PDeck{
/** Creates a new list **/
private ArrayList
/** Constructor. Contains a nested for loop to get the suit and the rank of the card **/
public CreateDeck () {
deck = new ArrayList
for (int i=1;i<15;i++) {
for(int j=1;j<5;j++) {
addCard(new CreateCard(i,j));
}
}
}
/** Randomizes the deck. */
@Override
public void shuffle() {
Collections.shuffle(deck);
}
/** Adds a card to the end of the deck. */
@Override
public void addCard(PCard card) {
deck.add(card); // add(ya lo tiene java)
}
/** Removes a card from the end of the deck. */
@Override
public PCard dealCard() {
return deck.remove(cardCount()-1);
}
/** Removes a card from the end of the deck and marks it as hidden. */
@Override
public PCard dealHiddenCard() {
// TODO Auto-generated method stub
return null;
}
/** Returns the number of cards in the deck. */
@Override
public int cardCount() {
return deck.size();
}
/**public static void main(String[] args) {
CreateDeck deck = new CreateDeck();
DeckTest.run(new CreateDeck());
System.out.println(deck.cardCount());
}**/
}
///////////////////////////////////////////////////////////
package maingame;
import java.util.ArrayList;
import plumb.PCard;
import plumb.PHand;
public class CreateHand implements PHand {
private ArrayList
/** Creates an array for the hand */
public CreateHand() {
hand = new ArrayList
}
/** Returns the number of cards in the hand. */
@Override
public int getSize() {
return hand.size();
}
/** Adds a card to the end of the hand. */
@Override
public void addCard(PCard card) {
hand.add(card);
}
/** Returns a card from the hand (but not removed). */
@Override
public PCard getCard(int index) {
return hand.get(index);
}
/** Removes and returns the card */
@Override
public PCard removeCard(int index) {
return hand.remove(index);
}
/** Returns the point value of the current hand. */
@Override
public int getValue() {
int value;
boolean ace; //Because of the rules Ace=1 or Ace=11
int cards;
value=0;
ace = false; //Because of the rules Ace=1 or Ace=11
cards = getSize();
for(int i=0; i PCard card; int cardValue; card=getCard(i); // i is the index cardValue = ((CreateCard) card).getRank(); //Gets the values if (cardValue > 10) { cardValue = 10; } //Because of the rules Ace=1 or Ace=11 if (cardValue == 1) { ace = true; } value = value + cardValue; } if ( ace == true && value + 10 <= 21 ) value = value + 10; return value; } } /////////////////////////////////////////////// End of this is what I have If there is something wrong with the code or you feel something is missing, please let me know.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started