代码如下所示:
namespace 骑士飞行棋
{
class program
{
//在下面的数组存储我们游戏地图各各关卡
//数组的下标为0的元素对应地图上的第1格 下标为1的元素对应元素第2格...下标为n的元素对应n+1格
//在数组中 1:表示幸运轮盘 ◎
// 2: 表示地雷 ☆
// 3: 表示暂停 ▲
// 4: 表示时空隧道 卍
// 0: 表示普通 □
static int[] map = new int[100];
static string[] names = new string[2]; //names[0]存储玩家a的姓名 name[1]存玩家b的姓名
static int[] playerpos = { 0, 0 };//playpos[0]存玩家a的位置,playpos[1]存玩家b的位置
static int step = 0; //用于存放产生的随机数
static string input = ""; //用户存储用户的输入
static string msg = ""; //用于存储用户踩到某个关卡,输出的话
static bool[] isstop = { false, false };//isstop[0]表示玩家a是否上次一走到暂停,似的话为true,不是为false
static random r = new random();//r是产生的随机数
static void main( string[] args)
{
showui(); //显示游戏
initialname();
console.clear();
showui();
console.writeline("对战开始......");
console.writeline("{0}用a来表示", names[0]);
console.writeline("{0}用b来表示", names[1]);
console.writeline("如果ab在同一位置,用<>表示");
initialmap();//初始化地图
drawmap();//绘制地图
console.writeline("开始游戏......");
//这个循环中让玩家a和玩家b轮流掷骰子 当玩家a或者玩家b的坐标>=99时,则循环结束
while (playerpos[0] < 99 && playerpos[1] < 99)
{
action(0);//a掷筛子
action(1);//b掷筛子
}
console.readkey();
}
/// <summary>
/// 用于绘制飞行棋的名称
/// </summary>
static void showui()
{
console.writeline("*******************************************************");
console.writeline("* *");
console.writeline("* 骑 士 飞 行 棋 *");
console.writeline("* *");
console.writeline("*******************************************************");
}
static void initialname()
{
console.writeline("请输入玩家a的姓名");
names[0] = console.readline();
//判断用书输入的内容是否为空,如果为空,则让用户重新输入
while (names[0] == "")
{
console.writeline("玩家a的姓名不能为空,请重新输入!");
names[0] = console.readline();
}
console.writeline("请输入玩家b的姓名");
names[1] = console.readline();
//判断用户输入的内容是否为空,如果为空,则让用户重新输入
while (names[1] == "" || names[1] == names[0])
{
if (names[1] == "")
{
console.writeline("玩家b的姓名不能为空,请重新输入!");
names[1] = console.readline();
}
else
{
console.writeline("你输入的姓名与玩家a的姓名{0}相同,请重新输入", names[0]);
names[1] = console.readline();
}
}
}
static void initialmap()
{
//用于存储在地图中为地雷的下标
int[] luckyturn = { 6, 23, 40, 55, 69, 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 < 100; i++) // 初始化map数组的数据
map[i] = 0;
//把幸运轮盘位置填入map中
for (int i = 0; i < luckyturn.length; i++)
map[luckyturn[i]] = 1;
//把地雷填入map中
for (int i = 0; i < landmine.length; i++)
map[landmine[i]] = 2;
//把暂停填入map中
for (int i = 0; i < pause.length; i++)
map[pause[i]] = 3;
//把时空隧道填入map中
for (int i = 0; i < timetunnel.length; i++)
map[timetunnel[i]] = 4;
}
/// <summary>
/// 获得第pos坐标上应该绘制的图案
/// </summary>
/// <param name="pos">要绘制的坐标</param>
/// <returns></returns>
static string getmapstring(int pos)
{
string result = "";
if (playerpos[0] == pos && playerpos[1] == pos)
{
console.foregroundcolor = consolecolor.yellow;
result = "<>";
}
else if (playerpos[0] == pos)
{
console.foregroundcolor = consolecolor.yellow;
result = "a";
}
else if (playerpos[1] == pos)
{
console.foregroundcolor = consolecolor.yellow;
result = "b";
}
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;
}
static void drawmap()
{
console.writeline("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卍 ");
//画第一行
for (int i = 0; i < 30; i++)
console.write(getmapstring(i));
console.writeline();
//左边的第一列
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
console.write(" ");
console.write(getmapstring(i));
console.writeline();
}
//第二行
for (int i = 64; i >= 35; i--)
console.write(getmapstring(i));
console.writeline();
//右边的列
for (int i = 65; i < 70; i++)
{
console.write(getmapstring(i));
console.writeline();
}
//第三行
for (int i = 70; i < 100; i++)
console.write(getmapstring(i));
console.resetcolor();
console.writeline();
}
static void checkpos()
{
for (int i = 0; i <= 1; i++)
{
if (playerpos[i] > 99)
{
playerpos[i] = 99;
}
if (playerpos[i] < 0)
{
playerpos[i] = 0;
}
}
}
static int readint()//产生一个整数
{
int i = readint(int.maxvalue, int.minvalue);
return i;
}
static int readint(int min, int max)//产生min--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("只能输入数字,请重新输入!");
}
}
}
/// <summary>
/// a或者b掷筛子的方法
/// </summary>
/// <param name="playernumber">a掷筛子传0过来 b掷筛子传1过来</param>
static void action(int playernumber)
{
if (isstop[playernumber] == false)
{
console.writeline("{0}按任意键开始掷筛子......", names[playernumber]);
consolekeyinfo sec = console.readkey(true);
step = r.next(1, 7);//产生一个1到6之间的随机数
if (sec.key == consolekey.tab)
{
consolekeyinfo sec1 = console.readkey(true);
if (sec1.key == consolekey.f1)
{
step = readint(1, 100);
}
}
console.writeline("{0}掷出了{1}", names[playernumber], step);
console.writeline("{0}按任意键开始行动......", names[playernumber]);
console.readkey(true);
playerpos[playernumber] += step; //注意,一旦坐标发生改变,就要判断坐标值是否>99||<0
checkpos();//检查坐标是否越界
if (playerpos[playernumber] == playerpos[1 - playernumber]) //玩家a采到玩家b
{
playerpos[1 - playernumber] = 0;
msg = string.format("{0}踩到了{1},{1}退回了原点", names[playernumber], names[1 - playernumber]);
}
else
{//没踩到,要判断玩家a现在所在的位置是否有其他关卡
switch (map[playerpos[playernumber]])
{
case 0:
//普通,没有效果
msg = "";
break;
case 1:
//走到了 幸运轮盘关卡
console.clear();
console.writeline("你走到了幸运轮盘,请选择运气?");
console.writeline("1 ---交换位置 2---轰炸对方");
int userselect = readint(1, 2);
if (userselect == 1)
{//要与对方交换位置
int temp = playerpos[playernumber];
playerpos[playernumber] = playerpos[1 - playernumber];
playerpos[1 - playernumber] = temp;
msg = string.format("{0}选了与对方交换位置", names[playernumber]);
}
else
{//轰炸对方
playerpos[1 - playernumber] -= 6;
msg = string.format("{0}轰炸了{1},{1}退回了6格", names[playernumber], names[1 - playernumber]);
checkpos();
}
break;
case 2:
//踩到了地雷
playerpos[playernumber] -= 6;
checkpos();
msg = string.format("{0}踩到了地雷,{0}退了6格", names[playernumber]);
break;
case 3:
//暂停一次
isstop[playernumber] = true;
msg = string.format("{0}走到了红灯,下次暂停一次啊", names[playernumber]);
break;
case 4:
//踩到时空隧道
playerpos[playernumber] += 10;
msg = string.format("{0}进入了时空隧道,爽死了,进了10格", names[playernumber]);
break;
}
}
}
else
{
isstop[playernumber] = false;
}
if (playerpos[playernumber] >= 99)
{
//判断谁胜利,谁失败
console.clear();
if (playerpos[0] >= 99)
{
console.writeline("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
}
else
{
console.writeline("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
}
}
console.clear();
drawmap();
if (msg != "")
{
console.writeline(msg);
}
console.writeline("{0}掷出了{1},行动完成!", names[playernumber], step);
console.writeline("*************玩家a和玩家b的位置*********");
console.writeline("{0}的位置为:{1}", names[0], playerpos[0] + 1);
console.writeline("{0}的位置为:{1}", names[1], playerpos[1] + 1);
}
}
}
如对本文有疑问, 点击进行留言回复!!
C# 对PDF文档加密、解密(基于Spire.Cloud.SDK for .NET)
Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)
网友评论