飞行棋项目
/*游戏规则
* 玩家A踩到玩家B,玩家B退六格
* 踩到地雷,推六格
* 幸运轮盘 ,交换位置或让对方退六格
* 暂停,暂停一轮
* 时空隧道,进10格
* 方块,什么都不干
* 地雷☆、幸运轮盘○、暂停▲、时空隧道◆、普通□*/
namespace _01_游戏头
{
class Program
{
//声明地图数组
static int[] map = new int[100];//静态字段声明到类中
//声明一个静态数组,存放两个玩家的位置坐标
static int[] location = new int[2];
//两个玩家的姓名数组
static string[] pName = new string[2];
//声明两个玩家的标记
static bool[] flags = new bool[2];
static void Main(string[] args)
{
//游戏头
GameShow();
#region 输入玩家姓名
Console.ForegroundColor = ConsoleColor.White;
Console.Write("请输入第一个玩家姓名:");
pName[0] = Console.ReadLine();
Console.WriteLine();
Console.Write("请输入第二个玩家姓名:");
pName[1] = Console.ReadLine();
Console.WriteLine();
//两个玩家姓名不能相同
while (pName[0] == pName[1])
{
Console.WriteLine("第二个玩家姓名不能与第一个相同!");
Console.Write("--请重新输入第二个玩家姓名:");
pName[1] = Console.ReadLine();
}
#endregion
//输入玩家姓名后清屏
Console.Clear();
GameShow();//游戏头
#region 游戏开始提示
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("--------{0}和{1}开始对决!--------", pName[0], pName[1]);
Console.WriteLine();
#endregion
//初始化地图
InitialMap();
//画地图
DrawMap();
//当两玩家没有到终点,就不停的玩下去
#region 循环
while (location[0] < 99 && location[1] < 99)
{
if (flags[0] == false)
{
PlayGame(0);
}
else
{
flags[0] = false;
}
if (location[0] > 99)
{
Console.WriteLine("{0}胜利!", pName[0]);
break;
}
if (flags[1] == false)
{
PlayGame(1);
}
else
{
flags[1] = false;
}
if (location[1] > 99)
{
Console.WriteLine("{0}胜利!", pName[1]);
break;
}
}//while
#endregion
Console.WriteLine("胜利!");
Console.ReadKey();
}
/// <summary>
/// 游戏头
/// </summary>
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("------------------------------");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("------------------------------");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("----------飞行棋游戏----------");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("------------------------------");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("------------------------------");
}
/// <summary>
/// 初始化地图:把数组当中的数字变成特定的字符的过程
/// </summary>
public static void InitialMap()
{
//数组中的元素不赋值默认为0
//给数组中的元素赋值
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘○
for (int i = 0; i < luckyTurn.Length; i++)
{
map[luckyTurn[i]] = 1;
}
int[] landMain = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
for (int i = 0; i < landMain.Length; i++)
{
map[landMain[i]] = 2;
}
int[] pause = { 9, 27, 63, 93 };//暂停▲
for (int i = 0; i < pause.Length; i++)
{
map[pause[i]] = 3;
}
int[] timeTurnnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道◆
for (int i = 0; i < timeTurnnel.Length; i++)
{
map[timeTurnnel[i]] = 4;
}
}
/// <summary>
/// 画地图
/// </summary>
public static void DrawMap()
{
Console.WriteLine("注释:幸运轮盘○、地雷☆、暂停▲、时空隧道◆、普通□");
#region 第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(Draw(i));
}
#endregion
Console.WriteLine();
#region 第一竖行
for (int i = 30; i < 35; i++)
{
//画空格,循环嵌套的使用
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
//画特殊字符
Console.Write(Draw(i));
Console.WriteLine();
}
#endregion
//第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(Draw(i));
}
Console.WriteLine();
//第二竖行
for (int i = 65; i < 70; i++)
{
Console.Write(Draw(i));
Console.WriteLine();
}
//第三横行
for (int i = 70; i < 100; i++)
{
Console.Write(Draw(i));
}
Console.WriteLine();
}
/// <summary>
/// 从画地图的方法中抽象出来的一个方法
/// </summary>
/// <param name="i">地图数组的坐标</param>
/// <returns>输出的字符</returns>
public static string Draw(int i)
{
string str = "";
//地图不能随机生成,可能造成死循环
//先确定A和B的坐标
//当两个玩家的坐标相同,且都在地图上
if (location[0] == location[1] && location[0] == i)
{
//console.write("<>");
str = "<>";
}
else if (location[0] == i)
{
str = "A";
}
else if (location[1] == i)
{
str = "B";
}
else
{
switch (map[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
str = "○";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Yellow;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Green;
str = "◆";
break;
}
}
return str;
}
/// <summary>
///游戏规则
/// </summary>
/// <param name="playerNum">玩家数字</param>
public static void PlayGame(int playerNum)
{
Random r = new Random();
int rNum = r.Next(1, 7);
Console.WriteLine("{0}按任意键开始掷骰子;", pName[playerNum]);
Console.ReadKey(true);//暂停,按任意键继续,并且不显示任意键的内容
Console.WriteLine("玩家{0}掷到了{1}", pName[playerNum], rNum);
location[playerNum] += rNum;
Console.ReadKey(true);
Console.WriteLine("玩家{0}向前进{1}格", pName[playerNum], rNum);
Console.ReadKey(true);
//玩家A的几种可能性
//a踩到了b
if (location[playerNum] == location[1 - playerNum])
{
Console.WriteLine("{0}踩到了{1},玩家{2}退6格!", pName[playerNum], pName[1 - playerNum], pName[1 - playerNum]);
location[1 - playerNum] -= 6;
Console.ReadKey(true);
}
else
{
//玩家的坐标恰好就是数组的下标,然后做定值判断
switch (map[location[0]])
{
case 0:
Console.WriteLine("玩家{0}踩到了方块,什么都不发生!", pName[playerNum]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到了幸运轮盘,可交换双方位置-1或让对方后退6格-2", pName[playerNum]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}选择与玩家{1}交换位置", pName[playerNum], pName[1 - playerNum]);
Console.ReadKey(true);
int temp;
temp = location[playerNum];
location[playerNum] = location[1 - playerNum];
location[1 - playerNum] = temp;
Console.WriteLine("交换完成!,按任意键继续游戏!");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}选择让玩家{1}向后退6格!", pName[playerNum], pName[1 - playerNum]);
Console.ReadKey(true);
location[1 - playerNum] -= 6;
Console.WriteLine("玩家{0}后退完成!按任意键继续游戏!", pName[playerNum]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("请重新输入,只能输入1--交换位置/2--轰炸对方。");
input = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("玩家{0}踩到了地雷,后退6格!", pName[playerNum]);
Console.ReadKey(true);
location[playerNum] -= 6;
break;
case 4:
Console.WriteLine("玩家{0}踩到了时空隧道,向前进10格", pName[playerNum]);
Console.ReadKey(true);
location[playerNum] += 10;
break;
case 3:
Console.WriteLine("玩家{0}踩到了暂停,暂停一轮", pName[playerNum]);
flags[playerNum] = true;
Console.ReadKey(true);
break;
}//switch
}//else
ChangeLoc();//在清屏前调用一次防止玩家坐标溢出
Console.Clear();
DrawMap();
}
/// <summary>
/// 当玩家坐标改变时都需要调用这个方法
/// </summary>
public static void ChangeLoc()
{
if (location[0] < 0)
{
location[0] = 0;
}
if (location[0] > 99)
{
location[0] = 99;
}
if (location[1] < 0)
{
location[1] = 0;
}
if (location[1] > 99)
{
location[1] = 99;
}
}
}
}