using System.Text;
using System.Threading.Tasks;
namespace Wuziqi
{
class Program
{
#region 棋盘的展示
/// <summary>
/// 棋盘的展示
/// </summary>
/// <param name="array">表示正方形棋盘的二维数组</param>
/// <param name="x">棋盘的长度</param>
static void game(int[,] array, int x)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x; j++)
{
Console.Write(array[i, j] + " ");
}
Console.WriteLine();
}
}
#endregion
#region 游戏运行
static void start(ref bool whitemove, int[,] array, int x, ref int round)
{
int row = 0, column = 0;
if (whitemove == true)
{
Console.WriteLine("该白子移动了。");
Console.WriteLine("请输入要走的行数");
row = int.Parse(Console.ReadLine());
Console.WriteLine("请输入要走的列数");
column = int.Parse(Console.ReadLine());
}
else
{
Console.WriteLine("该黑子移动了。");
Console.WriteLine("请输入要走的行数");
row = int.Parse(Console.ReadLine());
Console.WriteLine("请输入要走的列数");
column = int.Parse(Console.ReadLine());
}
if (whitemove && array[row, column] == 0)
{
array[row, column] = 1;
whitemove = false;
round++;
game(array, x);
}
else if (!whitemove && array[row, column] == 0)
{
array[row, column] = -1;
whitemove = true;
round++;
game(array, x);
}
else
{
Console.WriteLine("此处已经有子了,请换个位置落子");
}
}
#endregion
#region 获胜判断
/// <summary>
/// 胜利的判断
/// </summary>
/// <param name="array">表示正方形棋盘的二维数组</param>
/// <param name="x">长度</param>
/// <param name="whitewin">白棋获胜条件</param>
/// <param name="blackwin">黑棋获胜条件</param>
static void win(int[,]array,int x,ref bool whitewin,ref bool blackwin)
{
//行获胜
for (int hang = 0; hang < x; hang++)
{
for (int i = 0; i < x - 4; i++)
{
int sum = 0;
for (int j = i; j < i + 5; j++)
{
sum += array[hang, j];
}
if (sum == 5)
{
whitewin = true;
break;
}
else if (sum == -5)
{
blackwin = true;
break;
}
}
if (whitewin == true || blackwin == true)
{
break;
}
}
//列获胜
for (int lie = 0; lie < x; lie++)
{
for (int i = 0; i < x - 4; i++)
{
int sum = 0;
for (int j = i; j < i + 5; j++)
{
sum += array[j, lie];
}
if (sum == 5)
{
whitewin = true;
break;
}
else if (sum == -5)
{
blackwin = true;
break;
}
}
if (whitewin == true || blackwin == true)
{
break;
}
}
// \获胜
for (int hang = 0; hang < x - 5; hang++)
{
for (int lie = 0; lie < x - 5; lie++)
{
int sum = 0;
for (int i = hang, j = lie; i < hang + 5 && j < lie + 5;)
{
sum += array[i, j];
i++; j++;
}
if (sum == 5)
{
whitewin = true;
break;
}
else if (sum == -5)
{
blackwin = true;
break;
}
}
if (whitewin == true || blackwin == true)
{
break;
}
}
// /获胜
for (int hang = 0; hang < x - 5; hang++)
{
for (int lie = x-1; lie > 3; lie--)
{
int sum = 0;
for (int i = hang, j = lie; i < hang + 5;)
{
sum += array[i, j];
i++; j--;
}
if (sum == 5)
{
whitewin = true;
break;
}
else if (sum == -5)
{
blackwin = true;
break;
}
}
if (whitewin == true || blackwin == true)
{
break;
}
}
}
#endregion
static void Main(string[] args)
{
int round = 0;
int x = 15;
int[,] array = new int[x, x];
bool whitemove = true, whitewin = false, blackwin = false;
while (true)
{
start(ref whitemove, array, x, ref round);
win(array, x, ref whitewin, ref blackwin);
if (round == 225)
{
Console.WriteLine("平局");
break;
}
else if (whitewin)
{
Console.WriteLine("白色获胜");
break;
}
else if (blackwin)
{
Console.WriteLine("黑色获胜");
break;
}
}
}
}
}
用C#做一个控制台五子棋
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 分享给大家一个很久之前写的一个纯C#控制台 3D闯关游戏代码 直接复制就可以用 初学者可以看看 学习一下编程思想还...