翻出以前没有完成的一个五子棋
想写人机对战的,规则太复杂,放弃了……发给大家消遣一下
using System;
using System.Collections.Generic;
using System.Text;
namespace gobang
{
public sealed class Gobang : ICloneable
{
public const byte Black = 1;
public const byte White = 2;
public const byte Side = 8;
public const byte Blank = 0;
public const int Size = 19;
private const int GSize = Size + 1;
private const int Capability = GSize * (GSize + 1) + 1;
private readonly static int[] Direct = new int[8] { -GSize, -GSize + 1, 1, GSize + 1, GSize, GSize - 1, -1, -GSize - 1 };
private byte firstTurn;
private byte turn;
private bool end;
private byte[] points;
public Gobang()
{
points = new byte[Capability];
firstTurn = Black;
Reset();
}
public object Clone()
{
Gobang gb = new Gobang();
Array.Copy(points, gb.points, points.Length);
gb.firstTurn = firstTurn;
gb.end = end;
gb.turn = turn;
return gb;
}
public void Reset()
{
for (int i = 0; i < Capability; i++)
{
points[i] = Blank;
}
for (int i = 0; i <= GSize; i++)
{
points[i] = points[Capability - i - 1] = points[GSize * i] = Side;
}
turn = firstTurn;
end = false;
}
public byte GetGB(int x, int y)
{
if (x <= 0 || x > Size || y <= 0 || y > Size)
{
return Side;
}
return points[x * GSize + y];
}
public bool SetGB(byte gb, int x, int y)
{
if (x <= 0 || x > Size || y <= 0 || y > Size)
{
return false;
}
return SetGB(gb, x * GSize + y);
}
public byte GetTurn()
{
return turn;
}
public bool IsEnd()
{
return end;
}
private bool SetGB(byte gb, int pos)
{
if (pos < 0 || pos >= Capability || points[pos] != Blank || turn != gb || end)
{
return false;
}
points[pos] = gb;
if (CheckFive(pos))
{
end = true;
}
ChangeTurn();
return true;
}
private bool CheckFive(int pos)
{
if (points[pos] != White && points[pos] != Black)
{
return false;
}
int count,current_pos;
for (int i = 0; i < 4; i++)
{
count = 1;
current_pos = pos + Direct[i];
while (points[current_pos] == points[pos] && count < 5)
{
current_pos += Direct[i];
count++;
}
current_pos = pos + Direct[i + 4];
while (points[current_pos] == points[pos] && count < 5)
{
current_pos += Direct[i + 4];
count++;
}
if (count == 5) return true;
}
return false;
}
private void ChangeTurn()
{
turn = (byte)(Black ^ White ^ turn);
}
}
}
==========================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace gobang
{
public partial class Form1 : Form
{
private const int cellwidth = 30;
private const int dia = 24;
private const int start_X = cellwidth / 2;
private const int start_Y = cellwidth / 2;
private const int lineNum = Gobang.Size;
private const int point_dia = 8;
private Gobang instance = new Gobang();
private int[] sequence;
private Gobang[] gbsequence;
private int step;
private int max_step;
private Bitmap bufferedBackground;
public Form1()
{
InitializeComponent();
Init();
}
private void Init()
{
sequence = new int[Gobang.Size * Gobang.Size];
gbsequence = new Gobang[sequence.Length];
gbsequence[0] = (Gobang)instance.Clone();
bufferedBackground = new Bitmap((Gobang.Size + 1) * cellwidth, (Gobang.Size + 1) * cellwidth + 1);
DrawBoard();
panelBoard.BackgroundImage = bufferedBackground;
Reset();
}
private void Reset()
{
instance.Reset();
for (int i = 0; i < sequence.Length; i++)
{
sequence[i] = 0;
}
step = 0;
max_step = 0;
RefreshSequence();
}
private void panelBoard_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
for (int x = 0; x < Gobang.Size; x++)
{
for (int y = 0; y < Gobang.Size; y++)
{
DrawPoint(g, instance.GetGB(x + 1, y + 1), x, y);
}
}
}
private void DrawBoard()
{
Graphics g = Graphics.FromImage(bufferedBackground);
g.Clear(this.BackColor);
for (int i = 0; i < Gobang.Size; i++)
{
g.DrawLine(new Pen(Color.Black, 1.0f), start_X, start_Y + i * cellwidth, start_X + (lineNum - 1) * cellwidth, start_Y + i * cellwidth);
g.DrawLine(new Pen(Color.Black, 1.0f), start_X + i * cellwidth, start_Y, start_X + i * cellwidth, start_Y + (lineNum - 1) * cellwidth);
}
for (int x = 3; x < Gobang.Size - 3; x += (Gobang.Size - 6) / 2)
{
for (int y = 3; y < Gobang.Size - 3; y += (Gobang.Size - 6) / 2)
{
g.FillEllipse(Brushes.Black, x * cellwidth + start_X - point_dia / 2, y * cellwidth + start_Y - point_dia / 2, point_dia, point_dia);
}
}
}
private void DrawPoint(Graphics g, int color, int x, int y)
{
Rectangle rect = new Rectangle(x * cellwidth + start_X - dia / 2, y * cellwidth + start_Y - dia / 2, dia, dia);
switch (color)
{
case Gobang.Black:
g.FillEllipse(Brushes.Black, rect);
break;
case Gobang.White:
g.FillEllipse(Brushes.White, rect);
g.DrawEllipse(new Pen(Color.Black, 1.0f), rect);
break;
default:
break;
}
}