基本信息
源码名称:五子棋单机版(C#源码)
源码大小:0.34M
文件格式:.zip
开发语言:C#
更新时间:2020-03-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
用于同一台电脑两个人操作,可悔棋。有兴趣的可自行添加联机版本(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(); } } } } }