using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
    class Program
    {
        //□●
        static void Main(string[] args)
        {
            //赋值
            #region 初始化棋盘
            int length = 20;
            string[,] FiveChess = new string[length, length];
            ConsoleColor[,] Colors = new ConsoleColor[length, length];
            for (int i = 0; i < length; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < length; j++)
                    {
                        FiveChess[i, j] =string.Format("{0,2}",j.ToString());
                        //FiveChess[i, j] = "□";
                        //Colors[i, j] = ConsoleColor.Black;
                    }
                }
                else
                {
                    for (int j = 0; j < length; j++)
                    {
                        if (j == 0)
                        {
                            FiveChess[i, j] = string.Format("{0,2}", i.ToString());
                        }
                        else
                        {
                         FiveChess[i, j] = "□";
                        Colors[i, j] = ConsoleColor.Black;
                        }
                        //FiveChess[i, j] = j.ToString();                      
                    }
                }

            }
            #endregion
            bool mark = false;//落子双方顺序
            while (true)
            {
                if (Chessing(ref mark, FiveChess, Colors))
                {
                    ShowChess(FiveChess, Colors);
                    Console.WriteLine("赢了");
                    Thread.Sleep(2000);
                    break;
                }
            }
        }
        static bool Heng(ConsoleColor[,] Colors, int x, int y)
        {
            ConsoleColor cc = Colors[x, y];
            int sum = 1;
            for (int i = 1; i < 5; i++)
            {
                if (y - i >= 0 && Colors[x, y - i] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }
            for (int i = 1; i < 5; i++)
            {
                if (y + i < Colors.GetLength(1) && Colors[x, y + i] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }

            if (sum >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static bool Shu(ConsoleColor[,] Colors, int x, int y)
        {
            ConsoleColor cc = Colors[x, y];
            int sum = 1;
            for (int i = 1; i < 5; i++)
            {
                if (x - i >= 0 && Colors[x-i, y] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }
            for (int i = 1; i < 5; i++)
            {
                if (x + i < Colors.GetLength(1) && Colors[x+i, y ] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }
            if (sum >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static bool Na(ConsoleColor[,] Colors, int x, int y)
        {
            ConsoleColor cc = Colors[x, y];
            int sum = 1;
            for (int i = 1; i < 5; i++)
            {
                if (y - i >= 0 && x - i >= 0 && Colors[x - i, y - i] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }
            for (int i = 1; i < 5; i++)
            {
                if (y + i < Colors.GetLength(1) && x + i < Colors.GetLength(0) && Colors[x + i, y + i] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }

            if (sum >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        static bool Xie(ConsoleColor[,] Colors, int x, int y)
        {
            ConsoleColor cc = Colors[x, y];
            int sum = 1;
            for (int i = 1; i < 5; i++)
            {
                if (y - i >= 0 && x + i < Colors.GetLength(0) && Colors[x + i, y - i] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }
            for (int i = 1; i < 5; i++)
            {
                if (y + i < Colors.GetLength(1) && x - i >=0 && Colors[x - i, y + i] == cc)
                {
                    sum++;
                }
                else
                {
                    break;
                }
            }

            if (sum >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 落子
        /// </summary>
        /// <param name="mark">双方落子顺序</param>
        /// <param name="FiveChess">棋盘内容</param>
        /// <param name="Colors">棋子色</param>
        static bool Chessing(ref bool mark, string[,] FiveChess, ConsoleColor[,] Colors)
        {
            ShowChess(FiveChess, Colors);
            if (mark)
            {
                Console.Write("甲:");
            }
            else
            {
                Console.Write("乙:");
            }
            Console.WriteLine("请输入落子坐标(8,6):");
            string location = Console.ReadLine();//8,6
            string[] point = location.Split(',');
            int x = int.Parse(point[0]) - 1;
            int y = int.Parse(point[1]) - 1;
            if (FiveChess[x, y] == "□")
            {
                if (mark)
                {
                    Colors[x, y] = ConsoleColor.Red;
                }
                else
                {
                    Colors[x, y] = ConsoleColor.Blue;
                }
                mark = !mark;
                FiveChess[x, y] = "●";

            }
            else
            {
                Console.WriteLine("哥,这里不能落子");
                Thread.Sleep(1000);
            }

            return Heng(Colors, x, y) || Na(Colors, x, y)||Shu(Colors,x,y)||Xie(Colors,x,y);

        }

        /// <summary>
        /// 显示棋盘
        /// </summary>
        /// <param name="FiveChess">棋盘数组</param>
        static void ShowChess(string[,] FiveChess, ConsoleColor[,] Colors)
        {
            Console.Clear();
            for (int i = 0; i < FiveChess.GetLength(0); i++)
            {
                for (int j = 0; j < FiveChess.GetLength(1); j++)
                {
                    Console.ForegroundColor = Colors[i, j];
                    Console.Write(FiveChess[i, j]);
                    Console.ResetColor();
                }
                Console.WriteLine();
            }
        }
    }
}