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;
using System.Drawing.Drawing2D;
namespace gobang
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
boundary1 = new Point(this.Width / 2 / square - 2, this.Height / 2 / square - 2);
boundary2 = new Point(this.Width / 2 / square + 2, this.Height / 2 / square + 2);
timer1.Start();
}
private Point[] white, black;
private int whitenum = 0, blacknum = 0, x, y, square = 30, picnum=0, mousex=0, mousey=0;
private bool turn = false, preturn, busy = false;
private bool[] p, q, r, s, h, hh, pp, qq, rr, ss, np, nq, nr, ns, npp, nqq, nrr, nss;
private Point boundary1, boundary2;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
x = e.X / square;
y = e.Y / square;
if (busy == false)
{
busy = true;
playgo();
getboundary();
calculate(turn, black, white, blacknum, whitenum);
this.Refresh();
busy = false;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//board lines
for (int i = 0; i * square < this.Size.Width; i++)
{
Pen bluepen = new Pen(Color.DarkBlue, 2);
Point a = new Point(square / 2 + square * i, 0);
Point b = new Point(square / 2 + square * i, this.Size.Height);
e.Graphics.DrawLine(bluepen, a, b);
}
for (int i = 0; i * square < this.Size.Height; i++)
{
Pen bluepen = new Pen(Color.DarkBlue, 2);
Point a = new Point(0, square / 2 + square * i);
Point b = new Point(this.Size.Height, square / 2 + square * i);
e.Graphics.DrawLine(bluepen, a, b);
}
//white piece
for (int i = 0; i < whitenum; i++)
{
SolidBrush whitebrush = new SolidBrush(Color.White);
e.Graphics.FillEllipse(whitebrush, square * white[i].X, square * white[i].Y, square, square);
}
//black piece
for (int i = 0; i < blacknum; i++)
{
SolidBrush blackbrush = new SolidBrush(Color.Black);
e.Graphics.FillEllipse(blackbrush, square * black[i].X, square * black[i].Y, square, square);
}
//boundary
Pen greenpen = new Pen(Color.Green, 10);
e.Graphics.DrawRectangle(greenpen, square * boundary1.X - square / 2, square * boundary1.Y - square / 2, square * (boundary2.X - boundary1.X), square * (boundary2.Y - boundary1.Y));
//last move
Pen redpen = new Pen(Color.Red, 1);
e.Graphics.DrawRectangle(redpen, x*square, y*square, square, square);
//mouse
SolidBrush whitebrush1 = new SolidBrush(Color.White);
e.Graphics.FillEllipse(whitebrush1, mousex-square/2, mousey-square/2, square, square);
}
private void playgo()
{
busy = true;
bool occupy = false;
if (turn == false)//white turn
{
turn = true;
if (checkoccupy(white, whitenum, x, y) || checkoccupy(black, blacknum, x, y))
{
occupy = true;
}
if (occupy == false)
{
whitenum++;
Array.Resize(ref white, whitenum);
white = addarray(white, whitenum, x, y);
preturn = turn;
}
else
{
turn = preturn;
}
}
else//black turn
{
turn = false;
if (checkoccupy(white, whitenum, x, y) || checkoccupy(black, blacknum, x, y))
{
occupy = true;
}
if (occupy == false)
{
blacknum++;
Array.Resize(ref black, blacknum);
black = addarray(black, blacknum, x, y);
preturn = turn;
}
else
{
turn = preturn;
}
}
}
private void calculate(bool t, Point[] p1, Point[] p2, int num1, int num2)
{
if (t == true)//black turn
{
for (int depth = 0; depth != 35; depth++)
{
for (int i = boundary1.X; i < boundary2.X; i++)
{
for (int j = boundary1.Y; j < boundary2.Y; j++)
{
if (!checkoccupy(p1, num1, i, j) && !checkoccupy(p2, num2, i, j))
{
p = new bool[] { false, false, false, false, false, false, false, false };
np = new bool[] { false, false, false, false, false, false, false, false };
q = new bool[] { false, false, false, false, false, false, false, false };
nq = new bool[] { false, false, false, false, false, false, false, false };
r = new bool[] { false, false, false, false, false, false, false, false };
s = new bool[] { false, false, false, false, false, false, false, false };
nr = new bool[] { false, false, false, false, false, false, false, false };
h = new bool[] { false, false, false, false, false, false, false, false };
p = emptypointinfo(p1, num1, i, j, 1, p);
q = emptypointinfo(p1, num1, i, j, 2, q);
r = emptypointinfo(p1, num1, i, j, 3, r);
s = emptypointinfo(p1, num1, i, j, 4, s);
h = emptypointinfo(p1, num1, i, j, 5, h);
nq = emptypointinfo(p1, num1, i, j, -2, nq);
np = emptypointinfo(p1, num1, i, j, -1, np);
nr = emptypointinfo(p1, num1, i, j, -3, nr);
pp = new bool[] { false, false, false, false, false, false, false, false };
qq = new bool[] { false, false, false, false, false, false, false, false };
nqq = new bool[] { false, false, false, false, false, false, false, false };
npp = new bool[] { false, false, false, false, false, false, false, false };
rr = new bool[] { false, false, false, false, false, false, false, false };
nrr = new bool[] { false, false, false, false, false, false, false, false };
ss = new bool[] { false, false, false, false, false, false, false, false };
hh = new bool[] { false, false, false, false, false, false, false, false };
pp = emptypointinfo(p2, num2, i, j, 1, pp);
qq = emptypointinfo(p2, num2, i, j, 2, qq);
nqq = emptypointinfo(p2, num2, i, j, -2, nqq);
npp = emptypointinfo(p2, num2, i, j, -1, npp);
rr = emptypointinfo(p2, num2, i, j, 3, rr);
nrr = emptypointinfo(p2, num2, i, j, -3, nrr);
ss = emptypointinfo(p2, num2, i, j, 4, ss);
hh = emptypointinfo(p2, num2, i, j, 5, hh);
int[] count = new int[20];
for (int ii = 0; ii < 20; ii++)
{
count[ii] = 0;
}
for (int k = 0; k < 8; k++)
{
if (pp[k] && qq[k] && rr[k] && ss[k] && hh[k]) { count[0]++; }//oxxxxx
else if (p[k] && q[k] && r[k] && s[k]) { count[1]++; }//oxxxx
else if (p[k] && q[k] && r[k] && np[k]) { count[1]++; }//xoxxx
else if (p[k] && q[k] && np[k] && nq[k]) { count[1]++; }//xxoxx
else if (pp[k] && qq[k] && rr[k] && ss[k]) { count[2]++; }//oxxxx
else if (pp[k] && qq[k] && rr[k] && npp[k]) { count[2]++; }//xoxxx
else if (pp[k] && qq[k] && npp[k] && nqq[k]) { count[2]++; }//xxoxx
else if (p[k] && q[k] && r[k] && ss[k] && !npp[k] && !np[k]) { count[3]++; }//-oxxx\
else if (p[k] && q[k] && s[k] && !rr[k] && !r[k] && hh[k]) { count[3]++; }//oxx-x\
else if (p[k] && r[k] && s[k] && !qq[k] && !q[k] && hh[k]) { count[3]++; }//ox-xx\
else if (q[k] && r[k] && s[k] && !pp[k] && !p[k] && hh[k]) { count[3]++; }//o-xxx\
else if (p[k] && q[k] && np[k] && rr[k] && !nq[k] && !nqq[k]) { count[3]++; }//-xoxx\
else if (p[k] && q[k] && np[k] && !rr[k] && !r[k] && nqq[k]) { count[3]++; }//\xoxx-
else if (pp[k] && qq[k] && rr[k] && s[k] && !np[k] && !npp[k]) { count[4]++; }//-oxxx\
else if (pp[k] && qq[k] && ss[k] && !r[k] && !rr[k] && h[k]) { count[4]++; }//oxx-x\
else if (pp[k] && rr[k] && ss[k] && !q[k] && !qq[k] && h[k]) { count[4]++; }//ox-xx\
else if (qq[k] && rr[k] && ss[k] && !p[k] && !pp[k] && h[k]) { count[4]++; }//o-xxx\
else if (pp[k] && qq[k] && npp[k] && r[k] && !nq[k] && !nqq[k]) { count[4]++; }//-xoxx\
else if (pp[k] && qq[k] && npp[k] && !r[k] && !rr[k] && nq[k]) { count[4]++; }//\xoxx-
else if (p[k] && q[k] && r[k] && !ss[k] && !s[k] && !npp[k] && !np[k]) { count[7]++; } //-oxxx-
else if (p[k] && q[k] && np[k] && !rr[k] && !r[k] && !nqq[k] && !nq[k]) { count[7]++; } //-xoxx-
else if (pp[k] && qq[k] && rr[k] && !s[k] && !ss[k] && !np[k] && !npp[k]) { count[8]++; } //-oxxx-
else if (pp[k] && qq[k] && npp[k] && !r[k] && !rr[k] && !nq[k] && !nqq[k]) { count[8]++; } //-xoxx-
else if (k < 4 && p[k] && np[k] && !qq[k] && !nqq[k] && !nq[k] && !nqq[k] && !q[k] && !qq[k] && !(rr[k] && nrr[k])) { count[5]++; }//-xox-
else if (p[k] && q[k] && !rr[k] && !r[k] && !npp[k] && !np[k] && !(ss[k] && nqq[k])) { count[5]++; }//-oxx-
else if (!pp[k] && !p[k] && q[k] && r[k] && !ss[k] && !s[k] && !npp[k] && !np[k]) { count[5]++; }//-o-xx-
else if (!pp[k] && q[k] && !rr[k] && !r[k] && np[k] && !nqq[k] && !nq[k]) { count[5]++; }//-xo-x-
else if (p[k] && !qq[k] && !q[k] && r[k] && !ss[k] && !s[k] && !npp[k] && !np[k]) { count[5]++; }//-ox-x-
else if (k < 4 && pp[k] && npp[k] && !q[k] && !qq[k] && !nq[k] && !nqq[k] && !qq[k] && !nqq[k] && !(r[k] && nr[k])) { count[6]++; }//-xox-
else if (pp[k] && qq[k] && !r[k] && !rr[k] && !np[k] && !npp[k] && !(s[k] && nq[k])) { count[6]++; }//-oxx-
else if (!p[k] && !pp[k] && qq[k] && rr[k] && !s[k] && !ss[k] && !np[k] && !npp[k]) { count[6]++; }//-o-xx-
else if (!p[k] && !pp[k] && qq[k] && !r[k] && !rr[k] && npp[k] && !nq[k] && !nqq[k]) { count[6]++; }//-xo-x-
else if (pp[k] && !q[k] && !qq[k] && rr[k] && !s[k] && !ss[k] && !np[k] && !npp[k]) { count[6]++; }//-ox-x-
else if (!np[k] && !npp[k] && p[k] && !q[k] && !qq[k] && !r[k] && !rr[k]) { count[9]++; } //-ox--
else if (!np[k] && !npp[k] && !p[k] && !pp[k] && q[k] && !r[k] && !rr[k]) { count[9]++; }//-o-x-
else if (!np[k] && !npp[k] && pp[k] && !q[k] && !qq[k] && !r[k] && !rr[k]) { count[10]++; } //-ox--
else if (!np[k] && !npp[k] && !p[k] && !pp[k] && qq[k] && !r[k] && !rr[k]) { count[10]++; }//-o-x-
}
switch (depth)
{
case 0: //5
if (count[0] > 0)
{
MessageBox.Show("depth " + depth);
MessageBox.Show("white wins");
Application.Exit();
}
break;
case 1: //A -4
if (count[1] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 2://D -4
if (count[2] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 3://A \3-x\3-
if (count[3] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 4://D \3-x\3-
if (count[4] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 5://A \3-x-2-
if (count[3] > 0 && count[5] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 6://D \3-x-2-
if (count[4] > 0 && count[6] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 7://A -3-
if (count[7] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 8://D -3- && A -2-x-2-
if (count[8] > 0 && count[5] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 9://D -3- && A -2-x-1-
if (count[8] > 0 && count[5] > 0 && count[9] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 10://D -3- && A -1-x-1-
if (count[8] > 0 && count[9] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 11://D -3- && A -2-
if (count[8] > 0 && count[5] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 12://D -3-
if (count[8] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 13://A -2-x-2-
if (count[5] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 14://D -2-x-2-
if (count[6] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 15://A -2-x-1- && D -2-x-1-
if (count[5] > 0 && count[9] > 0 && count[6]>0 && count[10]>0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 16://A -2-x-1- && D -1-x-1-
if (count[5] > 0 && count[9] > 0 && count[10] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 17://A -2-x-1- && D -2-
if (count[5] > 0 && count[9] > 0 && count[6] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 18://A -2-x-1-
if (count[5] > 0 && count[9] >0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 19://D -2-x-1- && A -1-x-1-
if (count[6] > 0 && count[10] > 0 && count[9] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 20://D -2-x-1- && A -2-
if (count[6] > 0 && count[10] > 0 && count[5] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 21://D -2-x-1-
if (count[6] > 0 && count[10] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 22://A -1-x-1- && D -1-x-1-
if (count[9] > 1 && count[10] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 23://A -1-x-1- && D -2-
if (count[9] > 1 && count[6] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 24://A -1-x-1-
if (count[9] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 25://D -1-x-1- && A -2-
if (count[10] > 1 && count[5] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 26://D -1-x-1-
if (count[10] > 1)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 27://A -2-
if (count[5] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 28://D -2-
if (count[6] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 29://A \3-
if (count[3] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 30://D \3-
if (count[4] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 31://A -1-
if (count[9] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
case 32://D -1-
if (count[10] > 0)
{
MessageBox.Show("depth " + depth);
decision(i, j); if (turn == false) { return; }
}
break;
default:
return;
}
}
}
}
}
}
}
private bool checkoccupy(Point[] pp, int num, int px, int py)
{
for (int i = 0; i < num; i++)
{
if (pp[i].X == px && pp[i].Y == py) { return true; }
}
return false;
}
private Point[] addarray(Point[] array1, int num, int px, int py)
{
Point[] oldarray = array1;
for (int i = 0; i < num - 1; i++)
{
array1[i] = oldarray[i];
}
array1[num - 1].X = px;
array1[num - 1].Y = py;
return array1;
}
private bool[] emptypointinfo(Point[] p, int num, int ex, int ey, int level, bool[] s)
{
bool[] info = new bool[8];
info[0] = checkoccupy(p, num, ex - level, ey - level);
info[1] = checkoccupy(p, num, ex, ey - level);
info[2] = checkoccupy(p, num, ex + level, ey - level);
info[3] = checkoccupy(p, num, ex + level, ey);
info[4] = checkoccupy(p, num, ex + level, ey + level);
info[5] = checkoccupy(p, num, ex, ey + level);
info[6] = checkoccupy(p, num, ex - level, ey + level);
info[7] = checkoccupy(p, num, ex - level, ey);
return info;
}
private void decision(int i, int j)
{
x = i;
y = j;
playgo();
}
private void getboundary()
{
for (int i = 0; i < whitenum; i++)
{
if (white[i].X - 1 < boundary1.X) { boundary1.X = white[i].X - 1; }
if (white[i].Y - 1 < boundary1.Y) { boundary1.Y = white[i].Y - 1; }
if (white[i].X + 3 > boundary2.X) { boundary2.X = white[i].X + 3; }
if (white[i].Y + 3 > boundary2.Y) { boundary2.Y = white[i].Y + 3; }
}
for (int i = 0; i < blacknum; i++)
{
if (black[i].X - 1 < boundary1.X) { boundary1.X = black[i].X - 1; }
if (black[i].Y - 1 < boundary1.Y) { boundary1.Y = black[i].Y - 1; }
if (black[i].X + 3 > boundary2.X) { boundary2.X = black[i].X + 3; }
if (black[i].Y + 3 > boundary2.Y) { boundary2.Y = black[i].Y + 3; }
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle bounds = this.Bounds;
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
bitmap.Save("test" + picnum + ".jpg");
picnum++;
this.Refresh();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
mousex = e.X;
mousey = e.Y;
}
}
}
mouse event
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousemove(v=vs.110).aspx