Make your own free website on Tripod.com
/* Keno: The game! Written by Jonathan Held on December 13, 1997. Use of this program for entertainment purposes is permitted. Please be aware that in no way are you permitted to modigy this program without the express written consent of the author. Should you desire to make changes, you can contact the author via email at jsheld@prodigy.net or jsheld@cs.nps.navy.mil Enjoy the game! */ import java.awt.*; import java.applet.*; import java.util.*; import java.net.*; import oval; public class keno extends Applet { //vector of ovals Vector keno_ovals; //max number of ovals int OVALS = 50; //oval characteristics int XSEPARATION = 50, YSEPARATION = 50, HEIGHT = 30, WIDTH = 30; //initial starting position int xpos = 10, ypos = 100; //number player has selected int count = 0; //number both computer and player selected int both_picked = 0; //money int money = 500; //second graphics context Graphics graphics_context_two; //Button to play the game Button play_game; //Button for another game Button next_game; //individual's money TextField players_money; //computer messages and other information Label lab1, lab2, lab3; TextField computer_messages, total_selected, total_matched; //keep track of whether we've played a game or not boolean current_game_played = false; //main thread Thread program_thread = null; //--------------------------------------------------------------------------- //Function: init() //Parameters: none //Return type: none //Purpose: Initializes the applet. //--------------------------------------------------------------------------- public void init() { //initialize our applet display resize(500,350); setLayout(new FlowLayout(FlowLayout.CENTER)); keno_ovals = new Vector(OVALS); //define buttons, labels, etc... play_game = new Button("Play Keno!"); next_game = new Button("New Game"); players_money = new TextField(15); players_money.setBackground(Color.white); players_money.setText("You have: $" + Integer.toString(money)); players_money.setEditable(false); lab1 = new Label("Computer messages: "); lab1.setBackground(Color.cyan); lab2 = new Label("Number selected: "); lab2.setBackground(Color.cyan); lab3 = new Label("Matches: "); lab3.setBackground(Color.cyan); graphics_context_two = getGraphics(); computer_messages = new TextField(40); computer_messages.setBackground(Color.white); computer_messages.setEditable(false); total_selected = new TextField(3); total_selected.setBackground(Color.white); total_selected.setEditable(false); total_matched = new TextField(3); total_matched.setBackground(Color.white); total_matched.setEditable(false); //add buttons, labels, etc.. to applet add(play_game); add(lab1); add(computer_messages); add(players_money); add(next_game); add(lab2); add(total_selected); add(lab3); add(total_matched); //set the background color to orange (I like it!) setBackground(Color.cyan); setup_board(); program_thread = Thread.currentThread(); }//end init() //--------------------------------------------------------------------------- //Function: mouseDown() //Parameters: event - the event that occurred // x - the x-coordinate of the mouse where the event occurred // y - the y-coordinate of the mouse where the event occurred //Return type: boolean - true if this method handles mouseDown events //Purpose: Performs drawing functions when the mouse is clicked over the applet. //--------------------------------------------------------------------------- public boolean mouseDown(Event event, int x, int y){ if (!(current_game_played)){ check_location(x, y); return true; } return false; }//end mouseDown(); //--------------------------------------------------------------------------- //Function: action() //Parameters: event - the event that occurred // obj - not used //Return type: boolean - true if this method handles the action events //Purpose: Determines what action to take when the client clicks on one of //the buttons. //--------------------------------------------------------------------------- public boolean action(Event event, Object obj){ //check to see if the user wants to play a game if (event.target == play_game){ play_the_game(); return true; } //see if the user wants a new game else if (event.target == next_game){ if (current_game_played){ reset_game(); } else { computer_messages.setText("We can play a game with the current board!"); } return true; } return false; }//end action() //--------------------------------------------------------------------------- //Function: paint() //Parameters: g - a device context used for drawing on the applet //Return type: none //Purpose: Draw ovals on the applet. //--------------------------------------------------------------------------- public void paint(Graphics g){ update(g); }//end paint() //--------------------------------------------------------------------------- //Function: paint() //Parameters: g - a device context used for drawing on the applet //Return type: none //Purpose: Draw ovals on the applet. //--------------------------------------------------------------------------- public void update(Graphics g){ //add ovals and numbers to applet oval the_oval; for (int jx=0; jx= 10)){ if (count < 20){ computer_messages.setText("Must pick twenty numbers before playing!"); } else { for (int jx=0; jx<20; jx++){ boolean go_on = false; do { //computer picks a number computer_pick = (int) Math.round((Math.random() * (OVALS-1))); //need to go to the node selected and make sure the computer //hasn't already picked it oval_compare = (oval) keno_ovals.elementAt(computer_pick); if (oval_compare.computer_picked == false){ oval_compare.computer_picked = true; go_on = true; }//end if } while (!(go_on)); //search the ovals the user selected and compare based on //number for (int ix=0; ix= 10)){ reset_game(); } else { showStatus("You are out of money!"); computer_messages.setText("Stop gamblin! You've lost everything..."); } }//end else }//end play_the_game() //--------------------------------------------------------------------------- //Function: drawX() //Parameters: picked_oval - the oval the computer picked //Return type: none //Purpose: Draws an "X" in the oval that the computer selected. //--------------------------------------------------------------------------- private void drawX(int picked_oval){ //let's draw out X's in black graphics_context_two.setColor(Color.green); //differential used for ensuring lines are within the oval int xadj = 7, yadj = 7; oval marked_oval = (oval) keno_ovals.elementAt(picked_oval); //top left to bottom right line graphics_context_two.drawLine(marked_oval.xpos+xadj,marked_oval.ypos+yadj,marked_oval.xpos+WIDTH-xadj,marked_oval.ypos+HEIGHT-yadj); //bottom left to top right line graphics_context_two.drawLine(marked_oval.xpos+xadj,marked_oval.ypos+HEIGHT-yadj,marked_oval.xpos+WIDTH-xadj,marked_oval.ypos+yadj); }//end drawX() //--------------------------------------------------------------------------- //Function: reset_game() //Parameters: none //Return type: none //Purpose: Routine used to set all parameters to default. This allows us to //play the game multiple times. //--------------------------------------------------------------------------- private void reset_game(){ //reset number in common picked and number that user has selected count = both_picked = 0; //reset oval status for (int ix=0; ix= 12) && (both_picked <= 14)) winnings = 500; else if ((both_picked >= 15) && (both_picked <= 17)) winnings = 1000; else if ((both_picked == 18) || (both_picked == 19)) winnings = 2000; else if (both_picked == 20) winnings = 5000; //increase money money += winnings; //notify user he won computer_messages.setText("Congrats! You've won $ " + Integer.toString(winnings)); players_money.setText("You have: $" + Integer.toString(money)); }//end else }//end determine_game_status() }//end Applet keno