using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace butterfly_onfly
{
enum Gamers
{
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z
}
class Program
{
static string[] players; //存储玩家昵称
static Gamers[] gamers; //存储玩家字母(全角字符,游戏显示用)
static string[] gamersInput = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; //存储玩家字母(半角字符,获取用户输入用)
static int[] playerPosition; //存储玩家位置
static bool[] isStop; //记录每轮被暂停的玩家,为true时被暂停
static int winner; //记录获胜玩家序号
//chessboard数组中:0表示普通,1表示轮盘,2表示地雷,3表示暂停,4表示隧道(使用数字表示棋盘,效率高,稳定)
static int[] chessboard = new int[100]; //存储数字棋盘
static string[] map = new string[100]; //存储图像棋盘
//存储chessboard中各个关卡下标(各个特殊关卡的总数之和不能大于100)
static int[] wheel = new int[5]; //轮盘
static int[] mine = new int[5]; //地雷
static int[] timeout = new int[7]; //暂停
static int[] tunnel = new int[7]; //隧道
static void Main(string[] args)
{
ShowUI();
GetPlayers();
Console.Clear();
ShowUI();
ShowPlayers();
InitialChessboard(chessboard); //数字棋盘初始化
GetMap(chessboard, playerPosition); //获取图像棋盘(初始值)
ShowMap(map); //绘制图像棋盘(初始值)
if (IsGameOn())
{
do
{
if (Dice(out winner, isStop) == false)
{
continue;
}
else
{
Console.WriteLine(">>>>>Game End<<<<<<");
Console.WriteLine("玩家{0}胜利", players[winner]);
break;
}
}
while (true);
}
else
return;
Console.ReadKey();
}
/// <summary>
/// 输出游戏名称界面
/// </summary>
static void ShowUI()
{
Console.WriteLine("**********************************************************");
Console.WriteLine("* *");
Console.WriteLine("* flybird_onfly *");
Console.WriteLine("* *");
Console.WriteLine("**********************************************************");
}
/// <summary>
/// 获取玩家人数及昵称
/// </summary>
static void GetPlayers()
{
int nums;
string input;
bool mark;
#region 获取玩家人数
do
{
Console.Write("请输入玩家人数:");
if (int.TryParse(Console.ReadLine(), out nums))
{
break;
}
else
{
Console.WriteLine("请检查输入!");
}
}
while (true);
#endregion
players = new string[nums];
gamers = new Gamers[nums];
playerPosition = new int[nums];
isStop = new bool[nums]; //记录每轮被暂停的玩家,为true时被暂停(初始化全设为false)
for (int i = 0; i < isStop.Length; i++)
{
isStop[i] = false;
}
GetGamers();
#region 依次获取玩家昵称,不能为空,不能重名
for (int i = 0; i < players.Length; i++)
{
do
{
mark = false;
Console.Write("请输入玩家{0}的昵称:", i + 1);
input = Console.ReadLine();
if (input == "")
{
Console.WriteLine("昵称不能为空!");
continue;
}
else
{
for (int j = 0; j < i; j++)
{
if (input == players[j])
{
Console.WriteLine("昵称与玩家{0}【{1}】重名!", j + 1, players[j]);
mark = true;
break;
}
}
if (!mark)
{
players[i] = input;
break;
}
}
}
while (true);
}
#endregion
}
/// <summary>
/// 获取玩家昵称对应的字母
/// </summary>
static void GetGamers()
{
for (int i = 0; i < players.Length; i++)
{
#region 获取玩家昵称对应的字母
switch (i)
{
case 0:
gamers[i] = Gamers.A;
break;
case 1:
gamers[i] = Gamers.B;
break;
case 2:
gamers[i] = Gamers.C;
break;
case 3:
gamers[i] = Gamers.D;
break;
case 4:
gamers[i] = Gamers.E;
break;
case 5:
gamers[i] = Gamers.F;
break;
case 6:
gamers[i] = Gamers.G;
break;
case 7:
gamers[i] = Gamers.H;
break;
case 8:
gamers[i] = Gamers.I;
break;
case 9:
gamers[i] = Gamers.J;
break;
case 10:
gamers[i] = Gamers.K;
break;
case 11:
gamers[i] = Gamers.L;
break;
case 12:
gamers[i] = Gamers.M;
break;
case 13:
gamers[i] = Gamers.N;
break;
case 14:
gamers[i] = Gamers.O;
break;
case 15:
gamers[i] = Gamers.P;
break;
case 16:
gamers[i] = Gamers.Q;
break;
case 17:
gamers[i] = Gamers.R;
break;
case 18:
gamers[i] = Gamers.S;
break;
case 19:
gamers[i] = Gamers.T;
break;
case 20:
gamers[i] = Gamers.U;
break;
case 21:
gamers[i] = Gamers.V;
break;
case 22:
gamers[i] = Gamers.W;
break;
case 23:
gamers[i] = Gamers.X;
break;
case 24:
gamers[i] = Gamers.Y;
break;
case 25:
gamers[i] = Gamers.Z;
break;
default:
break;
}
#endregion
}
}
/// <summary>
/// 显示玩家及关卡符号
/// </summary>
static void ShowPlayers()
{
for (int i = 0; i < players.Length; i++)
{
Console.WriteLine("玩家【{0}】用{1}表示", players[i], gamers[i]);
}
Console.WriteLine("玩家处于同一位置时,用<>表示");
}
/// <summary>
/// 数字棋盘初始化(获取数字棋盘中的各个关卡)
/// </summary>
/// <param name="a">数字棋盘(初始值都为零)</param>
static void InitialChessboard(int[] a)
{
Random ran = new Random();
//轮盘
for (int i = 0; i < wheel.Length; i++)
{
wheel[i] = ran.Next(3, 97);
a[wheel[i]] = 1;
}
//地雷
for (int i = 0; i < mine.Length; i++)
{
do
{
mine[i] = ran.Next(3, 97);
if (a[mine[i]] == 0)
{
a[mine[i]] = 2;
break;
}
}
while (true);
}
//暂停
for (int i = 0; i < timeout.Length; i++)
{
do
{
timeout[i] = ran.Next(3, 97);
if (a[timeout[i]] == 0)
{
a[timeout[i]] = 3;
break;
}
}
while (true);
}
//隧道
for (int i = 0; i < tunnel.Length; i++)
{
do
{
tunnel[i] = ran.Next(3, 97);
if (a[tunnel[i]] == 0)
{
a[tunnel[i]] = 4;
break;
}
}
while (true);
}
}
/// <summary>
/// 根据数字棋盘获取图像棋盘
/// </summary>
/// <param name="a">数字棋盘(变更值)</param>
/// <param name="b">玩家位置(变更值)</param>
static void GetMap(int[] a, int[] b)
{
for (int i = 0; i < a.Length; i++)
{
#region 绘制图像棋盘
switch (a[i])
{
case 0:
map[i] = "□";
break;
case 1:
map[i] = "◎";
break;
case 2:
map[i] = "▼";
break;
case 3:
map[i] = "■";
break;
case 4:
map[i] = "卐";
break;
default:
break;
}
#endregion
}
for (int i = 0; i < b.Length; i++)
{
#region 绘制玩家位置
switch (i)
{
case 0:
map[b[i]] = Gamers.A.ToString();
break;
case 1:
map[b[i]] = Gamers.B.ToString();
//判断玩家位置是否有重叠,有重叠放置<>
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 2:
map[b[i]] = Gamers.C.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 3:
map[b[i]] = Gamers.D.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 4:
map[b[i]] = Gamers.E.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 5:
map[b[i]] = Gamers.F.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 6:
map[b[i]] = Gamers.G.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 7:
map[b[i]] = Gamers.H.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 8:
map[b[i]] = Gamers.I.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 9:
map[b[i]] = Gamers.J.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 10:
map[b[i]] = Gamers.K.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 11:
map[b[i]] = Gamers.L.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 12:
map[b[i]] = Gamers.M.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 13:
map[b[i]] = Gamers.N.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 14:
map[b[i]] = Gamers.O.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 15:
map[b[i]] = Gamers.P.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 16:
map[b[i]] = Gamers.Q.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 17:
map[b[i]] = Gamers.R.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 18:
map[b[i]] = Gamers.S.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 19:
map[b[i]] = Gamers.T.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 20:
map[b[i]] = Gamers.U.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 21:
map[b[i]] = Gamers.V.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 22:
map[b[i]] = Gamers.W.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 23:
map[b[i]] = Gamers.X.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 24:
map[b[i]] = Gamers.Y.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
case 25:
map[b[i]] = Gamers.Z.ToString();
for (int j = 0; j < i; j++)
{
if (b[i] == b[j])
map[b[i]] = "<>";
}
break;
default:
break;
}
#endregion
}
}
/// <summary>
/// 绘制图像棋盘
/// </summary>
/// <param name="a">图像棋盘字符串数组</param>
static void ShowMap(string[] a)
{
Console.Write("【关卡】");
Console.Write("轮盘:{0}", GetColor("◎"));
Console.Write("地雷:{0}", GetColor("▼"));
Console.Write("暂停:{0}", GetColor("■"));
Console.Write("隧道:{0}", GetColor("卐"));
Console.WriteLine();
//绘制第一行
for (int i = 0; i <= 29; i++)
{
Console.Write(GetColor(a[i]));
}
Console.WriteLine();
//绘制第一列
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" "); //输入29个空格,图像为全角符合,输出时两个半角空格表示一个全角空格
}
Console.WriteLine(GetColor(a[i]));
}
//绘制第二行
for (int i = 64; i >= 35; i--)
{
Console.Write(GetColor(a[i]));
}
Console.WriteLine();
//绘制第二列
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(GetColor(a[i]));
}
//绘制第三行
for (int i = 70; i <= 99; i++)
{
Console.Write(GetColor(a[i]));
}
Console.ResetColor();
Console.WriteLine();
}
/// <summary>
/// 确定图像棋盘某个位置的图标所对应的颜色
/// </summary>
/// <param name="s">图像棋盘某个位置的图标</param>
/// <returns>返回图标字符串(附带颜色)</returns>
static string GetColor(string s)
{
if (IsGamers(s) || s == "<>")
{
Console.ForegroundColor = ConsoleColor.Yellow;
return s;
}
else if (s == "◎")
{
Console.ForegroundColor = ConsoleColor.Blue;
return s;
}
else if (s == "▼")
{
Console.ForegroundColor = ConsoleColor.Red;
return s;
}
else if (s == "■")
{
Console.ForegroundColor = ConsoleColor.Cyan;
return s;
}
else if (s == "卐")
{
Console.ForegroundColor = ConsoleColor.Green;
return s;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
return s;
}
}
/// <summary>
/// 判断图像棋盘某个位置的图标是否为玩家图标
/// </summary>
/// <param name="s">图像棋盘某个位置的图标</param>
/// <returns>返回是否为玩家图标的判断结果</returns>
static bool IsGamers(string s)
{
for (int i = 0; i < gamers.Length; i++)
{
if (s == gamers[i].ToString())
{
return true;
}
}
return false;
}
/// <summary>
/// 判断是否开始游戏
/// </summary>
/// <returns>返回判断结果</returns>
static bool IsGameOn()
{
bool flag;
string input;
Console.WriteLine("Game Start? (y/n)");
do
{
input = Console.ReadLine();
if (input.Equals("y", StringComparison.OrdinalIgnoreCase))
{
flag = true;
break;
}
else if (input.Equals("n", StringComparison.OrdinalIgnoreCase))
{
flag = false;
break;
}
else
Console.WriteLine("输入有误!");
}
while (true);
return flag;
}
/// <summary>
/// 玩家轮流掷色子,一旦某个玩家的坐标到达终点(大于终点坐标时相当于等于),返回true并获取获胜玩家序号
/// </summary>
/// <param name="a">返回获胜玩家序号</param>
/// <param name="b">记录每轮被暂停的玩家数组,为true时被暂停</param>
/// <returns>返回是否有玩家到达终点</returns>
static bool Dice(out int a, bool[] b)
{
a = 0; //记录获胜玩家序号
bool flag = false; //标注是否有玩家到达终点
#region n个玩家依次轮流掷色子
for (int i = 0; i < players.Length; i++)
{
if (b[i] == false)
{
int[] backPlayers = new int[players.Length]; //记录被踩回原点的玩家序号
int pass; //关卡序号
string message; //当玩家遇到轮盘,并选择完操作,这里存储刷新完棋盘后的提示信息
Console.Write("玩家【{0}】按任意键掷色子...", players[i]);
//************程序后门**********
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Home) //如果用户输入的是End键+Home键则进入后门
{
cki = Console.ReadKey();
if (cki.Key == ConsoleKey.End)
{
Console.WriteLine("掷出了{0}", GetDiceRandom(i, backPlayers, out pass, 1)); //进入后门
}
else
{
Console.WriteLine("掷出了{0}", GetDiceRandom(i, backPlayers, out pass, 0)); //不满足条件,不进入后门
}
}
else
{
Console.WriteLine("掷出了{0}", GetDiceRandom(i, backPlayers, out pass, 0)); //不满足条件,不进入后门
}
Console.Write("玩家【{0}】按任意键行动...", players[i]);
Console.ReadKey(true);
Execute(i, pass, out message, b); //执行关卡操作
Console.Clear(); //清屏,并重绘棋盘
GetMap(chessboard, playerPosition);
ShowMap(map);
if (message != "")
{
Console.WriteLine(message);
}
Console.WriteLine("玩家【{0}】位置为:{1}", players[i], playerPosition[i] + 1);
OutputBackPlayers(backPlayers);
if (playerPosition[i] == 99)
{
flag = true;
a = i;
break;
}
}
else
{
b[i] = false; //跳过掷色子,并恢复初始值,以便下次恢复
}
}
#endregion
return flag;
}
/// <summary>
/// 获取每次掷色子的随机数并修改当前玩家的位置
/// </summary>
/// <param name="a">玩家序号</param>
/// <param name="b">记录被踩回原点的玩家序号数组</param>
/// <param name="c">关卡序号</param>
/// <param name="d">程序后门判断</param>
/// <returns>返回当前所掷的色子数目</returns>
static int GetDiceRandom(int a, int[] b, out int c, int d)
{
int step;
Random ran = new Random();
step = ran.Next(1, 7);
if (d == 0) //正常
{
playerPosition[a] += step;
CheckPosition(a);
JudgePass(a, b, out c);
return step;
}
else //开启后门
{
Console.WriteLine();
Console.Write("→获取后门,请输入您想掷出的数字:");
step = ReadAnyInt(); //修改step的值
playerPosition[a] += step;
CheckPosition(a);
JudgePass(a, b, out c);
return step;
}
}
static int ReadAnyInt()
{
int result;
do
{
if (int.TryParse(Console.ReadLine(), out result))
{
break;
}
else
Console.WriteLine("输入的内容有误!");
}
while (true);
return result;
}
/// <summary>
/// 检查玩家新坐标的位置是否超出棋盘坐标,若有,则退回最初、最末格
/// </summary>
/// <param name="a">玩家序号</param>
static void CheckPosition(int a)
{
if (playerPosition[a] < 0)
playerPosition[a] = 0;
else if (playerPosition[a] > 99)
playerPosition[a] = 99;
}
/// <summary>
/// 判断玩家是否踩到对手或踩到特殊关卡
/// </summary>
/// <param name="a">玩家序号</param>
/// <param name="b">记录被踩回原点的玩家序号数组</param>
/// <param name="c">关卡序号</param>
static void JudgePass(int a, int[] b, out int c)
{
c = 0;
for (int i = 0; i < playerPosition.Length; i++)
{
if (i != a)
{
if (playerPosition[a] == playerPosition[i])
{
playerPosition[i] = 0;
b[i] = 1;
}
else
{
switch (chessboard[playerPosition[a]])
{
case 0:
break;
case 1: //轮盘
c = 1;
break;
case 2: //地雷
c = 2;
break;
case 3: //暂停
c = 3;
break;
case 4: //隧道
c = 4;
break;
default:
break;
}
}
}
}
}
/// <summary>
/// 执行特殊关卡操作
/// </summary>
/// <param name="a">玩家序号</param>
/// <param name="b">关卡序号</param>
/// <param name="s">当玩家遇到轮盘,并选择完操作,这里存储刷新完棋盘后的提示信息</param>
/// <param name="c">记录每轮被暂停的玩家数组,为true时被暂停</param>
static void Execute(int a, int b, out string s, bool[] c)
{
switch (b)
{
case 0:
s = "";
break;
case 1: //轮盘
int gamer; //临时记录被交换的玩家序号
Console.Clear(); //清屏后,显示玩家新位置,为特殊关卡的位置变更做准备
GetMap(chessboard, playerPosition);
ShowMap(map);
Console.WriteLine("玩家【{0}】获得轮盘,请选择?(1-与某玩家交换位置,2-轰炸某玩家,退6格)", players[a]);
if (GetIntInput() == 1)
{
Console.WriteLine("请输入代表玩家的字母(A/B/C.....Z)");
gamer = GetGamerInt(Console.ReadLine());
int temp;
temp = playerPosition[a];
playerPosition[a] = playerPosition[gamer];
playerPosition[gamer] = temp;
s = string.Format("玩家【{0}】与玩家【{1}】交换了位置", players[a], players[gamer]);
}
else
{
Console.WriteLine("请输入代表玩家的字母(A/B/C.....Z)");
gamer = GetGamerInt(Console.ReadLine());
playerPosition[gamer] -= 6;
CheckPosition(gamer);
s = string.Format("玩家【{0}】轰炸了玩家【{1}】,玩家【{1}】退6格", players[a], players[gamer]);
}
Console.WriteLine("按任意键继续...");
Console.ReadKey(true);
break;
case 2: //地雷
Console.Clear(); //清屏后,显示玩家新位置,为特殊关卡的位置变更做准备
GetMap(chessboard, playerPosition);
ShowMap(map);
Console.WriteLine("玩家【{0}】踩到了地雷,后退6格!", players[a]); //提示玩家踩到的特殊关卡功能
Console.WriteLine("按任意键继续...");
Console.ReadKey(true);
playerPosition[a] -= 6;
CheckPosition(a);
s = "";
break;
case 3: //暂停
Console.Clear(); //清屏后,显示玩家新位置,为特殊关卡的位置变更做准备
GetMap(chessboard, playerPosition);
ShowMap(map);
Console.WriteLine("玩家【{0}】遇到了红灯,暂停一轮!", players[a]); //提示玩家踩到的特殊关卡功能
Console.WriteLine("按任意键继续...");
Console.ReadKey(true);
c[a] = true;
s = "";
break;
case 4: //隧道
Console.Clear(); //清屏后,显示玩家新位置,为特殊关卡的位置变更做准备
GetMap(chessboard, playerPosition);
ShowMap(map);
Console.WriteLine("玩家【{0}】进入隧道,前进10格!", players[a]);
Console.WriteLine("按任意键继续...");
Console.ReadKey(true);
playerPosition[a] += 10;
CheckPosition(a);
s = "";
break;
default:
s = "";
break;
}
}
/// <summary>
/// 输出被踩回原点的玩家昵称
/// </summary>
/// <param name="a">存储被踩回原点玩家信息的数组</param>
static void OutputBackPlayers(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] == 1)
{
Console.WriteLine("玩家【{0}】被踩回原点", players[i]);
}
}
Console.WriteLine();
}
/// <summary>
/// 获取一个整型数字的输入(这里限制为1/2)
/// </summary>
/// <returns>返回输入的数字</returns>
static int GetIntInput()
{
int input;
do
{
if (int.TryParse(Console.ReadLine(), out input))
{
if (input == 1 || input == 2)
{
break;
}
else
Console.WriteLine("输入数字有误!");
}
else
Console.WriteLine("输入有误!");
}
while (true);
return input;
}
/// <summary>
/// 获取玩家字母所对应的玩家序号
/// </summary>
/// <param name="s">玩家字母(半角大写)</param>
/// <returns>返回序号</returns>
static int GetGamerInt(string s)
{
for (int i = 0; i < gamersInput.Length; i++)
{
if (s.Equals(gamersInput[i], StringComparison.OrdinalIgnoreCase))
return i;
}
return -1;
}
}
}
05-10
08-01
01-13