天天看點

象棋程式設計遊戲——布局篇

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();

兵種: "黑卒","黑炮","黑車","黑馬","黑象","黑士","黑将"

       "紅兵","紅炮","紅車","紅馬","紅相","紅士","紅帥

 象棋布局代碼

 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;    

 運作效果:

象棋程式設計遊戲——布局篇

 如果覺得文章不錯的話,歡迎點一下右下角的推薦。

象棋程式設計遊戲——布局篇