| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3232 人关注过本帖, 2 人收藏
标题:我也国庆大赠送,新手尝试一下:猎狗追兔
只看楼主 加入收藏
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
知道错误在哪了, 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要改成下面这样
程序代码:
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 编辑 ]
2011-10-02 16:47
ouyangouyang
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:273
专家分:579
注 册:2009-10-8
收藏
得分:0 
额滴神呐这么复杂?

多少恨, 昨夜梦魂中。 还似旧时游上苑, 车如流水马如龙; 花月正春风!
2011-10-02 18:06
dreamofgod
Rank: 5Rank: 5
等 级:职业侠客
帖 子:194
专家分:341
注 册:2011-8-16
收藏
得分:0 
我总觉得吧,只要L/(v-u)<D/u,就能追上

[ 本帖最后由 dreamofgod 于 2011-10-2 18:47 编辑 ]

一个单片机就让我头疼不已~~~
2011-10-02 18:40
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
q332010372:看你的程序代码(我没用过C#,暂时无法运行),大概是这么三个问题:一,可能你对坐标解题不是很熟悉,方程对的话,与象限无关的;二,程序结构看起来有点复杂化了,如果不用图形,恐怕不用那么多行代码,就我想象,用C++面向对象写,也应该没这样的行数;三,你的速度预设有错,猎狗的速度绝对不可能跟兔子一样,那样是肯定追不上的。

dreamofgod:大致是这个最低速度,具体我记不清了,有求时间的、求加速度的,很多变型,具体的答案我从来不背。

授人以渔,不授人以鱼。
2011-10-02 20:03
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
回复 44楼 TonyDeng
还没学到windows窗体,所以就只能用描点法画图像了
实验一:
图片附件: 游客没有浏览图片的权限,请 登录注册


图片附件: 游客没有浏览图片的权限,请 登录注册

 
实验二:
图片附件: 游客没有浏览图片的权限,请 登录注册


实验二兔子移动数据:198 595, 196 590, 195 585, 193 581, 192 576, 190 571, 188 566, 187 562, 185 557, 184 552, 182 547, 181 543, 179 538, 177 533, 176 528, 174 524, 173 519, 171 514, 169 509, 168 505, 166 500, 165 495, 163 490, 162 486, 160 481, 158 476, 157 471, 155 467, 154 462, 152 457, 150 452, 149 448, 147 443, 146 438, 144 433, 143 429, 141 424, 139 419, 138 415, 136 410, 135 405, 133 400, 132 396, 130 391, 128 386, 127 381, 125 377, 124 372, 122 367, 120 362, 119 358, 117 353, 116 348, 114 343, 113 339, 111 334, 109 329, 108 324, 106 320, 105 315, 103 310, 101 305, 100 301, 98 296, 97 291, 95 286, 94 282, 92 277, 90 272, 89 267, 87 263, 86 258, 84 253, 82 248, 81 244, 79 239, 78 234, 76 230, 75 225, 73 220, 71 215, 70 211, 68 206, 67 201, 65 196, 64 192, 62 187, 60 182, 59 177, 57 173, 56 168, 54 163, 52 158, 51 154, 49 149, 48 144, 46 139, 45 135, 43 130, 41 125, 40 120, 38 116, 37 111, 35 106, 33 101, 32 97, 30 92, 29 87, 27 82, 26 78, 24 73, 22 68, 21 63, 19 59, 18 54, 16 49, 15 45, 13 40, 11 35, 10 30, 8 26, 7 21, 5 16, 3 11, 2 7, 0 2, 0 0

实验二猎犬的移动数据:396 403, 392 406, 389 410, 385 413, 381 416, 377 419, 373 422, 369 425, 365 428, 361 431, 357 434, 352 437, 348 439, 344 441, 339 444, 335 446, 330 448, 326 450, 321 452, 316 453, 311 455, 307 456, 302 457, 297 458, 292 459, 287 460, 282 460, 277 460, 272 461, 267 460, 262 460, 257 459, 252 459, 247 458, 242 457, 238 455, 233 454, 228 452, 223 450, 219 448, 214 446, 210 443, 206 441, 202 438, 198 435, 194 432, 190 429, 186 426, 182 422, 179 419, 175 415, 172 412, 168 408, 165 404, 162 400, 159 396, 156 392, 153 388, 150 384, 148 380, 145 376, 142 371, 140 367, 137 363, 135 358, 132 354, 130 350, 128 345, 125 341, 123 336, 121 332, 119 327, 117 323, 115 318, 112 314, 110 309, 108 304, 106 300, 105 295, 103 291, 101 286, 99 281, 97 277, 95 272, 93 267, 91 263, 90 258, 88 253, 86 249, 84 244, 82 239, 81 235, 79 230, 77 225, 76 221, 74 216, 72 211, 70 207, 69 202, 67 197, 65 192, 64 188, 62 183, 60 178, 59 174, 57 169, 55 164, 54 159, 52 155, 51 150, 49 145, 47 140, 46 136, 44 131, 42 126, 41 122, 39 117, 38 112, 36 107, 34 103, 33 98, 31 93, 30 88, 28 84, 26 79, 25 74,

由于只保留小数点后的两位有效数字,所以会存在一些误差,但如果算兔子和猎犬两次分别移动的距离话,基本上是差不多的,可以忽略。
2011-10-03 00:49
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
对游戏坐标不是很了解,我说的象限是数学坐标系原点的位置,也就是说把原点定到画面的中间。这样兔子依然朝着原点跑,并且兔子和猎犬可以在4个象限中的任何一个位置,当然,也可以用第一象限,然后更改兔子窝的坐标,再使兔子朝着窝的坐标跑。

另一个就是,不知道是我看的复杂还是你看的简单,我的方法是求出sina,然后利用公式y=sina*斜边c,算出y,再利用公式x^2=c^2-y^2算出x,算出猎犬在移动下一步之前,需要移动的x,y的值各是多少。接着,判断兔子在猎犬的哪个方位,从而判断猎犬是往上或者下走动(±x,±y)的距离,还是左右(±x,±y)的距离。

progam后面修改了一次,速度可以你自己去设置。另外纠正你一点,从实验1可以看出,如果猎狗在兔子回家的线路上(或附近不远的话),即使它们的速度相同,但猎狗依然有可能抓到兔子的。

[ 本帖最后由 q332010372 于 2011-10-3 01:15 编辑 ]
2011-10-03 01:04
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
我今天就学写C##,写一个看看到底有没有你说的那样。
从前方截,确实有可以守株待兔的时候,但这种情形没太大意义。坐标法,你的那种理解是自找麻烦,随便一个熟悉物理运动学解题的人都知晓,不用那样的。

授人以渔,不授人以鱼。
2011-10-03 10:49
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
回复 47楼 TonyDeng
当你使用过3Dmax、MAYA、coreldraw等一些带坐标的软件之后,你就会明白到底需不需要4个象限
2011-10-03 12:25
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
回复 48楼 q332010372
行了,不用你教我自己的专业,这种运算我整天都做。一个运动学方程要分象限考虑,天大的笑话,不要侮辱物理学家和数学家们的发明。

授人以渔,不授人以鱼。
2011-10-03 12:32
q332010372
Rank: 2
等 级:论坛游民
帖 子:52
专家分:61
注 册:2010-7-27
收藏
得分:0 
回复 49楼 TonyDeng
好吧,我错了,不该跟你讨论这些
2011-10-03 12:40
快速回复:我也国庆大赠送,新手尝试一下:猎狗追兔
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016255 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved