这代码想要运行,新建控制台程序。将命名空间内的代码全删掉,复制下面代码。即可运行。
class Program
{
//静态字段,全局变量
public static int[] maps = new int[100];
//玩家A和玩家B
public static int[] player = new int[2];
public static string[] playerNames = new string[2];
public static bool[] flags = new bool[2];//判断是否踩到暂停
static void Main(string[] args)
{
GameShow();
#region 输入玩家名字
Console.WriteLine("请输入玩家A的姓名");
playerNames[0] = Console.ReadLine();
while (playerNames[0] == "")
{
Console.WriteLine("玩家A的姓名不能为空,请重新输入。");
playerNames[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
playerNames[1] = Console.ReadLine();
while ( playerNames[1] == ""||playerNames[1] == playerNames[0])
{
if (playerNames[1] == "")
{
Console.WriteLine("玩家B的名字不能为空,请重新输入。");
playerNames[1]= Console.ReadLine();
}
else
{
Console.WriteLine("玩家B的名字不能和玩家A的姓名相同,请重新输入。");
playerNames[1]=Console.ReadLine();
}
}
#endregion
//输入完名字后,清屏。
Console.Clear();
GameShow();
Console.WriteLine("{0}的士兵用A表示",playerNames[0]);
Console.WriteLine("{0}的士兵用B表示", playerNames[1]);
InitailMap();
DropMap();
//没人胜利就一直玩,正式开始游戏
while(player[0] <99 &&player[1]<99)
{
if (flags[0] == false)
{
playgame(0);
}
else
{
flags[0] = false;
}
if (player[0] >= 99)
{
Console.WriteLine("玩家{0}无耻的赢了{1}",playerNames[0],playerNames[1]);
break;
}
if (flags[1] == false)
{
playgame(1);
}
else
{
flags[1] = false;
}
if (player[1] >= 99)
{
Console.WriteLine("玩家{0}无耻的赢了{1}", playerNames[1], playerNames[0]);
break;
}
}
//胜利显示
win();
Console.ReadKey();
}
//游戏头
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ********************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" ********************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" *****骑士飞行棋*****");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" ********************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ********************");
}
//初始化地图
public static void InitailMap()
{
//幸运轮盘¤
int[] lucklyTurn = { 6, 23, 40, 55, 69, 83 };
for (int i = 0; i < lucklyTurn.Length; i++)
{
maps[lucklyTurn[i]] = 1;
}
//暂停▲
int[] stop = { 9, 27, 60, 93 };
for (int i = 0; i <stop.Length; i++)
{
maps[stop[i]]=3;
}
//地雷☆
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
for (int i = 0; i < landMine.Length; i++)
{
maps[landMine[i]] = 2;
}
//时空隧道卍
int[] timeTurn = { 20, 25, 45, 63, 72, 88, 90 };
for (int i = 0; i < timeTurn.Length; i++)
{
maps[timeTurn[i]] = 4;
}
}
// 地图加载
public static void DropMap()
{
Console.WriteLine("游戏说明:幸运转盘:¤ 地雷:☆ 暂停:▲ 时空隧道:卍");
//第一行
for (int i = 0; i < 30; i++)
{
Console.Write(DrowStringMap(i));
}
//第一行后应该换行
Console.WriteLine();
//第一竖行
for (int i = 30; i < 35; i++)
{
for (int j = 0; j <=28; j++)
{
Console.Write(" ");
}
Console.Write(DrowStringMap(i));
Console.WriteLine();
}
//第二横行
for (int i=64;i>=35;i--)
{
Console.Write(DrowStringMap(i));
}
//第二行完后应该换行
Console.WriteLine();
//第二竖行
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrowStringMap(i));
}
//第三横行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrowStringMap(i));
}
//画完最后一行应该换行
Console.WriteLine();
}
// 返回一个符号(从画地图中抽象出来的)
public static string DrowStringMap(int i)
{
string str = "";
if ((player[0] == player[1]) && (player[0] == i))
{
str="<>";
}
else if (player[0] == i)
{
str = "A";
}
else if (player[1] == i)
{
str = "B";
}
else
{
switch (maps[i])
{
case 0: Console.ForegroundColor = ConsoleColor.White; str = "□"; break;
case 1: Console.ForegroundColor = ConsoleColor.Magenta; str = "¤"; break;
case 2: Console.ForegroundColor = ConsoleColor.Gray; str = "☆"; break;
case 3: Console.ForegroundColor = ConsoleColor.Yellow; str = "▲"; break;
case 4: Console.ForegroundColor = ConsoleColor.Red; str = "卍"; break;
}
}
return str;
}
//玩游戏
public static void playgame(int playernumer)
{
Random r = new Random();
int rnumer = r.Next(1,7);
Console.WriteLine("{0}按任意键开始掷骰子", playerNames[playernumer]);
Console.ReadKey(true);
Console.WriteLine("{0}掷出了{1}", playerNames[playernumer],rnumer);
player[playernumer] +=rnumer;
ChangePos();
Console.ReadKey(true);
Console.WriteLine("{0}按任意键开始行动", playerNames[playernumer]);
Console.ReadKey(true);
Console.WriteLine("{0}行动完了", playerNames[playernumer]);
Console.ReadKey(true);
if (player[playernumer] == player[1 - playernumer])
{
Console.WriteLine("玩家{0}踩到玩家{1},玩家{2}退6格", playerNames[playernumer], playerNames[1 - playernumer], playerNames[1 - playernumer]);
player[1 - playernumer] -= 6;
ChangePos();
Console.ReadKey(true);
}
else //踩到关卡
{
switch (maps[player[playernumer]])
{
case 0: Console.WriteLine("玩家{0}踩到了方块,安全。", playerNames[playernumer]);
Console.ReadKey(true);
break;
case 1: Console.WriteLine("玩家踩到了幸运盘,请选择 1.--交换位置 2.--轰炸对方");
string inout = Console.ReadLine();
while (true)
{
if (inout == "1")
{
Console.WriteLine("玩家{0}选择和玩家{1}交换位置", playerNames[playernumer], playerNames[1 - playernumer]);
Console.ReadKey(true);
int temp = player[playernumer];
player[playernumer] = player[1 - playernumer];
player[1 - playernumer] = temp;
Console.WriteLine("交换完成。。。按任意键继续游戏。");
Console.ReadKey(true);
break;
}
else if (inout == "2")
{
Console.WriteLine("玩家{0}选择轰炸玩家{1}", playerNames[playernumer], playerNames[1 - playernumer]);
Console.ReadKey(true);
player[1 - playernumer] -= 6;
ChangePos();
Console.WriteLine("玩家{0}退了6格", playerNames[1 - playernumer]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("只能输入1或者2.");
inout = Console.ReadLine();
}
}
break;
case 2: Console.WriteLine("玩家{0}踩到地雷,退6格。", playerNames[playernumer]);
Console.ReadKey(true);
player[playernumer] -= 6;
ChangePos();
break;
case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合。", playerNames[playernumer]);
flags[playernumer] = true;
Console.ReadKey(true);
break;
case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格。", playerNames[playernumer]);
player[playernumer] += 10;
ChangePos();
Console.ReadKey(true);
break;
}
}
ChangePos();
Console.Clear();
DropMap();
}
//判断是否走出了地图
public static void ChangePos()
{
if (player[0] < 0)
{
player[0] = 0;
}
if (player[0] >= 99)
{
player[0] = 99;
}
if (player[1] < 0)
{
player[1] = 0;
}
if (player[1] >= 99)
{
player[1] = 99;
}
}
//胜利显示
public static void win()
{
Console.WriteLine("■ ■ ■■■■■■■■ ■ ");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■■■■■■■■ ■ ■");
}
}