飞行棋小项目

  1. 飞行器代码如下:

namespace 棋士飞行棋

{

internal class Program

{

//在下面的数组存储我们游戏地图的各个关卡

//数组下表为0的元素对应地图上的第一格,下标为1的元素代表地图上的第2.....下标为n的元素对应地图上的第n+1格!

//在数组中用 1:表示幸运轮盘

// 2:地雷

// 3:暂停

// 4:时空隧道¤

// 0:表示普通

private static int[] Map = new int[100];

private static int[] playerPos = { 0, 0 }; //playerPos[0]存玩家A的下标!playerPos[1]存玩家B的下标!

private static string[] name = new string[2]; //name[0]存玩家a的姓名,name[1]存玩家b的姓名!

private static bool[] isStop = { false, false };//isStop[0]表示A是否走到了暂停,isStop[1]表示B是否走到了暂停。false表示允许通过!反之不通过!

private static void Main(string[] args)

{

//string input = "";//用于存储用户的输入!

ShowUI(); //显示游戏名称!

Console.WriteLine("请输入玩家A的姓名?");

name[0] = Console.ReadLine();

//用户输入的名称是否为空,如果为空,提示用户重新输入!

while (name[0] == "")

{

Console.WriteLine("玩家A的姓名不能为空,请重新输入!");

name[0] = Console.ReadLine();

}

Console.WriteLine("请输入玩家B的姓名?");

name[1] = Console.ReadLine();

while (name[1] == "" || name[1] == name[0])

{

if (name[1] == "")

{

Console.WriteLine("玩家B的姓名不能为空,请重新输入!");

}

if (name[1] == name[0]) //如果AB重名!

{

Console.WriteLine("你输入的姓名与玩家A{0}】的姓名重名!请重新输入!", name[0]);

}

name[1] = Console.ReadLine();

}

Console.Clear();

ShowUI();

Console.WriteLine("对战开始.....");

Console.WriteLine("{0}A来表示", name[0]);

Console.WriteLine("{0}B来表示", name[1]);

Console.WriteLine("如果AB在同一位置,用<>来表示");

 

InitialMap();//初始化地图

DrawMap();//制度图

Console.WriteLine("开始游戏......");

//这个循环中让玩家A和玩家B轮流掷骰子 当玩家A或玩家B>=99时,则结束循环!

while (playerPos[0] < 99 && playerPos[1] < 99)

{

if (isStop[0] == false)

{

//玩家A掷筛子

Action(0);

}

else

{

//说明isStop[0]=true

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}胜利了!!!!!!",name[0]);

}

if (playerPos[1] == 99)

{

Console.WriteLine("玩家{0}胜利了!!!!!!",name[1]);

}

Console.WriteLine("游戏结束!");

Console.ReadKey();

}

/// <summary>

/// 玩家掷骰子的方法!

/// </summary>

/// <param name="playerNumber"></param>

private static void Action(int playerNumber)

{

//playerNumber当中存的就是当前玩家的姓名、坐标、是否暂停 这三个数组的下标

//1-playerNumber就是对方的姓名、坐标、是否暂停 这三个数组的下标

// 当前为0 对方为1

// 当前为1 对方为0

            //定义一个能够产生随机数的随机变量

Random r = new Random();//产生随机数

string msg = "";//用于用户踩到某个关卡,所输出的话!

int stemp = 0;//用于存放产生的随机数!

Console.WriteLine("玩家{0}按任意键开始掷筛子.....", name[playerNumber]);

            //当给Console.ReadKey()传值为True时,表明用户按任意键时,屏幕不会显示按键字符。

ConsoleKeyInfo rec = Console.ReadKey(true);

//Control+Shift+F1走捷径!这里注意:组合剑只能是在ControlShiftAlt这三个里面选择1||2||3个,再外加一个负键就可以!不能加多个负键!

if (rec.Key == ConsoleKey.F1 && rec.Modifiers == (ConsoleModifiers.Control | ConsoleModifiers.Alt))

{

stemp = 20;

}

else

{

stemp = r.Next(1, 7);//产生随机整数!

}

Console.WriteLine("玩家{0}掷了:{1}", name[playerNumber], stemp);

Console.WriteLine("请按任意键开始行动.......");

Console.ReadKey(true);

playerPos[playerNumber] = playerPos[playerNumber] + stemp;//注意,一旦坐标放生改变就要判断坐标是否>99或者<0

CheckPos();

if (playerPos[0] == playerPos[1])//玩家A踩到玩家B

{

playerPos[1 - playerNumber] = 0;

msg = string.Format("{0}踩到{1}{1}退回原点!", name[playerNumber], name[1 - playerNumber]);

}

else

{

//判断玩家A现在的位置是否在其他关卡!

switch (Map[playerPos[playerNumber]])

{

case 0:

//普通没有效果!

msg = "";

break;

case 1:

//走到了幸运轮盘

Console.Clear();

DrawMap();

Console.WriteLine("{0}走到了幸运轮盘,请选择运气!", name[playerNumber]);

Console.WriteLine("1--交换位置 2--轰炸对方");

int userSelect = ReadInt(1, 2);

if (userSelect == 1)

{

//要与对方交换位置

int tem = playerPos[0];

playerPos[0] = playerPos[1];

playerPos[1] = tem;

msg = string.Format("{0}选择了与对方交换位置!", name[playerNumber]);

}

else

{

//轰炸对方

playerPos[1 - playerNumber] = playerPos[1 - playerNumber] - 6;

CheckPos();

msg = string.Format("{0}选择了轰炸对方,{1}退6格!", name[playerNumber], name[1 - playerNumber]);

}

break;

case 2:

//踩到了地雷

playerPos[playerNumber] = playerPos[playerNumber] - 6;

CheckPos();

 

msg = string.Format("{0}踩到了地雷退6格!", name[playerNumber]);

break;

case 3:

//暂停一次

isStop[0] = true;

msg = string.Format("{0}走到红灯,下次暂停一次!", name[playerNumber]);

break;

case 4:

//时空隧道

playerPos[playerNumber] = playerPos[playerNumber] + 10;

CheckPos();

msg = string.Format("{0}进入时空隧道,爽死了!", name[playerNumber]);

break;

}

}

Console.Clear();

DrawMap();

if (msg != "")

{

Console.WriteLine(msg);

}

Console.WriteLine("{0}掷出了{1},行动完成!", name[playerNumber], stemp);

Console.WriteLine("**********玩家A和玩家B的位置***********");

Console.WriteLine("玩家{0}的位置在{1}", name[0], playerPos[0] + 1);

Console.WriteLine("玩家{0}的位置在{1}", name[1], playerPos[1] + 1);

}

 

/// <summary>

/// 用于绘制飞行棋的名称!

/// </summary>

private static void ShowUI()

{

Console.WriteLine("*****************************");

Console.WriteLine("* *");

Console.WriteLine("* 骑士飞行棋 *");

Console.WriteLine("* *");

Console.WriteLine("*****************************");

}

 

private static void CheckPos()

{

for (int i = 0; i < 2; i++)

{

if (playerPos[i] > 99)

{

playerPos[i] = 99;

Console.WriteLine("");

}

if (playerPos[i] < 0)

{

playerPos[i] = 0;

}

}

 

}

 

/// <summary>

/// 对地图中的关卡进行初始化!

/// </summary>

private static void InitialMap()

{

//用于存储在地图中为地雷的下标

int[] luckyTurn = { 6, 23, 40, 55, 59, 83 }; //幸运轮盘 1

int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; //地雷 2

int[] pause = { 9, 27, 60, 93 }; //暂停的坐标 3

int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; //时空隧道 4

 

for (int i = 0; i < luckyTurn.Length; i++)

{

int pos = luckyTurn[i];

Map[pos] = 1;

}

for (int i = 0; i < landMine.Length; i++)

{

Map[landMine[i]] = 2;

}

for (int i = 0; i < pause.Length; i++)

{

Map[pause[i]] = 3;

}

for (int i = 0; i < timeTunnel.Length; i++)

{

Map[timeTunnel[i]] = 4;

}

}

/// <summary>

/// 获得pos坐标上应该绘制的图案!

/// </summary>

/// <param name="pos">要绘制的坐标</param>

/// <returns></returns>

private static string GetMapString(int pos)

{

string result = "";

//判断AB是否在当前要画的第i格上

if (playerPos[0] == pos && playerPos[1] == pos)

{

            //改变字符颜色!

Console.ForegroundColor = ConsoleColor.Yellow;

result = "<>";

}

else if (playerPos[0] == pos)

{

Console.ForegroundColor = ConsoleColor.Yellow;

result = "";

}

else if (playerPos[1] == pos)

{

Console.ForegroundColor = ConsoleColor.Yellow;

result = "";

}

else

{

switch (Map[pos])

{

case 0:

Console.ForegroundColor = ConsoleColor.White;

result = "□";

break;

case 1:

Console.ForegroundColor = ConsoleColor.Red;

result = "◎";

break;

case 2:

Console.ForegroundColor = ConsoleColor.Green;

result = "☆";

break;

case 3:

Console.ForegroundColor = ConsoleColor.Blue;

result = "▲";

break;

case 4:

Console.ForegroundColor = ConsoleColor.DarkBlue;

result = "¤";

break;

}

}

return result;

}

 

private static void DrawMap()

{

//绘制图例:

Console.WriteLine("图例:幸运轮盘:地雷:暂停:时空隧道:¤");

//画第一行

for (int i = 0; i <= 29; i++)

{

Console.Write(GetMapString(i));

}

Console.WriteLine();

//绘制第一列

for (int i = 30; i <= 34; i++)

{

//绘制29个空格!

for (int j = 0; j < 29; j++)

{

Console.Write(" ");

}

 

Console.WriteLine(GetMapString(i));

}

//绘制第二行

for (int i = 64; i >= 35; i--)

{

Console.Write(GetMapString(i));

}

Console.WriteLine();

//绘制第二列

for (int i = 65; i <= 69; i++)

{

Console.WriteLine(GetMapString(i));

}

//绘制第三行

for (int i = 70; i <= 99; i++)

{

Console.Write(GetMapString(i));

}

Console.WriteLine();

            //恢复字体颜色!如果最后为非白色,那么如果去掉下面这句话,后面的语句就会和最后面的字符颜色相同!

Console.ResetColor();

}

//只允许使用两数之间的数字!

private static int ReadInt()

{

int i = ReadInt(int.MinValue, int.MaxValue);

return i;

}

 

private 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("只能输入数字,请重新输入!");

}

}

 

}

}

你可能感兴趣的:(项目)