gitee网址
https://gitee.com/kittysmith5/gobang/blob/master/WindowsFormsApplication1/Form1.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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private readonly int size;
private int X, Y;
private readonly int[,] chess;
private int counter;
private int q1;
private int q2;
public Form1()
{
InitializeComponent();
this.size = 15;
this.chess = new int[this.size, this.size];
this.counter = 0;
}
private void DrawLines()
{
Graphics g = CreateGraphics();
for (int i = 0; i < size; i++)
{
g.DrawLine(new Pen(Color.Black), new Point(50, 50 + i * 20),
new Point(50 + (size - 1) * 20, 50 + i * 20));
g.DrawLine(new Pen(Color.Black), new Point(50 + i * 20, 50),
new Point(50 + i * 20, 50 + (size - 1) * 20));
}
}
private void Form1_Shown(object sender, EventArgs e)
{
DrawLines();
}
private void DrawCircle(int c, int x, int y)
{
Graphics g1 = this.CreateGraphics();
if (c % 2 == 1)
g1.FillEllipse(Brushes.White, x - 9, y - 9, 18, 18);//来实心圆
else
g1.FillEllipse(Brushes.Black, x - 9, y - 9, 18, 18);//来实心圆
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
X = Y = 0;
q1 = q2 = 0;
for (int i = 0; i < size; i++)
{
if (e.X >= i * 20 + 50 - 8 && e.X <= i * 20 + 50 + 8)
{
X = i * 20 + 50;
q1 = i;
}
if (e.Y >= i * 20 + 50 - 8 && e.Y <= i * 20 + 50 + 8)
{
Y = i * 20 + 50;
q2 = i;
}
}
if (X >= 0 * size + 50 - 9 && X <= 50 + (size - 1) * 20 &&
Y >= 0 * size + 50 - 9 && Y <= 50 + (size - 1) * 20 &&
chess[q2, q1] == 0 && counter % 2 == 0)
{
chess[q2, q1] = 1;
DrawCircle(counter, X, Y);
counter++;
}
if (X >= 0 * size + 50 - 9 && X <= 50 + (size - 1) * 20 &&
Y >= 0 * size + 50 - 9 && Y <= 50 + (size - 1) * 20 &&
chess[q2, q1] == 0 && counter % 2 != 0)
{
chess[q2, q1] = 2;
DrawCircle(counter, X, Y);
counter++;
}
/*判断胜负开始*/
//横向判断
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 4; j++)
{
if (chess[i, j] == 1 && chess[i, j + 1] == 1 && chess[i, j + 2] == 1
&& chess[i, j + 3] == 1 && chess[i, j + 4] == 1)
{
MessageBox.Show("Black Win!");
Application.Exit();
}
if (chess[i, j] == 2 && chess[i, j + 1] == 2 && chess[i, j + 2] == 2
&& chess[i, j + 3] == 2 && chess[i, j + 4] == 2)
{
MessageBox.Show("White Win!");
Application.Exit();
}
}
}
//纵向判断
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 4; j++)
{
if (chess[j, i] == 1 && chess[j + 1, i] == 1 && chess[j + 2, i] == 1
&& chess[j + 3, i] == 1 && chess[j + 4, i] == 1)
{
MessageBox.Show("Black Win!");
Application.Exit();
}
if (chess[j, i] == 2 && chess[j + 1, i] == 2 && chess[j + 2, i] == 2
&& chess[j + 3, i] == 2 && chess[j + 4, i] == 2)
{
MessageBox.Show("White Win!");
Application.Exit();
}
}
}
//主对角线
for (int i = 0; i < size - 4; i++)
{
for (int j = 0; j < size - 4; j++)
{
if (chess[i, j] == 1 && chess[i + 1, j + 1] == 1 && chess[i + 2, j + 2] == 1
&& chess[i + 3, j + 3] == 1 && chess[i + 4, j + 4] == 1)
{
MessageBox.Show("Black Win!");
Application.Exit();
}
if (chess[i, j] == 2 && chess[i + 1, j + 1] == 2 && chess[i + 2, j + 2] == 2
&& chess[i + 3, j + 3] == 2 && chess[i + 4, j + 4] == 2)
{
MessageBox.Show("White Win!");
Application.Exit();
}
}
}
//副对角线
for (int i = 0; i < size - 4; i++)
{
for (int j = 4; j < size; j++)
{
if (chess[i, j] == 1 && chess[i + 1, j - 1] == 1 && chess[i + 2, j - 2] == 1
&& chess[i + 3, j - 3] == 1 && chess[i + 4, j - 4] == 1)
{
MessageBox.Show("Black Win!");
Application.Exit();
}
if (chess[i, j] == 2 && chess[i + 1, j - 1] == 2 && chess[i + 2, j - 2] == 2
&& chess[i + 3, j - 3] == 2 && chess[i + 4, j - 4] == 2)
{
MessageBox.Show("White Win!");
Application.Exit();
}
}
}
/*判断胜负结束*/
}
}
}