象棋编程游戏——布局篇
BingWay原创作品,转载请注明作者和出处。
这段时间,发现自己的象棋水平有点小进步,大师级别的现在还不敢想,高手级别的也肯定是比不上的了。在我两三个月象棋生涯中,先是屡战屡败,屡败屡战,而后还能侥幸胜几局,也感觉到了自己的进步。回想一下我的战绩:胜6局,平3局,输的不记得多少局了。
最近我完成了象棋里最基本的开盘布局,不知道别人是怎么做这个功能的。我是通过.ini文件读取象棋游戏布局的。
Code
public const string FileName="..\\..\\象棋布局文件.ini";
public Hashtable chessHT=new Hashtable(); //建立初始表,用于存放对应的棋子字符串和棋子图象
public System.Windows.Forms.PictureBox[] pictureBoxs=new PictureBox[32]; //存放棋子的一组图片框
public Image[] imgs=new Image[14];
public string battle=null;
public string[] soldiers=new string[32];
public Point[] tempPoint=new Point[32]; //存放盛棋子的32个点
public bool whetherSelected=false; //保证只有一个图像移动,并且确保移动正确
public Point p=new Point(); //移动图象时,记录鼠标的旧坐标
public Point beforeMovePoint=new Point();
public Hashtable chessHT=new Hashtable(); //建立初始表,用于存放对应的棋子字符串和棋子图象
public System.Windows.Forms.PictureBox[] pictureBoxs=new PictureBox[32]; //存放棋子的一组图片框
public Image[] imgs=new Image[14];
public string battle=null;
public string[] soldiers=new string[32];
public Point[] tempPoint=new Point[32]; //存放盛棋子的32个点
public bool whetherSelected=false; //保证只有一个图像移动,并且确保移动正确
public Point p=new Point(); //移动图象时,记录鼠标的旧坐标
public Point beforeMovePoint=new Point();
兵种: "黑卒","黑炮","黑車","黑马","黑象","黑士","黑将"
"红兵","红炮","红車","红马","红相","红士","红帅
象棋布局代码
1 //建立pictureBoxs的实例
2 for(int i=0;i<32;i++)
3 this.pictureBoxs[i]=new PictureBox();
4
5 InitializeComponent();
6
7 //以下代码初始化chessHT对照表
8 string[] str={"黑卒","黑炮","黑車","黑马","黑象","黑士","黑将","红兵","红炮","红車","红马","红相","红士","红帅"};
9 //读取棋子文件所在位置
10 imgs[0]=Image.FromFile(@"..\..\img\black-bing.gif");
11 imgs[1]=Image.FromFile(@"..\..\img\black-pao.gif");
12 imgs[2]=Image.FromFile(@"..\..\img\black-ju.gif");
13 imgs[3]=Image.FromFile(@"..\..\img\black-ma.gif");
14 imgs[4]=Image.FromFile(@"..\..\img\black-xiang.gif");
15 imgs[5]=Image.FromFile(@"..\..\img\black-shi.gif");
16 imgs[6]=Image.FromFile(@"..\..\img\black-jiang.gif");
17 imgs[7]=Image.FromFile(@"..\..\img\red-zu.gif");
18 imgs[8]=Image.FromFile(@"..\..\img\red-pao.gif");
19 imgs[9]=Image.FromFile(@"..\..\img\red-ju.gif");
20 imgs[10]=Image.FromFile(@"..\..\img\red-ma.gif");
21 imgs[11]=Image.FromFile(@"..\..\img\red-xiang.gif");
22 imgs[12]=Image.FromFile(@"..\..\img\red-shi.gif");
23 imgs[13]=Image.FromFile(@"..\..\img\red-shuai.gif");
24 for(int i=0;i<=13;i++)
25 {
26 chessHT.Add(str[i],imgs[i]);
27 }
28
29 //往panel1中添加pictureBoxs数组控件
30 this.panel1.Controls.AddRange(pictureBoxs);
31
32 //使所有PictureBoxs隐藏,不影响主界面的显示
33 for(int i=0;i<32;i++)
34 this.pictureBoxs[i].Visible=false;
35
36 for(int i=0;i<32;i++)
37 {
38 this.pictureBoxs[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBoxs_MouseUp);
39 this.pictureBoxs[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBoxs_MouseMove);
40 this.pictureBoxs[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBoxs_MouseDown);
41 }
42
43 signString[0]="black"; //初始化标志哪方走棋字符串
44 signString[1]="red";
45
46 this.pictureBox6.Width=this.width/2-5;
47 this.pictureBox6.Height=this.height/2+3;
2 for(int i=0;i<32;i++)
3 this.pictureBoxs[i]=new PictureBox();
4
5 InitializeComponent();
6
7 //以下代码初始化chessHT对照表
8 string[] str={"黑卒","黑炮","黑車","黑马","黑象","黑士","黑将","红兵","红炮","红車","红马","红相","红士","红帅"};
9 //读取棋子文件所在位置
10 imgs[0]=Image.FromFile(@"..\..\img\black-bing.gif");
11 imgs[1]=Image.FromFile(@"..\..\img\black-pao.gif");
12 imgs[2]=Image.FromFile(@"..\..\img\black-ju.gif");
13 imgs[3]=Image.FromFile(@"..\..\img\black-ma.gif");
14 imgs[4]=Image.FromFile(@"..\..\img\black-xiang.gif");
15 imgs[5]=Image.FromFile(@"..\..\img\black-shi.gif");
16 imgs[6]=Image.FromFile(@"..\..\img\black-jiang.gif");
17 imgs[7]=Image.FromFile(@"..\..\img\red-zu.gif");
18 imgs[8]=Image.FromFile(@"..\..\img\red-pao.gif");
19 imgs[9]=Image.FromFile(@"..\..\img\red-ju.gif");
20 imgs[10]=Image.FromFile(@"..\..\img\red-ma.gif");
21 imgs[11]=Image.FromFile(@"..\..\img\red-xiang.gif");
22 imgs[12]=Image.FromFile(@"..\..\img\red-shi.gif");
23 imgs[13]=Image.FromFile(@"..\..\img\red-shuai.gif");
24 for(int i=0;i<=13;i++)
25 {
26 chessHT.Add(str[i],imgs[i]);
27 }
28
29 //往panel1中添加pictureBoxs数组控件
30 this.panel1.Controls.AddRange(pictureBoxs);
31
32 //使所有PictureBoxs隐藏,不影响主界面的显示
33 for(int i=0;i<32;i++)
34 this.pictureBoxs[i].Visible=false;
35
36 for(int i=0;i<32;i++)
37 {
38 this.pictureBoxs[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBoxs_MouseUp);
39 this.pictureBoxs[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBoxs_MouseMove);
40 this.pictureBoxs[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBoxs_MouseDown);
41 }
42
43 signString[0]="black"; //初始化标志哪方走棋字符串
44 signString[1]="red";
45
46 this.pictureBox6.Width=this.width/2-5;
47 this.pictureBox6.Height=this.height/2+3;
运行效果:
如果觉得文章不错的话,欢迎点一下右下角的推荐。