美文网首页
用C#做一个控制台五子棋

用C#做一个控制台五子棋

作者: DarkknightM | 来源:发表于2018-07-12 08:42 被阅读0次
    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#做一个控制台五子棋

          本文链接:https://www.haomeiwen.com/subject/obklpftx.html