📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(500, 500);
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5,
this.menuItem6,
this.menuItem7});
this.menuItem1.Text = "游戏";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "重新开始";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "暂停游戏";
//
// menuItem6
//
this.menuItem6.Index = 2;
this.menuItem6.Text = "-";
//
// menuItem7
//
this.menuItem7.Index = 3;
this.menuItem7.Text = "退出游戏";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem8});
this.menuItem2.Text = "设置";
//
// menuItem8
//
this.menuItem8.Index = 0;
this.menuItem8.Text = "游戏设置";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem9});
this.menuItem3.Text = "帮助";
//
// menuItem9
//
this.menuItem9.Index = 0;
this.menuItem9.Text = "关于";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// label1
//
this.label1.Font = new System.Drawing.Font("黑体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(184, 520);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 24);
this.label1.TabIndex = 1;
this.label1.Text = "沈:五子棋";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(520, 565);
this.Controls.Add(this.label1);
this.Controls.Add(this.panel1);
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "沈: 五子棋";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private Checker[,] Gobang = new Checker[15, 15];
private CheckerEnum Chessr = CheckerEnum.BlackChess;
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//获取鼠标点击的棋子位置
ChessPoint chessPoint = new ChessPoint(e);
if (Gobang[chessPoint.X,chessPoint.Y]!=null)
{
MessageBox.Show("此处已放置棋子!");
return;
}
Checker checker = new Checker(Chessr,chessPoint);
//放置棋子
SetChecker(checker);
//添加到数组
Gobang[chessPoint.X,chessPoint.Y] = checker;
//检查是否获胜
if (Chessr == CheckerEnum.WhiteChes)
{
if (CheckWin(CheckerEnum.WhiteChes))
{
MessageBox.Show("白棋胜利!");
label1.Text = "白棋胜利";
panel1.Enabled = false;
}
else
{
label1.Text = "黑棋出";
Chessr = CheckerEnum.BlackChess;
}
}
else if (Chessr == CheckerEnum.BlackChess)
{
if (CheckWin(CheckerEnum.BlackChess))
{
MessageBox.Show("黑棋胜利!");
label1.Text = "黑棋胜利";
panel1.Enabled = false;
}
else
{
label1.Text = ("白棋出");
Chessr = CheckerEnum.WhiteChes;
}
}
}
}
/// <summary>
/// 绘制棋盘和棋子
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//绘制棋盘
Graphics graphics = e.Graphics;
Image image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\bg.jpg");
graphics.DrawImage(image,new Rectangle(0,0,500,500));
//绘制棋子
foreach(Checker checker in Gobang)
{
if (checker != null)
SetChecker(checker);
}
}
/// <summary>
/// 摆放棋子在棋盘
/// </summary>
/// <param name="checker"></param>
/// <param name="chessPoint"></param>
private void SetChecker(CheckerEnum checker,ChessPoint chessPoint)
{
Brush brush; //判断棋子的颜色
if (checker == CheckerEnum.BlackChess)
{
brush = new SolidBrush(Color.Black);
}
else if(checker == CheckerEnum.WhiteChes)
{
brush = new SolidBrush(Color.White);
}
else
{
return;
}
//获取摆放位置
Point point = chessPoint.GetChessPoint();
Graphics graphics= panel1.CreateGraphics();
graphics.FillEllipse(brush,point.X, point.Y, 20, 20);
graphics.Dispose();
}
private void SetChecker(Checker checker)
{
SetChecker(checker.ChessColor,checker.ChessPoint);
}
/// <summary>
/// 检查是否获胜
/// </summary>
/// <param name="checker"></param>
/// <returns></returns>
private bool CheckWin(CheckerEnum checker)
{
bool IsWin = false;
for (int x = 0; x < 15;x++)
{
for (int y = 0; y < 15;y++)
{
if (Gobang[x,y] != null)
{
if (Gobang[x,y].ChessColor == checker)
{
int ChessWinX,ChessWinY,ChessWinXY,ChessWinYX;
ChessWinX = ChessWinY = ChessWinXY = ChessWinYX = 0;
for (int i = 0;i <= 4;i++)
{
if (!(y > 10))
{
//判断行
if (Gobang[x,y+i] != null && Gobang[x,y+i].ChessColor == checker)
{
ChessWinX++;
}
if (ChessWinX==5)
IsWin=true;
}
if (!(x > 10))
{
//判断列
if (Gobang[x+i,y] != null && Gobang[x+i,y].ChessColor == checker)
{
ChessWinY++;
}
if (ChessWinY==5)
IsWin=true;
}
if (!(x > 10 || y > 10))
{
//判断 \ 斜线
if (Gobang[x+i,y+i] != null && Gobang[x+i,y+i].ChessColor == checker)
{
ChessWinXY++;
}
if (ChessWinXY==5)
IsWin=true;
}
if ( y > 3 && x < 11)
{
//判断 / 斜线
if (Gobang[x+i,y-i] != null && Gobang[x+i,y-i].ChessColor == checker)
{
ChessWinYX++;
}
if (ChessWinYX==5)
IsWin=true;
}
}
}
}
}
}
return IsWin;
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuItem9_Click(object sender, System.EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
Gobang = new Checker[15, 15];
Chessr = CheckerEnum.BlackChess;
panel1.Invalidate();
label1.Text = "黑棋出";
panel1.Enabled = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -