import java.awt.*;
import java.applet.*;
import java.lang.*;
public class Game extends Applet
{
Graphics sG;
Graphics bG;
Image bI;
Graphics cG;
Image cI;
Color black = new Color(0, 0, 0);
Color ivory = new Color(240, 240, 240);
Color back = new Color(192, 192, 255);
Color red = new Color(255, 0, 0);
Font sfont = new Font("Courier", Font.PLAIN, 12);
int point, state, newstate, statei, newstatei;
public void init()
{
state = 1;
statei = 10;
bI = createImage(this.size().width, this.size().height);
bG = bI.getGraphics();
cI = createImage(this.size().width, this.size().height);
cG = cI.getGraphics();
bG.setFont(sfont);
drawit();
cG.drawImage(bI, 0, 0, this);
cG.setColor(red);
cG.fillOval(5, statei, 10, statei);
}
public void paint(Graphics g)
{
g.drawImage(cI, 0, 0, this);
sG = getGraphics();
}
public void update(Graphics g)
{
g.drawImage(cI, 0, 0, this);
}
public void drawit()
{
bG.setColor(back);
bG.fillRect(0, 0, 201, 346);
onedie(60, 0);
onedie(104,0);
bG.setColor(black);
for (int i = 25; i < 301; i = i + 25)
{
bG.drawLine(0, i, 200, i);
}
bG.drawString("About to start", 20, 20);
bG.drawString("Player has won", 20, 45);
bG.drawString("Player has lost", 20, 70);
bG.drawString("Playing -- Point = 3", 20, 95);
bG.drawString("Playing -- Point = 4", 20, 120);
bG.drawString("Playing -- Point = 5", 20, 145);
bG.drawString("Playing -- Point = 6", 20, 170);
bG.drawString("Playing -- Point = 8", 20, 195);
bG.drawString("Playing -- Point = 9", 20, 220);
bG.drawString("Playing -- Point = 10", 20, 245);
bG.drawString("Playing -- Point = 11", 20, 270);
bG.drawString("Playing -- Point = 12", 20, 295);
}
public void onedie(int x, int pips)
{
int y = 305;
bG.setColor(ivory);
bG.fillRect(x, y, 37, 37);
bG.setColor(black);
bG.drawRect(x, y, 36, 36);
if (pips == 1)
{
bG.fillOval(x + 16, y + 16, 5, 5);
}
if (pips == 2)
{
bG.fillOval(x + 7, y + 7, 5, 5);
bG.fillOval(x + 26, y + 26, 5, 5);
}
if (pips == 3)
{
bG.fillOval(x + 16, y + 16, 5, 5);
bG.fillOval(x + 4, y + 4, 5, 5);
bG.fillOval(x + 29, y + 29, 5, 5);
}
if (pips == 4)
{
bG.fillOval(x + 7, y + 7, 5, 5);
bG.fillOval(x + 26, y + 7, 5, 5);
bG.fillOval(x + 7, y + 26, 5, 5);
bG.fillOval(x + 26, y + 26, 5, 5);
}
if (pips == 5)
{
bG.fillOval(x + 16, y + 16, 5, 5);
bG.fillOval(x + 4, y + 4, 5, 5);
bG.fillOval(x + 29, y + 29, 5, 5);
bG.fillOval(x + 4, y + 29, 5, 5);
bG.fillOval(x + 29, y + 4, 5, 5);
}
if (pips == 6)
{
bG.fillOval(x + 4, y + 7, 5, 5);
bG.fillOval(x + 16, y + 7, 5, 5);
bG.fillOval(x + 28, y + 7, 5, 5);
bG.fillOval(x + 4, y + 26, 5, 5);
bG.fillOval(x + 16, y + 26, 5, 5);
bG.fillOval(x + 28, y + 26, 5, 5);
}
}
public boolean mouseDown(Event evt, int mx, int my)
{
int die1, die2;
if (my > 300)
{
die1 = (int) java.lang.Math.round(
0.5 + 6 * java.lang.Math.random());
die2 = (int) java.lang.Math.round(
0.5 + 6 * java.lang.Math.random());
onedie(60, die1);
onedie(104,die2);
cG.drawImage(bI, 0, 0, this);
cG.setColor(red);
cG.fillOval(5, statei, 10, 10);
sG.drawImage(cI, 0, 0, this);
if (state == 1)
{
point = die1 + die2;
newstate = point + 1;
if (point > 7)
{
newstate = point;
}
if (point == 7)
{
newstate = 2;
}
if (point == 2)
{
newstate = 3;
}
newstatei = newstate * 25 - 15;
for (int y = statei; y <= newstatei; y = y + 1)
{
cG.drawImage(bI, 0, 0, this);
cG.setColor(red);
cG.fillOval(5, y, 10, 10);
sG.drawImage(cI, 0, 0, this);
}
}
if (state > 3)
{
if (die1 + die2 == point)
{
newstate = 2;
newstatei = newstate * 25 - 15;
}
if (die1 + die2 == 7)
{
newstate = 3;
newstatei = newstate * 25 - 15;
}
for (int y = statei; y >= newstatei; y = y - 1)
{
cG.drawImage(bI, 0, 0, this);
cG.setColor(red);
cG.fillOval(5, y, 10, 10);
sG.drawImage(cI, 0, 0, this);
}
}
state = newstate;
statei = newstatei;
}
if (my < 25)
{
init();
sG.drawImage(cI, 0, 0, this);
}
return true;
}
}
Copyright c 1997 by
Frank Wattenberg, Department of Mathematics, Montana State University,
Bozeman, MT 59717