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0 0 0 0 0 0 0 8192 16384 32768 65536 131072 262144 ~ ~
19 0 0 0 0 0 0 0 0 0 0 0 0 0 16384 32768 65536 131072 262144 524288 ~
20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32768 65536 131072 262144 524288 1048576
// author: anthony f. ortiz program: keno.java
import java.applet.Applet;
import java.awt.*;
import java.math.*;
public class keno extends Applet
{
private Label bet_label, win1, win2, counter1, counter2;
private TextField bet_textfield;
private Button draw, reset;
private int bet, pay, counter, number_of_picks, flag;
private int numbers [][] = new int [80][4];
private int picks [] = new int [20];
private int keno_picks [] = new int [20];
public void init ()
{
load_components ();
}
public void start ()
{
counter = 100;
counter2.setText ("" + counter);
counter2.invalidate ();
counter2.validate ();
display_components ();
initialize_values ();
initialize_numbers ();
}
// paint the initial board (10 x 8).
public void paint (Graphics g)
{
int count = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
count++;
g.drawRect (195 + j * 25, 140 + i * 25, 25, 25);
g.drawString ("" + count, 195 + j * 25 + 8, 140 + i * 25 + 18);
for (int k = 0; k < 20; k++)
{
if (picks [k] == count)
{
g.setColor (Color.red);
g.drawString ("" + count,
195 + j * 25 + 8,
140 + i * 25 + 18);
g.setColor (Color.black);
}
if (count == keno_picks [k])
{
g.drawString ("X", 195 + j * 25 + 8,
140 + i * 25 + 18);
}
}
}
}
}
// load the components (buttons, labels, textfields).
public void load_components ()
{
Color color = new Color (0, 128, 0);
setBackground (color);
bet_label = new Label ("bet 1-5:");
bet_textfield = new TextField ();
win1 = new Label ("you won:");
win2 = new Label ("");
counter1 = new Label ("counter:");
counter2 = new Label ("");
draw = new Button ("draw");
reset = new Button ("reset");
}
// display the components (buttons, labels, textfields).
public void display_components ()
{
setLayout (null);
bet_label.reshape (25, 145, 75, 10);
add (bet_label);
bet_textfield.reshape (100, 140, 75, 25);
add (bet_textfield);
win1.reshape (25, 175, 75, 10);
add (win1);
win2.reshape (100, 175, 75, 10);
add (win2);
counter1.reshape (25, 205, 75, 10);
add (counter1);
counter2.reshape (100, 205, 75, 10);
add (counter2);
draw.reshape (100, 265, 75, 25);
add (draw);
reset.reshape (100, 315, 75, 25);
add (reset);
}
// picks 20 computer generated numbers.
public void keno_picks ()
{
int same_pick, ii, jj;
for (int i = 0; i < 20; i++)
{
do
{
same_pick = 0;
keno_picks [i] = (int) (Math.random () * 80 + 1);
for (int j = 0; j < keno_picks.length; j++)
{
if (keno_picks [i] == keno_picks [j] && i != j)
{
same_pick = 1;
break;
}
else
{
same_pick = 0;
}
}
}
while (same_pick == 1);
}
}
// find the matches between user and computer.
public void find_matches ()
{
int matches = 0;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < number_of_picks; j++)
{
if (picks [j] == keno_picks [i])
{
matches++;
}
}
}
if ((matches / number_of_picks) >= .66)
{
pay = (int) Math.pow (2.0 * bet, matches);
}
else
{
pay = 0;
}
counter = counter - bet + pay;
win2.setText ("" + pay);
win2.invalidate ();
win2.validate ();
counter2.setText ("" + counter);
counter2.invalidate ();
counter2.validate ();
}
// show the matches with a x through the number.
public void show_picks ()
{
int count = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 20; k++)
{
if (count + 1 == keno_picks [k])
{
getGraphics ().drawString ("X", 195 + j * 25 + 8,
140 + i * 25 + 18);
}
}
count++;
}
}
}
// initialize the squares (the coordinates of the squares).
public void initialize_numbers ()
{
int count = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
numbers [count][0] = 195 + j * 25;
numbers [count][1] = 140 + i * 25;
numbers [count][2] = 195 + j * 25 + 25;
numbers [count][3] = 140 + i * 25 + 25;
count++;
}
}
}
// initialize values (bet, pay, etc.).
public void initialize_values ()
{
bet = 1;
pay = 0;
number_of_picks = 0;
flag = 0;
for (int i = 0; i < 20; i++)
{
picks [i] = 0;
keno_picks [i] = 0;
}
draw.enable (true);
reset.enable (false);
bet_textfield.enable (true);
bet_textfield.setText ("" + 1);
bet_textfield.invalidate ();
bet_textfield.validate ();
win2.setText ("");
win2.invalidate ();
win2.validate ();
int count = 0;
Color color = new Color (0, 128, 0);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
setForeground (color);
getGraphics ().drawString ("X", 195 + j * 25 + 8,
140 + i * 25 + 18);
setForeground (Color.black);
getGraphics ().drawString ("" + (count + 1), 195 + j * 25 + 8,
140 + i * 25 + 18);
count++;
}
}
}
// draw or reset the board.
public boolean action (Event event, Object object)
{
if (event.target == draw && flag == 1)
{
draw.enable (false);
reset.enable (true);
bet_textfield.enable (false);
keno_picks ();
show_picks ();
find_matches ();
return (true);
}
else if (event.target == reset)
{
initialize_values ();
initialize_numbers ();
return (true);
}
else
{
return (false);
}
}
// pick anywhere from 1 to 20 numbers.
public boolean mouseDown (Event e, int x, int y)
{
int count = 0;
if (draw.isEnabled () == true && number_of_picks < 20)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 10; j++)
{
if (x >= numbers [count][0] && x <= numbers [count][2] &&
y >= numbers [count][1] && y <= numbers [count][3])
{
setForeground (Color.red);
getGraphics ().drawString ("" + (count + 1),
195 + j * 25 + 8,
140 + i * 25 + 18);
setForeground (Color.black);
picks [number_of_picks] = count + 1;
numbers [count][0] = 0;
numbers [count][1] = 0;
numbers [count][2] = 0;
numbers [count][3] = 0;
flag = 1;
number_of_picks++;
return (true);
}
count++;
}
}
}
return (true);
}
// determine whether the bet is appropriate (1-5).
public boolean keyUp (Event event, int key)
{
if (event.target == bet_textfield)
{
if (key > 53 || key < 49)
{
bet_textfield.setText ("1");
}
else
{
bet = key - 48;
bet_textfield.setText (bet + "");
}
return (true);
}
else
{
return (false);
}
}
}
BACK TO KENO APPLET.