黑马程序员-骑士飞行骑
class Program
{
static int[] Map = new int[100];//数组下标为0的元素对应地图上的第一格。0表示普通
static int[] playerPos = {0,0};//这里表示有两个元素playerpos[0]和playerpos[1],他们的初始值为0
static bool[] isStop ={false,false};//bool类型的数组!用于判断暂停!
static string[] names = new string[2];
static void Main(string[] args)
{
ShowUI();
Console.WriteLine("请输入玩家A的姓名");
names[0] = Console.ReadLine();
while(names[0]=="")
{ Console.WriteLine("你输入的不能为空,请重新输入");
names[0]=Console.ReadLine();
}
Console.WriteLine("请输入玩家b的姓名");
names[1] = Console.ReadLine();
while (names[0] == names[1] || names[1] == "")
{ if (names[0] == names[1])
{ Console.WriteLine("你输入的名字已被占用,请重新输入"); }
else { Console.WriteLine("输入不能为空,请重新输入"); }
names[1] = Console.ReadLine(); }
Console.Clear();
ShowUI();
Console.WriteLine("{0}与{1}开战!{0}用A表示。{1}用B表示,如果AB在同一位置用<>来表示",names[0],names[1]);
InitialMap();//初始化游戏
DrawMap();//
Console.WriteLine("游戏开始"); //这个循环就是扔筛子。当有一方的坐标大于99时。则结束循环!
//循环条件就是
while (playerPos[0] < 99 && playerPos[1] < 99)
{
if (isStop[0] == false)
{ action(0); }
else { isStop[0] = false; }
if (playerPos[0] >= 99) { break; }
if (isStop[1] == false) { action(1); }
else { isStop[1] = false; } }
Console.Clear();
ShowUI();
if (playerPos[0] >= 99)
{ Console.WriteLine("{0}打败了{1}", names[0], names[1]); }
else { Console.WriteLine("{1}打败了{0}",names[1],names[0]); }
Console.ReadKey();
}
static int ReadInt(int min, int max)
{
while(true)
{
try
{
int number = Convert.ToInt32(Console.ReadLine());
if (number < min || number > max)
{ Console.WriteLine("只能输入{0}到{1}之间的数,请重新输入!", min, max);
continue; }
return number; }
catch { Console.WriteLine("只能输入数字,请重新输入"); } } }
static void checkmap()
{ for (int i = 0; i <= 1; i++)
{ if (playerPos[i] > 99)
{ playerPos[i] = 99; }
if (playerPos[i] < 0)
{ playerPos[i] = 0; } } }
static void ShowUI()
{
Console.WriteLine("*****************************");
Console.WriteLine("* *");
Console.WriteLine("* 阿建飞行棋 *");
Console.WriteLine("* *");
Console.WriteLine("*****************************");
}
static void InitialMap()
{ int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
int[] pause = { 9, 27, 60, 63 };//暂停
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道
for (int i = 0; i < luckyTurn.Length; i++)
{ int pos = luckyTurn[i];
Map[pos] = 1;//幸运轮盘用1表示 }
for (int i = 0; i < landMine.Length;i++ )
{ Map[landMine[i]] = 2;//地雷用2表示 }
for (int i = 0; i < pause.Length; i++)
{ Map[pause[i]] = 3;//暂停用3表示 }
for (int i = 0; i < timeTunnel.Length; i++)
{ Map[timeTunnel[i]] = 4;//时空隧道用4表示 } }
static void DrawMap()//画地图!
{
for (int i = 0; i <= 29; i++)
{ Console.Write(GetMap(i));//让int pos 接收,然后getmap(i)接收返回值; }
Console.WriteLine();
for (int i = 30; i <= 34; i++)
{ for (int j = 0; j < 29; j++)
{ Console.Write(" ");
}
Console.WriteLine(GetMap(i));
} for (int i =64; i>= 35; i--)
{ Console.Write(GetMap(i)); }
Console.WriteLine();
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(GetMap(i));
}
for (int i = 70; i <= 99; i++)
{ Console.Write(GetMap(i));
} Console.WriteLine(); }
static string GetMap(int pos)
{ string result = "";
if (playerPos[0] == pos && pos == playerPos[1])//A和B在一起时!
{ Console.ForegroundColor= ConsoleColor.Red;
result="<>";
}
else if (playerPos[0] == pos)//A在当前画的格子上时
{Console.ForegroundColor= ConsoleColor.Yellow; result="A"; }
else if (playerPos[1] == pos)//B在当前画的格子上时
{ Console.ForegroundColor = ConsoleColor.Green; result = "B"; }
else
{ switch (Map[pos])
{ case 0: Console.ForegroundColor = ConsoleColor.White; result = "□";// break;
case 1: Console.ForegroundColor = ConsoleColor.Blue; result = "◎"; break;
case 2: Console.ForegroundColor = ConsoleColor.Gray; result = "★"; break;
case 3: Console.ForegroundColor = ConsoleColor.Magenta; result = "▲"; break;
case 4: Console.ForegroundColor = ConsoleColor.Cyan; result = "※"; break; }
} return result;
} static void action(int playNumber)
{ Random r = new Random();//产生了一个随机数!
string msg = "";//用于储存用户踩到关卡时输入的话;
int step = 0;
Console.WriteLine("{0}按任意键扔筛子", names[playNumber]);
ConsoleKeyInfo rec = Console.ReadKey(true);//这里的readykey是consolekeyinfo类型!把鼠标放在readkey上就能查出 !
if (rec.Key == ConsoleKey.Tab)
{ step = 20; }
else
{ step = r.Next(1, 7); }
Console.WriteLine("{0}扔出了{1}", names[playNumber], step);
Console.WriteLine("按任意键开始行动");
Console.ReadKey(true);
playerPos[playNumber] += step;
checkmap();
if (playerPos[0] == playerPos[1])
{ playerPos[1 - playNumber] = 0;
msg = string.Format("{0}踩到了{1},{1}退回原点", names[playNumber], names[1-playNumber]); }
else
{ switch (Map[playerPos[playNumber]])
{ case 0:
msg = "";
break;
case 1:
Console.Clear();
DrawMap();
Console.WriteLine("{0}走到了幸运轮盘,请选择运气", names[playNumber]);
Console.WriteLine("1----交换位置。2-----轰炸对方");
int yourSelect = ReadInt(1, 2);
if (yourSelect == 1)
{ int temp = playerPos[playNumber];
playerPos[playNumber] = playerPos[1 - playNumber];
playerPos[1 - playNumber] = temp;
msg = string.Format("{0}选择了与对方交换位置!", names[playNumber]);
}
else
{ playerPos[1 - playNumber] = playerPos[1 - playNumber] - 6;
checkmap();
msg = string.Format("{0}选择了轰炸{1},{1}退六格!", names[playNumber], names[1-playNumber]);
}
break;
case 2:
playerPos[playNumber] = playerPos[playNumber] - 6;
checkmap();
msg = string.Format("{0}踩到了地雷,{0}退六格!", names[playNumber]);
break;
case 3:
isStop[playNumber] = true;
msg = string.Format("{0}走到了红灯区,奖励你下次暂停一次", names[playNumber]);
break;
case 4:
playerPos[playNumber] = playerPos[playNumber] + 10;
checkmap();
msg = string.Format("{0}进入了时空隧道,{0}前进10格!", names[playNumber]);
break; }
}
Console.Clear();
DrawMap();
if (msg != "")
{ Console.WriteLine(msg); }
Console.WriteLine("{0}的位置为{1}", names[playNumber], playerPos[playNumber] + 1);
Console.WriteLine("{0}的位置为{1}", names[1 - playNumber], playerPos[1 - playNumber] + 1); Console.ReadKey(); } }
浙公网安备 33010602011771号