Chessboard.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:5k
源码类别:
C#编程
开发平台:
Others
- using System;
- using System.Collections;
- using System.Drawing;
- using System.Windows.Forms;
- namespace FiveStones
- {
- /// <summary>
- /// Chessboard 的摘要说明。
- /// 棋盘类
- /// </summary>
- public class Chessboard
- {
- //arrchessboard为棋盘情况数组,arrchessboard[i,j]=2表示此处无子,arrchessboard[i,j]=0表示此处为黑子,arrchessboard[i,j]=1表示此处为白子
- private int[,] arrchessboard = new int [15,15];
- //绘制的对象
- private Graphics mg;
- //棋子对象
- private Stone stone;
- //电脑对象
- private Computer computer;
- //判断当前棋子是黑(true)是白(false)
- private bool stoneflag = true;
- //判断先手玩家(是电脑(true)还是人(false),先手下黑棋)
- private bool mplayfirstflag = false;
- //历史记录堆栈
- private Stack mStarckHistory = new Stack();
- /// <summary>
- /// 历史记录属性
- /// </summary>
- public Stack StarckHistory
- {
- get
- {
- return mStarckHistory;
- }
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="g">绘制的对象</param>
- public Chessboard(Graphics g)
- {
- mg = g;
- Initialization();
- }
- /// <summary>
- /// 画棋盘
- /// </summary>
- public void Draw()
- {
- //读取棋盘图像
- //从资源中获取
- System.Reflection.Assembly thisExe;
- thisExe = System.Reflection.Assembly.GetExecutingAssembly();
- System.IO.Stream file = thisExe.GetManifestResourceStream("FiveStones.chessboard.gif");
- Image imgChessboard = System.Drawing.Image.FromStream(file); //棋盘图片 大小600*600像素
- file.Close();
- mg.DrawImage(imgChessboard,0,0,imgChessboard.Width,imgChessboard.Height);
- //绘制棋子
- for (int i=0;i<15;i++)
- {
- for(int j=0;j<15;j++)
- {
- if (arrchessboard[i,j] == 0)
- {
- stone.DrawStone(i,j,true);
- }
- if (arrchessboard[i,j] == 1)
- {
- stone.DrawStone(i,j,false);
- }
- }
- }
- }
- /// <summary>
- /// 下棋
- /// </summary>
- /// <param name="m"></param>
- /// <param name="n"></param>
- private void DownStone(int m,int n)
- {
- stone.DrawStone(m,n,stoneflag);
- //记录情况
- if (stoneflag)
- {
- arrchessboard[m,n] = 0;
- }
- else
- {
- arrchessboard[m,n] = 1;
- }
- //记录历史记录
- if (stoneflag)
- {
- mStarckHistory.Push("黑:"+m.ToString()+","+n.ToString());
- }
- else
- {
- mStarckHistory.Push("白:"+m.ToString()+","+n.ToString());
- }
- //判断结果
- if (Rule.Result(m,n,arrchessboard) < 6)
- {
- switch(Rule.Result(m,n,arrchessboard))
- {
- case 1:
- MessageBox.Show("黑棋双三禁手失败!");
- break;
- case 2:
- MessageBox.Show("黑棋双四禁手失败!");
- break;
- case 3:
- MessageBox.Show("黑棋长连禁手失败!");
- break;
- case 4:
- if (stoneflag)
- {
- MessageBox.Show("黑棋胜利!");
- }
- else
- {
- MessageBox.Show("白棋胜利!");
- }
- break;
- case 5:
- MessageBox.Show("平局!");
- break;
- }
- //重新开始!
- Start();
- return;
- }
- else
- {
- //交换当前棋子颜色
- stoneflag = !stoneflag;
- }
- //测试
- //MessageBox.Show(Rule.Result(m,n,arrchessboard).ToString());
- }
- /// <summary>
- /// 人下棋
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- public void PersonDownStone(int x,int y)
- {
- if (x < 600 && y < 600)
- {
- //取下棋点
- int m = (int)Math.Floor(x/40);
- int n = (int)Math.Floor(y/40);
- if (!Rule.IsExist(m,n,arrchessboard))
- {
- DownStone(m,n);
- ComputerDownStone();
- }
- }
- }
- /// <summary>
- /// 电脑下棋
- /// </summary>
- private void ComputerDownStone()
- {
- int m,n,count=0;
- do
- {
- computer.Down(arrchessboard);
- m = computer.X;
- n = computer.Y;
- count++;
- if (count > 100)
- {
- MessageBox.Show("异常!");
- Start();
- return;
- }
- }
- while(Rule.IsExist(m,n,arrchessboard));
- DownStone(m,n);
- }
- /// <summary>
- /// 开始(是电脑(true)还是人(false))
- /// </summary>
- /// <param name="playfirstflag">判断先手玩家(是电脑(true)还是人(false),先手下黑棋)</param>
- public void Start(bool playfirstflag)
- {
- mplayfirstflag = playfirstflag;
- Start();
- }
- /// <summary>
- /// 开始
- /// </summary>
- private void Start()
- {
- Initialization();
- //构造电脑对象
- computer = new Computer(mplayfirstflag);
- //画棋盘
- Draw();
- if (mplayfirstflag)
- {
- ComputerDownStone();
- }
- }
- /// <summary>
- /// 初始化
- /// </summary>
- private void Initialization()
- {
- //置当前要走的棋为黑棋(黑棋先走)
- stoneflag = true;
- //把棋盘所有位置置为空(未下子)
- for(int i=0;i<15;i++)
- {
- for(int j=0;j<15;j++)
- {
- arrchessboard[i,j] = 2;
- }
- }
- //清空历史记录堆栈
- mStarckHistory.Clear();
- //构造棋子对象
- stone = new Stone(mg);
- }
- }
- }