C#基础 五子棋布局

第十三讲:五子棋布局
代码如下:namespace Chapter13Demo1
{
    class Program
    {
        static void Main(string[] args)
        {
           
            string EmpStr = "□";
            string BlaStr = "●";
            string Witstr = "○";
            string [,] ArrStr=new string[11,11];//第一行第一列放数字
            int WLen=ArrStr.GetLength(1);
            int HLen=ArrStr.GetLength(0);
            //给数组赋值(方框)
            for (int r = 0; r < HLen; r++)
            {
                if (r == 0)//如果是第一行,就添加成坐标数字
                {
                    for (int c = 0; c <WLen; c++)
                    {
                        ArrStr[r, c] =string.Format("{0,-2}", c.ToString());//负号靠右对齐
                    }
                }
                else//添加其他行的方格
                {

                    for (int c = 0; c < WLen; c++)
                    {
                        if (c == 0)//如果是第一列,就添加成坐标数字
                        {

                            ArrStr[r, c] = string.Format("{0,-2}", r.ToString());//负号靠右对齐
                        }
                        else//添加其他列的方格
                        {
                            ArrStr[r, c] = EmpStr;

                        }
                    }
                }
            }
            bool mark = true;
            while (true)//把数组中的元素替换成棋子
            {
                Console.Clear();//清除刚开始的棋盘
                //遍历数组并打印棋盘布局
                for (int r = 0; r < HLen; r++)
                {
                    for (int c = 0; c < WLen; c++)
                    {
                        Console.Write(ArrStr[r, c]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("请输入落子的坐标,例如:\"4,5\"");
                if (mark)
                {
                    Console.Write("甲:");
                }
                else
                {
                    Console.Write("乙");
                }
                    string roadstr = Console.ReadLine();
                    string[] ArrCo = roadstr.Split(',', ',');
                    if (ArrStr[int.Parse(ArrCo[0]), int.Parse(ArrCo[1])] == EmpStr)
                    {
                        if (mark)
                        {
                            ArrStr[int.Parse(ArrCo[0]), int.Parse(ArrCo[1])] = BlaStr;//把数组中的元素替换成黑子
                        }
                        else
                        {
                            ArrStr[int.Parse(ArrCo[0]), int.Parse(ArrCo[1])] = Witstr;
                        }
                        mark = !mark;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("对不起,这里不能落子!");
                        System.Threading.Thread.Sleep(1000);
                        Console.ForegroundColor = ConsoleColor.White;
                    }
            }
        }
    }
}

你可能感兴趣的:(C#,职场,基础知识,休闲,C#基础)