跪求五子棋的悔棋问题
class 棋盘
{
int number;
int[] 记录 = new int[255]; int[,] 棋局 = new int[15, 15] {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
public void 绘制棋局()
{
Console.Clear();
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (棋局[i, j] == 0)
Console.Write("┼");
else if (棋局[i, j] == 1)
Console.Write("○");
else
Console.Write("●");
}
Console.WriteLine();
}
}
public bool 下棋()
{
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
if (棋局[x, y] != 0)
return false;
else if(x==-1&&y==-1)
{
悔棋(); return true;
}
else
{
棋局[x, y] = 下棋方();//下一颗棋
记录[number]=棋局[x,y];
number++;
return true;
}
}
public int 下棋方()
{
int 黑棋数 = 0;
int 白棋数 = 0;
foreach (int e in 棋局)
{
if (e == 1)
黑棋数++;
else if (e == 2)
白棋数++;
}
if (黑棋数 == 白棋数)
return 1;
else
return 2;
}
public void 悔棋()
{
if (记录[this.number] != 0)
{
记录[this.number] = 0;
number--;
}
} public int 是否赢棋()
{
for (int y = 0; y < 15; y++)
{
for (int x = 0; x < 15; x++)
{
if (x < 0 || x > 14 || y < 0 || y > 14)
continue; ;
if (棋局[x, y] == 0)
continue;
int s = 棋局[x, y];
if (x > 1 && x < 13 && 棋局[x, y - 1] == s && 棋局[x, y - 2] == s && 棋局[x, y + 1] == s && 棋局[x, y + 2] == s)
return s;
else if ((x > 1 && x < 13) && (y > 1 && y < 13) && 棋局[x - 1, y - 1] == s && 棋局[x - 2, y - 2] == s && 棋局[x + 1, y + 1] == s && 棋局[x + 2, y + 2] == s)
return s;
else if (x > 1 && x < 13 && 棋局[x - 1, y] == s && 棋局[x - 2, y] == s && 棋局[x + 1, y] == s && 棋局[x + 2, y] == s)
return s;
else if ((x > 1 && x < 13) && (y > 1 && y < 13) && 棋局[x - 2, y + 2] == s && 棋局[x - 1, y + 1] == s && 棋局[x + 1, y - 1] == s && 棋局[x + 2, y - 2] == s)
return s;
continue;
}
}
return 0;
}
}
class Program
{
static void Main(string[] args)
{
棋盘 b = new 棋盘();
while (b.是否赢棋() == 0)
{
b.绘制棋局();
b.下棋();
}
b.绘制棋局();
if (b.是否赢棋() == 1)
Console.WriteLine("黑棋赢");
else if (b.是否赢棋() == 2)
Console.WriteLine("白棋赢");
else
Console.WriteLine("错误");
}
}