コード例 #1
0
        public void Play(ref Chessboard board)
        {
            count++;
            //设置光标不可见
            Console.CursorVisible = false;

            do
            {
                board.ShowBord(Px, Py, (this.black ? ConsoleColor.Red : ConsoleColor.Green));
                //信息栏
                Console.SetCursorPosition(0, 0);
                Info     info = new Info();
                string[] user = { "我:" + this.name, "\t第" + count + "步", "\t位置:" + (char)(Px + 65) + (15 - Py) };
                info.ShowInfo(black ? user : new string[0], black ? new string[0] : user);

                ConsoleKeyInfo In = Console.ReadKey(true);             //用户按下一个键

                #region 移动光标
                if (In.Key == ConsoleKey.LeftArrow) //按向左键
                {
                    Px = (Px + 14) % 15;
                    this.PlayMusic("move.wav", false);
                    continue;
                }
                if (In.Key == ConsoleKey.RightArrow) //按向右键
                {
                    Px = (Px + 1) % 15;
                    this.PlayMusic("move.wav", false);
                    continue;
                }
                if (In.Key == ConsoleKey.UpArrow) //按向上键
                {
                    Py = (Py + 14) % 15;
                    this.PlayMusic("move.wav", false);
                    continue;
                }
                if (In.Key == ConsoleKey.DownArrow) //按向下键
                {
                    Py = (Py + 1) % 15;
                    this.PlayMusic("move.wav", false);
                    continue;
                }
                #endregion

                #region 出棋
                if (In.Key == ConsoleKey.Enter && board.bord[Py, Px] == 0) //按Enter键
                {
                    if (board.bord[Py, Px] == 0)
                    {
                        board.bord[Py, Px] = (sbyte)(this.black ? -1 : 1);
                    }
                    this.PlayMusic("LuoQi.wav", false);
                    break;  //完成出棋,退出循环
                }
                #endregion
            } while (true);
        }
コード例 #2
0
        /// <summary>
        /// 单机
        /// </summary>
        public void One()
        {
            //设置背景色
            Console.BackgroundColor = ConsoleColor.DarkYellow;
            Console.Clear();
            //取消玩家2的优先权
            u2.main = !u1.main;
            //设置音效开关
            u1.voice = this.voice;
            u2.voice = this.voice;
            //玩家2设置信息
            u2.SetInfo();
            //玩家1设置信息
            u1.SetInfo();
            //设置玩家2的棋子颜色与玩家1相反
            u2.black = !u1.black;

            //信息栏
            Info info = new Info();

            string[] black = { "黑棋:", "\t先出棋", "\t开始" };
            string[] white = { "白棋", "\t等待黑棋", "\t等待。。。" };
            info.ShowInfo(black, white);

            //创建棋盘
            Chessboard board = new Chessboard();

            board.Px = 0; board.Py = 5;
            board.ShowBord(u1.Px, u1.Py, ConsoleColor.Green);
            board.ShowFrom();

            #region 对弈
            sbyte success = 0;  //胜利方0:平 1:白 -1:黑
            for (int i = 0; i < 113; i++)
            {
                if (u1.black)
                {
                    u1.Play(ref board);         //玩家1出棋

                    if (jud.Umpire(board.bord)) //判断胜利方
                    {
                        success = -1;
                        break;
                    }
                    u2.Play(ref board);         //玩家2出棋
                    if (jud.Umpire(board.bord)) //判断胜利方
                    {
                        success = 1;
                        break;
                    }
                }
                else
                {
                    u2.Play(ref board);         //玩家2出棋
                    if (jud.Umpire(board.bord)) //判断胜利方
                    {
                        success = -1;
                        break;
                    }
                    u1.Play(ref board);         //玩家1出棋
                    if (jud.Umpire(board.bord)) //判断胜利方
                    {
                        success = 1;
                        break;
                    }
                }
            }
            #endregion

            #region 显示对弈结果
            for (int i = 0; i < 10; i++)
            {
                //sleep一下
                System.Threading.Thread.Sleep(100 + i * 60);
                Console.MoveBufferArea(0, i, Console.WindowWidth, 5, 0, i + 1, '*', ConsoleColor.Yellow, ConsoleColor.Red);
            }
            Console.SetCursorPosition(15, 6);
            this.PlayMusic("game over.wav", false);
            //sleep一下
            System.Threading.Thread.Sleep(600);
            if (u1.black)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
                if (success == -1)
                {
                    Console.Write("●胜利!");
                }
                if (success == 1)
                {
                    Console.Write("●失败");
                }
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.White;
                if (success == 1)
                {
                    Console.Write("●胜利!");
                }
                if (success == -1)
                {
                    Console.Write("●失败");
                }
            }
            Console.ForegroundColor = ConsoleColor.Green;
            if (success == 0)
            {
                Console.Write("完美●平局");
            }
            #endregion
        }
コード例 #3
0
        public void SetInfo()
        {
            #region 输入姓名
            Console.Write("请输入玩家姓名:\n\t\t");
            this.name = Console.ReadLine();
            Console.WriteLine();
            this.PlayMusic("wong.wav", false);
            #endregion

            //没有优先权就不能设置棋子颜色
            if (main == false)
            {
                return;
            }

            #region  择黑白棋
            Console.WriteLine("请按←或→选择:");
            int L0 = Console.CursorTop;//记录当前行

            Info     info  = new Info();
            string[] info1 = { "  黑棋●", "    先出棋", "" };
            string[] info2 = { "  白棋●", "    后出棋", "" };
            info.ShowInfo(info1, info2);
            Console.SetCursorPosition(7, L0 + 5);
            Console.WriteLine("默认为白棋");

            //设置光标不可见
            Console.CursorVisible = false;

            do
            {
                ConsoleKeyInfo In = Console.ReadKey(true);             //用户按下一个键
                this.PlayMusic("wong.wav", false);

                if (In.Key == ConsoleKey.LeftArrow) //按向左键
                {
                    black = true;                   //是黑棋
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.SetCursorPosition(0, L0);
                    Console.Write("┏");
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 2, L0);
                    Console.Write("┓");
                    Console.SetCursorPosition(0, L0 + 3);
                    Console.Write("┗");
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 2, L0 + 3);
                    Console.Write("┛");
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.SetCursorPosition(Console.WindowWidth / 2 + 1, L0);
                    Console.WriteLine("  ");
                    Console.SetCursorPosition(Console.WindowWidth - 2, L0);
                    Console.WriteLine("  ");
                    Console.SetCursorPosition(Console.WindowWidth / 2 + 1, L0 + 3);
                    Console.WriteLine("  ");
                    Console.SetCursorPosition(Console.WindowWidth - 2, L0 + 3);
                    Console.WriteLine("  ");
                    Console.ForegroundColor = ConsoleColor.Black;
                }
                if (In.Key == ConsoleKey.RightArrow) //按向右键
                {
                    black = false;                   //是白棋
                    Console.BackgroundColor = ConsoleColor.White;
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.SetCursorPosition(0, L0);
                    Console.Write("  ");
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 2, L0);
                    Console.Write("  ");
                    Console.SetCursorPosition(0, L0 + 3);
                    Console.Write("  ");
                    Console.SetCursorPosition(Console.WindowWidth / 2 - 2, L0 + 3);
                    Console.Write("  ");
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.SetCursorPosition(Console.WindowWidth / 2 + 1, L0);
                    Console.WriteLine("┏");
                    Console.SetCursorPosition(Console.WindowWidth - 2, L0);
                    Console.WriteLine("┓");
                    Console.SetCursorPosition(Console.WindowWidth / 2 + 1, L0 + 3);
                    Console.WriteLine("┗");
                    Console.SetCursorPosition(Console.WindowWidth - 2, L0 + 3);
                    Console.WriteLine("┛");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                Console.BackgroundColor = ConsoleColor.DarkYellow;
                Console.SetCursorPosition(Math.Min(10 + this.name.Length * 2 + 6, Console.WindowWidth - 1), L0 - 3);
                Console.WriteLine("●");
                if (In.Key == ConsoleKey.Enter)     //按Enter键
                {
                    break;
                }
                Console.SetCursorPosition(7, L0 + 5);
                Console.WriteLine("按Enter键确定");
            } while (true);
            Console.Clear();
            #endregion
        }