热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#实现简单的五子棋程序

gitee网址https:gitee.comkittysmith5gobangblobmasterWindowsFormsApplication1Form1.cs核心源代码?usi

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 {
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 {
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 {
for (int j = 0; 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 {
for (int j = 0; 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 {
for (int j = 0; 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 {
for (int j = 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();
}
}
}
/*判断胜负结束*/
}
}
}


推荐阅读
author-avatar
手机用户2502896067
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有