C#编程练习——龟兔赛跑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpMethod
{
class Program
{
static void Main(string[] args)
{
Tortoise tortoise = new Tortoise(1);
Rabbit rabbit = new Rabbit(1);
while (tortoise.tortoiseLocation < 70 && rabbit.rabbitLocation < 70)
{
tortoise.ChangeLocation();
Console.WriteLine(tortoise.tortoiseLocation);
rabbit.ChangeLoation();
Console.WriteLine(rabbit.rabbitLocation);
if (tortoise.tortoiseLocation == rabbit.rabbitLocation)
{
Console.WriteLine("Bite!");
}
//System.Threading.Thread.Sleep(1000);
}
}
}
class Tortoise
{
public int tortoiseLocation = 1;
enum SportMold
{
FastPlod = 1,
Slip = 2,
SlowPlod = 3
}
public Tortoise(int tortoiseLocation)
{
this.tortoiseLocation = tortoiseLocation;
}
~Tortoise()
{
Console.WriteLine("Tortoise类已销毁!");
}
public void ChangeLocation()
{
Random seed = new Random();
int seedInt = seed.Next(1, 11);
int sportStatus;
if (seedInt >= 1 && seedInt <= 5)
{
sportStatus = (int)SportMold.FastPlod;
}
else if (seedInt >= 6 && seedInt <= 7)
{
sportStatus = (int)SportMold.Slip;
}
else
{
sportStatus = (int)SportMold.SlowPlod;
}
switch (sportStatus)
{
case 1: tortoiseLocation = tortoiseLocation + 3;break;
case 2: tortoiseLocation = ((tortoiseLocation - 6) >= 1 ? (tortoiseLocation - 6) : 1);break;
case 3: tortoiseLocation = tortoiseLocation + 1;break;
default: Console.WriteLine("sportStatus变量出现错误!");break;
}
return;
}
}
class Rabbit
{
public int rabbitLocation = 1;
enum SportMold
{
Sleep = 1,
BigHop = 2,
BigSlip = 3,
SmallHop = 4,
SmallSlip = 5,
}
public Rabbit(int rabbitLocation)
{
this.rabbitLocation = rabbitLocation;
}
~Rabbit()
{
Console.WriteLine("Rabbit类已销毁!");
}
public void ChangeLoation()
{
Random seed = new Random();
int seedInt = seed.Next(1, 11);
int status;
if (seedInt >= 1 && seedInt <= 2)
{
status = (int)SportMold.Sleep;
}
else if (seedInt >= 3 && seedInt <= 4)
{
status = (int)SportMold.BigHop;
}
else if (seedInt == 5)
{
status = (int)SportMold.BigSlip;
}
else if (seedInt >= 6 && seedInt <= 8)
{
status = (int)SportMold.SmallHop;
}
else
{
status = (int)SportMold.SmallSlip;
}
switch (status)
{
case 1: rabbitLocation = rabbitLocation;break;
case 2: rabbitLocation = rabbitLocation + 9;break;
case 3: rabbitLocation = ((rabbitLocation - 12) >= 1 ? (rabbitLocation - 12) : 1);break;
case 4: rabbitLocation = rabbitLocation + 1;break;
case 5: rabbitLocation = ((rabbitLocation - 2) >= 1 ? (rabbitLocation - 2) : 1);break;
default: Console.WriteLine("Rabbit类中status变量出现错误!");break;
}
return;
}
}
}