using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 飞行棋
{
class Program
{
//下标为0对应地图第一格,下标为2对应第2格……
//在数组中1:幸运轮盘◎;2:地雷☆;3:暂停▲;4:时空隧道卐;0:普通地图
static Random ra = new Random();
static string[] names = new string[2]; //names[0]玩家一,names[1]玩家二
static bool[] isStop = { false, false };//表示两人上一次是否暂停,如果走到暂停,设为true
static string msg = "";//莫关卡要输出的话
static int step = 0;//投掷骰子的步数
static int[] Map = new int[100];
static int[] playerPos = { 0, 0 };//playerPos[0]为玩家A的坐标,playerPos[1]为玩家B的坐标
static void Main(string[] args)
{
ShowUI();
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姓名为空,请重新输入");
}
if (names[1] == names[0])
{
Console.WriteLine("玩家B姓名与玩家A相同,请重新输入");
}
names[1] = Console.ReadLine();
}
Console.Clear();//清屏,很多根屏幕输出有关的都在Console中。
ShowUI();
Console.WriteLine("对战开始");
Console.WriteLine("{0}用A来表示",names[0]);
Console.WriteLine("{0}用A来表示", names[1]);
Console.WriteLine("AB在同一位置用<>表示");
InitialMap();
DrawMap();
//开始掷骰子ab轮流投,只要有一个到终点就结束
while (playerPos[0] < 99 && playerPos[1] < 99)
{
if (isStop[0] == false)
{
Action(0);
}
else
{
//A上一次走到暂停了
isStop[0] = false;
}
if (playerPos[0] >= 99)//A到99,游戏结束
{
break;
}
if (isStop[1] == false)
{
Action(1);
}
else
{
//B上一次走到暂停了
isStop[1] = false;
}
}
Console.Clear();
ShowUI();
if (playerPos[0] > 99)
{
Console.WriteLine("{0}胜利啦!!", names[0]);
}
else
{
Console.WriteLine("{0}胜利啦!!", names[1]);
}
Console.ReadKey();
}
/// <summary>
/// A、B的行动代码
/// </summary>
/// <param name="playerNumber"></param>
static void Action(int playerNumber)
{
//playernumber中存放的是当前玩家姓名、坐标以及是否暂停的坐标
//当前是1对方就是0,当前是1对方就是0
int playerNumber2=1-playerNumber;
Console.WriteLine("{0}按任意键开始掷骰子", names[playerNumber]);
ConsoleKeyInfo rec = Console.ReadKey(true);
if (rec.Key == ConsoleKey.Tab)//对Console.ReadKey的返回值的判断,要是tap键的话走s输入的格(测试挂)
{
Console.WriteLine("输入你想走的步数");
step = Convert.ToInt32(Console.ReadLine());
}
else
{
step = ra.Next(1, 7);
}
Console.WriteLine("{0}投出了{1}点", names[playerNumber], step);
Console.WriteLine("按任意键开始行动");
Console.ReadKey(true);
playerPos[playerNumber] = playerPos[playerNumber] + step;
CheckPos();//一旦发生改变,就要判断
Console.Clear();
if (playerPos[playerNumber] == playerPos[playerNumber2])//判断A有没有猜到B
{
playerPos[playerNumber2] = 0;
msg = string.Format("{0}猜到了{1},{1}回到原点", names[playerNumber], names[playerNumber2]);//string.Format给字符串赋值,又能写占位符
}
else //没猜到,看有没有其他管卡
{
switch (Map[playerPos[playerNumber]])
{
case 0://普通
break;
case 1://幸运轮盘
Console.Clear();
DrawMap();
Console.WriteLine("{0}走到了幸运轮盘,请选择运气?", names[playerNumber]);
Console.WriteLine("1:交换位置。2:轰炸对方");
int userSelect = ReadInt(1, 2);
if (userSelect == 1)
{
int temp = playerPos[playerNumber];
playerPos[playerNumber] = playerPos[playerNumber2];
playerPos[playerNumber2] = temp;
msg = string.Format("{0}选择了与对方交换位置", names[playerNumber]);
}
else
{
playerPos[playerNumber] = playerPos[playerNumber] - 6;
CheckPos();
msg = string.Format("{0}轰炸{1},{1}退6格", names[playerNumber], names[playerNumber2]);
}
break;
case 2://地雷
playerPos[playerNumber] = playerPos[playerNumber] - 6;
msg = string.Format("{0}猜到地雷,退6格", names[playerNumber]);
break;
case 3://暂停
isStop[0] = true;
msg = string.Format("{0}走到暂停,下次暂停", names[playerNumber]);
break;
case 4://时空隧道
playerPos[playerNumber] = playerPos[playerNumber] + 10;
msg = string.Format("{0}进入数控隧道,前进10格", names[playerNumber]);
break;
}
}
Console.Clear();
DrawMap();
if (msg != "")
{
Console.WriteLine(msg);
}
Console.WriteLine("{0}投出了{1},行动完成", names[playerNumber], step);
Console.WriteLine("玩家{0}和玩家{1}的位置如下", names[playerNumber], names[playerNumber2]);
Console.WriteLine("玩家{0}的位置为:{1}", names[playerNumber], playerPos[playerNumber] + 1);
Console.WriteLine("玩家{0}的位置为:{1}", names[playerNumber2], playerPos[playerNumber2] + 1);
}
/// <summary>
/// 判断用户输入的是否是在min到max中的数字
/// </summary>
/// <returns></returns>
static int ReadInt()
{
int i = ReadInt(int.MinValue, int.MaxValue);
return i;
}
static int ReadInt(int min, int max)
{
while (true)
{
try
{
int number = Convert.ToInt32(Console.ReadLine());
if (number < min || number > max)
{
Console.WriteLine("请输入{0}到{01}之间的数字", min, max);
continue;
}
return number;
}
catch
{
Console.WriteLine("输入有误,请重新输入!");
}
}
}
/// <summary>
/// 进行玩家坐标越界的判断
/// </summary>
static void CheckPos()
{
for (int i = 0; i <= 1; i++)
{
if (playerPos[i] > 99)
{
playerPos[i] = 99;
}
if (playerPos[i] <0)
{
playerPos[i] = 0;
}
}
}
/// <summary>
/// 绘制飞行棋名称
/// </summary>
static void ShowUI()
{
Console.WriteLine("****************************");
Console.WriteLine("* *");
Console.WriteLine("* 飞行棋 *");
Console.WriteLine("* *");
Console.WriteLine("****************************");
}
/// <summary>
/// 初始化游戏地图
/// </summary>
static void InitialMap()
{
//用于存储在地图中为地雷的下标
int[] lunckyYurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
int[] landMinc = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
int[] pause = { 9, 27, 60, 93 };//暂停▲
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//4:时空隧道卐
for (int i = 0; i < lunckyYurn.Length; i++)
{
int pos = lunckyYurn[i];
Map[pos] = 1;//eg:当i=0时,pos为6,说明第6+1个位置要放幸运轮盘
}
for (int i = 0; i < landMinc.Length; i++)
{
Map[landMinc[i]] = 2;//同上,简化版。
}
for (int i = 0; i < pause.Length; i++)
{
Map[pause[i]] = 3;//同上,简化版。
}
for (int i = 0; i < timeTunnel.Length; i++)
{
Map[timeTunnel[i]] = 4;//同上,简化版。
}
}
/// <summary>
/// 判断pos坐标上绘制的图案应该
/// </summary>
/// <param name="pos"></param>
static string GetMapString(int pos)
{
//判断A和B是否在当前要花的第i格上。
string result = "";//不要在方法里面绘制,要把该绘制什么作为结果输出
if (playerPos[0] == pos && playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result="<>";
}
else if (playerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.White;
result="Α";
}
else if (playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.White;
result="Β";
}
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.DarkCyan;
result="▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Blue;
result="卍";
break;
}
}
return result;
}
/// <summary>
/// 画游戏地图
/// </summary>
static void DrawMap()
{
//输出图例
Console.WriteLine("幸运轮盘◎;地雷☆;暂停▲;时空隧道卐");
//第一个横行
for (int i=0;i<=29;i++)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();//第一行画完之后话第一个竖列,要在之前换行
//第一个竖行
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
Console.WriteLine(GetMapString(i));
}
//第二个横行
for (int i = 64; i >34; i--)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
//第二个竖行
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(GetMapString(i));
}
//最后一个横行
for (int i = 70; i <= 99; i++)
{
Console.Write(GetMapString(i));
}
//绘制题图时改变之后所有字的颜色,所以需要reset
Console.WriteLine();
Console.ResetColor();
}
}
}
笔记——飞行棋
最新推荐文章于 2024-01-29 13:34:57 发布