2022.02.13 / Microsoft Visual Studio 2022 / C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 飞行棋_v2._0
{
internal class Program
{
// 用静态字段模拟全局变量
public static int[] GameMap = new int[100];
// 用静态数组存储玩家坐标
public static int[] PlayerPos = new int[2];
// 用字符串数组存储玩家姓名
public static string[] PlayerNames = new string[2];
// 用布尔数组标记玩家是否处于暂停状态
public static bool[] flags = new bool[2];
static void Main(string[] args)
{
// 游戏开始时,显示游戏头
GameShow();
// 输入玩家A的姓名
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("玩家A的姓名:");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.Write("玩家A的姓名不能为空,请重新输入:");
PlayerNames[0] = Console.ReadLine();
}
Console.WriteLine();
// 玩家B为电脑
Console.WriteLine("玩家B的姓名:电脑");
PlayerNames[1] = "电脑";
Console.WriteLine();
Console.WriteLine("请按任意键开始游戏……");
Console.ReadKey(true);
// 玩家的姓名输入完成后,清屏
Console.Clear();
// 初始化地图
InitailGameMap();
// 绘制地图
DrawGameMap();
// 游戏开始。没有玩家到达终点前,游戏会一直进行
while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
{
// 没有踩到暂停
if (flags[0] == false)
{
PlayGame(0);
}
// 上回合踩到了暂停,这回合不能玩,但须变更flags标记,确保下回合可以玩
else
{
flags[0] = false;
}
if (PlayerPos[0] >= 99)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" 恭喜! 玩家 {0} 战胜了 电脑 !", PlayerNames[0]);
break;
}
// 没有踩到暂停
if (flags[1] == false)
{
AutoGame(1);
}
// 上回合踩到了暂停,这回合不能玩,但须变更flags标记,确保下回合可以玩
else
{
flags[1] = false;
}
if (PlayerPos[1] >= 99)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" 很遗憾! 玩家 {0} 输给了 电脑 !", PlayerNames[0]);
break;
}
} // while结尾
Console.WriteLine(" 请按任意键退出游戏……");
Console.ReadKey();
} // Main结尾
/// <summary>
/// 游戏头
/// </summary>
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("| 飞行棋 (人机对战) v2.0 |");
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine(" ");
}
/// <summary>
/// 初始化地图
/// </summary>
public static void InitailGameMap()
{
int[] landMine = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96 }; // 地雷点
for (int i = 0; i < landMine.Length; i++)
{
GameMap[landMine[i]] = 1;
}
int[] pause = { 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98 }; // 暂停点
for (int i = 0; i < pause.Length; i++)
{
GameMap[pause[i]] = 2;
}
int[] lucky = { 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 71, 75, 79, 83, 87, 91, 95 }; // 幸运点
for (int i = 0; i < lucky.Length; i++)
{
GameMap[lucky[i]] = 3;
}
}
/