C#基础:飞行棋游戏

时间:2022-08-08 15:36:27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Pachee
{
class Program
{
#region 静态字段
// 关卡数量
public static int[] Maps = new int[];
// 玩家坐标
public static int[] PlayerPos = new int[];
// 玩家名称
public static string[] PlayerNames = new string[];
// 判断玩家是否暂停
public static bool[] Flags = new bool[];
#endregion /// <summary>
/// 输出游戏头
/// </summary>
public static void ShowGame()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("***C#基础练习:飞行棋项目***");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("****************************");
}
/// <summary>
/// 接受用户输入的游戏名称,判断是否合法
/// </summary>
/// <returns>游戏名称</returns>
public static string[] InputPlayerNames()
{
PlayerNames[] = "";
PlayerNames[] = "";
Console.ForegroundColor = ConsoleColor.White;
while (PlayerNames[] == "")
{
Console.Write("Please enter the name of game A player: ");
PlayerNames[] = Console.ReadLine().Trim();
if (PlayerNames[] == "")
{
Console.WriteLine("A player name cannot be empty, please enter again.");
continue;
}
break;
}
while (PlayerNames[] == "" || PlayerNames[] == PlayerNames[])
{
Console.Write("Please enter the name of game B player: ");
PlayerNames[] = Console.ReadLine().Trim();
if (PlayerNames[] == "")
{
Console.WriteLine("B player name cannot be empty, please enter again.");
continue;
}
else if (PlayerNames[] == PlayerNames[])
{
Console.WriteLine("The player name cannot be the same as the player A B, please enter again.");
continue;
}
break;
}
return PlayerNames;
}
/// <summary>
/// 初始化地图,改变默认的地图坐标类型
/// 0:方块
/// 1:轮盘
/// 2:地雷
/// 3:暂停
/// 4:隧道
/// </summary>
public static void InitailMap()
{
#region 轮盘
int[] luckTrun = { , , , , , };
for (int i = ; i < luckTrun.Length; i++)
{
Maps[luckTrun[i]] = ;
}
#endregion #region 地雷
int[] landMine = { , , , , , , , , };
for (int i = ; i < landMine.Length; i++)
{
Maps[landMine[i]] = ;
}
#endregion #region 暂停
int[] pause = { , , , };
for (int i = ; i < pause.Length; i++)
{
Maps[pause[i]] = ;
}
#endregion #region 隧道
int[] timeTunnel = { , , , , , , };
for (int i = ; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = ;
}
#endregion
}
/// <summary>
/// 设定当前坐标的类型
/// </summary>
/// <param name="i">坐标</param>
/// <returns>坐标类型</returns>
public static string DrawStringMap(int i)
{
string str = null;
if (PlayerPos[] == PlayerPos[] && PlayerPos[] == i)
{
str = "<>";
}
else if (PlayerPos[] == i)
{
str = "A";
}
else if (PlayerPos[] == i)
{
str = "B";
}
else
{
switch (Maps[i])
{
case :
Console.ForegroundColor = ConsoleColor.Yellow;
str = "□";
break;
case :
Console.ForegroundColor = ConsoleColor.Blue;
str = "◎";
break;
case :
Console.ForegroundColor = ConsoleColor.Green;
str = "☆";
break;
case :
Console.ForegroundColor = ConsoleColor.Red;
str = "▲";
break;
case :
Console.ForegroundColor = ConsoleColor.Cyan;
str = "*";
break;
}
}
return str;
}
/// <summary>
/// 生成所有坐标
/// </summary>
public static void DrawMap()
{
Console.WriteLine("Legend: LuckTrun<◎> landMine<☆> Pause<▲> timeTunnel<*>"); #region 第一橫行
for (int i = ; i < ; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion #region 第一竖行
for (int i = ; i < ; i++)
{
for (int j = ; j <= ; j++)
{
Console.Write(" ");
}
Console.Write(DrawStringMap(i));
Console.WriteLine();
}
#endregion #region 第二橫行
for (int i = ; i >= ; i--)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion #region 第二竖行
for (int i = ; i < ; i++)
{
Console.WriteLine(DrawStringMap(i));
}
#endregion #region 第三橫行
for (int i = ; i <= ; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion
}
/// <summary>
/// 判断坐标是否超出范围
/// </summary>
public static void ChangePos()
{
#region Player A
if (PlayerPos[] < )
{
PlayerPos[] = ;
}
if (PlayerPos[] > )
{
PlayerPos[] = ;
}
#endregion #region Player B
if (PlayerPos[] < )
{
PlayerPos[] = ;
}
if (PlayerPos[] > )
{
PlayerPos[] = ;
}
#endregion
}
/// <summary>
/// 踩到轮盘时,选择功能
/// </summary>
/// <param name="input">玩家的选择</param>
/// <param name="player">玩家标示</param>
public static void PlayerSelect(string input, int player)
{
while (true)
{
if (input == "")
{
Console.WriteLine("Player {0} select and {1} swap places.", PlayerNames[player], PlayerNames[ - player]);
int temp = PlayerPos[player];
PlayerPos[player] = PlayerPos[ - player];
PlayerPos[ - player] = temp;
Console.WriteLine("Swap complete, press any key continue.");
Console.ReadKey(true);
break;
}
else if (input == "")
{
Console.WriteLine("Player {0} select bombing {1}, Player {2} back to 6.", PlayerNames[player], PlayerNames[ - player], PlayerNames[ - player]);
PlayerPos[ - player] -= ;
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("Can only select: 1--Swap places 2--bombing: ");
input = Console.ReadLine();
}
}
}
/// <summary>
/// 进行游戏
/// </summary>
/// <param name="player">玩家标示位</param>
public static void PlayGame(int player)
{
Random r = new Random();
int next = r.Next(, );
Console.WriteLine("{0} press any key to start rolling the dice.", PlayerNames[player]);
Console.ReadKey(true);
Console.WriteLine("{0} Throw out of {1}", PlayerNames[player], next);
PlayerPos[player] += next;
ChangePos();
Console.ReadKey(true);
Console.WriteLine("{0} press any key to start action.", PlayerNames[player]);
Console.ReadKey(true);
Console.WriteLine("{0} action complete.", PlayerNames[player]);
Console.ReadKey(true);
// Player A 有可能踩到: Player B、方块、轮盘、地雷、暂停、隧道
if (PlayerPos[player] == PlayerPos[ - player])
{
Console.WriteLine("Player {0} step on {1}, {2} back to 6.", PlayerNames[player], PlayerNames[ - player], PlayerNames[ - player]);
PlayerPos[ - player] -= ;
Console.ReadKey(true);
}
else
{
switch (Maps[PlayerPos[player]])
{
case :
Console.WriteLine("Player {0} step on Square, safe.", PlayerNames[player]);
Console.ReadKey(true);
break;
case :
Console.WriteLine("Player {0} step on a LuckTrun, please select: 1--Swap places 2--bombing: ", PlayerNames[player]);
string input = Console.ReadLine().Trim();
PlayerSelect(input, player);
Console.ReadKey(true);
break;
case :
Console.WriteLine("Player {0} step on a LandMine, back to 6", PlayerNames[player]);
PlayerPos[player] -= ;
Console.ReadKey(true);
break;
case :
Console.WriteLine("Player {0} step on a Pause, to suspend a round.", PlayerNames[player]);
Console.ReadKey(true);
Flags[player] = true;
break;
case :
Console.WriteLine("Player {0} step on a TimeTunnel, forward 10.", PlayerNames[player]);
PlayerPos[player] += ;
Console.ReadKey();
break;
}
}
ChangePos();
Console.Clear();
DrawMap();
}
static void Main(string[] args)
{
ShowGame();
InputPlayerNames();
Console.WriteLine("Player {0} is A.", PlayerNames[]);
Console.WriteLine("Player {0} is B.", PlayerNames[]);
InitailMap();
DrawMap(); while (PlayerPos[] < && PlayerPos[] < )
{
#region A
if (Flags[] == false)
{
PlayGame();
}
else
{
Flags[] = false;
}
#endregion
#region B
if (Flags[] == false)
{
PlayGame();
}
else
{
Flags[] = false;
}
#endregion
}
#region 判断玩家胜利 if (PlayerPos[] == )
{
Console.Clear();
Console.WriteLine("Player {0} Win.", PlayerNames[]);
}
if (PlayerPos[] == )
{
Console.Clear();
Console.WriteLine("Player {0} Win.", PlayerNames[]);
}
#endregion Console.ReadKey();
}
}
}