Code 1


import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MMapplet extends Applet
implements MouseListener
{

public MMapplet()
{
allowDups = false;
guessChoices = new int[4];
answer = new int[4];
}

public void mouseClicked(MouseEvent mouseevent)
{
}

public void mouseEntered(MouseEvent mouseevent)
{
}

public void mouseExited(MouseEvent mouseevent)
{
}

public void mousePressed(MouseEvent mouseevent)
{
}

public void mouseReleased(MouseEvent mouseevent)
{
int i = tgObj.toTurtleSpaceX(mouseevent.getX());
int j = tgObj.toTurtleSpaceY(mouseevent.getY());
myMouseClickHandler(i, j);
}

int anyMatches()
{
int i = 0;
int ai[] = (int[])answer.clone();
for(int j = 0; j {
for(int k = 0; k {
if(guessChoices[j] != ai[k])
continue;
i++;
ai[k] = -1;
break;
}

}

return i;
}

void drawBox(int i, int j, int k, int l)
{
tgObj.penup();
tgObj.setxy(i, j);
tgObj.setheading(90);
tgObj.pendown();
tgObj.forward(k);
tgObj.right(90);
tgObj.forward(l);
tgObj.right(90);
tgObj.forward(k);
tgObj.right(90);
tgObj.forward(l);
tgObj.penup();
}

void drawButton(int i, int j, String s)
{
tgObj.setpensize(2);
tgObj.setpencolor(0);
drawBox(i, j, 85, 25);
tgObj.setxy(i + 4, (j - 25) + 8);
tgObj.setfontsize(15);
tgObj.label(s);
}

void drawCheckmark(int i, int j, int k, int l)
{
tgObj.setpensize(5);
tgObj.setpencolor(0);
tgObj.penup();
tgObj.setxy(i + 1, j - l / 2);
tgObj.pendown();
tgObj.setxy(i + k / 3, j - (l - 1));
tgObj.setxy(i + (k - 1), j - 1);
}

void drawColorChoices()
{
for(int j = 0; j {
tgObj.setpensize(1);
tgObj.setpencolor(0);
int i = 150 - j * 25;
drawBox(-100, i, 25, 25);
fillBox(-99, i - 1, 23, 24, colorChoices[j]);
}

}

void drawClearColorButton()
{
drawButton(-130, -65, "Clear Color");
}

void drawDupsCheckbox()
{
tgObj.setpensize(1);
tgObj.setpencolor(0);
drawBox(-130, -25, 12, 12);
tgObj.setxy(-113, -37);
tgObj.setfontsize(14);
tgObj.label("Allow Dups");
if(allowDups)
drawCheckmark(-130, -25, 12, 12);
}

void drawGuessBoxes()
{
tgObj.setpensize(1);
tgObj.setpencolor(0);
int j = 150 - guessNumber * 25;
for(int k = 0; k {
int i = -15 + k * 25;
drawBox(i, j, 20, 20);
}

}

void drawGuessButton()
{
drawButton(-130, -95, "Guess");
}

void drawNewGameButton()
{
drawButton(-130, -125, "New Game");
}

int exactMatches()
{
int i = 0;
for(int j = 0; j if(guessChoices[j] == answer[j])
i++;

return i;
}

void fillBox(int i, int j, int k, int l, int i1)
{
tgObj.setpensize(1);
tgObj.setpencolor(i1);
tgObj.setheading(90);
for(int j1 = 0; j1 {
tgObj.penup();
tgObj.setxy(i, j - j1);
tgObj.pendown();
tgObj.forward(k);
}

}

void gameLost()
{
tgObj.penup();
tgObj.setxy(-15, -120);
tgObj.setfontsize(14);
tgObj.setpencolor(0);
tgObj.label("Sorry! Answer was:");
tgObj.setpensize(1);
for(int j = 0; j {
int i = 15 + j * 18;
tgObj.setpencolor(0);
drawBox(i, -125, 15, 15);
fillBox(i + 1, -126, 13, 14, answer[j]);
}

gameOver = true;
}

void gameWon()
{
tgObj.penup();
tgObj.setxy(0, -150);
tgObj.setfontsize(24);
tgObj.label("You Win");
gameOver = true;
}

void initialize()
{
tgObj.hideturtle();
drawColorChoices();
drawDupsCheckbox();
drawClearColorButton();
drawGuessButton();
drawNewGameButton();
makeAnswer();
guessNumber = 0;
choiceNumber = 0;
for(int i = 0; i guessChoices[i] = -1;

drawGuessBoxes();
gameOver = false;
}

void makeAnswer()
{
for(int j = 0; j {
double d = Math.random() * (double)colorChoices.length;
int i = colorChoices[(int)d];
if(!allowDups)
{
for(int k = 0; k {
if(i != answer[k])
continue;
i = -1;
break;
}

}
if(i != -1)
{
answer[j] = i;
j++;
}
}

}

void showExactMatches(int i)
{
int j = 150 - guessNumber * 25;
tgObj.penup();
tgObj.setxy(100, j - 20);
tgObj.pendown();
tgObj.setfontsize(24);
tgObj.setpencolor(0);
tgObj.label(Integer.toString(i));
}

void showInexactMatches(int i)
{
int j = 150 - guessNumber * 25;
tgObj.penup();
tgObj.setxy(120, j - 20);
tgObj.pendown();
tgObj.setfontsize(24);
tgObj.setpencolor(15);
tgObj.label(Integer.toString(i));
}

boolean tryAllowDupsBox(int i, int j)
{
if(i -118)
return false;
if(j > -25 || j return false;
if(allowDups)
{
fillBox(-129, -26, 10, 11, 7);
allowDups = false;
} else
{
drawCheckmark(-130, -25, 12, 12);
allowDups = true;
}
return true;
}

boolean tryClearColor(int i, int j)
{
if(i -45)
return false;
if(j > -65 || j return false;
if(choiceNumber > 0)
{
choiceNumber--;
i = -15 + choiceNumber * 25;
j = 150 - guessNumber * 25;
fillBox(i + 1, j - 1, 18, 19, 7);
}
return true;
}

boolean tryColorChoice(int i, int j)
{
if(i = -75)
return false;
if(j > 150 || j return false;
if(choiceNumber == 4)
{
return true;
} else
{
int k = colorChoices[Math.abs(j - 150) / 25];
i = -15 + choiceNumber * 25;
j = 150 - guessNumber * 25;
fillBox(i + 1, j - 1, 18, 19, k);
guessChoices[choiceNumber] = k;
choiceNumber++;
return true;
}
}

boolean tryGuess(int i, int j)
{
if(i -45)
return false;
if(j > -95 || j return false;
if(choiceNumber return true;
int k = exactMatches();
showExactMatches(k);
if(k == 4)
{
gameWon();
return true;
}
showInexactMatches(anyMatches() - k);
guessNumber++;
if(guessNumber == 10)
{
gameLost();
return true;
} else
{
drawGuessBoxes();
choiceNumber = 0;
return true;
}
}

boolean tryNewGame(int i, int j)
{
if(i -45)
return false;
if(j > -125 || j {
return false;
} else
{
tgObj.clean();
initialize();
return true;
}
}

public void myMouseClickHandler(int i, int j)
{
if(tryAllowDupsBox(i, j))
return;
if(tryNewGame(i, j))
return;
if(gameOver)
return;
if(tryClearColor(i, j))
return;
if(tryGuess(i, j))
{
return;
} else
{
tryColorChoice(i, j);
return;
}
}

public void init()
{
setLayout(new BorderLayout());
tgObj = new TurtleGraphics(400, 400);
tgObj.setBackground(Color.white);
add("Center", tgObj);
tgObj.addMouseListener(this);
initialize();
}

static final int black = 0;
static final int blue = 1;
static final int green = 2;
static final int red = 4;
static final int yellow = 6;
static final int white = 7;
static final int purple = 13;
static final int orange = 14;
static final int gray = 15;
static final int choiceBoxSize = 25;
static final int choicesLeftX = -100;
static final int choicesTopY = 150;
static final int redChoiceY = 150;
static final int orangeChoiceY = 125;
static final int yellowChoiceY = 100;
static final int greenChoiceY = 75;
static final int blueChoiceY = 50;
static final int purpleChoiceY = 25;
static final int choicesBottomY = 1;
static final int colorChoices[] = {
4, 14, 6, 2, 1, 13
};
static final int checkBoxWidth = 12;
static final int checkBoxHeight = 12;
static final int dupsCheckBoxX = -130;
static final int dupsCheckBoxY = -25;
static final int buttonWidth = 85;
static final int buttonHeight = 25;
static final int clearcolorX = -130;
static final int clearcolorY = -65;
static final int guessX = -130;
static final int guessY = -95;
static final int newgameX = -130;
static final int newgameY = -125;
static final int maxGuesses = 10;
static final int guessBoxSize = 20;
static final int guessBoxSpacing = 5;
static final int guessBoxesX = -15;
static final int guessBoxesY = 150;
static final int exactCountX = 100;
static final int inexactCountX = 120;
static final int answerBoxSize = 15;
static final int answerBoxSpacing = 3;
static final int answerBoxesX = 15;
static final int answerBoxesY = -125;
boolean allowDups;
boolean gameOver;
int choiceNumber;
int guessNumber;
int guessChoices[];
int answer[];
TurtleGraphics tgObj;

}
請您先登陸,再發跟帖!