项目源代码 提取码:3306
备注:本程序基于Microsoft Visual Studio 2019,请选择支持本案例解决方案的编辑工具打开。
视频详解请戳:传智播客C#控制台飞行棋小程序
using System;
using System.ComponentModel;
namespace AeroplaneChessPojo
{
class PlaneChess
{
/// <summary>
/// 地图数组
/// </summary>
public static int[] Maps = new int[100];
/// <summary>
/// 玩家坐标(0 - 玩家1;1 - 玩家2)
/// </summary>
public static int[] PlayerPosition = new int[2];
/// <summary>
/// 玩家昵称(非空字段)
/// </summary>
public static string[] PlayerNickName = new string[2];
/// <summary>
/// 玩家位置标记
///
/// 布尔类型数组元素默认值均为0
/// </summary>
public static bool[] Flags = new bool[2];
/// <summary>
/// [飞行棋]项目测试
///
/// 游戏规则:
/// ☆地雷(后退6格)
/// ▲暂停(暂停一回合)
/// 卐时空隧道(飞跃10格)
/// □普通格子(正常行进且原地不动)
/// ⊙幸运轮盘(双方交换位置或一方轰炸另一方,被轰炸的一方后退6格)
/// 若玩家A踩到了玩家B,则玩家B后退6格(被追上的一方后退6格)
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
TheGameFirst();
PlayerPreparation();
// 双方玩家就绪后重新载入地图
Console.Clear(); // 清屏
TheGameFirst();
Console.WriteLine("系统提示:玩家“{0}”的棋子为A,玩家“{1}”的棋子为B!", PlayerNickName[0], PlayerNickName[1]);
MapInitialization();
DrawMap();
// 双方无任何一方到达终点时继续游戏(终点位置循环判断)
while (PlayerPosition[0] < 99 && PlayerPosition[1] < 99)
{
// A(0)和B(1)循环进行游戏
if (Flags[0] == false)
{
PlayGames(0);
}
else
{
Flags[0] = false;
}
if (PlayerPosition[0] >= 99)
{
Console.WriteLine("\n系统提示:游戏结束,玩家{0}获胜!", PlayerNickName[0]);
break;
}
if (Flags[0] == false)
{
PlayGames(1);
}
else
{
Flags[1] = false;
}
if (PlayerPosition[1] >= 99)
{
Console.WriteLine("\n系统提示:游戏结束,玩家{0}获胜!", PlayerNickName[1]);
break;
}
}
Console.ReadKey();
}
/// <summary>
/// [飞行棋]玩家位置限定
/// </summary>
public static void Translocation()
{
if (PlayerPosition[0] < 0)
{
PlayerPosition[0] = 0;
}
if (PlayerPosition[0] >= 99)
{
PlayerPosition[0] = 99;
}
if (PlayerPosition[1] < 0)
{
PlayerPosition[1] = 0;
}
if (PlayerPosition[1] >= 99)
{
PlayerPosition[1] = 99;
}
}
/// <summary>
/// [飞行棋]开始游戏
/// </summary>
/// <param name="playerNumber">玩家编号</param>
private static void PlayGames(int playerNumber)
{
Random rd = new Random();
int rdNum = rd.Next(1, 7); // [1, 7)
Console.WriteLine("\n提示:玩家{0}按任意键开始掷骰子!", PlayerNickName[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("提示:玩家{0}投掷结果为{1}!", PlayerNickName[playerNumber], rdNum);
PlayerPosition[playerNumber] += rdNum;
Translocation(); // 玩家位置限定
Console.ReadKey(true);
Console.WriteLine("提示:玩家{0}按任意键开始行动!", PlayerNickName[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("提示:玩家{0}行动完毕!", PlayerNickName[playerNumber]);
Console.ReadKey(true);
// 玩家A追上玩家B时
if (PlayerPosition[playerNumber] == PlayerPosition[1 - playerNumber])
{
Console.WriteLine("\n提示:玩家{0}追上玩家{1},玩家{2}后退6格!", PlayerNickName[playerNumber], PlayerNickName[1 - playerNumber], PlayerNickName[1 - playerNumber]);
PlayerPosition[1 - playerNumber] -= 6;
Translocation(); // 玩家位置限定
Console.ReadKey(true);
}
else // 玩家A未追上玩家B而处于其他位置时
{
switch (Maps[PlayerPosition[playerNumber]])
{
case 0:
Console.WriteLine("\n提示:玩家{0}正处于方块之上,正常行进且原地不动!", PlayerNickName[playerNumber]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("\n提示:玩家{0}正处于幸运轮盘之上,请行使您的超能力!(1-大转移;2-高空轰炸)", PlayerNickName[playerNumber]);
string pleaseSelect = Console.ReadLine();
while (true)
{
if (pleaseSelect.Equals("1"))
{
Console.WriteLine("提示:玩家{0}行使大转移,双方位置交换!", PlayerNickName[playerNumber]);
int temp = PlayerPosition[playerNumber];
PlayerPosition[playerNumber] = PlayerPosition[1 - playerNumber];
PlayerPosition[1 - playerNumber] = temp;
Console.ReadKey(true);
Console.WriteLine("提示:大转移已生效,请按任意键继续!");
Console.ReadKey(true);
break;
}
else if (pleaseSelect.Equals("2"))
{
Console.WriteLine("提示:玩家{0}行使高空轰炸,玩家{1}后退6格!", PlayerNickName[playerNumber], PlayerNickName[1 - playerNumber]);
PlayerPosition[1 - playerNumber] -= 6;
Translocation(); // 玩家位置限定
Console.ReadKey(true);
Console.WriteLine("提示:高空轰炸已生效,请按任意键继续!");
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("\n提示:幸运轮盘不存在该项能力哦,换一个试试吧(1-大转移;2-高空轰炸)!");
Console.Write("请行使您的超能力:");
pleaseSelect = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("\n提示:玩家{0}正处于地雷之上,行进坐标后退6格!", PlayerNickName[playerNumber]);
PlayerPosition[playerNumber] -= 6;
Translocation(); // 玩家位置限定
Console.ReadKey(true);
break;
case 3:
Console.WriteLine("\n提示:玩家{0}正处于暂停之上,单回合不可移动!", PlayerNickName[playerNumber]);
Flags[playerNumber] = true; // 玩家处于暂停方块时
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("\n提示:玩家{0}正处于时空隧道之上,飞跃10格!", PlayerNickName[playerNumber]);
PlayerPosition[playerNumber] += 10;
Translocation(); // 玩家位置限定
Console.ReadKey(true);
break;
} // switch-case
} // if-else
// 清屏并重新载入地图
Translocation(); // 玩家位置限定
Console.Clear();
DrawMap();
}
/// <summary>
/// [飞行棋]玩家准备
/// </summary>
private static void PlayerPreparation()
{
#region 玩家设置昵称
Console.WriteLine("------------------------------------------------------------");
Console.Write("当前步骤:玩家A设置游戏昵称:");
PlayerNickName[0] = Console.ReadLine();
while ("".Equals(PlayerNickName[0]) || string.IsNullOrWhiteSpace(PlayerNickName[0])) // 玩家昵称非空判定
{
if ("".Equals(PlayerNickName[0]))
{
Console.WriteLine("\n系统提示:玩家昵称不能为空!");
Console.Write("当前步骤:玩家A设置游戏昵称:");
PlayerNickName[0] = Console.ReadLine();
}
else
{
Console.WriteLine("\n系统提示:玩家昵称不支持空白格!");
Console.Write("当前步骤:玩家A设置游戏昵称:");
PlayerNickName[0] = Console.ReadLine();
}
}
Console.WriteLine("\n系统提示:玩家A已就绪!");
Console.WriteLine("\n------------------------------------------------------------\n");
Console.Write("当前步骤:玩家B设置游戏昵称:");
PlayerNickName[1] = Console.ReadLine();
// 玩家昵称非空 & 唯一判定
while ("".Equals(PlayerNickName[1]) || PlayerNickName[0].Equals(PlayerNickName[1]) || string.IsNullOrWhiteSpace(PlayerNickName[1]))
{
if ("".Equals(PlayerNickName[1]))
{
Console.WriteLine("\n系统提示:玩家昵称不能为空!");
Console.Write("当前步骤:玩家B设置游戏昵称:");
PlayerNickName[1] = Console.ReadLine();
}
else if (PlayerNickName[0].Equals(PlayerNickName[1]))
{
Console.WriteLine("\n系统提示:玩家B的昵称不能与玩家A的昵称一致!");
Console.Write("当前步骤:玩家B设置游戏昵称:");
PlayerNickName[1] = Console.ReadLine();
}
else
{
Console.WriteLine("\n系统提示:玩家昵称不支持空白格!");
Console.Write("当前步骤:玩家B设置游戏昵称:");
PlayerNickName[1] = Console.ReadLine();
}
}
Console.WriteLine("\n系统提示:玩家B已就绪!");
#endregion
}
/// <summary>
/// [飞行棋]游戏地图
/// </summary>
private static void DrawMap()
{
Console.WriteLine("\n------------------------------------------------------------\n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("图例:幸运轮盘:⊙;地雷:☆;暂停:▲;时空隧道:卐");
Console.WriteLine();
#region 绘制第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine(); // 绘制完第一横行后换行
#endregion
#region 绘制第二纵行
for (int i = 30; i < 35; i++)
{
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
Console.Write(DrawStringMap(i));
Console.WriteLine();
}
#endregion
#region 绘制第三横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion
#region 绘制第四纵行
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawStringMap(i));
}
#endregion
#region 绘制第五横行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
#endregion
}
/// <summary>
/// [飞行棋]地图方块
/// </summary>
/// <param name="i">地图下标</param>
public static string DrawStringMap(int i)
{
string str = "";
if (PlayerPosition[0] == PlayerPosition[1] && PlayerPosition[0] == i)
{
str = "<>"; // <>表示两位玩家均处于同一位置
}
else if (PlayerPosition[0] == i)
{
str = "A";
}
else if (PlayerPosition[1] == i)
{
str = "B";
}
else
{
switch (Maps[i]) // 为地图填充游戏方格
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Green;
str = "⊙";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Red;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.DarkBlue;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Yellow;
str = "卐";
break;
} // switch-case
} // if-esle if
return str;
}
/// <summary>
/// [飞行棋]地图初始化
/// </summary>
private static void MapInitialization()
{
int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; // 幸运轮盘
for (int i = 0; i < luckyturn.Length; i++)
{
// 使用幸运轮盘下标替换地图数组下标,使其由0变为1,此时地图数组中存在两种值(0,1)
int index = luckyturn[i];
Maps[index] = 1;
}
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; // 地雷
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
int[] pause = { 9, 27, 60, 93 }; // 暂停
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; // 时空隧道
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
}
/// <summary>
/// [飞行棋]项目头
/// </summary>
private static void TheGameFirst()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("############################################################");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("************************************************************");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("************************************************************");
Console.WriteLine("**************************[飞行棋]**************************");
Console.WriteLine("************************************************************");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("************************************************************");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("############################################################");
Console.WriteLine();
}
}
}