The Artima Developer Community
Sponsored Link

Java Answers Forum
Hi Lo Card Game

1 reply on 1 page. Most recent reply: Jul 13, 2004 1:57 PM by Nate Hultman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Nate Hultman

Posts: 8
Nickname: vtgunner14
Registered: May, 2004

Hi Lo Card Game Posted: Jul 13, 2004 12:36 PM
Reply to this message Reply
Advertisement
Just a quick question for anyone on here. Has anyone ever had to make a program with that title or sound anything like that. Please let me know as im in a bind with this program that I have to do by friday. Thanks alot


Nate Hultman

Posts: 8
Nickname: vtgunner14
Registered: May, 2004

Re: Hi Lo Card Game Posted: Jul 13, 2004 1:57 PM
Reply to this message Reply
This is what I have so far for the CardGame Class, It is relatively long.
package cardgame;
// Game program with a deck of cards using the AWT & double buffering
// Nate Hultman
// 7/11/04
 
 
 
// the following imports are for applet, awt graphics and events
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
 
public class CardGame  extends Applet implements ActionListener,
  MouseListener, Runnable
{ //Two listeners implemented and Runnable is needed for threads
 
	//*********************declaration of global variables **********************************
	private Image [] deckOfCards = new Image[52];       // array of deck of cards
	private Image table;                               //table image and back of card image
	private Image backOfCard;                          // image object for back of card.
	private Image workspace;                           // needed for double buffering
	private MediaTracker tracker;                      //Media Tracker needed to load images
	private BlueButton dealButton = new BlueButton("Deal");    //creates the deal button
	private BlueButton directionsButton = new BlueButton ("Directions");  // creates the Directions button
	private TextField moneyBox = new TextField("0",20);  // creates TextField for bet $
	private Label chosenFirstCardLabel = new Label ("First Card Was Chosen");
	private Label chosenSecondCardLabel = new Label ("Second Card Was Chosen");
	private Label chosenThirdCardLabel = new Label ("Third Card Was Chosen");
	int first =0;            // the random number generated for first, second, third cards on table
	int second=0;
	int third=0;
	Graphics g;           //allows us to draw on Graphics object g anywhere instead of just paint method
	MyFrame directionsFrame = new MyFrame ("Game Rules"); // creates the MyFrame for directions
 
 
	public void init()
	{
	  workspace = createImage(getSize().width,getSize().height);  // creates blank image size of applet
		g = workspace.getGraphics();         // creates the Graphics g object.
		addMouseListener(this);              // listen for mouse events such as clicks on cards
		setLayout(null);                     // means every GUI will need to be sized and located
 
		//**************add GUIs to the applet ***********************************************
 
		dealButton.setLocation(150,10);
		dealButton.setSize(75,35);
		add(dealButton);  // place shuffle button on applet
		dealButton.addActionListener(this);
 
		directionsButton.setLocation(280,10);
		directionsButton.setSize(100,35);
		add(directionsButton);
		directionsButton.addActionListener(this);
 
		moneyBox.setLocation(450,15);
		moneyBox.setSize(35,25);
		add (moneyBox);
		moneyBox.addActionListener(this);
 
		/*  **************************************************************************************
		When you have lots of images to load into a program it is best to use the
		Media Tracker.  Without the Media Tracker, the computer will begin downloading images
		to the Internet user and will move on in program while the download is happening.  It is possible
		that you will click on a card and the image has not been downloaded yet so it won't appear.  With
		MediaTracker used, the computer will wait for all images that are loaded into tracker with
		a certain code (0 in this case) before it proceeds with rest of program.  This way you are assured
		that the program will work properly and all images will be there when you need them.
		******************************************************************************************* */
		showStatus("Please wait while loading pictures for game");  // shows message on status line
		MediaTracker  myTracker = new MediaTracker(this);
		table = getImage(getCodeBase(), "table.jpg");
		myTracker.addImage(table,0); // add table image to tracker
		backOfCard = getImage(getCodeBase(), "back.JPG");
		myTracker.addImage(backOfCard,0);// adds backside of card to tracker
		for (int i=0; i<52; i++)
		{
			// puts card0.JPG into deckofCards[0] etc.
			deckOfCards[i] = getImage(getCodeBase(),"card"+i+".JPG");
			// puts deckofCards[i] into tracker with code of 0, etc.
			myTracker.addImage(deckOfCards [i], 0);
		}// end for loop
		// Tell the computer to wait until all images with code 0 are loaded.
		try {myTracker.waitForID(0); }  catch (InterruptedException e) {}
 
		g.drawImage(table,0,0,this);  // draws only the table on the screen to begin.
	} // end init
 
 
 
	public void paint (Graphics screen)
	{
		screen.drawImage(workspace,0,0,this);  //displays the current image of workspace
 
	} // end paint
 
 
 
	public void update(Graphics screen)
	{
	  /* The normal update method that is called when you issue a repaint generally contains:
		a setting of the background color,  drawing a rectangle the size of the entire applet with
		the background color, setting the foreground color, and then a call to the paint method.
		Thus, the update method will show a blank screen and then show whatever you have in paint
		method.  This causes a flicker because you will see a blank screen, then the image, then blank screen
		and then the image.  By "overloading" this method in our program, it will use this update method
		instead of the update method that is in the Graphics class.
		*/
 
	  paint(screen);
	}
 
 
	public void actionPerformed (ActionEvent e)
	{
		// ************* The following checks to see which button or Textfield was just entered
		if (e.getSource() == dealButton)
			dealing();
		else if (e.getSource() == directionsButton)
			displayDirections();   // this calls the direction method for Directions Frame
		else if (e.getSource() == moneyBox)
			checkMoney();
 
	}// ends actionPerformed
 
 
 
	public void mouseClicked (MouseEvent e)
	{
		int x, y;
		x = e.getX();
		y = e.getY();
 
		// Gets position of mouseclick and then turns over first card and displays label
		if ( ( x>=150) && (x<=220) && (y>=50) && (y<=145))
		{
			g.drawImage(deckOfCards [first],150,50,this);
			repaint();  // makes the computer display card on screen.
			chosenFirstCardLabel.setLocation(150,175);
			chosenFirstCardLabel.setSize(150,25);
			add(chosenFirstCardLabel);
		}
 
		// Gets position of mouseclick and then turns over second card and displays label
		if ( ( x>=300) && (x<=370) && (y>=50) && (y<=145))
		{
			g.drawImage(deckOfCards [second],300,50,this);
			repaint();  // makes the computer display card on screen.
			chosenSecondCardLabel.setLocation(300,175);
			chosenSecondCardLabel.setSize(150,25);
			add(chosenSecondCardLabel);
		}
 
		// Gets position of mouseclick and then turns over third card and displays label
		if ( ( x>=450) && (x<=520) && (y>=50) && (y<=145))
		{
			g.drawImage(deckOfCards [third],450,50,this);
			repaint();  // makes the computer display card on screen.
			chosenThirdCardLabel.setLocation(450,175);
			chosenThirdCardLabel.setSize(150,25);
			add(chosenThirdCardLabel);
		}
 
	}// ends mouseClicked
 
	// empty methods needed for MouseListener
	public void mousePressed(MouseEvent e)  {}
	public void mouseReleased(MouseEvent e)  {}
	public void mouseEntered(MouseEvent e)  {}
	public void mouseExited(MouseEvent e)  {}
 
 
 
	public void dealing ()
	{
		/*  ***************************************************************************************
		The following will generate a random number between 0 and 51.  We use Math.Random()*52
		because it generates up to number in parenthesis but not including that number.  Since
		Math.Random generates a double (decimal) number and we want an integer number, we must cast the
		result with int.  The first number generated is placed into variable called first.  Let's say it
		generates the random number of 3.  This means that for the first card, we will use the element
		3 in the deckOfCards which is 2 of hearts.  When we do the second card, we don't want the computer
		to have it the same as the first card, so it is placed into a dowhile that says to keep generating
		random numbers for second until they are not equal.  The same is done for the third card.  With this
		logic, we should end up with 3 numbers in first, second, and third that are unique.
		******************************************************************************************* */
 
		first = (int)(Math.random()*52);  // generates an int from 0 to 51  for first card
 
		do
		{
			second = (int)(Math.random()*52);   // generates an int from 0 to 51
		}
		while(second == first); //end do-while Keeps trying to get second random number if second is equal to first
 
 
		// The following dowhile keeps trying to get third random number if third is equal to first OR second
		do
		{
			third  = (int)(Math.random()*52);   // generates an int from 0 to 51
		}
		while(third == second || third == first); //end do-while
 
		// Begin a thread to do the animation of the cards moving on table.  Whenever a thread is created,
		// the run method will be called and this thread will keep going until the run method is complete.
		Thread runner = new Thread(this);// a thread is needed for the animation of dealing cards.   runner = new Thread(this);
		runner.start();
 
		remove(chosenFirstCardLabel);
		remove(chosenSecondCardLabel);
		remove(chosenThirdCardLabel);
 
	}// ends dealing method
 
 
 
	public void run ()
	{ // This method is called whenever a Thread is started.
	  int xPosition, yPosition;
 
		// placement of first card on table
		for (yPosition = 0;  yPosition <=50; yPosition +=10)
		{
			g.drawImage(table, 0, 0, this);
			g.drawImage(backOfCard, 150, yPosition, this);
			repaint();
			try {Thread.sleep(100);} catch (InterruptedException e ) { }
		}
 
		// placement of second card on table while first card stays in place
		for (yPosition=0; yPosition<=50; yPosition +=10)
		{
			g.drawImage(table, 0, 0, this);
			g.drawImage(backOfCard, 150, 50, this);  //first card stays in place
			g.drawImage(backOfCard,300,yPosition, this);
			repaint();
			try {Thread.sleep(100);} catch (InterruptedException e ) { }
		}
 
		// placement of third card on table while first & second cards stay in place
		for (yPosition=0; yPosition<=50; yPosition +=10)
		{
			g.drawImage(table, 0, 0, this);
			g.drawImage(backOfCard, 150, 50, this);// first card stays in place
			g.drawImage(backOfCard, 300, 50, this);// second card stays in place
			g.drawImage(backOfCard, 450, yPosition, this);
			repaint();
			try {Thread.sleep(100);} catch (InterruptedException e ) { }
		}
	}// ends run method
 
 
	public void displayDirections()
	{
		// ... place your statements here to display your directions in MyFrame or JFrame
		directionsFrame.setSize(200,100);      // How large do you want frame?
		directionsFrame.setLocation(450,350); // where do you want frame on screen?
		directionsFrame.setVisible(true);     // makes it show frame on screen
	}
 
	public void checkMoney()
	{
		//  ... place your statements here to check the amount of money bet
		//  ....do exception handling so if someone enters letters it displays error on table.
 
	} // end checkMoney
 
}// ends entire program
 
 

Flat View: This topic has 1 reply on 1 page
Topic: SWT Accelerartor Previous Topic   Next Topic Topic: silent install (need help)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use