Java基础练习:龟兔赛跑

在这个问题中,我们将再现经典的龟兔赛跑。程序中使用随机数生成法来开发一个模拟这一著名事件的应用程序。
比赛场地为70个方格,参赛者从“方格1”开始出发。每个方格代表比赛过程中所经过的一个位置。终点为“方格70”。最先到达或通过“方格70”的参赛者将赢得一桶新鲜的胡萝卜和莴苣。在比赛过程中可能会经过一段很滑的山路,所以参赛者可能会滑到。
程序中有一个时钟,每秒滴答一次。随着每次时钟滴答,程序应该根据下列规则来调整动物的位置:


image.png

image.png

使用几个变量来跟踪动物的位置(即位置号1-70)。在位置1(即起跑线)上启动每个动物。如果动物在方格1前向左滑动,则将动物移回方格1。 通过产生一个随机整数i来生成表中的百分比,i的范围是1〈=i〈=10。对于乌龟而言,当1〈=i〈=5时“快速走”,当6〈=i〈=7时“打滑”,当8〈=i〈=10时“慢速走”。使用类似的方法来移动兔子。比赛开始时打印以下的字符串: 比赛开始了! 程序继续执行,时钟每滴答一次(即每循环一次),就打印70号方格位置的一条线,其中乌龟的位置用T表示,兔子的位置用H表示。偶尔,竞赛者们会挤到同一个格子上。此时,乌龟会咬兔子,程序要在这位置上打印“OUCH!!!”。所有不是T、H或OUCH!!!(僵局情形)的地方都用空格代替。

父类

package com.TurtleRabbit;

/**
 * Created by ttc on 18-1-3.
 */
public abstract class Animal {
    int location=0;

    public Animal() {}
    public abstract void run();

    public int getLocation() {
        if(location>=69)
        {
            return 69;
        }
        if(location<=0)
        {
            return 0;
        }
        else
        {
            return location;
        }

    }

    public void setLocation(int location) {
        this.location = location;
    }
}

乌龟子类

package com.TurtleRabbit;

/**
 * Created by ttc on 18-1-3.
 */
public class Turtle extends Animal{
    @Override
    public void run()
    {
        int random=(int)(Math.random()*10)+1;
        if(random>=1&&random<=5)
        {
            location=location+3;
        }
        if(random>=6&&random<=7)
        {
            location=location-6;
            if(location<=0)
            {
            location=0;
            }
        }
        if(random>=8&&random<=10)
        {
            location=location+1;
        }
    }
}

兔子子类

package com.TurtleRabbit;

/**
 * Created by ttc on 18-1-3.
 */
public class Rabbit extends Animal{
    @Override
    public void run()
    {
        int random=(int)(Math.random()*10)+1;
        if(random>=1&&random<=2)
        {
            location=location+0;
        }
        if(random>=3&&random<=4)
        {
            location=location+9;
        }
        if(random==5)
        {
            location=location-12;
            if(location<=0)
            {
                location=0;
            }
        }
        if(random>=6&&random<=8)
        {
            location=location+1;
        }
        if(random>=9&&random<=10)
        {
            location=location-2;
            if(location<=0)
            {
                location=0;
            }
        }
    }
}

跑道与赛跑类

package com.TurtleRabbit;

/**
 * Created by ttc on 18-1-3.
 */
public class Run {
    private String[] runway=new String[70];

    public void runwayInit()
    {
        for(int i=0;i<runway.length;i++)
        {
            runway[i]=" ";
        }
    }

    public void start()  throws InterruptedException
    {
        Rabbit rabbit=new Rabbit();
        Turtle turtle=new Turtle();
        System.out.println("比赛开始!");
        for(int i=0;i>=0;i++)
        {
            System.out.println("第"+(i+1)+"秒");
            if(i>=1)
            {
                runway[rabbit.getLocation()]=" ";
                runway[turtle.getLocation()]=" ";
            }
            rabbit.run();
            runway[rabbit.getLocation()]="H";
            turtle.run();
            runway[turtle.getLocation()]="T";
            if(rabbit.getLocation()==turtle.getLocation())
            {
                runway[rabbit.getLocation()]="OUCH!";
            }
            showRunway();
            if(rabbit.getLocation()>=runway.length-1&&
                    turtle.getLocation()>=runway.length-1)
            {
                System.out.println("平局!");
                break;
            }
            if(rabbit.getLocation()>=runway.length-1)
            {
                System.out.println("兔子获胜!");
                break;
            }
            if(turtle.getLocation()>=runway.length-1)
            {
                System.out.println("乌龟获胜!");
                break;
            }
            Thread.sleep(1000);
        }
    }

    public void showRunway()
    {
        for(int i=0;i<runway.length;i++)
        {
            System.out.print(runway[i]);
        }
        System.out.println();
    }
}

Main类

package com.TurtleRabbit;

/**
 * Created by ttc on 18-1-3.
 */
public class RunTest {
    public static void main(String[] args) throws InterruptedException {
        Run run=new Run();
        run.runwayInit();
        run.start();
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这已经是乌龟与兔子的第一场比赛后的n次比赛了,有了n多个批评和经验后,龟与兔决定调整心态,重整旗鼓,秀出一个全新的...
    冰眉铁面阅读 3,496评论 0 0
  • 一直一来我都以为《龟兔赛跑》是一个励志的故事:讲述乌龟如何每天坚持十八个小时练习,最后终于战胜了兔子。后来我发现百...
    福克斯记阅读 8,363评论 2 1
  • 不知什么时候,小乌龟就暗暗地喜欢上了森林里的兔子姐姐。兔子姐姐大大的红眼睛,长长的耳朵,还有那一双撩人的大长腿让小...
    XM小魔阅读 2,939评论 3 1
  • “您账户于10月20日入账劳务费,人民币540.00[招商银行]” 原本还在想专题后台怎么写代码才能实现的脑子有一...
    秋落巴士阅读 6,004评论 32 46
  • 《这世上,男人对女人的好只有一种》: 这世上,男人对女人的好只有一种:尊重她,爱惜她,不让她与痛苦为伴,与委屈为伍...
    你笑得很明媚阅读 3,349评论 0 2