因为这几天在学习C#,所以就想用C#做一个人机对战的五子棋小游戏,但是人机对战的设计出来较双人对战的复杂,所以我先把双人对战的部分做出来,也是非常简陋,没有设置那些开始游戏之类的button,只是把主要的下棋部分和判断胜负部分完成了,在后面几天再加上最为复杂的人机对战部分,双人对战设计主要用到的就是GDI+绘图。游戏界面设计好之后效果如下所示:
代码部分也是非常简单,如下所示:
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 FiveChess
{
public partial class five_chess : Form
{
bool Black;
int[,] Chess = new int[19, 19];
public five_chess()
{
InitializeComponent();
}
public void Judge_Winner() //判断胜负的函数
{
int i, j;
for (i = 0; i <= 14; i++)
for (j = 0; j <= 18; j++)
{
if (Chess[i, j] == 1 && Chess[i + 1, j] == 1 && Chess[i + 2, j] == 1 && Chess[i + 3, j] == 1 && Chess[i + 4, j] == 1) MessageBox.Show("白棋胜");
if (Chess[i, j] == 2 && Chess[i + 1, j] == 2 && Chess[i + 2, j] == 2 && Chess[i + 3, j] == 2 && Chess[i + 4, j] == 2) MessageBox.Show("黑棋胜");
}
for (j = 0; j <= 14; j++)
for (i = 0; i <= 18; i++)
{
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("白棋胜");
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("黑棋胜");
}
for(i=0;i<=14;i++)
for(j=0;j<=14;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("白棋胜");
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("黑棋胜");
}
for (i = 0; i <= 14; i++)
for (j = 18; 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("白棋胜");
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("黑棋胜");
}
}
private void five_chess_Load(object sender, EventArgs e)
{
}
private void five_chess_Paint(object sender, PaintEventArgs e) //画棋盘
{
int i, j;
Graphics g;
g = this.CreateGraphics();
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(Color.Black);
Brush chess_1 = new SolidBrush(Color.White);
Brush chess_2 = new SolidBrush(Color.Black);
for (i = 20; i < 400; i += 20)
for (j = 20; j < 400; j += 20)
{
g.DrawLine(pen, i, j, i, 400 - j);
g.DrawLine(pen, j, i, 400 -j, i);
}
for(i=76; i<320; i+=120)
{
g.FillEllipse(brush,i, 76, 8, 8);
g.FillEllipse(brush,i, 196, 8, 8);
g.FillEllipse(brush,i, 316, 8, 8);
}
}
private void five_chess_MouseClick(object sender, MouseEventArgs e) //落棋,画棋子
{
Graphics g;
g = this.CreateGraphics();
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(Color.Black);
Brush chess_1 = new SolidBrush(Color.White);
Brush chess_2 = new SolidBrush(Color.Black);
int L_X, L_Y;
if (e.X % 20 > 12) L_X = e.X / 20 + 1;
else if (e.X % 20 < 8) L_X = e.X / 20;
else L_X = 0;
if (e.Y % 20 > 12) L_Y = e.Y / 20 + 1;
else if (e.Y % 20 < 8) L_Y = e.Y / 20;
else L_Y = 0;
if (L_X > 0 && L_Y > 0 && Chess[L_X-1,L_Y-1]==0)
{
if (Black == false)
{
Chess[L_X - 1, L_Y - 1] = 1; //白棋为1
g.FillEllipse(chess_1, L_X * 20 - 8, L_Y * 20 - 8, 16, 16);
}
if (Black == true)
{
Chess[L_X - 1, L_Y - 1] = 2; //黑棋为2
g.FillEllipse(chess_2, L_X * 20 - 8, L_Y * 20 - 8, 16, 16);
}
if (Black == true) Black = false;
else Black = true;
}
Judge_Winner();
}
}
}