- UID
- 140
- 精华
- 积分
- 604
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
本帖最后由 mzflz 于 2014-12-21 19:07 编辑
这东西虽然使用 面向对象的语言写的,但却没有用到面向对象的思想,这也是本程序的缺点所在,
代码很简单,但不保证可读性。
本程序一定还有Bug,请大神多多指教
不多说了,在这儿只把主要代码粘出来,至于“窗体设计器”生成的代码,大家可以在我上传的附件里找到
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace WindowsFormsApplication1
- {
- struct point
- {
- public int x, y;
- };
- public partial class Form1 : Form
- {
-
-
- int[,] map = new int[4, 4];
- point r = new point {
- x = 2,
- y = 2
- };
- bool isClick = false;
- point click = new point();
-
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
-
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- map[i, j] = 0;
- }
- }
- map[r.x, r.y] = 2;
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- for (int i = 0; i < 4; i++) //x
- {
- for (int j = 0; j < 4; j++) //y
- {
-
-
- Pen p = Pens.Black;
- int x = j%2 == 0 ? i * 20 : i*20+10,
- y = j * 20;
- const int w = 20, h = 20;
- g.DrawEllipse(Pens.Black, x, y, 20, 20);
- if (map[i, j] == 0) g.FillEllipse(Brushes.Red, x, y, w, h); //No Wall or Stone
- if (map[i, j] == 1) g.FillEllipse(Brushes.Green, x, y, w, h); // Has Green
- if (map[i, j] == 2) g.FillEllipse(Brushes.Yellow,x,y, w, h); //Rabbit
-
- }
- }
- }
- private void Form1_MouseClick(object sender, MouseEventArgs e)
- {
- this.isClick = true;
-
- if(e.Y < 80&&(e.X/20)%2 == 0)
- {
- click.x = (e.X - 10) / 20 ;
- click.y = (e.Y / 20) ;
- }
- else if(e.Y<80)
- {
- click.x = e.X / 20;
- click.y = e.Y / 20;
- }
- else
- {
- click.x = click.y = -1;
- }
- if(click.x<4&&click.y<4&&click.x>=0&&click.y>=0)
- {
- if (map[click.x, click.y] == 2) return;
- map[click.x, click.y] = 1;
- this.Refresh();
- move();
-
- }
-
- }
- private void move()
- {
- int left = r.x;
- int right = 4 - r.x;
- int top = r.y;
- int bottom = 4 - r.y;
- /* List<int> list = new List<int>();
- list.Add(left);
- list.Add(right);
- list.Add(top);
- list.Add(bottom);
- list.Sort();
- */
- if (left > right) //right
- {
- if (top > bottom) // top
- {
-
- bool isX = true, //行上是否有障碍
- isY = true ; //列上是否有障碍
- //自上而下遍历
- for (int i = r.y;i<4;i++)
- {
- if (map[r.x, i] == 1)
- {
- isY = false;
- break;
- }
- }
- for (int i = r.x; i < 4; i++)
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- map[r.x, r.y] = 0;
- if (bottom > right)
- {
- if (isX)
- {
- r.x += 1;
- Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y += 1;
- Check();
- map[r.x, r.y ] = 2;
- }
- }
- if(bottom<right)
- {
- if (isY)
- {
- r.y += r.y; Check();
- map[r.x, r.y] = 2;
- }
- else
- {
- r.x += 1; Check();
- map[r.x , r.y] = 2;
- }
- }
-
- }
- else if (top < bottom)
- {
- bool isX = true, //行上是否有障碍
- isY = true; //列上是否有障碍
- for (int i = r.x; i < 4; i++)
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- for (int i = r.y; i >= 0; i--)
- {
- if (map[r.x, i] == 1)
- {
- isY = false;
- break;
- }
- }
- map[r.x, r.y] = 0;
- if (top < right)
- {
- if (isY)
- {
- r.y -= 1; Check();
- map[r.x, r.y ] = 2;
- }
- else
- {
- r.x += 1; Check();
- map[r.x , r.y] = 2;
- }
- }
- else if (top >= right)
- {
- if (isX)
- {
- r.x += 1; Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y -= r.y; Check();
- map[r.x, r.y ] = 2;
- }
- }
-
- }
- }
- else if (right >=left)
- {
- if (top >= bottom)
- {
- bool isX = true, //行上是否有障碍
- isY = true; //列上是否有障碍
- for (int i = r.x; i >= 0;i-- )//x
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- for (int i = r.y; i < 4; i++)//y
- {
- if (map[r.x, i] == 1)
- {
- isY = false;
- }
- }
- map[r.x, r.y] = 0;
- if (left >= bottom)
- {
- if (isY)
- {
- r.y += 1; Check();
- map[r.x, r.y] = 2;
- }
- else
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2;
- }
- }
- else if (left <= bottom)
- {
- if (isX)
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y += 1; Check();
- map[r.x, r.y ] = 2;
- }
- }
- }
- else if (top < bottom)
- {
- bool isX = true, //行上是否有障碍
- isY = true; //列上是否有障碍
- for (int i = r.x; i >= 0;i-- )//x
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- for (int i = r.y; i >= 0; i--)
- {
- if (map[r.x, i] == 1)
- {
- isX = false;
- break;
- }
- }
- map[r.x, r.y] = 0;
- if (top < left)
- {
- if (isY)
- {
- r.y -= 1; Check();
- map[r.x, r.y ] = 2;
- }
- else
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2; }
- }
- if (top > left)
- {
- if (isX)
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y -= 1; Check();
- map[r.x, r.y ] = 2;
- }
- }
- }
- }
- //if (r.x < 0 || r.x >3 || r.y < 0 || r.y >3)
- //{
- ;
- //}
- this.Refresh();
-
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (this.Opacity == 1) timer1.Enabled = false;
- this.Opacity += 0.1;
-
- }
- private void Check()
- {
- if (r.x < 0 || r.x > 3 || r.y < 0 || r.y > 3)
- {
- GameOver();
- }
- }
- private void GameOver()
- {
- MessageBox.Show("Game Over");
- Process p = Process.GetCurrentProcess();
- p.Kill();
- //p.Kill();
- }
- }
- }
复制代码
刚才重新看了下代码,发现了几个bug,修复了下,现在重新把代码发上来
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace WindowsFormsApplication1
- {
- struct point
- {
- public int x, y;
- };
- struct pos : IComparable
- {
- public string name;
- public int distance;
- public static bool operator <(pos a, pos b)
- {
- if (a.distance < b.distance)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool operator >(pos a, pos b)
- {
- return a.distance > b.distance ? true : false;
- }
- public int CompareTo(object obj)
- {
- pos p = (pos)obj;
- return this.distance.CompareTo(p.distance);
- }
- };
- public partial class Form1 : Form
- {
- bool leftHave = false;
- bool rightHave = false;
- bool topHave = false;
- bool bottomHave = false;
-
- int[,] map = new int[4, 4];
- point r = new point {
- x = 2,
- y = 1
- };
- bool isClick = false;
- point click = new point();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
-
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- map[i, j] = 0;
- }
- }
- map[r.x, r.y] = 2;
-
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- for (int i = 0; i < 4; i++) //x
- {
- for (int j = 0; j < 4; j++) //y
- {
-
-
- Pen p = Pens.Black;
- int x = j%2 == 0 ? i * 20 : i*20+10,
- y = j * 20;
- const int w = 20, h = 20;
- g.DrawEllipse(Pens.Black, x, y, 20, 20);
- if (map[i, j] == 0) g.FillEllipse(Brushes.Red, x, y, w, h); //No Wall or Stone
- if (map[i, j] == 1) g.FillEllipse(Brushes.Green, x, y, w, h); // Has Green
- if (map[i, j] == 2) g.FillEllipse(Brushes.Yellow,x,y, w, h); //Rabbit
-
- }
- }
- }
- private void Form1_MouseClick(object sender, MouseEventArgs e)
- {
- this.isClick = true;
-
- if(e.Y < 80&&(e.X/20)%2 == 0)
- {
- click.x = (e.X - 10) / 20 ;
- click.y = (e.Y / 20) ;
- }
- else if(e.Y<80)
- {
- click.x = e.X / 20;
- click.y = e.Y / 20;
- }
- else
- {
- click.x = click.y = -1;
- }
- if(click.x<4&&click.y<4&&click.x>=0&&click.y>=0)
- {
- if (map[click.x, click.y] == 2) return;
- map[click.x, click.y] = 1;
- this.Refresh();
- _move();
-
- }
-
- }
- private void move()
- {
- int left = r.x;
- int right = 4 - r.x;
- int top = r.y;
- int bottom = 4 - r.y;
- /* List<int> list = new List<int>();
- list.Add(left);
- list.Add(right);
- list.Add(top);
- list.Add(bottom);
- list.Sort();
- */
- if (left > right) //right
- {
- if (top > bottom) // top
- {
-
- bool isX = true, //行上是否有障碍
- isY = true ; //列上是否有障碍
- //自上而下遍历
- for (int i = r.y;i<4;i++)
- {
- if (map[r.x, i] == 1)
- {
- isY = false;
- break;
- }
- }
- for (int i = r.x; i < 4; i++)
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- map[r.x, r.y] = 0;
- if (bottom > right)
- {
- if (isX)
- {
- r.x += 1;
- Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y += 1;
- Check();
- map[r.x, r.y ] = 2;
- }
- }
- if(bottom<right)
- {
- if (isY)
- {
- r.y += r.y; Check();
- map[r.x, r.y] = 2;
- }
- else
- {
- r.x += 1; Check();
- map[r.x , r.y] = 2;
- }
- }
-
- }
- else if (top < bottom)
- {
- bool isX = true, //行上是否有障碍
- isY = true; //列上是否有障碍
- for (int i = r.x; i < 4; i++)
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- for (int i = r.y; i >= 0; i--)
- {
- if (map[r.x, i] == 1)
- {
- isY = false;
- break;
- }
- }
- map[r.x, r.y] = 0;
- if (top < right)
- {
- if (isY)
- {
- r.y -= 1; Check();
- map[r.x, r.y ] = 2;
- }
- else
- {
- r.x += 1; Check();
- map[r.x , r.y] = 2;
- }
- }
- else if (top >= right)
- {
- if (isX)
- {
- r.x += 1; Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y -= r.y; Check();
- map[r.x, r.y ] = 2;
- }
- }
-
- }
- }
- else if (right >=left)
- {
- if (top >= bottom)
- {
- bool isX = true, //行上是否有障碍
- isY = true; //列上是否有障碍
- for (int i = r.x; i >= 0;i-- )//x
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- for (int i = r.y; i < 4; i++)//y
- {
- if (map[r.x, i] == 1)
- {
- isY = false;
- }
- }
- map[r.x, r.y] = 0;
- if (left >= bottom)
- {
- if (isY)
- {
- r.y += 1; Check();
- map[r.x, r.y] = 2;
- }
- else
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2;
- }
- }
- else if (left <= bottom)
- {
- if (isX)
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y += 1; Check();
- map[r.x, r.y ] = 2;
- }
- }
- }
- else if (top < bottom)
- {
- bool isX = true, //行上是否有障碍
- isY = true; //列上是否有障碍
- for (int i = r.x; i >= 0;i-- )//x
- {
- if (map[i, r.y] == 1)
- {
- isX = false;
- break;
- }
- }
- for (int i = r.y; i >= 0; i--)
- {
- if (map[r.x, i] == 1)
- {
- isX = false;
- break;
- }
- }
- map[r.x, r.y] = 0;
- if (top < left)
- {
- if (isY)
- {
- r.y -= 1; Check();
- map[r.x, r.y ] = 2;
- }
- else
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2; }
- }
- if (top > left)
- {
- if (isX)
- {
- r.x -= 1; Check();
- map[r.x , r.y] = 2;
- }
- else
- {
- r.y -= 1; Check();
- map[r.x, r.y ] = 2;
- }
- }
- }
- }
- //if (r.x < 0 || r.x >3 || r.y < 0 || r.y >3)
- //{
- ;
- //}
- this.Refresh();
-
- }
- private void _move()
- {
- int bottom = 3 - r.y;
- int top = r.y;
- int left = r.x;
- int right = 3 - r.x;
- pos[] pp = new pos[4];
- pp[0].name = "top";
- pp[0].distance = top;
- pp[1].name = "bottom";
- pp[1].distance = bottom;
- pp[2].name = "left";
- pp[2].distance = left;
- pp[3].name = "right";
- pp[3].distance = right;
- Array.Sort(pp);
- for (int i = r.y; i >= 0; i--)//top
- {
- if (map[r.x, i] == 1)
- {
- topHave = true;
- break;
- }
- }
- for (int i = r.y ; i < 4; i++) // bottom
- {
- if (map[r.x, i] == 1)
- {
- bottomHave = true;
- break;
- }
- }
- for (int i = r.x; i >= 0; i--) //left
- {
- if (map[i,r.y] == 1)
- {
- leftHave = true;
- break;
- }
- }
- for (int i = r.x; i < 4; i++) //right
- {
- if (map[i, r.y] == 1)
- {
- rightHave = true;
- break;
- }
- }
- if (!__move(0,pp))
- {
- GameOver();
- }
- if (leftHave && rightHave && topHave && bottomHave)
- {
- MessageBox.Show("You Win");
- Process p = Process.GetCurrentProcess();
- p.Kill();
- }
- }
- private bool check()
- {
- if (r.y < 0 || r.y > 3 || r.x < 0 || r.x>3)
- {
- return false;
- }
- else
- return true;
- }
- bool __move(int i ,pos[] pp)
- {
- if (i >= 3) return false;
-
- switch (pp[i].name)
- {
- case "top":
- {
- if (topHave) return __move(i + 1, pp);
- else
- {
- return move_top();
-
- }
- break;
- }
- case "bottom":
- {
- if (bottomHave) return __move(i + 1, pp);
- else
- {
- return move_bottom();
- }
- break;
- }
- case "left":
- {
- if (leftHave) return __move(i + 1, pp);
- else
- {
- return move_left();
- }
- break;
- }
- case "right":
- {
- if (rightHave) return __move(i + 1, pp);
- else
- {
- return move_right();
- }
- break;
- }
- }
- return true;
- }
- bool move_left()
- {
- map[r.x, r.y] = 0;
- r.x -= 1;
- {
- if (!check()) return false;
- map[r.x, r.y] = 2;
- // if (!check()) return false;
- this.Refresh();
- return true;
- }
- }
- bool move_right()
- {
- map[r.x, r.y] = 0;
- r.x += 1;
- if (!check()) return false;
- map[r.x , r.y] = 2;
- this.Refresh();
- return true;
-
-
- }
- bool move_top()
- {
-
- map[r.x, r.y] = 0;
- r.y -= 1;
- if (!check()) return false;
- map[r.x, r.y] = 2;
- this.Refresh();
- return true;
- }
- bool move_bottom()
- {
- // if (!check()) return false;
- map[r.x, r.y] = 0;
- r.y += 1;
- if (!check()) return false;
- map[r.x, r.y ] = 2;
- this.Refresh();
- return true;
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (this.Opacity == 1) timer1.Enabled = false;
- this.Opacity += 0.1;
-
- }
- private void Check()
- {
- if (r.x < 0 || r.x > 3 || r.y < 0 || r.y > 3)
- {
- GameOver();
- }
- }
- private void GameOver()
- {
- MessageBox.Show("Game Over");
- Process p = Process.GetCurrentProcess();
- p.Kill();
- //p.Kill();
- }
- }
- }
复制代码
============================================================================================================================
|
|