这是本人学习C#之后,写的第一个代码量比较大的控制台程序,如果有什么问题欢迎批评指导。
class Program
{
//定义地图数组
public static int[] Maps = new int[100];
//定义两位玩家的位置信息
public static int[] playerPos = new int[2];
//定义玩家的姓名数组
public static string[] playerName = new string[2];
//定义玩家游戏状态数组
public static bool[] playState = new bool[2];
static void Main(string[] args)
{
GameShow();//游戏头显示
InitMaps();//初始化地图数组
DrawMap();//画地图
InputPlayName();//输入玩家的名字
while(true)//开始游戏,知道一个玩家到达结束位置,游戏结束
{
if (playState[0] == false)
{
Game(0);//玩家1进行游戏
if (playerPos[0] == 99)
{
Console.WriteLine("玩家{0}获得胜利", playerName[0]);
break;
}
}
else
{
playState[0] = false;
}
if (playState[1] == false)
{
Game(1);//玩家2进行游戏
if (playerPos[0] == 99)
{
Console.WriteLine("玩家{0}获得胜利", playerName[1]);
break;
}
}
else
{
playState[1] = false;
}
}
Console.ReadKey();
}
///
/// 完成游戏头
///
public static void GameShow()
{
//选择字体颜色
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("***********************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("***********************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("*************飞行棋****************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("***********************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("***********************************");
}
///
/// 初始化地图数组
///
public static void InitMaps()
{
//定义幸运转盘数组,将其在地图数组内赋值
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };
for (int i = 0; i < luckyTurn.Length; i++)
{
Maps[luckyTurn[i]] = 1;
}
//定义地雷数组,将其在地图数组内赋值
int[] landMine = { 5, 13, 17, 33, 50, 64, 80, 94 };
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
//定义暂停数组,将其在地图数组内赋值
int[] pause = { 9, 27, 60, 93 };
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
//定义时空隧道数组,将其在地图数组内赋值
int[] timeTunne = { 20, 25, 45, 63, 72, 88, 90 };
for (int i = 0; i < timeTunne.Length; i++)
{
Maps[timeTunne[i]] = 4;
}
}
///
/// 画地图
///
public static void DrawMap()
{
//定义每个图形的含义
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("@代表什么都不发生;#幸运转盘;$地雷;&暂停一次;*时空隧道");
//画第一横行
for(int i = 0; i <= 29; i++)
{
Console.Write(DrawInfo(i));
}
//第一竖行
for (int i = 30; i < 35; i++)
{
Console.WriteLine();
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
Console.Write(DrawInfo(i));
}
//第二横行
Console.WriteLine();
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawInfo(i));
}
//第二竖行
for (int i = 65; i < 70; i++)
{
Console.WriteLine();
Console.Write(DrawInfo(i));
}
//第三横行
Console.WriteLine();
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawInfo(i));
}
Console.WriteLine();
}
///
/// 输入位置信息返回需要画的图形
///
/// 地图位置信息
///
public static string DrawInfo(int i)
{
string str = "";
if (playerPos[0] == playerPos[1] && playerPos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Cyan;
str = "<";
}
else if (playerPos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Red;
str = "A";
}
else if (playerPos[1] == i)
{
Console.ForegroundColor = ConsoleColor.Red;
str = "B";
}
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.Yellow;
str = "@";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Blue;
str = "#";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
str = "$";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Magenta;
str = "&";
break;
case 4:
Console.ForegroundColor = ConsoleColor.White;
str = "*";
break;
}
}
return str;
}
///
/// 输入玩家姓名
///
public static void InputPlayName()
{
Console.WriteLine();
Console.WriteLine("请输入玩家A姓名:");
playerName[0] = Console.ReadLine();
while (playerName[0] == "")
{
Console.WriteLine("玩家A姓名不能为空,请重新输入玩家A姓名:");
playerName[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B姓名:");
playerName[1] = Console.ReadLine();
while (playerName[1] == "" || playerName[0] == playerName[1])
{
if (playerName[1] == "")
{
Console.WriteLine("玩家B姓名不能为空,请重新输入玩家B姓名:");
playerName[1] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家B姓名不能与玩家A相同,请重新输入玩家B姓名:");
playerName[1] = Console.ReadLine();
}
}
}
///
/// 判断玩家位置信息
///
public static void JudgePlayerPos()
{
if (playerPos[0] < 0)
{
playerPos[0] = 0;
}
if(playerPos[0] > 99)
{
playerPos[0] = 99;
}
if (playerPos[1] < 0)
{
playerPos[1] = 0;
}
if (playerPos[1] > 99)
{
playerPos[1] = 99;
}
}
///
/// 开始游戏
///
/// 传入玩家的数组编号
public static void Game(int i)
{
Console.WriteLine("请玩家{0}按任意键开始摇色子", playerName[i]);
Console.ReadKey(true);
Random rand = new Random();
int num = rand.Next(1, 7);
Console.WriteLine("玩家{0}摇了{1}", playerName[i], num);
Console.WriteLine("请按任意键开始行动");
Console.ReadKey(true);
playerPos[i] += num;
JudgePlayerPos();
JudgeGame(i);
}
///
/// 游戏规则
///
/// 当前的玩家
public static void JudgeGame(int i)
{
//游戏规则:当玩家A的位置与玩家B的位置重叠时,玩家B退六格
//当在@时,无变化;#时1交换位置,2轰炸玩家退6格;&暂停一次;*进10格
if (playerPos[i] == playerPos[1 - i])
{
Console.WriteLine("玩家{0}与玩家{1}在同一个位置");
Console.WriteLine("玩家{0}往后退6格", playerName[1 - i]);
Console.ReadKey(true);
playerPos[1 - i] -= 6;
JudgePlayerPos();
}
else
{
switch (Maps[playerPos[i]])
{
case 0:
Console.WriteLine("什么都没有发生");
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("进入幸运转盘,请选择:1.交换位置;2.轰炸玩家");
string str = Console.ReadLine();
while (str != "1" && str !="2")
{
Console.WriteLine("输入出错,请重新选择:1.交换位置;2.轰炸玩家");
str = Console.ReadLine();
}
if (str == "1")
{
Console.WriteLine("交互玩家{0}与玩家{1}位置", playerName[i], playerName[1 - i]);
int temp = playerPos[i];
playerPos[i] = playerPos[1 - i];
playerPos[1 - i] = temp;
}
break;
case 2:
Console.WriteLine("踩到地雷,后退六步");
Console.ReadKey(true);
playerPos[i] -= 6;
JudgePlayerPos();
break;
case 3:
Console.WriteLine("暂停一次");
Console.ReadKey(true);
playState[i] = true;
break;
case 4:
Console.WriteLine("进入时空通道,前进十步");
Console.ReadKey(true);
playerPos[i] += 10;
JudgePlayerPos();
break;
}//switch
}//else
Console.Clear();
DrawMap();
}