根据是否结束游戏的逻辑选择对掷骰子的过程用bool申明变量
掷骰子
static bool RollDice (w,h,ref Player p1,ref Player p2,Map map)
{InfoClear(h);Console.ForegroundColor=p1.type==E_PlayerType.Player?ConsoleColor.Cyan:ConsoleColor.Meganta;if(p1.isPause){p1.isPause = false;Console.SetCursorPosition(2,h-5)Console.WriteLine("收到了停滞的影响,{0}停止行动一回合",p1.type==E_PlayerType.Player?"你":"电脑");Console.SetCursorPosition(2,h-4)Console.WriteLine("按任意键,让{0}掷骰子",p1.type==E_PlayerType.Player?"电脑":"你");return false;}Random r = new Random();int dice = r.Next(1, 7);p1.mapIndex+=dice;Console.SetCursorPosition(2,h-5)Console.WriteLine("{0}掷除了{1}点",p1.type==E_PlayerType.Player?"你":"电脑",dice);//判断是否到达终点,索引从0开始,所以最后一格是Lengt-1if(p1.mapIndex>=map.grids.Length-1){p1.mapIndex = map.grids.Length-1;Console.SetCursorPosition(2,h-4);if(p1.type==E_PlayerType.Player){Console.Write("恭喜你率先抵达终点");}else{Console.Write("很遗憾,你输掉了比赛");}Console.SetCursorPosition(2,h-3);Console.Write("按任意键结束游戏");return true;}else{//没到达终点的时候就判断玩家到了什么样的格子Grid grid=map.grids[p1.mapIndex];switch(grid.type){case E_Grid_Type.Normal:Console.SetCursorPosition(2,h-4)Console.WriteLine("{0}到达了一个安全位置",p1.type==E_PlayerType.Player?"你":"电脑");Console.SetCursorPosition(2,h-3)Console.WriteLine("按任意键,让{0}掷骰子",p1.type==E_PlayerType.Player?"电脑":"你");break;case E_Grid_Type.Bomb:p1.mapIndex -= 5;Console.SetCursorPosition(2,h-4)Console.WriteLine("{0}踩到了炸弹,倒退了5格",p1.type==E_PlayerType.Player?"你":"电脑");Console.SetCursorPosition(2,h-3)Console.WriteLine("按任意键,让{0}掷骰子",p1.type==E_PlayerType.Player?"电脑":"你");if(p1.mapIndex<=0){p1.mapIndex = 0;}break;case E_Grid_Type.Pause:Console.SetCursorPosition(2,h-4)Console.WriteLine("{0}被时空影响",p1.type==E_PlayerType.Player?"你":"电脑");Console.SetCursorPosition(2,h-3)Console.WriteLine("按任意键,让{0}掷骰子",p1.type==E_PlayerType.Player?"电脑":"你");p1.isPause=true;break;case E_Grid_Type.Tunnel:int temp=p2.mapIndex;p2.mapIndex=p1.mapIndex;p1.mapIndex=temp;Console.SetCursorPosition(2,h-4)Console.WriteLine("{0}进入了隧道,双方交换位置",p1.type==E_PlayerType.Player?"你":"电脑",);Console.SetCursorPosition(2,h-3)Console.WriteLine("按任意键,让{0}掷骰子",p1.type==E_PlayerType.Player?"电脑":"你");break;}}return false;
}
游戏场景循环
bool isGameOver = false;
while(true)
{Console.ReadKey(true);isGameOver=RollDice(w,h,ref player,ref robot,map);map.Draw();DrawPlayer(player,robot,map);if(isGameOver){Console.ReadKey(true);nowSceneType=E_SceneType.End;break;}Console.ReadKey(true);isGameOver=RollDice(w,h,ref robot,ref player,map);map.Draw();DrawPlayer(player,robot,map);if(isGameOver){Console.ReadKey(true);nowSceneType=E_SceneType.End;break;}
}
提示信息擦除
{Console.SetCursorPosition(2,h-5)Console.WriteLine(" ");Console.SetCursorPosition(2,h-5)Console.WriteLine(" ");Console.SetCursorPosition(2,h-5)Console.WriteLine(" ");Console.SetCursorPosition(2,h-5)Console.WriteLine(" ");
}