实例介绍
【实例简介】用于同一台电脑两个人操作,可悔棋。有兴趣的可自行添加联机版本(socket协议)
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FiveConfucianists { public enum Player { 无, 黑, 白 } public enum Piece { 无, 黑子, 白子 } public partial class Form1 : Form { public Form1() { InitializeComponent(); for (int row = 1; row <= 15; row ) { for (int col = 1; col <= 15; col ) { PieceAll[row, col] = Piece.无; } } for (int i = 1; i <= 2; i ) { Bitmaps[i ] = new Bitmap(((Piece)i).ToString() ".bmp"); } } private Point LeftPoint = new Point(30, 30); private const int Wide = 60; private const int Lenth = 60; private Bitmap backGrand = new Bitmap("bck.bmp"); private Piece[,] PieceAll = new Piece[16, 16]; private Bitmap[] Bitmaps = new Bitmap[3]; private int pieceR = 20; private Player Player = Player.无; List<Step> StepAll = new List<Step>(); private void Form1_Paint(object sender, PaintEventArgs e) { PaintQP(e.Graphics); PaintQz(e.Graphics); } private void PaintQP(Graphics g) { Pen thinPen = new Pen(Color.White, 2); //Pen fatPen = new Pen(Color.Yellow, 3); //g.DrawRectangle(fatPen, LeftPoint.X - 10, LeftPoint.Y - 10, Wide * 14 LeftPoint.X , Lenth * 14-6 LeftPoint.Y-6); g.DrawImage(backGrand,new Point(0,0)); for (int i = 0; i < 15; i ) { g.DrawLine(thinPen, LeftPoint.X , LeftPoint.Y i*Lenth, Wide * 14 LeftPoint.X , LeftPoint.Y i * Lenth); } for (int i = 0; i < 15; i ) { g.DrawLine(thinPen, LeftPoint.X i * Wide, LeftPoint.Y , LeftPoint.X i * Wide, LeftPoint.Y Lenth*14); } } private void PaintQz(Graphics g) { for (int row = 1; row <= 15; row ) { for (int col = 1; col <=15; col ) { if (PieceAll[row, col] != Piece.无)//(2,1) { g.DrawImage(Bitmaps[(int)PieceAll[row, col]], new Point(LeftPoint.X (col -1)*Wide-pieceR, LeftPoint.Y (row-1)*Lenth-pieceR)); } } } } bool IsStart = false; private void 开始ToolStripMenuItem_Click(object sender, EventArgs e) { IsStart = true; lbl_player.Text = "白"; StepAll.Clear(); for (int row = 1; row <= 15; row ) { for (int col = 1; col <=15; col ) { PieceAll[row, col] = Piece.无; } } Player = Player.白; Invalidate(); //默认白子优先 } bool ConvertPointToRowCol(Point p, out int row, out int col) { row = (p.Y - LeftPoint.Y) / Lenth 1; if (((p.Y - LeftPoint.Y) % Wide) >= Wide / 2) row = row 1; col = (p.X - LeftPoint.X) / Wide 1; if (((p.X - LeftPoint.X) % Wide) >= Wide / 2) col = col 1; Point chessP = new Point(); chessP.X = LeftPoint.X Wide * (col - 1); chessP.Y = LeftPoint.Y Wide * (row - 1); double dist = Math.Sqrt(Math.Pow(p.X - chessP.X, 2) Math.Pow(p.Y - chessP.Y, 2)); if ((dist <= pieceR) && (row <= 15) && (row >= 1) && (col <= 15) && (col >= 1)) { return true; } else { row = 0; col = 0; return false; } } private void Form1_MouseDown(object sender, MouseEventArgs e) { if (IsStart) { if (e.Button == MouseButtons.Left) { int row, col; bool valid = ConvertPointToRowCol(new Point(e.X, e.Y), out row, out col); if (valid) { if (PieceAll[row, col] == Piece.无) { if (Player == Player.白) { PieceAll[row, col] = Piece.白子; } else { PieceAll[row, col] = Piece.黑子; } //存储步数 Step step = new Step(); step.Player = Player; step.DropRow = row; step.DropCol = col; StepAll.Add(step); Invalidate(); if (IfWin()) { MessageBox.Show("玩家:" Player " 胜!"); lbl_player.Text = ""; IsStart = false; } else { if (Player == Player.白) { Player = Player.黑; lbl_player.Text = "黑"; } else { Player = Player.白; lbl_player.Text = "白"; } } } } } } } private bool IfWin() { bool IsWin = false; for(int row = 1; row <= 15; row ) { for(int col = 1; col <= 11; col ) { if (PieceAll[row, col] != Piece.无) { if(PieceAll[row, col] == PieceAll[row , col 1] && PieceAll[row, col] == PieceAll[row , col 2] && PieceAll[row, col] == PieceAll[row , col 3] && PieceAll[row, col] == PieceAll[row , col 4]) { IsWin = true; } } } } for (int col = 1; col <= 15; col ) { for (int row = 1; row <= 11; row ) { if (PieceAll[row, col] != Piece.无) { if (PieceAll[row, col] == PieceAll[row 1, col]&& PieceAll[row, col] == PieceAll[row 2, col]&& PieceAll[row, col] == PieceAll[row 3, col]&& PieceAll[row, col] == PieceAll[row 4, col]) { IsWin = true; } } } } for (int col = 1; col <= 10; col ) { for (int row = 1; row <= 10; row ) { if (PieceAll[row, col] != Piece.无) { if (PieceAll[row, col] == PieceAll[row 1, col 1] && PieceAll[row, col] == PieceAll[row 2, col 2] && PieceAll[row, col] == PieceAll[row 3, col 3] && PieceAll[row, col] == PieceAll[row 4, col 4]) IsWin = true; } } } for (int row = 1; row <=11; row ) { for (int col = 15; col >=5; col--) { if (PieceAll[row, col] != Piece.无) { if (PieceAll[row, col] == PieceAll[row 1, col - 1] && PieceAll[row, col] == PieceAll[row 2, col - 2] && PieceAll[row, col] == PieceAll[row 3, col - 3] && PieceAll[row, col] == PieceAll[row 4, col - 4]) IsWin = true; } } } return IsWin; } private void 悔棋ToolStripMenuItem_Click(object sender, EventArgs e) { if (IsStart) { if (StepAll.Count > 0) { Step step = StepAll.Last(); if (step.Player == Player.白) { Player = Player.白; PieceAll[step.DropRow, step.DropCol] = Piece.无; lbl_player.Text = "白"; } else { Player = Player.黑; PieceAll[step.DropRow, step.DropCol] = Piece.无; lbl_player.Text = "黑"; } StepAll.RemoveAt(StepAll.Count - 1); Invalidate(); } } } } }
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论