Hi/Lo code problems

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Hi/Lo code problems

 
0
  #1
Dec 1st, 2008
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class HiLo extends JApplet
{
Image cardImages;
/**
* An image that holds the pictures of all the cards
*/

public void init()
{
/**
* This method loads the card picture and lays out the applet.
*/

cardImages = getImage(getCodeBase(), "smallcards.gif");

setBackground(Color.blue);

HiLo board = new HiLo();
getContentPane().add(board, BorderLayout.CENTER);

JPanel panel = new JPanel();
panel.setBackground(Color.green);
getContentPane().add(panel, BorderLayout.SOUTH);

JButton hi = new JButton("Higher");
hi.addActionListener(board);
buttonPanel.add(hi);

JButton lo = new JButton("Lower");
lo.addActionListener(board);
buttonPanel.add(lo);

JButton newGame = new JButton("New Game");
newGame.addActionListener(board);
buttonPanel.add(newGame);
}

public Insets getInsets()
{
/**
* Determines the boarders of the container
*/

return new Insets(4,3,4,3);
}
}

class HiLoCanvas extends JPanel implements ActionListener
{
/**
* The Canvas component represents a rectangular area on the screen
* which the application can draw or trap input events.
* [Help from Dad in this class]
*/

Deck deck;
/**
* The deck being used
*/
Hand hand;
/**
* Cards already dealt
*/
String message;
/**
* The message that is shown, saying the state of the game.
*/


boolean gameInProgress;
/**
* True when game begins, since it's in progress, and false at the end,
* when it is no longer in progress.
*/

Font mediumFont;
/**
* The font being used to show the messages.
*/

Font smallFont;
/**
* Being used to draw the cards.
*/

HiLoCanvas()
{
/**
* HiLoCanvas() is the constructor that creates and sets colors,
* and starts the games.
* [Help recieved from Dad]
*/

setBackground(Color.darkGray);
/**
* Sets the background color to dark gray.
*/
setForeground(Color.red);
/**
* Sets foreground to red
*/

smallFont = new Font("Arial", Font.PLAIN, 12);
/**
* Makes the small font that is being used to draw the cards of font
* type Arial, also plain (instead of italics, bold, etc.), and font
* size 12.
*/
mediumFont = new Font("Arial", Font.BOLD, 15);
/**
* The font being used to show the messages is font face Arial, bold,
* and size 15.
*/
doNewGame();
}

public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();

if (command.equals("Higher"))
{
doHi();
}
else if (command.equals("Lower"))
{
doLo();
}
else if (command.equals("New Game"))
{
doNewGame();
}
}

void doHi()
{
/**
* this is called by the actionPerformed() when the "Higher" button
* is clicked on. Game ends if user guessed wrong, or if they have
* correctly guessed four times.
*/
if (gameInProgress==false)
{
message = "Click\"New Game\" first";
repaint();
return;
}
hand.addCard(deck.dealCard());
/**
* Deals a card to the hand
*/
int cardCt = hand.getCardCount();
Card thisCard = hand.getCard(cardCt-1);
/**
* This is the card just dealt
*/
Card prevCard = hand.getCard(cardCt-2);
/**
* This is the previous card
*/
if(thisCard.getValue()<prevCard.getValue())
{
gameInProgress = false;
message = "Sorry, you lose!";
}
else if (thisCard.getValue()==prevCard.getValue())
{
gameInProgress=false;
message= "It's a tie--you lose!";
}
else if (cardCt==4)
{
gameInProgress=false;
message="Congratulations! You win!";
}
else
{
gameInProgress= true;
message = "Correct! Go for "+ cardCt +".";
}
repaint();
}

void doLo();
{
/**
* Same as doHi, but instead is called when the "Lower" button is
* selected.
*/
if (gameInProgress==false)
{
/**
* If the game is not in progress, if it's ended, then an
* error message will show.
*/
message="Click\"New Game\" first!";
repaint();
return;
}
hand.addCard(deck.dealCard());
int cardCt=hand.getCardCount();
Card thisCard=hand.getCard(cardCt-1);
Card prevCard=hand.getCard(cardCt-2);
if(thisCard.getValue()>prevCard.getValue())
{
gameInProgress=false;
message="Sorry! You lose!";
}
else if(thisCard.getValue()==prevCard.getValue())
{
gameInProgress=false;
message="It's a tie--you lose!";
}
else if(cardCt==4)
{
gameInProgress=false;
message="Congratulations! You win!";
}
else
{
gameInProgress=true;
message="Correct! Go for "+ cardCt+".";
}
repaint();
}

void doNewGame()
{
/**
* Called by actionPerformed() and the constructor, when user clicks
* on "New Game" button, a new game starts
*/
if (gameInProgress)
{
/**
* If a game is currently in progress, and error message will show
*/
message="You haven't finished the game yet!";
repaint();
return;
}
deck=new Deck();
hand = new Hand();
/**
* creat deck and hand used in this game
*/
deck.shuffle();
hand.addCard(deck.dealCar());
message="Do you think the next card is Higher or Lower?";
gameInProgress=true;
repaint();
}

public void paintComponent(Graphics g)
{
/**
* Draws message at the bottom of the canvas, and draws all of the
* cards spread across the canvas. When game is in progress, another
* card is dealt to represent the card that will be dealt next
* ******
*/
super.paintComponent(g);
g.setFont(mediumFont);
g.drawString(message,10, getSize().height-10);
g.setFont(smallFont);
int cardCt=hand.getCardCount();
for (int i=0; i<cardCt;i++);
drawCard(g, hand.getCard(i); 30 + i * 70, 10);
if(gameInProgress)
drawCard(g,null,30+cardCt*70, 10);

}

void drawCard(Graphis g, Card card, int x, int y)
{
/**
* Draws and 40 by 60 rectangle. The Card is drawn in the graphics g.
* When the card is null, a face-down card is then drawn.
* *********
*/
if(card==null)
{
g.setColor(Color.blue);
g.fillRect(x,y,40,60);
g.setColor(Color.white);
g.drawRect(x+3, y+3, 33, 53);
g.drawRect(x+4, y+4, 31, 51);
}
else
{
int row = 0;
switch(card.getSuit())
{
case Card.CLUBS: row=0; break;
case Card.HEARTS: row=1; break;
case Card.SPADES: row=2; break;
case Card.DIAMONDS: row=3; break;
}

int sx,sy
/**
* The coordinates of the top left corner in the image
*/
sx=40*(card.getValue()-1);
sy=60*row;
g.drawImage(cardImages,x,y,x+40,y+60,sx,sy,sx+60,this);
}
}

}


}

public class Card
/**
* This is the same Card class that we were required to create previously
*/
{
private int value;
private int suit;

public void Card()
{
value = 1;
suit = 3;
}

public Card(int value, int suit)
{
value = value;
value = suit;
}

public int getSuit()
{
return suit;
}

private void setSuit(int newsuit)
{
suit = newsuit;
}

public int getValue()
{
return value;
}

private void setValue(int newVal)
{
value = newVal;
}


public String getSuitString()
{
if(suit==1)
{
return "Clubs";
}
if(suit==2)
{
return "Diamonds";
}
if(suit==3)
{
return "Hearts";
}
else
{
return "Spades";
}
}

public String getValueString()
{
if(value==1)
{
return "Ace";
}
if(value==2)
{
return "2";
}
if(value==3)
{
return "3";
}
if(value==4)
{
return "4";
}
if(value==5)
{
return "5";
}
if(value==6)
{
return "6";
}
if(value==7)
{
return "7";
}
if(value==8)
{
return "8";
}
if(value==9)
{
return "9";
}
if(value==10)
{
return "10";
}
if(value==11)
{
return "Jack";
}
if(value==12)
{
return "Queen";
}
else
{
return "King";
}
}

public boolean equals(Card card)
{
boolean equals = false;

if(getValue() == card.getValue()
&& getSuit() == card.getSuit())
{
equals = true;
}
return equals;
}

public Card clone()
{
Card equals = new Card(getValue(), getSuit());
return equals;

}

}

public class Deck
/**
* This Deck class has been slightly modified from the one we were required to
* create. In this class it keeps track of how many cards have been dealt and
* how many are remaing after cards have been dealt, as well as actually dealing
* cards.
*/
{
private Card[] deck;
private int cardsUsed;
/**
* The number of cards that have been dealt
*/
public Deck()
{
deck = new Card[52];
int cardCt = 0;
/**
* The number of cards created
*/
for (int i=0; i<=3; i++)
{
for (int j=1;j<=13;j++)
{
deck[cardCt]=new Card(j,i);
cardCt++;
}
}
cardsUsed=0;
}

public void shuffle()
{
current Card= -1;

for(int i=0; i<deck.length; i++)
{
int rand = (int) (Math.random()*52);
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
cardsUsed=0;
}
public int cardsRemaing()
{
/**
* When cards are dealt, the number remaining decrease. This returns
* the number remaing
*/
return 52- cardsUsed;
}

public Card dealCard()
{
/**
* Deals a card and returns it
*/
if (cardsUsed==52)
shuffle();
cardsUsed++;
return deck[cardsUsed-1];
}
}
}

This is what I have so far for my code for the card game of Hi/Lo. The line in green gets highlighted and an error message comes up saying " ')' expected". I'm not sure why, since I have the parantheses, and I tried brackets, but I got the same error message. I'm also not sure where to go from here. I'm doing this with BlueJ, so I don't need to create a main method. But how do I get everything together and working?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Hi/Lo code problems

 
0
  #2
Dec 1st, 2008
Originally Posted by krhillery View Post
This is what I have so far for my code for the card game of Hi/Lo. The line in green gets highlighted ... and an error message comes up saying " ')' expected". I'm not sure why, since I have the parantheses, and I tried brackets, but I got the same error message. I'm also not sure where to go from here. I'm doing this with BlueJ, so I don't need to create a main method. But how do I get everything together and working?
well ... I don't see a line in green, so ... which line are we talking about? also, use Code-tags, it makes your code much easier to read
Originally Posted by krhillery View Post
... and an error message comes up saying " ')' expected". I'm not sure why, since I have the parantheses, and I tried brackets, but I got the same error message.
propably because even though you don't intend to close the brackets at that location, there's an error in your code what makes the compiler expect an end-bracket
Originally Posted by krhillery View Post
I'm doing this with BlueJ, so I don't need to create a main method. But how do I get everything together and working?
by creating and using a main method would be my first guess
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: Hi/Lo code problems

 
0
  #3
Dec 1st, 2008
the green line is in the paintComponent method.

And I'm taking a highschool beginner computer science class, so we are not expected to be able to create main methods [we've never been taught how, or told to figure it out, and im confused as is so im not sure if i should bother trying to learn how to make one]. Hence using BlueJ
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Hi/Lo code problems

 
0
  #4
Dec 1st, 2008
Originally Posted by krhillery View Post
the green line is in the paintComponent method.

And I'm taking a highschool beginner computer science class, so we are not expected to be able to create main methods [we've never been taught how, or told to figure it out, and im confused as is so im not sure if i should bother trying to learn how to make one]. Hence using BlueJ
  1. public static void main (String args[]){
  2. System.out.println("this is a main method");
  3. // here you call your classes and methods.
  4. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Hi/Lo code problems

 
0
  #5
Dec 1st, 2008
  1. for (int i=0; i<cardCt;i++);
  2. drawCard(g, hand.getCard(i); 30 + i * 70, 10);
replace the above with:
  1. for (int i=0; i<cardCt;i++)
  2. drawCard(g, hand.getCard(i), 30 + i * 70, 10);

and replace
  1. void drawCard(Graphis g, Card card, int x, int y)
with
  1. void drawCard(Graphics g, Card card, int x, int y)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: Hi/Lo code problems

 
0
  #6
Dec 1st, 2008
ohh, silly me! spelling mistakes.

thank you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: Hi/Lo code problems

 
0
  #7
Dec 1st, 2008
For Hi/Lo to work, I need the Deck class and Hand class. For the Deck class to work I need the Card class. I cannot put the card class in the same frame as the Deck class, else it says "class Card is public, should be declared in a file named Card.java" but this is the DECK file that I'm working on.
So I separated card and deck, and inserted a "uses relation" arrow (BlueJ). I have no clue if this will do the same thing, or if there's another way to make it work.

Also, code tags? How do they work? The explanations I've seen so far haven't been very useful.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: Hi/Lo code problems

 
0
  #8
Dec 1st, 2008
  1. public void init()
  2. {
  3. /**
  4. * This method loads the card picture and lays out the applet.
  5. */
  6.  
  7. cardImages = getImage(getCodeBase(), "smallcards.gif");
  8.  
  9. setBackground(Color.blue);
  10.  
  11. HiLo board = new HiLo();
  12. getContentPane().add(board, BorderLayout.CENTER);
  13.  
  14. JPanel panel = new JPanel();
  15. panel.setBackground(Color.green);
  16. getContentPane().add(panel, BorderLayout.SOUTH);
  17.  
  18. JButton hi = new JButton("Higher");
  19. hi.addActionListener(board);
  20. buttonPanel.add(hi);
  21.  
  22. JButton lo = new JButton("Lower");
  23. lo.addActionListener(board);
  24. buttonPanel.add(lo);
  25.  
  26. JButton newGame = new JButton("New Game");
  27. newGame.addActionListener(board);
  28. buttonPanel.add(newGame);
  29. }

the error message says that "hi.addActionListener(board);" cannot be applied.
My Java book said this was the format, and code I've done before that has compiled properly was done the same way. I'm not sure what's wrong?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Hi/Lo code problems

 
0
  #9
Dec 1st, 2008
It can't be applied if "board" does not implement ActionListener.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 13
Reputation: krhillery is an unknown quantity at this point 
Solved Threads: 0
krhillery krhillery is offline Offline
Newbie Poster

Re: Hi/Lo code problems

 
0
  #10
Dec 1st, 2008
  1. class AddButtonListener implements board()
  2. {
  3. JButton higher = new JButton("Higher");
  4. higher.addActionListener(board);
  5. buttonPanel.add(higher);
  6.  
  7. JButton lower = new JButton("Lower");
  8. lower.addActionListener(board);
  9. buttonPanel.add(lower);
  10.  
  11. JButton newGame = new JButton("New Game");
  12. newGame.addActionListener(board);
  13. buttonPanel.add(newGame);
  14. }

class AddButtonListener implements board()
error message: '{' expected.
now really? really? does this code not realize how long I've been working? will it not just compile already? sigh.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1111 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC