這段時間,發現自己的象棋水平有點小進步,大師級別的現在還不敢想,高手級別的也肯定是比不上的了。在我兩三個月象棋生涯中,先是屢戰屢敗,屢敗屢戰,而後還能僥幸勝幾局,也感覺到了自己的進步。回想一下我的戰績:勝6局,平3局,輸的不記得多少局了。
最近我完成了象棋裡最基本的開盤布局,不知道別人是怎麼做這個功能的。我是通過.ini文件讀取象棋游戲布局的。
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();
兵種: "黑卒","黑炮","黑車","黑馬","黑象","黑士","黑將"
"紅兵","紅炮","紅車","紅馬","紅相","紅士","紅帥
象棋布局代碼
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;
運行效果: