开发工具:
文件大小: 159kb
下载次数: 0
上传时间: 2013-01-08
详细说明: 自编五子棋游戏 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace FiveStones { public partial class frmMain : Form { //定义棋盘 private Chessboard chessboard; private bool begined=false; Random rnd; public frmMain() { InitializeComponent(); Graphics g=this.CreateGraphics(); chessboard = new Chessboar d(this.CreateGraphics(), false, true); begined = false; rnd = new Random(); } private void frmMain_Load(object sender, EventArgs e) { //玩家先ToolStripMenuItem1.Checked = true; //玩家先ToolStripMenuItem1.Enabled = false; //电脑先ToolStripMenuItem1.Checked = false; //电脑先ToolStripMenuItem1.Enabled = true; //初始载入时,默认玩家先行 btnClear.Enabled = false; } private void frmMain_Paint(object sender, PaintEventArgs e) { //绘制棋盘 chessboard.Draw(); } private void frmMain_MouseDown(object sender, MouseEventArgs e) { //玩家走子 if (begined)//(chessboard.ComputerFirst && !chessboard.CurrentStoneFlag) || (!chessboard.ComputerFirst && chessboard.CurrentStoneFlag) { if ((e.X > 21 && e.X < 819) && (e.Y > 21 && e.Y < 819)) { int m = ((int)(e.X - 21)) / 42; int n = ((int)(e.Y - 21)) / 42; chessboard.PersonDownStone(m, n); ////显示历史记录 Array arr = chessboard.StackHistory.ToArray(); Array.Reverse(arr); lstHistory.DataSource = arr; //lstHistory.SelectedIndex = -1; } //调试用 //chessboard.MyStone.DrawStone(m, n, true); } } private void 开始ToolStripMenuItem_Click(object sender, EventArgs e) { // lblWhitePlayer.Enabled = false; //if (玩家先ToolStripMenuItem1.Checked) //{ // chessboard.Start(false); //} //else //{ // chessboard.Start(true); //} } private void 玩家先ToolStripMenuItem1_Click(object sender, EventArgs e) { 玩家先ToolStripMenuItem1.Checked = true; 电脑先ToolStripMenuItem1.Checked = false; lblBlackPlayer.Text = "玩家"; lblWhitePlayer.Text = "电脑"; // lblWhitePlayer.Enabled = false; } private void 电脑先ToolStripMenuItem1_Click(object sender, EventArgs e) { 玩家先ToolStripMenuItem1.Checked = false; 电脑先ToolStripMenuItem1.Checked = true; lblBlackPlayer.Text = "电脑"; lblWhitePlayer.Text = "玩家"; // lblWhitePlayer.Enabled = false; } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) { } private void frmMain_MouseMove(object sender, MouseEventArgs e) { //玩家走子时给出提示 if ((chessboard.ComputerFirst && !chessboard.CurrentStoneFlag) || (!chessboard.ComputerFirst && chessboard.CurrentStoneFlag)) { if ((e.X > 21 && e.X < 819) && (e.Y > 21 && e.Y < 819)) { int m = ((int)(e.X - 21)) / 42; int n = ((int)(e.Y - 21)) / 42; lblPosition.Text = "X:" + m.ToString() + " Y:" + n.ToString(); } } } private void 新游戏toolStripMenuItem2_Click(object sender, EventArgs e) { Start startWin=new Start(); startWin.ShowDialog(); if (startWin.DialogResult==DialogResult.OK) { //chessboard.Start(startWin.ComputerFirst); if (startWin.ComputerFirst)//电脑先走 { int x = rnd.Next(6, 13); int y = rnd.Next(6, 13); chessboard = new Chessboard(this.CreateGraphics(), startWin.ComputerFirst, true ); chessboard.StackHistory.Clear(); chessboard.DownStone(x,y); Array arr = chessboard.StackHistory.ToArray(); Array.Reverse(arr); lstHistory.DataSource = arr; startWin.Close(); } else { chessboard = new Chessboard(this.CreateGraphics(), startWin.ComputerFirst, true); startWin.Close(); } begined = true; } } private void btnClear_Click(object sender, EventArgs e) { //if (MessageBox.Show("棋步删除后不可恢复,您确定要从当前招法开始删除?", "是否删除", // MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) //{ // //for (int i = lstHistory.SelectedIndex; i < lstHistory.Items.Count; i++) //chessboard.StackHistory.Clear(); int temp = lstHistory.SelectedIndex; if (temp > -1) { for (int i = lstHistory.Items.Count - 1; i >= temp; i--) { chessboard.StackHistory.Pop(); //lstHistory.Items.RemoveAt(i); } for (int i = temp; i <= lstHistory.Items.Count - 1; i++) { //lstHistory.Items[i].ToString(); deleteMove(lstHistory.Items[i].ToString()); } if ((lstHistory.Items.Count - temp) % 2 == 1)//删除奇数步棋,当前走子方换成对方 { //chessboard.CurrentStoneFlag = !chessboard.CurrentStoneFlag; chessboard.ComputerDownStone(); } //deleteMove(lstHistory.Items[i].ToString()); chessboard.GameOver = false; //如果棋局已经结束,让棋局继续 Array arr = chessboard.StackHistory.ToArray(); Array.Reverse(arr); lstHistory.DataSource = arr; lstHistory.SelectedIndex = -1; } //} btnClear.Enabled = false; } private void lstHistory_Click(object sender, EventArgs e) { if (lstHistory.SelectedIndex != -1) { btnClear.Enabled = true; } else { btnClear.Enabled = false; } } private void lstHistory_DataSourceChanged(object sender, EventArgs e) { ////显示历史记录 //Array arr = chessboard.StackHistory.ToArray(); //Array.Reverse(arr); //lstHistory.DataSource = arr; //lstHistory.SelectedIndex = -1; } private void deleteMove(string s) { string num1, num2; int fenHaoPos=s.IndexOf(':'); int dotPos = s.IndexOf(','); num1 = s.Substring(fenHaoPos + 1, dotPos-fenHaoPos-1); num2 = s.Substring(dotPos + 1); //MessageBox.Show(num1,num2,MessageBoxButtons.OK); chessboard.Arrchessboard[Convert.ToInt16(num1),Convert.ToInt16(num2)] = 2; } private void lstHistory_SelectedIndexChanged(object sender, EventArgs e) { } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.