c#winform 您所在的位置:网站首页 winform五子棋码源 c#winform

c#winform

2024-07-13 23:57| 来源: 网络整理| 查看: 265

每日励志

基本五子棋结构和代码请见c#winform——Gobang五子棋简易版双人对战制作(基本结构+代码) 

开始

在我们有了基础性的结构后,我们可以在这些上面稍微做一些改进

1.当下了一个子后,出现音效“砰”(我不太清楚下棋的音效咋样,但砰听起来很霸气(*/ω\*))

2.每打完一局后,记录所有棋子的坐标以及颜色的属性,通过System.IO来将其存储起来

3.在还没有开始游戏时,可以查看之前下棋的数据,并打印在上面。

4.记录时间,思考时间只留60s,超过则判定为输

有了以上规划后,我们就可以来制作五子棋了!

首先是封面↓

这两个分别是保存和打开的界面

有了这些之后,便可以开始去做相对应的模块

首先就是砰的音效,我在网上搜了一下,最后是在这上面下载的

https://sc.chinaz.com/yinxiao/?classid=&keyword=%E4%B8%8B%E6%A3%8B

我用的是第三排的那个音效

下载完后,你就可以得到一个这样的文件

 得到这个文件后,把他拖到这里来

 (也就是相对debug的相对目录下)

放完后就开始编辑代码,现在共有环境下添加下列代码

SoundPlayer playsound = new SoundPlayer();//实例化soundplayer public bool sound1 = false;//音乐是否存在/使用

在Form1_Load的目录下添加代码↓

try { playsound.SoundLocation = @"5390.wav"; sound1 = true; } catch { MessageBox.Show("下棋音响丢失,无法出现下棋声音!"); }

用这个来检测音效是否存在或者使用

添加完后,在下棋检测成功中间添加

 

if (sound1) { playsound.Play();//播放声音 }

这样音效部分就完成了! 

后面就是记录部分

双击这个

然后可以得到 

(应该是局,但这里显示错误) 

SaveFileDialog sv = new SaveFileDialog(); sv.Title = "要保存的路径";//开头名 sv.Filter = "文本文件(*.txt)|*.txt";//文件名 sv.RestoreDirectory = true;//是否记录位置 if (sv.ShowDialog() == DialogResult.OK) { string Path = sv.FileName.ToString();//得到位置 File.Create(@Path).Close();//在该位置新建文件 using(StreamWriter wr = new StreamWriter(@Path)) { for(int i = 0; i < steps.Length; i++) { wr.Write(steps[i]);//重复将棋盘中的每一项存进去 } wr.Close();//关闭流 } MessageBox.Show("保存成功!"); }

保存之后应该得到是

这种一百个字符的txt文件

———————————————————————————————————————————

下面就是保存输赢

在保存输赢中添加 

if (!startgame) { if (!File.Exists(@"savepage.txt")) { File.Create(@"savepage.txt").Close(); } using (StreamWriter wr = new StreamWriter(@"savepage.txt")) { if (whowin) { wr.WriteLine(DateTime.Now.ToString() + "——白方胜利!"); MessageBox.Show("保存成功!"); } else if (!whowin) { wr.WriteLine(DateTime.Now.ToString() + "——黑方胜利!"); MessageBox.Show("保存成功!"); } else { MessageBox.Show("请开始新的一把!"); } } } else { MessageBox.Show("请在这一把结束后再选择!"); }

记录后应该是

这种格式 

———————————————————————————————————————————

接下来就到了重头戏,也就是读取

其实读取并不难,因为他是由100个数字组成,每一个数字的下标应该是

a      b                 如23:            2                  3

↑      ↑                                        ↑                  ↑

列    行                                   第二列          第三行

所以,依据这个特性,就可以得出下标为23的数字所对应的下标就是(2,3)。

除此之外之前我们也规定过:

0是没人下过——1是白色下过——2是黑色下过

所以我们能够判断这个坐标所处的状态

所以代码如下

if (!startgame) { Pen Wh = new Pen(Color.White, 20);//白色画笔 Pen BL = new Pen(Color.Black, 20);//黑色画笔 Graphics g = this.CreateGraphics();//实例化 Pen sc = new Pen(Color.Brown, 2);//棋盘画笔 g.Clear(Color.IndianRed);//清理棋盘 int start = ((this.Width - 100) % 11) / 2; int final = ((this.Height - 100) % 11) / 2; OpenFileDialog op = new OpenFileDialog(); op.Filter = "文本文件(*.txt)|*.txt"; op.Title = ("请选择您要查看的文件"); op.RestoreDirectory = true; if (op.ShowDialog() == DialogResult.OK) { string Path = op.FileName.ToString(); string get; using (StreamReader sr = new StreamReader(@Path,Encoding.UTF8)) { get = sr.ReadToEnd(); if (get.Length == 100)//文件长度只能是100 { char[] fin = get.ToCharArray();//将这个分成一个个字符,这样就可以判断它的下标 //绘制棋盘 for (int i = 0; i < 10; i++)//竖着的线 { g.DrawLine(sc, 60 + start + 40 * i, 40, 60 + start + 40 * i, this.Height - 70);//绘制棋盘,每条线之间格40个像素 Thread.Sleep(10); } for (int i = 0; i < 10; i++)//横着的线 { g.DrawLine(sc, 40, 60 + final + 40 * i, 438, 60 + final + 40 * i); Thread.Sleep(10); } //绘制棋子 for (int i = 0; i < 100; i++) { int x = i / 10;//确定列 int y = i - 10 * x;//确定行 if (Convert.ToString(fin[i]) == "1")//如果监测得到这是白色 { g.DrawEllipse(Wh, 59 + 40 * y, 54 + 40 * x, 10, 10); Thread.Sleep(5);//制作一捏捏动画 } else if (Convert.ToString(fin[i]) == "2")//如果监测得到这是黑色 { g.DrawEllipse(BL, 59 + 40 * y, 54 + 40 * x, 10, 10); Thread.Sleep(5);//制作一捏捏动画 } } } else { MessageBox.Show("请选择正确的文件格式"); } } } }

这样就能打开了,实现结果请在最后看

———————————————————————————————————————————

后面就是时间(倒计时)限制

在这里先是Form1_Load的全部过程,计时是在最后几段

startgame = false; this.MaximizeBox = false; this.MinimizeBox = false; this.MouseClick += Form1_MouseClick; try { playsound.SoundLocation = @"5390.wav"; sound1 = true; } catch { MessageBox.Show("下棋音响丢失,无法出现下棋声音!"); } //设置时间间隔ms int interval = 1000; Mytimer = new System.Timers.Timer(interval); //设置重复计时 Mytimer.AutoReset = true; //设置执行System.Timers.Timer.Elapsed事件 Mytimer.Elapsed += new System.Timers.ElapsedEventHandler(Mytimer_tick);

其他加载模块

private void Mytimer_tick(object sender, System.Timers.ElapsedEventArgs e) { this.Invoke(new SetControlValue(showtime), TimeCount); TimeCount--; } private void showtime(long t) { if (t >= 0) { TimeSpan temp = new TimeSpan(0, 0, (int)t); label1.Text = "倒计时:" + string.Format("{0:00}:{1:00}:{2:00}", temp.Hours, temp.Minutes, temp.Seconds); } else { if (ones) { MessageBox.Show("超时未落子,黑方胜利!"); } else { MessageBox.Show("超时未落子,白方胜利!"); } startgame = false; Mytimer.Stop(); } }

同时在游戏初始模块添加(这里为了方便全给出来了)

private void 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e) { ones = true; Pen sc = new Pen(Color.Brown, 2); Graphics g = this.CreateGraphics(); g.Clear(Color.IndianRed);//清理棋盘 g.DrawRectangle(sc, 40, 40, this.Width - 130, this.Height - 110); int start = ((this.Width - 100) % 11) / 2; int final = ((this.Height - 100) % 11) / 2; for (int i = 0; i < 10; i++)//竖着的线 { g.DrawLine(sc, 60 + start + 40 * i, 40, 60 + start + 40 * i, this.Height - 70);//绘制棋盘,每条线之间格40个像素 Thread.Sleep(10); } for (int i = 0; i < 10; i++)//横着的线 { g.DrawLine(sc, 40, 60 + final + 40 * i, 438, 60 + final + 40 * i); Thread.Sleep(10); } //string[] steps = new string[100]; for (int i = 0; i < 10; i++)//行 { for (int j = 0; j < 10; j++)//列 { steps[i * 10 + j] = "0"; } } ones = true; startgame = true; //计时器模块 Mytimer.Start(); TimeCount = 60; }

同时再点击方法下面也添加如此(这里为了方便全给出来了)

private void Form1_MouseClick(object sender, MouseEventArgs e) { if (startgame) { string tiqv = string.Empty;//辅助,看过程更明白 int num = 0;//连的个数 Pen Wh = new Pen(Color.White, 20); Pen BL = new Pen(Color.Black, 20); Graphics g = this.CreateGraphics(); Point formPoint = this.PointToClient(Control.MousePosition); int x = formPoint.X; int y = formPoint.Y; //i为列,j为行 for (int i = 0; i < 10; i++) { if (x < 75 + 40 * i && x > 55 + 40 * i)//判断是在那一列上 { for (int j = 0; j < 10; j++) { if (y < 70 + 40 * j && y > 50 + 40 * j)//判断是在那一行上 { Mytimer.Stop(); tiqv = steps[j * 10 + i]; if (sound1) { playsound.Play();//播放声音 } if (tiqv == "0") { if (ones) { g.DrawEllipse(Wh, 59 + 40 * i, 54 + 40 * j, 10, 10); ones = false; steps[j * 10 + i] = "1";//设置为白,代表这里是白下过的 //竖着有五个子的胜利判断 bool win = white_judge_vertical(i, j); if (win) { MessageBox.Show("白方赢了!"); whowin = true; } else { win = white_judge_transverse(i, j); if (win) { MessageBox.Show("白方赢了!"); whowin = true; } else { win = white_judge_oblique(i, j); if (win) { MessageBox.Show("白方赢了!"); whowin = true; } } } } else { g.DrawEllipse(BL, 59 + 40 * i, 54 + 40 * j, 10, 10); ones = true; steps[j * 10 + i] = "2";//设置为黑,代表这里是黑下过的 //竖着有五个子的胜利判断 bool win = black_judge_vertical(i, j); if (win) { MessageBox.Show("黑方赢了!"); whowin = false; } else { win = black_judge_transverse(i, j); if (win) { MessageBox.Show("黑方赢了!"); whowin = false; } else { win = black_judge_oblique(i, j); if (win) { MessageBox.Show("黑方赢了!"); whowin = false; } } } } if (!whowin) { Mytimer.Start(); } TimeCount = 60; break; } else { MessageBox.Show("你不可以在别人或自己下过的地方在下一次!"); } } } } } } }

这样,整体代码就完全结束了(但并未完结,后面会出联网模块)

测试视频如下↓

GOBANG——五子棋演示

以上就是这一篇文章的全部内容,若有问题请在下方评论区回复

谢谢!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有