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); }
/// <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 }