知道错误在哪了, else if (dogDistance <= 0.02) {//计算狗的捕捉范围
dog.dogWin = true;
Console.WriteLine("The dog win for {0} seconds !", count/20);
}
和 theDog.speed /= 20.0;//细节化移动轨迹
theRabbit.speed /= 20.0;
这两个地方。
如果狗的移动速度是2m/s,兔子的移动速度是1m/s。当游戏开始,设定兔子先跑,然后狗朝着兔子的方向跑。兔子移动之后,轮到狗移动,如此反复进行。而当兔子移动后,如果狗离兔子的距离在狗的速度之内,那么兔子一定会在狗的下一次移动中被捕捉。
所以progam.cs要改成下面这样
[ 本帖最后由 q332010372 于 2011-10-2 18:34 编辑 ]
dog.dogWin = true;
Console.WriteLine("The dog win for {0} seconds !", count/20);
}
和 theDog.speed /= 20.0;//细节化移动轨迹
theRabbit.speed /= 20.0;
这两个地方。
如果狗的移动速度是2m/s,兔子的移动速度是1m/s。当游戏开始,设定兔子先跑,然后狗朝着兔子的方向跑。兔子移动之后,轮到狗移动,如此反复进行。而当兔子移动后,如果狗离兔子的距离在狗的速度之内,那么兔子一定会在狗的下一次移动中被捕捉。
所以progam.cs要改成下面这样
程序代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace Date20111002 { class Program { public void Star(Rabbit rabbit, Dog dog) { double count = 0; double dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY)); Console.WriteLine("兔子离自己的窝距离为: {0}", rabbit.RabbitDistance); Console.WriteLine("猎犬离兔子的距离为:{0}\n", dogDistance); while (dog.dogWin == false && rabbit.rabbitWin == false) { rabbit.RabbitDistance = rabbit.RabbitDistance - rabbit.speed; rabbit.y1 = rabbit.speed * rabbit.sina; rabbit.x1 = Math.Sqrt((rabbit.speed * rabbit.speed) - (rabbit.y1 * rabbit.y1)); rabbit.locationY = rabbit.locationY - rabbit.y1; rabbit.locationX = rabbit.locationX - rabbit.x1; Console.WriteLine("兔子已跑至坐标: ({0},{1})", rabbit.locationX, rabbit.locationY); Console.WriteLine("兔子离自己的窝距离为: {0}", rabbit.RabbitDistance); if ((rabbit.locationX == 0 && rabbit.locationY == 0) || rabbit.RabbitDistance == 0) { rabbit.rabbitWin = true; Console.WriteLine("The rabbit win for {0} seconds !", count / 20); break; } dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY)); dog.sina = Math.Abs(dog.locationY - rabbit.locationY) / dogDistance; Console.WriteLine("猎犬离兔子的距离为: {0}\n",dogDistance); if (dogDistance <= (dog.speed-0.00000000000001))//double类型只能保留14位小数,但兔子的坐标14位以后的数会被舍去,导致兔子和猎犬在同一个起点时,猎犬赢的错误。所以就减了0.00000000000001 { dog.dogWin = true; count++; Console.WriteLine("猎犬离兔子的距离为: 0\n"); Console.WriteLine("The dog win for {0} seconds !", count / 20); } if (rabbit.locationX <= dog.locationX && rabbit.locationY >= dog.locationY) { dog.y1 = dog.speed * dog.sina; dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1))); dog.locationX -= dog.x1; dog.locationY += dog.y1; } else if (rabbit.locationX >= dog.locationX && rabbit.locationY >= dog.locationY) { dog.y1 = dog.speed * dog.sina; dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1))); dog.locationX += dog.x1; dog.locationY += dog.y1; } else if (rabbit.locationX <= dog.locationX && rabbit.locationY <= dog.locationY) { dog.y1 = dog.speed * dog.sina; dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1))); dog.locationX -= dog.x1; dog.locationY -= dog.y1; } else if (rabbit.locationX >= dog.locationX && rabbit.locationY <= dog.locationY) { dog.y1 = dog.speed * dog.sina; dog.x1 = Math.Sqrt(((dog.speed * dog.speed) - (dog.y1 * dog.y1))); dog.locationX += dog.x1; dog.locationY -= dog.y1; } dogDistance = Math.Sqrt((dog.locationX - rabbit.locationX) * (dog.locationX - rabbit.locationX) + (dog.locationY - rabbit.locationY) * (dog.locationY - rabbit.locationY)); if (dog.dogWin == false) { Console.WriteLine("猎犬已跑至坐标: ({0},{1})", dog.locationX, dog.locationY); Console.WriteLine("猎犬离兔子的距离为:{0}\n", dogDistance); } count++; } } static void Main(string[] args) { int x1, y1, s1; int x2, y2, s2; Console.WriteLine("Please input the rabbit coordinates X:"); x1 = (int)(Convert.ToUInt32(Console.ReadLine())); Console.WriteLine("Please input the rabbit coordinates Y:"); y1 = (int)(Convert.ToUInt32(Console.ReadLine())); Console.WriteLine("Please input the rabbit's speed:"); s1 = (int)(Convert.ToUInt32(Console.ReadLine())); Console.WriteLine("Please input the dog coordinates X:"); x2 = (int)(Convert.ToUInt32(Console.ReadLine())); Console.WriteLine("Please input the dog coordinates Y:"); y2 = (int)(Convert.ToUInt32(Console.ReadLine())); Console.WriteLine("Please input the dog's speed:"); s2 = (int)(Convert.ToUInt32(Console.ReadLine())); Program newStar = new Program(); Rabbit theRabbit = new Rabbit(x1,y1, s1); Dog theDog = new Dog(x2, y2, s2); theDog.speed /= 20.0; theRabbit.speed /= 20.0; Console.WriteLine("兔子的坐标:({0},{1}), 速度:{2}", theRabbit.locationX, theRabbit.locationY, theRabbit.speed*20); Console.WriteLine("猎犬的坐标:({0},{1}), 速度:{2}\n", theDog.locationX, theDog.locationY, theDog.speed*20); Console.WriteLine("开始计算...\n"); newStar.Star(theRabbit, theDog); Console.ReadKey(); } } }
[ 本帖最后由 q332010372 于 2011-10-2 18:34 编辑 ]