五子棋遊戲
文章目錄
- 五子棋遊戲
- 前言
- 一、結果呈現
-
- 1. 界面設計
- 2. 運作結果呈現
- 二、源碼
-
- 1.Form.cs
- 2.Form.Designer.cs
- 三、總結
前言
C#實踐開發_Winform 系列第九篇篇:五子棋遊戲,進一步熟悉pictureBox控件使用。
一、結果呈現
1. 界面設計
窗體界面設計:兩個label标簽,兩個文本框textBox,兩個Button按鈕,一個pictureBox。
2. 運作結果呈現
二、源碼
1.Form.cs
代碼如下(示例):
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 test5
{
public partial class Form3 : Form
{
private enum Chess { none = 0, Black, White };
private Chess[,] Box = new Chess[15, 15];
private Chess mplayer = Chess.Black; //假設持黑子
private int r;
public Form3()
{
InitializeComponent();
}
//繪制棋盤
private void DrawBoard()
{
int i;
Graphics g = this.pictureBox1.CreateGraphics();
Pen myPen = new Pen(Color.Gray); //棋盤顔色
myPen.Width = 1;
r = pictureBox1.Width / 30;
pictureBox1.Height = pictureBox1.Width;
for (i = 0; i <= 14; i++) //豎線
{
if (i == 0 || i == 14)
myPen.Width = 2;
else
myPen.Width = 1;
g.DrawLine(myPen, r + i * 2 * r, r, r + i * 2 * r, r * 2 * 15 - r - 1);
}
for (i = 0; i <= 14; i++) //橫線
{
if (i == 0 || i == 14)
myPen.Width = 2;
else
myPen.Width = 1;
g.DrawLine(myPen, r, r + i * 2 * r, r * 2 * 15 - r - 1, r + i * 2 * r);
}
SolidBrush myBrush = new SolidBrush(Color.Black);
//繪制4個天星
g.FillEllipse(myBrush, r + 3 * r * 2 - 4, r + 3 * r * 2 - 4, 8, 8);
g.FillEllipse(myBrush, r + 3 * r * 2 - 4, r + 11 * r * 2 - 4, 8, 8);
g.FillEllipse(myBrush, r + 11 * r * 2 - 4, r + 11 * r * 2 - 4, 8, 8);
g.FillEllipse(myBrush, r + 11 * r * 2 - 4, r + 3 * r * 2 - 4, 8, 8);
}
//繪制棋子
private void Draw(Graphics g, Point p2, Chess mplayer)
{
SolidBrush myBrush;
if (mplayer == Chess.Black)
myBrush = new SolidBrush(Color.Black);
else
myBrush = new SolidBrush(Color.White);
g.FillEllipse(myBrush, p2.X * 2 * r, p2.Y * 2 * r, 2 * r, 2 * r);
}
//轉換使用者角色,提示該哪兒方先走棋
private void reverseRole()
{
if (mplayer == Chess.Black)
{
mplayer = Chess.White;
textBox2.Text = "你是白方,請走棋";
}
else
{
mplayer = Chess.Black;
textBox2.Text = "你是黑方,請走棋";
}
}
private bool IsWin()
{
Chess a = mplayer;
int i, j;
for (i = 0; i < 11; i++)
for (j = 0; j < 11; j++)
{
if (Box[i, j] == a && Box[i + 1, j + 1] == a && Box[i + 2, j + 2] == a && Box[i + 3, j + 3] == a && Box[i + 4, j + 4] == a)
return true;
}
for (i = 4; i < 15; i++)
for (j = 0; j < 11; j++)
{
if (Box[i, j] == a && Box[i - 1, j + 1] == a && Box[i - 2, j + 2] == a && Box[i - 3, j + 3] == a && Box[i - 4, j + 4] == a)
return true;
}
for (i = 0; i < 15; i++)
for (j = 4; j < 15; j++)
{
if (Box[i, j] == a && Box[i, j - 1] == a && Box[i, j - 2] == a && Box[i, j - 3] == a && Box[i, j - 4] == a)
return true;
}
for (i = 0; i < 11; i++)
for (j = 0; j < 15; j++)
{
if (Box[i, j] == a && Box[i + 1, j] == a && Box[i + 2, j] == a && Box[i + 3, j] == a && Box[i + 4, j] == a)
return true;
}
return false;
}
private void Form3_Load(object sender, EventArgs e)
{
DrawBoard();
}
private void button1_Click(object sender, EventArgs e)
{
DrawBoard();
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
Box[i, j] = Chess.none;
mplayer = Chess.Black;
textBox2.Text = "你是黑方,請先手";
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Refresh();
DrawBoard();
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
Box[i, j] = Chess.none;
mplayer = Chess.Black;
textBox2.Text = "你是黑方,請走棋";
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Graphics g = this.pictureBox1.CreateGraphics();
Point p = new Point((e.X - r / 2 + 1) / (2 * r), (e.Y - r / 2 + 1) / (2 * r));
if (p.X < 0 || p.Y < 0 || p.X > 15 || p.Y > 15)
{
MessageBox.Show("超邊界了");
return;
}
textBox1.Text = p.X.ToString() + "|" + p.Y.ToString() + "|" + e.X.ToString() + "|" + e.X.ToString();
if (Box[p.X, p.Y] != Chess.none)
{
MessageBox.Show("已有棋子");
return;
}
Draw(g, p, mplayer);
Box[p.X, p.Y] = mplayer;
if (IsWin() == true)
{
MessageBox.Show(mplayer.ToString() + "赢了此局");
button1.Enabled = true;
return;
}
reverseRole();
}
}
}
2.Form.Designer.cs
代碼如下(示例):
namespace test5
{
partial class Form3
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(817, 543);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(98, 42);
this.button1.TabIndex = 6;
this.button1.Text = "重新開始";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button2_Click);
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Location = new System.Drawing.Point(31, 60);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(612, 580);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(698, 305);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(114, 20);
this.label1.TabIndex = 2;
this.label1.Text = "棋子位置:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(704, 402);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(93, 20);
this.label2.TabIndex = 3;
this.label2.Text = "走棋方:";
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("宋體", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.textBox1.Location = new System.Drawing.Point(702, 339);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(183, 31);
this.textBox1.TabIndex = 4;
//
// textBox2
//
this.textBox2.Font = new System.Drawing.Font("宋體", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.textBox2.Location = new System.Drawing.Point(702, 437);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(183, 31);
this.textBox2.TabIndex = 5;
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.Location = new System.Drawing.Point(687, 543);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(98, 42);
this.button2.TabIndex = 0;
this.button2.Text = "開始遊戲";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button1_Click);
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(948, 745);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Name = "Form3";
this.Text = "五子棋遊戲";
this.Load += new System.EventHandler(this.Form3_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
}
}
三、總結
設計思路: Box數組儲存下過棋子的位置,Box數組初值為枚舉值Chess.none,Chess.Black,Chess.White,分别表示此處無棋子、黑子、白子。
項目源碼位址: (Form3)
連結:https://pan.baidu.com/s/1VeCR1VT9ubSinioN9fAztA
提取碼:muda
示例參考來源: Visual C#.net基礎與應用教程(夏靈活 羅菁 主編)