C#飞行棋项目
学了一周半的C#,根据视频跟着做了一遍飞行棋
// A code block
var foo = 'bar';
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _0720_飞行棋
{
class Program
{
//用静态字段模拟全局变量
public static int[] Maps = new int[100];
//设置玩家A玩家B
public static int[] PlayPos = new int[2];
//储存玩家A玩家B的名字
public static string[] PlayNames = new string[2];
public static bool[] Flags = new bool[2];//Flags[0]是玩家A的标记,默认为false,Flags[1]是玩家B的标记,默认为Flages
static void Main(string[] args)
{
#region 输入玩家姓名
do
{
Console.Clear();
GameShow();
Console.WriteLine("输入玩家A的姓名(不能为空且不能超过8个字符)");
PlayNames[0] = Console.ReadLine();
}
while (PlayNames[0] == "" || PlayNames[0].Length > 8);
do
{
Console.Clear();
GameShow();
Console.WriteLine("输入玩家A的姓名(不能为空且不能超过8个字符)");
Console.WriteLine(PlayNames[0]);
if (PlayNames[0] == PlayNames[1])
{
Console.WriteLine("名字重复,请重新输入");
}
Console.WriteLine("输入玩家B的姓名(不能为空且不能超过8个字符)");
PlayNames[1] = Console.ReadLine();
}
while (PlayNames[1] == "" || PlayNames[1].Length > 8 || PlayNames[0] == PlayNames[1]);
Console.Clear();
#endregion
GameShow();
InitialMap();
DrawMap();
//当玩家A和玩家B一直都没出地图
while (PlayPos[0] <= 99 && PlayPos[1] <= 99)
{
if (Flags[0] == false)
{
PlayGame(0);
}
else
{
Flags[0] = false;
PlayGame(1);
}
if (Flags[1] == false)
{
PlayGame(1);
}
else
{
Flags[1] = false;
PlayGame(0);
}
}
}
/// <summary>
/// 画游戏头
/// </summary>
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("********************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("********************");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("*****");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("飞行棋项目");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("*****");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("********************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("********************");
}
/// <summary>
/// 初始化地图
/// </summary>
public static void InitialMap()
{
//后期将所有的装置限制数量,并随机
int[] luckyturn = new int[7];
RealRandom(ref luckyturn, luckyturn.Length);
for (int i = 0; i < luckyturn.Length; i++)
{
Maps[luckyturn[i]] = 1;
}
int[] landMine = new int[7];//地雷
RealRandom(ref landMine, landMine.Length);
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
int[] pause = new int[7];//暂停
RealRandom(ref pause, pause.Length);
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = new int[7];//时空隧道
RealRandom(ref timeTunnel, timeTunnel.Length);
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
Maps[0] = 0;
}
/// <summary>
/// 画地图
/// </summary>
public static void DrawMap()//□◎☆▲卐
{
Console.WriteLine("\t\t\t\t\t\t\t\tA士兵的名字是: {0}", PlayNames[0]);
Console.WriteLine("\t\t\t\t\t\t\t\tB士兵的名字是: {0}", PlayNames[1]);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("图例:新云轮盘:◎");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" 地雷:☆");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" 暂停:▲");
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write(" 时空隧道:卐");
Console.WriteLine();
#region 第一横行
for (int i = 0; i < 30; i++)
{
MapS(i);
}
#endregion
Console.WriteLine();
#region 第一竖列
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
MapS(i);
Console.WriteLine();
}
#endregion
#region 第二横行
for (int i = 64; i > 34; i--)
{
MapS(i);
}
#endregion
Console.WriteLine();
#region 第二竖列
for (int i = 65; i < 70; i++)
{
MapS(i);
Console.WriteLine();
}
#endregion
#region 第三横行
for (int i = 70; i < 100; i++)
{
MapS(i);
}
#endregion
Console.WriteLine();
}
/// <summary>
/// 画地图辅助
/// </summary>
/// <param name="i">地图块</param>
public static void MapS(int i)
{
if (PlayPos[0] == PlayPos[1] && PlayPos[1] == i)//表示A玩家的坐标和玩家B的坐标相同
{
Console.Write("<>");
}
else if (PlayPos[0] == i)
{
Console.Write("A");
} //A的位置
else if (PlayPos[1] == i)
{
Console.Write("B");
} //B的位置
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("□"); break;
case 1:
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("◎"); break;
case 2:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("☆"); break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("▲"); break;
case 4:
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write("卐"); break;
}
} //其他装置
}
/// <summary>
/// 玩游戏
/// </summary>
public static void PlayGame(int playerNumber)
{
Console.WriteLine("玩家{0}按任意键开始掷骰子:", PlayNames[playerNumber]);
Console.ReadKey(true);
Random r = new Random();
int rNumber = r.Next(1, 7);
PlayPos[playerNumber] += rNumber;
ChangePos();
Console.WriteLine("玩家{0}掷出了{1}", PlayNames[playerNumber], rNumber);
Console.ReadKey(true);
Console.WriteLine("玩家{0}按任意键开始行动", PlayNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("玩家{0}行动完了", PlayNames[playerNumber]);
bool b = true;
while (b)
{
GameRule(playerNumber, ref b);
}
Console.Clear();
GameShow();
DrawMap();
}
/// <summary>
/// 每次坐标变换都要使用
/// </summary>
public static void ChangePos()
{
if (PlayPos[0] < 0)
{
PlayPos[0] = 0;
}
if (PlayPos[0] >= 99)
{
PlayPos[0] = 99;
}
if (PlayPos[1] < 0)
{
PlayPos[1] = 0;
}
if (PlayPos[1] >= 99)
{
PlayPos[1] = 99;
}
}
/// <summary>
/// 游戏规则
/// </summary>
/// <param name="playerNumber">行动的玩家</param>
/// <param name="b">是否二次判断</param>
public static void GameRule(int playerNumber, ref bool b)
{
ChangePos();
if (PlayPos[playerNumber] == PlayPos[1 - playerNumber])
{
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退6格", PlayNames[playerNumber], PlayNames[1 - playerNumber]);
PlayPos[1 - playerNumber] -= 6;
if (PlayPos[0] == PlayPos[1])
{
b = false;
Console.ReadKey(true);
return;
}
Console.WriteLine("按任意键继续");
Console.ReadKey(true);
}
else
{
switch (Maps[PlayPos[playerNumber]])
{
case 0:
Console.WriteLine("玩家{0}踩到了方块,安全", PlayNames[playerNumber]);
b = false;
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择:", PlayNames[playerNumber]);
Console.WriteLine("1:交换位置 2:轰炸对方,使其后退6格");
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.Clear();
GameShow();
DrawMap();
Console.WriteLine("玩家{0}和玩家{1}位置交换成功!!", PlayNames[playerNumber], PlayNames[playerNumber]);
int temp = PlayPos[playerNumber];
PlayPos[playerNumber] = PlayPos[1 - playerNumber];
PlayPos[1 - playerNumber] = temp;
b = false;
Console.WriteLine("按任意键继续");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}选择轰炸玩家{1}", PlayNames[playerNumber], PlayNames[1 - playerNumber]);
PlayPos[1 - playerNumber] -= 6;
ChangePos();
Console.WriteLine("玩家{0}遭到轰炸后退6格", PlayNames[1 - playerNumber]);
GameRule(1 - playerNumber, ref b);
Console.ReadKey(true);
break;
}
else
{
Console.Clear();
GameShow();
DrawMap();
Console.WriteLine("选择错误,请重新输入!!");
Console.WriteLine("1:交换位置 2:轰炸对方,使其后退6格");
input = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayNames[playerNumber]);
PlayPos[playerNumber] -= 6;
break;
case 3:
Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayNames[playerNumber]);
Flags[playerNumber] = true;
b = false;
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("玩家{0}踩到了时空隧道", PlayNames[playerNumber]); //后期改成传送隧道之间随机出现
Console.WriteLine("玩家{0}开启时空之旅!", PlayNames[playerNumber]);
Random r = new Random();
int[] vs = new int[5];
int temp1 = 0;
for (int j = 0; j < 5; j++)
{
for (int i = temp1; i < 100; i++)
{
if (Maps[i] == 4)
{
if (j > 0 && vs[j] != i)
{
vs[j] = i;
temp1 = i+1;
break;
}
else if (j == 0)
{
vs[j] = i;
temp1 = i+1;
break;
}
else
{
j--;
break;
}
}
}
}
int rNumber = r.Next(2, 5);
while(PlayPos[playerNumber] == vs[rNumber])
{
rNumber = r.Next(2, 5);
}
Console.WriteLine("玩家{0}通过时空之旅来到了第{1}个时空隧道", PlayNames[playerNumber],rNumber + 1);
Console.WriteLine("按任意按键继续");
Console.ReadKey(true);
b = false;
break;
}
}
}
/// <summary>
/// 产生随机数量,随机位置的装置
/// </summary>
/// <param name="vs"></param>
/// <param name="length"></param>
public static void RealRandom(ref int[] vs, int length)
{
Random r = new Random();
int rNumber = r.Next(5, length);//装置随机产生的数量
for (int i = 0; i < rNumber; i++)
{
bool b = true;
int nums = r.Next(4, 99);//装置随机出现的位置
for (int j = 0; j < i; j++)
{
if (vs[j] == nums || Maps[nums] == 1 || Maps[nums] == 2 || Maps[nums] == 3 || Maps[nums] == 4 || ((vs[j] - nums < 4) && (nums - vs[j] < 4)))
{
i -= 1;
b = false;
break;
}
}
if (b)
{
vs[i] = nums;
}
}
}
}
}
关于内容上,只有地图的外表是抄视频里的,其他的方法基本上都是自主完成,因为视频里的部分函数不够完善,就改了几个,比如机关的产生位置以及数量原本都不是随机的,经过修改可以产生随机数量,随机位置的装置
原本想设置不同难度的等级,对应不同数量的装置,但工作量对我来说比较大,所以没有施行。
希望各位能指出代码中的不足,我会虚心接受的,十分感谢。