Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > I need help with card game game of war in GUI enviroment

Reply
Thread Tools

I need help with card game game of war in GUI enviroment

 
 
judith
Guest
Posts: n/a
 
      11-01-2006
Hi i'm needing some help with compile errors.
This program was allowed by our instructor to be copied out of the book
exactly as it was written except i changed the the name from game of
war to projectJS

Instructions the program should implement the simple card game of war,
in which the dealer and player each draw a card from the deck. The one
with the highest card wins. The rank is considered when comparing
cards, but the suit is unimportant,so two cards with the same rand
produce a tie. The dealer wins on ties. The opponent is to be given
1000.00 at the beginning of the game and should be allowed to selct
bets in one of three amount: 50,100, or 150. Upon command a card should
be dealt to both the dealer andthe opponent. when the cards are dealt,
they should appear face down. Upo command they should be turned face up
and the winner announced. The opponent's sinnings should updated based
on who won the amount of the bet. The deck should be reshuffled once
the last card has been drawn and also at the opponent's request. Upon
the apppropriate request, the game should be restarted or terminated.
a deck has 52 cards
a card has a rank
a card has a suit
a dealer is a player
an opponent is a player
a game has two players


Here are the programs that have been copied from the book except that i
changed the GameOfWar to be projectJS as per instructer request



enum Rank implements Comparable<Rank>
{
TWO("2"),
THREE("3"),
FOUR("4"),
FIVE("5"),
SIX("6"),
SEVEN("7"),
EIGHT("8"),
NINE("9"),
TEN("10"),
JACK("J"),
QUEEN("Q"),
KING("K"),
ACE("A");

private String symbol;

private Rank(String symbol)
{
this.symbol = symbol;
}
public static Rank aRank(int ordinal)
{
return Rank.values()[ordinal];
}
public String getSymbol()
{
return symbol;
}
}

import java.awt.*;

enum Suits
{
CLUB
{
void drawSymbol(Graphics graphics)
{
int x1[] = {38, 33, 43};
int y1[] = {38, 45, 45};

graphics.setColor(Color.BLACK);
graphics.fillOval(33, 25, 9, 9);
graphics.fillOval(30, 31, 9, 9);
graphics.fillOval(36, 31, 9, 9);
graphics.fillPolygon(x1, y1, 3);
}
},
DIAMOND
{
void drawSymbol(Graphics graphics)
{
int x1[] = {38, 31, 38, 45};
int y1[] = {23, 33, 43, 33};

graphics.setColor(Color.RED);
graphics.fillPolygon(x1, y1, 4);
}
},
HEART
{
void drawSymbol(Graphics graphics)
{
int x1[] = {31, 38, 45};
int y1[] = {33, 43, 33};

graphics.setColor(Color.RED);
graphics.fillOval(30, 25, 9, 9);
graphics.fillOval(36, 25, 9, 9);
graphics.fillPolygon(x1, y1, 3);
}
},
SPADE
{
void drawSymbol(Graphics graphics)
{
int x1[] = {38, 31, 45};
int y1[] = {23, 33, 33};
int x2[] = {38, 31, 43};
int y2[] = {38, 45, 45};

graphics.setColor(Color.BLACK);
graphics.fillOval(30, 31, 9, 9);
graphics.fillOval(36, 31, 9, 9);
graphics.fillPolygon(x1, y1, 3);
graphics.fillPolygon(x2, y2, 3);
}
};

public static Suits aSuit(int ordinal)
{
return Suits.values()[ordinal];
}
abstract void drawSymbol(Graphics graphics);
}


import java.awt.*;

enum CardOrientation
{
FACE_UP
{
void draw(Graphics graphics, Suits suit,
Rank rank)
{
drawBorder(graphics);
suit.drawSymbol(graphics);
graphics.setFont(rankFont);
graphics.drawString("" + rank.getSymbol(), 12, 43);
}
},
FACE_DOWN
{
void draw(Graphics graphics, Suits suit,
Rank rank)
{
graphics.setColor(projectJS.lightYellow);
graphics.fillRect(0, 0, Card.WIDTH, Card.HEIGHT);
graphics.setColor(projectJS.darkGreen);
for (int y = 10; y < Card.HEIGHT; y += 10)
graphics.drawLine(0, y, Card.WIDTH, y);
for (int x = 10; x < Card.WIDTH; x += 10)
graphics.drawLine(x, 0, x, Card.HEIGHT);
drawBorder(graphics);
graphics.setFont(backFont);
graphics.drawString("D", 20, 22);
graphics.drawString("J", 20, 44);
graphics.drawString("J", 20, 66);
}
};

private static final Font
rankFont = new Font("Serif", Font.BOLD, 24),
backFont = new Font("Serif", Font.ITALIC, 24);

abstract void draw(Graphics graphics, Suits suit,
Rank rank);
private static void drawBorder(Graphics graphics)
{
graphics.setColor(Color.BLACK);
graphics.drawRect(0, 0, Card.WIDTH - 1, Card.HEIGHT - 1);
graphics.drawRect(1, 1, Card.WIDTH - 3, Card.HEIGHT - 3);
}
}


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

class Card extends JPanel implements Comparable<Card>
{
public static final int WIDTH = 60, HEIGHT = 80;
private Rank rank;
private Suits suit;
private CardOrientation orientation;

public Card(int cardOrdinal)
{
setCard(cardOrdinal);
}
public int compareTo(Card otherCard)
{
return rank.compareTo(otherCard.rank);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public Dimension getPreferredSize()
{
return new Dimension(WIDTH, HEIGHT);
}
public void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
orientation.draw(graphics, suit, rank);
}
public void setCard(int cardOrdinal)
{
int suitOrdinal = cardOrdinal / Rank.values().length;
int rankOrdinal = cardOrdinal % Rank.values().length;

this.rank = Rank.aRank(rankOrdinal);
this.suit = Suits.aSuit(suitOrdinal);;
orientation = CardOrientation.FACE_DOWN;
}
public void turnFaceUp()
{
orientation = CardOrientation.FACE_UP;
repaint();
}
}


class Deck
{
private static final int CARDS_IN_DECK = 52;
private int cards[];
private int nextCard;

public Deck()
{
shuffle();
}
public boolean isEmpty()
{
return nextCard == CARDS_IN_DECK;
}
public int deal()
{
return cards[nextCard++];
}
public void shuffle()
{
cards = Permutation.randomize(CARDS_IN_DECK);
nextCard = 0;
}
}

import java.awt.*;
import javax.swing.*;
import static projectJS.*;

abstract class Players extends JPanel
implements Comparable<Players>
{
public static final Font cardFont =
new Font("Helvetica", Font.BOLD, 16);
private static final int GAP = 10;
private JPanel innerPanel = new JPanel();
private Label label;
private Card card;
private String name;

public Players(String name, Deck deck)
{
this.name = name;
card = new Card(deck.deal());
setLayout(new BorderLayout(GAP, GAP));
setBackground(darkGreen);
label = new Label(name, Label.CENTER);
label.setBackground(Color.white);
label.setFont(cardFont);
add(label, BorderLayout.NORTH);
innerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
innerPanel.setBackground(darkGreen);
innerPanel.add(card);
add(innerPanel, BorderLayout.SOUTH);
}
public int compareTo(Players otherPlayer)
{
return card.compareTo(otherPlayer.card);
}
public void deal(Deck deck)
{
if (deck.isEmpty())
deck.shuffle();
card.setCard(deck.deal());
repaint();
}
abstract public int pay(int winnings, int bet);
public String toString()
{
return name;
}
public void turnFaceUp()
{
card.turnFaceUp();
}
}


class Dealer extends Players
{
public Dealer(Deck deck)
{
super ("Dealer", deck);
}
public int pay(int winnings, int bet)
{
return winnings - bet;
}
}


class Opponent extends Players
{
public Opponent(Deck deck)
{
super ("Opponent", deck);
}
public int pay(int winnings, int bet)
{
return winnings + bet;
}
}

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

class BoardPanel extends JPanel implements ActionListener
{
private static final int GAP = 20;
private Deck deck = new Deck();
private JPanel controls = new JPanel();
private Players dealer = new Dealer(deck),
opponent = new Opponent(deck);
private JButton deal = new JButton("Deal"),
play = new JButton("Play");

public BoardPanel()
{
setBackground(projectJS.darkGreen);
setLayout(new BorderLayout(GAP, GAP));
controls.setLayout(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
controls.setBackground(projectJS.darkGreen);
controls.add(deal);
controls.add(play);
add(dealer, BorderLayout.WEST);
add(controls, BorderLayout.CENTER);
add(opponent, BorderLayout.EAST);
deal.addActionListener(this);
play.addActionListener(this);
deal.setEnabled(false);
}
public void actionPerformed(ActionEvent event)
{
Object object = event.getSource();

if (object == deal)
{
dealer.deal(deck);
opponent.deal(deck);
deal.setEnabled(false);
play.setEnabled(true);
winningsPanel.clearMessage();
repaint();
}
else if (object == play)
{
dealer.turnFaceUp();
opponent.turnFaceUp();
if (dealer.compareTo(opponent) >= 0)
winningsPanel.updateWinnings(dealer);
else
winningsPanel.updateWinnings(opponent);
deal.setEnabled(true);
play.setEnabled(false);
}
}
public void shuffle()
{
deck.shuffle();
dealer.deal(deck);
opponent.deal(deck);
deal.setEnabled(false);
play.setEnabled(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static projectJS.*;

class WinningsPanel extends JPanel implements ItemListener
{
private static final int GAP = 20, TEXT_WIDTH = 20;
private static final Font winningsFont =
new Font("Sans Serif", Font.BOLD, 14);
private JTextField winnings = new JTextField(TEXT_WIDTH),
message = new JTextField(TEXT_WIDTH);
private JPanel betPanel = new JPanel();
private JRadioButton bet50 = new JRadioButton("$50", false),
bet100 = new JRadioButton("$100", true),
bet150 = new JRadioButton("$150", false);
private ButtonGroup betGroup = new ButtonGroup();
private int bet = 100, winningsAmount = 1000;

public WinningsPanel()
{
JLabel betLabel = new JLabel("Bet");

setBackground(darkGreen);
setLayout(new BorderLayout(GAP, GAP));
winnings.setFont(winningsFont);
winnings.setHorizontalAlignment(JTextField.CENTER) ;
winnings.setText("Winnings = $ " + winningsAmount);
message.setFont(winningsFont);
message.setHorizontalAlignment(JTextField.CENTER);
betGroup.add(bet50);
betGroup.add(bet100);
betGroup.add(bet150);
betLabel.setFont(winningsFont);
bet50.setFont(winningsFont);
bet100.setFont(winningsFont);
bet150.setFont(winningsFont);
bet50.addItemListener(this);
bet100.addItemListener(this);
bet150.addItemListener(this);
betPanel.add(betLabel);
betPanel.add(bet50);
betPanel.add(bet100);
betPanel.add(bet150);
add(betPanel, BorderLayout.NORTH);
add(winnings, BorderLayout.CENTER);
add(message, BorderLayout.SOUTH);
}
public void clearMessage()
{
message.setText("");
}
public void init()
{
bet = 100;
bet100.setSelected(true);
winningsAmount = 1000;
winnings.setText("Winnings = $ " + winningsAmount);
message.setText("");
}
public void itemStateChanged(ItemEvent event)
{
Object object = event.getSource();

if (object == bet50)
bet = 50;
else if (object == bet100)
bet = 100;
else if (object == bet150)
bet = 150;
}
public void updateWinnings(Players winner)
{
winningsAmount = winner.pay(winningsAmount, bet);
winnings.setText("Winnings = $ " + winningsAmount);
message.setText(winner.toString() + " wins");
}
}

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

class WinningsPanel extends JPanel implements ItemListener
{
private static final int GAP = 20, TEXT_WIDTH = 20;
private static final Font winningsFont =
new Font("Sans Serif", Font.BOLD, 14);
private JTextField winnings = new JTextField(TEXT_WIDTH),
message = new JTextField(TEXT_WIDTH);
private JPanel betPanel = new JPanel();
private JRadioButton bet50 = new JRadioButton("$50", false),
bet100 = new JRadioButton("$100", true),
bet150 = new JRadioButton("$150", false);
private ButtonGroup betGroup = new ButtonGroup();
private int bet = 100, winningsAmount = 1000;

public WinningsPanel()
{
JLabel betLabel = new JLabel("Bet");

setBackground(darkGreen);
setLayout(new BorderLayout(GAP, GAP));
winnings.setFont(winningsFont);
winnings.setHorizontalAlignment(JTextField.CENTER) ;
winnings.setText("Winnings = $ " + winningsAmount);
message.setFont(winningsFont);
message.setHorizontalAlignment(JTextField.CENTER);
betGroup.add(bet50);
betGroup.add(bet100);
betGroup.add(bet150);
betLabel.setFont(winningsFont);
bet50.setFont(winningsFont);
bet100.setFont(winningsFont);
bet150.setFont(winningsFont);
bet50.addItemListener(this);
bet100.addItemListener(this);
bet150.addItemListener(this);
betPanel.add(betLabel);
betPanel.add(bet50);
betPanel.add(bet100);
betPanel.add(bet150);
add(betPanel, BorderLayout.NORTH);
add(winnings, BorderLayout.CENTER);
add(message, BorderLayout.SOUTH);
}
public void clearMessage()
{
message.setText("");
}
public void init()
{
bet = 100;
bet100.setSelected(true);
winningsAmount = 1000;
winnings.setText("Winnings = $ " + winningsAmount);
message.setText("");
}
public void itemStateChanged(ItemEvent event)
{
Object object = event.getSource();

if (object == bet50)
bet = 50;
else if (object == bet100)
bet = 100;
else if (object == bet150)
bet = 150;
}
public void updateWinnings(Players winner)
{
winningsAmount = winner.pay(winningsAmount, bet);
winnings.setText("Winnings = $ " + winningsAmount);
message.setText(winner.toString() + " wins");
}
}


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

class GameFrame implements ActionListener
{
private static final int GAP = 20, WIDTH = 450, HEIGHT = 380;
private JFrame frame;
private JMenuItem newGameItem = new JMenuItem("New Game"),
shuffleItem = new JMenuItem("Shuffle"),
exitItem = new JMenuItem("Exit");
private BoardPanel boardPanel;
private WinningsPanel winningsPanel;

public GameFrame(String name, BoardPanel boardPanel,
WinningsPanel winningsPanel)
{
JMenu gameMenu = new JMenu("Game");
JMenuBar menuBar = new JMenuBar();;

this.boardPanel = boardPanel;
this.winningsPanel = winningsPanel;
frame = new JFrame(name);
frame.setSize(HEIGHT, WIDTH);
frame.getContentPane().setLayout(
new FlowLayout(FlowLayout.CENTER, GAP, GAP));
frame.getContentPane().setBackground(darkGreen);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
gameMenu.add(newGameItem);
gameMenu.add(shuffleItem);
gameMenu.add(exitItem);
menuBar.add(gameMenu);
newGameItem.addActionListener(this);
shuffleItem.addActionListener(this);
exitItem.addActionListener(this);
frame.setJMenuBar(menuBar);
frame.setSize(WIDTH, HEIGHT);
frame.add(boardPanel, BorderLayout.NORTH);
frame.add(winningsPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent event)
{
Object object = event.getSource();

if (object == newGameItem)
{
boardPanel.shuffle();
winningsPanel.init();
frame.repaint();
}
else if (object == shuffleItem)
{
boardPanel.shuffle();
frame.repaint();
}
else if (object == exitItem)
frame.setVisible(false);
}
public void display()
{
frame.setVisible(true);
}
}


import java.awt.*;

public class projectJS
{
public static final Color darkGreen = new Color(0, 128, 0),
lightYellow = new Color(255, 255, 12;
public static WinningsPanel winningsPanel =
new WinningsPanel();
public static BoardPanel boardPanel =
new BoardPanel();;

public static void main(String[] args)
{
GameFrame frame = new GameFrame("Card Game of War",
boardPanel, winningsPanel);

frame.display();
}
}

Theese are the compile errors that i can't figure out and for
GameFrame, WinningsPanel,BoardPanel,Players I canged the statement
import static GameOfWar to
import static projectJS.*; and it doesn't work and i can't figure it
out or the rest of the errors. If anyone can help or suggest i would
appreciate it Thanks Judith spurlock


compile errors


C:\>javac projectJS.java
..\WinningsPanel.java:5: cannot find symbol
symbol: class projectJS
import static projectJS.*;
^
..\BoardPanel.java:5: cannot find symbol
symbol: class projectJS
import static projectJS.*;
^
..\Players.java:4: cannot find symbol
symbol: class projectJS
import static projectJS.*;
^
..\GameFrame.java:6: cannot find symbol
symbol: class projectJS
import static projectJS.*;
^
..\WinningsPanel.java:25: cannot find symbol
symbol : variable darkGreen
location: class WinningsPanel
setBackground(darkGreen);
^
..\BoardPanel.java:42: cannot find symbol
symbol : variable winningsPanel
location: class BoardPanel
winningsPanel.clearMessage();
^
..\BoardPanel.java:50: cannot find symbol
symbol : variable winningsPanel
location: class BoardPanel
winningsPanel.updateWinnings(dealer);
^
..\BoardPanel.java:52: cannot find symbol
symbol : variable winningsPanel
location: class BoardPanel
winningsPanel.updateWinnings(opponent);
^
..\Players.java:22: cannot find symbol
symbol : variable darkGreen
location: class Players
setBackground(darkGreen);
^
..\Players.java:28: cannot find symbol
symbol : variable darkGreen
location: class Players
innerPanel.setBackground(darkGreen);
^
..\Deck.java:23: cannot find symbol
symbol : variable Permutation
location: class Deck
cards = Permutation.randomize(CARDS_IN_DECK); // I
don't know what Permutation means
^
..\GameFrame.java:30: cannot find symbol
symbol : variable darkGreen
location: class GameFrame
frame.getContentPane().setBackground(darkGreen);
^
12 errors

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The War Criminal about to Start another War Ctrl¤/Alt¤/Del¤ Computer Support 30 11-30-2010 02:45 PM
I need Java Runtime Enviroment coolwebsites@gmail.com Computer Support 5 01-01-2007 05:55 AM
i'm needing help with compile errors for java GUI enviroment Game of war judith Java 0 11-01-2006 07:43 PM
i need help with this card game! kingyof2thejring C Programming 8 11-21-2004 05:38 PM
Dell Inprion 1100 see's network with air card and regular network card but does not see the card help! Paul Tutle Sr. - OCC Computer Support 1 10-01-2004 09:23 PM



Advertisments