*最近培训的一个题目:蚂蚁爬竿

本文探讨了蚂蚁爬杆问题的编程求解方法,通过JavaScript和C#两种语言实现了问题的模拟,旨在寻找所有蚂蚁离开木杆所需的最小时间和最大时间。
原题:
Problem
某企业面试编程题:蚂蚁爬杆
有一根300厘米的细木杆,在第30厘米、80厘米、110厘米、160厘米、250厘米这五个位置上各有一只蚂蚁。
木杆很细,不能同时通过两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。
当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝相反方向走。假设蚂蚁们每秒钟可以走5厘米的距离。
请编写一个程序,计算各种可能情形下所有蚂蚁都离开木杆的最小时间和最大时间。

我这个题目做了两次,一次是用JS实现滴,一次是用C#实现的
js的实现类分析不明确,没能挖掘出隐藏的类,所以感觉很乱,后面的C#版本有很大的改进,
但是后面涉及图形的演示的时候,又发现自己设计有遗漏:

现在 把代码发上来以后可以看看 还有可以修改的地方吧!
js:
  1 function walk()
  2 {//debugger;
  3     this._pos += this._dir*this._speed;
  4 }
  5 function whenMeet()
  6 {
  7     this._dir = -1*this._dir;
  8 }
  9 function subMakeAnts(pos)
 10 {
 11     var _res = new Array();
 12     var _ant_1 = new ants();
 13     _ant_1._pos = pos;
 14     _ant_1._dir = 1;
 15     _res.push(_ant_1);
 16     var _ant_2 = new ants();
 17     _ant_2._pos = pos;
 18     _ant_2._dir = -1;
 19     _res.push(_ant_2);
 20     
 21     return _res;
 22 }
 23 function makeAntGroup(tempAnts,indexs)
 24 {
 25     var _res = new Array();
 26     _res.push(tempAnts[0][indexs[0]]);
 27     _res.push(tempAnts[1][indexs[1]]);
 28     _res.push(tempAnts[2][indexs[2]]);
 29     _res.push(tempAnts[3][indexs[3]]);
 30     _res.push(tempAnts[4][indexs[4]]);
 31     return _res;
 32 }
 33 function makeTempAnts()
 34 {
 35     var _pos = new Array();
 36     _pos.push(60);
 37     _pos.push(160);
 38     _pos.push(220);
 39     _pos.push(320);
 40     _pos.push(500);
 41     var _tempAnts = new Array();
 42     for(var i=0; i<_pos.length; i++)
 43     {
 44         _tempAnts.push(subMakeAnts(_pos[i]));
 45     }
 46     return _tempAnts;
 47 }
 48 function makeAnts()
 49 {
 50     var _tempAnts = makeTempAnts();
 51     var _res = new Array();
 52     for(var a=0; a<2; a++ )
 53     {
 54         for(var b=0; b<2; b++ )
 55         {
 56             for(var c=0; c<2; c++ )
 57             {
 58                 for(var d=0; d<2; d++ )
 59                 {
 60                     for(var e=0; e<2; e++ )
 61                     {
 62                         var _indexs = new Array();
 63                         _indexs.push(a);
 64                         _indexs.push(b);
 65                         _indexs.push(c);
 66                         _indexs.push(d);
 67                         _indexs.push(e);
 68                         _res.push(makeAntGroup(_tempAnts,_indexs));
 69                     }
 70                 }
 71             }
 72         }
 73     }
 74     //alert(_res.length);//must32
 75     return _res;
 76 }
 77 
 78 function isLeave()
 79 {
 80     if(this._pos<=0 || this._pos>=600)
 81     {
 82         return true;
 83     }
 84     return false;
 85 }
 86 
 87 function ants()
 88 {
 89     this._pos = 0;
 90     this._dir = 1;
 91     this._speed = 1;
 92     this.walk = walk;
 93     this.whenMeet = whenMeet;
 94     this.makeAnts = makeAnts;
 95     this.isLeave = isLeave;
 96 }
 97 /*-----------stick-------------*/
 98 function makeSticks(ants)
 99 {
100     var _res = new Array();
101     var _stick;
102     for(var i=0; i<ants.length; i++)
103     {
104         _stick = new sticks();
105         _stick._ants = ants[i];
106         _res.push(_stick);
107     }
108     return _res;
109 }
110 
111 function judgeMeet()
112 {
113     for(var i=0; i<this._ants.length; i++)
114     {
115         for(var j=i+1;j<this._ants.length; j++)
116         {
117             if(this._ants[i]._pos == this._ants[j]._pos&&
118                 !this._ants[i].isLeave()&&
119                 !this._ants[j].isLeave()&&
120                 j!=i)
121             {
122                 this._ants[i].whenMeet();
123                 this._ants[j].whenMeet();
124             }
125         }
126     }
127 }
128 
129 function judgeAllLeave()
130 {
131     for(var i=0; i<this._ants.length; i++)
132     {
133         if(!this._ants[i].isLeave())
134             return false;
135     }
136     return true;
137 }
138 
139 function moveAnts()
140 {
141     this._time +=1;
142     this.judgeMeet();
143     for(var i=0; i<this._ants.length; i++)
144     {
145         if(!this._ants[i].isLeave())
146         {
147             this._ants[i].walk();
148         }
149     }
150 }
151 function resetAnts()
152 {
153 /*
154     _pos.push(60);
155     _pos.push(160);
156     _pos.push(220);
157     _pos.push(320);
158     _pos.push(500);
159     this._ants[0]._pos
160     this._ants[1]
161     this._ants[2]
162     this._ants[3]
163     this._ants[4]
164     */
165 }
166 function sticks()
167 {
168     this._width = 600;
169     this._ants = new Array();
170     this._time = 0;
171     
172     this.makeSticks = makeSticks;
173     this.judgeMeet = judgeMeet;
174     this.judgeAllLeave = judgeAllLeave;
175     this.moveAnts = moveAnts;
176     this.resetAnts = resetAnts;
177 }
178 /*----------manager------------*/
179 function initAnts()
180 {
181     var _ant = new ants();
182     this._ants = _ant.makeAnts();
183 }
184 function initSticks()
185 {
186     var _stick = new sticks();
187     this._sticks = _stick.makeSticks(this._ants);
188 }
189 var cal_p = 0;
190 var stick_p = null;
191 var antDivs_p = null;
192 function moveAntDivs()
193 {
194     for(var i=0; i<antDivs_p.length; i++)
195     {
196         antDivs_p[i].style.left =  88+stick_p._ants[i]._pos;
197     }
198 }
199 function doTest(stick)
200 {
201     var _antDivs = new Array();
202     var _antDiv;
203     //alert(stick._ants.length);//must5
204     for(var i=0; i<stick._ants.length; i++)
205     {
206         _antDiv = document.createElement("DIV");
207         _antDiv.style.position = "absolute";
208         _antDiv.style.width = 20;
209         _antDiv.style.height = 20;
210         _antDiv.innerText = i;
211         _antDiv.style.left = 110+stick._ants[i]._pos;
212         _antDiv.style.top = 225;
213         document.body.appendChild(_antDiv);
214         _antDivs.push(_antDiv);
215     }
216     stick_p = stick;
217     antDivs_p = _antDivs;
218     moveAction();
219 }
220 function moveAction()
221 {
222     moveDiv();
223     stick_p.moveAnts();
224     cal_p = 0;
225     if(stick_p.judgeAllLeave())
226     {
227         alert(stick_p._time/10+"S");
228         _manager._results.push(stick_p._time/10);
229         while(antDivs_p.length>0)
230         {
231             var _div = antDivs_p.pop();
232             document.body.removeChild(_div);
233         }
234         window.location.reload();
235     }
236     else
237         setTimeout("moveAction();",1);        
238 }
239 function moveDiv()
240 {
241     for(var i=0; i<stick_p._ants.length; i++)
242     {
243         if(!stick_p._ants[i].isLeave())
244             antDivs_p[i].style.left = 110+stick_p._ants[i]._pos;
245     }
246 }
247 function doSolute(group)
248 {
249     //for(var i=0; i<this._sticks.length; i++)
250     //{
251         this.doTest(this._sticks[group]);
252     //}
253 }
254 function doSort()
255 {
256     var _min = this._results[0];
257     var _max = this._resules[0];
258     for(var i=1; i<this._results.length; i++)
259     {
260         if(_min>this._results[i])
261             _min = this._results[i];
262         if(_max<this._results[i])
263             _max = this._results[i];
264     }
265     alert("最短时间:"+_min+" 最大时间:"+_max);
266 }
267 function manager()
268 {
269     this._ants = new Array();
270     this._sticks = new Array();
271     this._results = new Array();
272     
273     this.initAnts = initAnts;
274     this.initSticks = initSticks;
275     this.doTest = doTest;
276     this.doSolute = doSolute;
277     this.doSort = doSort;
278 }
C#:
1、Ant.cs
 1None.gifusing System;
 2None.gif
 3None.gifnamespace AntExcise
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Ant 的摘要说明。
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public class Ant
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        private int _position;
11InBlock.gif        private int _direction;
12InBlock.gif        private int _speed;
13InBlock.gif        public Ant()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            //
16InBlock.gif            // TODO: 在此处添加构造函数逻辑
17InBlock.gif            //
18ExpandedSubBlockEnd.gif        }

19InBlock.gif        
20InBlock.gif        public Ant(int position,int direction,int speed)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            this._position = position;
23InBlock.gif            this._direction = direction;
24InBlock.gif            this._speed = speed;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif        public int Position
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return this._position;}
29ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{this._position = value;}
30ExpandedSubBlockEnd.gif        }

31InBlock.gif        public int Direction
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return this._direction;}
34ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{this._direction=value;}
35ExpandedSubBlockEnd.gif        }

36InBlock.gif        public int Speed
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gif{return this._speed;}
39ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{this._speed=value;}
40ExpandedSubBlockEnd.gif        }

41InBlock.gif        // 行进
42InBlock.gif        public void MoveForward()
43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
44InBlock.gif            this._position += this._speed*this._direction; 
45ExpandedSubBlockEnd.gif        }

46InBlock.gif        //掉头
47InBlock.gif        public void TurnDirection()
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49InBlock.gif            this._direction = (-1)*this._direction;
50ExpandedSubBlockEnd.gif        }

51InBlock.gif        //判断掉落
52InBlock.gif        public bool IsDrop()
53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
54InBlock.gif            if(this._position<=0||this._position>=Stick.Length())
55InBlock.gif                return true;
56InBlock.gif            else
57InBlock.gif                return false;
58ExpandedSubBlockEnd.gif        }

59InBlock.gif        //判断与另外一只碰头
60InBlock.gif        public bool IsMeetWith(Ant ant)
61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
62InBlock.gif            if(this._position==ant._position)
63InBlock.gif                return true;
64InBlock.gif            else
65InBlock.gif                return false;
66ExpandedSubBlockEnd.gif        }

67InBlock.gif        public static bool operator == (Ant antA,Ant antB)
68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
69InBlock.gif            return ((antA._direction == antB._direction)&&
70InBlock.gif                    (antA._position == antB._position)&&
71InBlock.gif                    (antA._speed == antB._speed));
72ExpandedSubBlockEnd.gif        }

73InBlock.gif        public static bool operator != (Ant antA,Ant antB)
74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
75InBlock.gif            return ((antA._direction != antB._direction)||
76InBlock.gif                (antA._position != antB._position)||
77InBlock.gif                (antA._speed != antB._speed));
78ExpandedSubBlockEnd.gif        }

79ExpandedSubBlockEnd.gif    }

80ExpandedBlockEnd.gif}

81None.gif
2、Environment.cs
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Windows.Forms;
  4None.gif
  5None.gifnamespace AntExcise
  6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// Environment 的摘要说明。
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class Environment
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        private ArrayList _instances;
 13InBlock.gif        private int _instanceNum;
 14InBlock.gif        public Environment()
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 16InBlock.gif            //
 17InBlock.gif            // TODO: 在此处添加构造函数逻辑
 18InBlock.gif            //
 19InBlock.gif            this.InitInstances();
 20InBlock.gif            this._instanceNum = this._instances.Count;
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif        public int getInstanceNum()
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            return this._instanceNum;
 25ExpandedSubBlockEnd.gif        }

 26InBlock.gif        public Instance GetInstance(int index)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            return (Instance)this._instances[index];
 29ExpandedSubBlockEnd.gif        }

 30ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*
 31InBlock.gif        private int getDir(int count,int dig)
 32InBlock.gif        {
 33InBlock.gif            string str = count.ToString();
 34InBlock.gif            if(str.Length<dig)
 35InBlock.gif                return -1;
 36InBlock.gif            else
 37InBlock.gif                return Convert.ToInt32(str.Substring(dig-1,1))==0?-1:1;
 38InBlock.gif        }
 39InBlock.gif        private int getInt(int num)
 40InBlock.gif        {
 41InBlock.gif            int res = 1;
 42InBlock.gif            for(int i=0; i<num; i++)
 43InBlock.gif            {
 44InBlock.gif                res*=2;
 45InBlock.gif            }
 46InBlock.gif            return res;
 47InBlock.gif        }
 48ExpandedSubBlockEnd.gif        */

 49InBlock.gif        //初始化所有场景
 50InBlock.gif        private void InitInstances()
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*
 53InBlock.gif            this._instances = new ArrayList();
 54InBlock.gif            ArrayList dir;
 55InBlock.gif            for(int count = 0;count<getInt(5);count++)
 56InBlock.gif            {
 57InBlock.gif                dir = new ArrayList();
 58InBlock.gif                dir.Add(getDir(count,1));
 59InBlock.gif                dir.Add(getDir(count,2));
 60InBlock.gif                dir.Add(getDir(count,3));
 61InBlock.gif                dir.Add(getDir(count,4));
 62InBlock.gif                dir.Add(getDir(count,5));
 63InBlock.gif                this._instances.Add(new Instance(dir));
 64InBlock.gif            }
 65ExpandedSubBlockEnd.gif            */

 66InBlock.gif
 67InBlock.gif            this._instances = new ArrayList();
 68InBlock.gif            ArrayList dir;
 69InBlock.gif            for(int a=0; a<2; a++ )
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                for(int b=0; b<2; b++ )
 72ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 73InBlock.gif                    for(int c=0; c<2; c++ )
 74ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 75InBlock.gif                        for(int d=0; d<2; d++ )
 76ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 77InBlock.gif                            for(int e=0; e<2; e++ )
 78ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
 79InBlock.gif                                dir = new ArrayList();
 80InBlock.gif                                dir.Add(a==0?-1:1);
 81InBlock.gif                                dir.Add(b==0?-1:1);
 82InBlock.gif                                dir.Add(c==0?-1:1);
 83InBlock.gif                                dir.Add(d==0?-1:1);
 84InBlock.gif                                dir.Add(e==0?-1:1);
 85InBlock.gif                                this._instances.Add(new Instance(dir));
 86ExpandedSubBlockEnd.gif                            }

 87ExpandedSubBlockEnd.gif                        }

 88ExpandedSubBlockEnd.gif                    }

 89ExpandedSubBlockEnd.gif                }

 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif        //a=b 2
 93InBlock.gif        //a>b 0
 94InBlock.gif        //a<b 1
 95InBlock.gif        private int CompareInstance(Instance instanceA,Instance instanceB)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            int temp = instanceA.GetTimeCount()-instanceB.GetTimeCount();
 98InBlock.gif            if(temp==0)
 99InBlock.gif                return 2;
100InBlock.gif            else if(temp>0)
101InBlock.gif                return 0;
102InBlock.gif            else//if(temp<0)
103InBlock.gif                return 1;
104InBlock.gif
105ExpandedSubBlockEnd.gif        }

106InBlock.gif        public void Start()
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            this.RunInstanceAll();
109ExpandedSubBlockEnd.gif        }

110InBlock.gif        public void RunInstanceAll()
111ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
112InBlock.gif            Instance temp;
113InBlock.gif            string _res = "";
114InBlock.gif            for(int i=0; i<this._instanceNum; i++)
115ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
116InBlock.gif                temp = (Instance)this._instances[i];
117InBlock.gif                temp.Run();
118InBlock.gif                _res+=temp.GetTimeCount().ToString()+"_";
119ExpandedSubBlockEnd.gif            }

120InBlock.gif            //MessageBox.Show(_res);
121InBlock.gif            //MessageBox.Show(this.GetMin().GetTimeCount().ToString());
122ExpandedSubBlockEnd.gif        }

123InBlock.gif        //时间值最大的场景
124InBlock.gif        public Instance GetMax()
125ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
126InBlock.gif            Instance tempIns = (Instance)this._instances[0];
127InBlock.gif            for(int i=0; i<this._instances.Count; i++)
128ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
129InBlock.gif                if(this.CompareInstance(tempIns,(Instance)this._instances[i])==1)
130ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
131InBlock.gif                    tempIns = (Instance)this._instances[i];
132ExpandedSubBlockEnd.gif                }

133ExpandedSubBlockEnd.gif            }

134InBlock.gif            return tempIns;
135ExpandedSubBlockEnd.gif        }

136InBlock.gif        //时间值最小的场景
137InBlock.gif        public Instance GetMin()
138ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
139InBlock.gif            Instance tempIns = (Instance)this._instances[0];
140InBlock.gif            for(int i=0; i<this._instances.Count; i++)
141ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
142InBlock.gif                
143InBlock.gif                if(this.CompareInstance(tempIns,(Instance)this._instances[i])==0)
144ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
145InBlock.gif                    tempIns = (Instance)this._instances[i];
146ExpandedSubBlockEnd.gif                }

147ExpandedSubBlockEnd.gif            }

148InBlock.gif            return tempIns;
149ExpandedSubBlockEnd.gif        }

150InBlock.gif        
151ExpandedSubBlockEnd.gif    }

152ExpandedBlockEnd.gif}

153None.gif
3、Instance.cs
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Windows.Forms;
  4None.gif
  5None.gifnamespace AntExcise
  6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// Instance 的摘要说明。
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public class Instance
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        private ArrayList _ants;
 13InBlock.gif        private Stick _stick;
 14InBlock.gif        private int _timeCount;
 15InBlock.gif        //通过GroupID来初始化
 16InBlock.gif        public Instance(int groupID)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            //
 19InBlock.gif            // TODO: 在此处添加构造函数逻辑
 20InBlock.gif            //
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif        public Instance(ArrayList directions)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            this._timeCount = 0;
 25InBlock.gif            this.InitStick();
 26InBlock.gif            this.InitAnts(directions);
 27ExpandedSubBlockEnd.gif        }

 28InBlock.gif        public int GetTimeCount()
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            return this._timeCount;
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif        public Ant GetAnt(int index)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34InBlock.gif            return (Ant)this._ants[index];
 35ExpandedSubBlockEnd.gif        }

 36InBlock.gif        //初始化棍子
 37InBlock.gif        private void InitStick()
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 39InBlock.gif            this._stick = new Stick();
 40InBlock.gif            int _length = 300;
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            int [] pos = dot.gif{30,80,110,160,250};
 42InBlock.gif            this._stick.SetLength(_length);
 43InBlock.gif            this._stick.SetPositions(pos);
 44ExpandedSubBlockEnd.gif        }

 45ExpandedSubBlockStart.gifContractedSubBlock.gif        /**/////通过蚂蚁方向来初始化蚂蚁
 46InBlock.gif        private void InitAnts(ArrayList dir)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 48InBlock.gif            this._ants = new ArrayList();
 49InBlock.gif            int [] _pos = this._stick.GetPositions();
 50InBlock.gif            Ant ant;
 51InBlock.gif            ant = new Ant(_pos[0],(int)dir[0],5);
 52InBlock.gif            this._ants.Add(ant);
 53InBlock.gif            ant = new Ant(_pos[1],(int)dir[1],5);
 54InBlock.gif            this._ants.Add(ant);
 55InBlock.gif            ant = new Ant(_pos[2],(int)dir[2],5);
 56InBlock.gif            this._ants.Add(ant);
 57InBlock.gif            ant = new Ant(_pos[3],(int)dir[3],5);
 58InBlock.gif            this._ants.Add(ant);
 59InBlock.gif            ant = new Ant(_pos[4],(int)dir[4],5);
 60InBlock.gif            this._ants.Add(ant);
 61ExpandedSubBlockEnd.gif        }

 62InBlock.gif        //所有蚂蚁行动起来
 63InBlock.gif        public void MoveAnts()
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            Ant ant;
 66InBlock.gif            for(int i=this._ants.Count-1;i>=0;i--)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 68InBlock.gif                ant = (Ant)this._ants[i];
 69InBlock.gif                if(!ant.IsDrop())
 70InBlock.gif                    ant.MoveForward();
 71ExpandedSubBlockEnd.gif            }

 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif        //时间累加
 74InBlock.gif        private void TimeAdd()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            this._timeCount++;
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif        //是否所有的都掉下来
 79InBlock.gif        public bool IsAllDropped()
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            foreach(Ant ant in this._ants)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                if(!ant.IsDrop())
 84InBlock.gif                    return false;
 85ExpandedSubBlockEnd.gif            }

 86InBlock.gif            return true;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif
 89InBlock.gif        //处理相遇的蚂蚁
 90InBlock.gif        private void ProcMeet()
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 92InBlock.gif            Ant tempA;
 93InBlock.gif            Ant tempB;
 94InBlock.gif            for(int i=0; i<this._ants.Count; i++)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 96InBlock.gif                for(int j=i+1;j<this._ants.Count; j++)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 98InBlock.gif                    tempA = (Ant)this._ants[i];
 99InBlock.gif                    tempB = (Ant)this._ants[j];
100InBlock.gif                    if(tempA.IsMeetWith(tempB)&&
101InBlock.gif                        !tempA.IsDrop()&&
102InBlock.gif                        !tempB.IsDrop()&&
103InBlock.gif                        j!=i)
104ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
105InBlock.gif                        tempA.TurnDirection();
106InBlock.gif                        tempB.TurnDirection();
107ExpandedSubBlockEnd.gif                    }

108ExpandedSubBlockEnd.gif                }

109ExpandedSubBlockEnd.gif            }

110ExpandedSubBlockEnd.gif        }

111InBlock.gif        //单步运行
112InBlock.gif        public void RunStep()
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            this.MoveAnts();
115InBlock.gif            this.ProcMeet();
116InBlock.gif            this.TimeAdd();
117ExpandedSubBlockEnd.gif        }

118InBlock.gif        //运行场景
119InBlock.gif        public void Run()
120ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
121InBlock.gif            while(!this.IsAllDropped())
122ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
123InBlock.gif                this.RunStep();
124ExpandedSubBlockEnd.gif            }

125ExpandedSubBlockEnd.gif        }

126ExpandedSubBlockEnd.gif    }

127ExpandedBlockEnd.gif}

128None.gif

4、 Stick.cs

 1None.gifusing System;
 2None.gifusing System.Collections;
 3None.gif
 4None.gifnamespace AntExcise
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// Stick 的摘要说明。
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    public class Stick
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        private static int _length;
12InBlock.gif        private int [] _positions;
13InBlock.gif        public Stick()
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            //
16InBlock.gif            // TODO: 在此处添加构造函数逻辑
17InBlock.gif            //
18ExpandedSubBlockEnd.gif        }

19InBlock.gif        public Stick(int length)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            _length = length;
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        public void SetPositions(int [] pos)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            this._positions = pos;
26ExpandedSubBlockEnd.gif        }

27InBlock.gif        public void SetLength(int length)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            _length = length;
30ExpandedSubBlockEnd.gif        }

31InBlock.gif        public int GetLength()
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            return _length;
34ExpandedSubBlockEnd.gif        }

35InBlock.gif        public int [] GetPositions()
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif            return this._positions;
38ExpandedSubBlockEnd.gif        }

39InBlock.gif        public static int Length()
40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
41InBlock.gif            return _length;
42ExpandedSubBlockEnd.gif        }

43ExpandedSubBlockEnd.gif    }

44ExpandedBlockEnd.gif}

45None.gif

转载于:https://www.cnblogs.com/dwjaissk/archive/2007/03/08/668446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值