我必须为我的Java类做这件事,在那里我制作一个显示乌龟和小龙虾的小程序。野兔种族。这是作业:
这个项目涉及编写小程序以模拟乌龟和野兔种族。该 竞争者将沿着水平路线进行比赛,每个路线包含一条车道 他们该课程应该至少有50个方格或位置...你可以添加 更多,如果你愿意。比赛从每个竞争者的位置1开始 各自的车道。首先到达或通过最后一个方格的竞争者 该课程是比赛的胜利者。 下表显示了每个竞争者可以进行的移动类型。
Contender Type of Move Percentage of Time Result of Move
Tortoise Fast plod 50% 3 squares to right
Slow plod 30% 1 square to right
Slip 20% 6 squares to left
Hare Big hop 20% 9 squares to right
Small hop 30% 1 square to right
Big slip 10% 12 squares to left
Small slip 20% 2 squares to left
Fall asleep 20%
每个竞争者从位置1开始。当竞争者滑倒时,他们不会滑倒 比位置1更左侧。您将使用随机数生成器进行模拟 表中所示的每种移动的百分比。 生成一个随机整数n,范围为1≤n≤10。对于乌龟,执行a 快速plod如果数字是1-5,如果数字是6-8慢速plod,如果数字是滑动 号码是9-10。对于野兔,如果数量为1-2,则进行大跳,小跳 如果数字为3-5,如果数字为6则为大滑,如果数字为7-8则为小滑动, 如果数字是9-10则入睡。 你必须跟踪每个竞争者的位置并显示每个竞争者的位置 使用图形图像的位置。乌龟和野兔的图形图像可以 可以在课程下载文件夹中找到。您可能需要调整长度 applet窗口的过程和大小,以获得正确的外观。 提示:在处理之前获取程序的整体逻辑 图形。您可能会发现它有用(但并非绝对必要) 基于模拟时钟的总体控制流程。每次时钟“滴答” 竞争者的举动。
我的程序没有给出错误信息,但是当我尝试运行它时,我得到一个带有一堆分区的两个矩形的空白图像,它表示" Race"和#34;转空。"我尽了最大努力找到了错误,但我认为它可能会使用另一双眼睛。请尽可能帮忙!
这是我的代码:
import java.awt.*;
import java.applet.*;
public class TortoiseAndHareProject extends Applet {
Image tortoise, hare; //creates two images, tortoise and hare
int tortX = 250, hareX = 250; //sets them both to start at pixel 250
final int tortY = 100, hareY = 300, WIDTH = 15, HEIGHT = 50; //sets finals tortY, hareY, WIDTH, and HEIGHT = variables to use in the race
//WIDTH will serve as a counter for distance
int turn;
String turnNum;
int move;
String tMove, hMove;
public void init() {
tortoise = getImage(getDocumentBase(), "tortoise.gif");
hare = getImage(getDocumentBase(), "hare.gif");
move = 0;
turn = 0;
}
public void control() throws InterruptedException {
while ((tortX < 985) || (hareX < 985)) { //while neither has won the race yet (985 = finish)
move = (int) (10 * Math.random());
switch (move) {
case 1:
case 2:
tortX += (3 * WIDTH);
hareX += (9 * WIDTH);
tMove = "Fast Plod";
hMove = "Big Hop";
break;
case 3:
case 4:
case 5:
tortX += (3 * WIDTH);
hareX += WIDTH;
tMove = "Fast Plod";
hMove = "Small Hop";
break;
case 6:
tortX += WIDTH;
if (hareX == 250) { //nothing happens, the hare just doesn't move
} else if (hareX <= (250 + (11 * WIDTH))) //if hare is less than 12 squares ahead of 250
hareX = 250; //the hare just goes back to 250
else
hareX -= (12 * WIDTH); //the hare just moves back 12 * WIDTH
tMove = "Slow Plod";
hMove = "Big Slip";
break;
case 7:
case 8:
tortX += (1 * WIDTH);
if (hareX == 250) { //nothing happens, the hare just doesn't move
} else if (hareX <= (250 + (WIDTH))) //if hare is less than 2 squares ahead of 250
hareX = 250; //the hare just goes back to 250
else
hareX -= (2 * WIDTH); //the hare just moves back 2 * WIDTH
tMove = "Slow Plod";
hMove = "Small Slip";
break;
case 9:
case 10:
if (tortX == 250) { //nothing happens
} else if (tortX <= (250 + (5 * WIDTH))) //if the tortoise is less than 6 squares ahead of 250
tortX = 250; //the tortoise just goes back to 250
else
tortX -= (6 * WIDTH);
tMove = "Slip";
hMove = "Fall Asleep.";
break;
}
turn++;
turnNum = (turn + "");
repaint();
Thread.sleep(30);
}
tortX = 985;
hareX = 985;
repaint();
}
public void paint(Graphics screen) {
drawRace(screen);
if (tortX >= 985) {
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Tortoise Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
} else if (hareX >= 985) { //if hareX has gotten greater than 985
screen.setFont(new Font("Times New Roman", Font.ITALIC, 48));
screen.drawString("Hare Wins", 650, 240);
clearCurrent(screen);
fillNext(screen);
} else {
screen.drawString(("Turn " + turnNum), 621, 55);
screen.setFont(new Font("Times New Roman", Font.ITALIC, 12));
if (tMove != null) {
screen.drawString(tMove, 59, 65);
}
if (hMove != null) {
screen.drawString(hMove, 66, 255);
}
clearCurrent(screen);
fillNext(screen);
}
stop();
}
public void clearCurrent(Graphics s) {
s.clearRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.clearRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
//in order to get a new screen
}
public void fillNext(Graphics s) {
s.fillRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.fillRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
//in order to fill the screen with the tortoise & hare
}
public void drawRace(Graphics s) {
s.drawRect(250, 100, 750, 50); //draws a rectangle for the race
s.drawRect(250, 300, 750, 50); //draws another rectangle for the race
int lineX = 265, lineYi = 100, lineYf = 150;
for (int i = 1; i <= 98; i++) { //for the duration of the race
if (lineX == 1000) {
lineX = 265;
lineYi = 300;
lineYf = 350;
}
s.drawLine(lineX, lineYi, lineX, lineYf);
lineX += 15;
}
s.fillRect(tortX + 1, tortY + 1, WIDTH - 1, HEIGHT - 1);
s.fillRect(hareX + 1, hareY + 1, WIDTH - 1, HEIGHT - 1);
s.drawImage(tortoise, 59, 80, this);
s.drawImage(hare, 66, 271, this);
s.setFont(new Font("Times New Roman", Font.BOLD, 24));
s.drawString("Race", 250, 55);
}
public void stop() {
}
}