跪求五子棋的悔棋问题

lx1215 2008-01-08 02:02:54
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("错误");

}
}
...全文
299 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ProjectDD 2008-01-09
  • 打赏
  • 举报
回复
第一次看到控制台的五子棋,

程序也可以这样写,学习..
Q_282898034 2008-01-09
  • 打赏
  • 举报
回复
用Stack<>记录每一步下的位置,悔棋就pop出来位置,同时撤掉该位置棋子
Q_282898034 2008-01-09
  • 打赏
  • 举报
回复
这个还用问?还不给分的问?
lx1215 2008-01-09
  • 打赏
  • 举报
回复
ding yi ding
rangeon 2008-01-08
  • 打赏
  • 举报
回复
帮顶
lx1215 2008-01-08
  • 打赏
  • 举报
回复
ding yi ding

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧