C#飞行棋(新手简洁版)

我们要在主函数的顶部写一些全局静态字段 确保能在后续的静态方法中能够获取到这些值和修改

static int[] Maps = new int[100];
 static string[] PlayerName = new string[2];
 static int[] PlayerScore = new int[2];
 static bool[] Flags= new bool[2] {true,true };

 static int[] Maps = new int[100]; 这段代码是设置一个整数类数组方便我们存储后续飞行棋的每个格子和格子所具有的特殊功能 以便于后续的取用 100 代表格子个位

static string[] PlayerName = new string[2];这是存储两个玩家的名字的字符串数组 方便后续调用两个玩家

 static int[] PlayerScore = new int[2];这是用于存储两位玩家所处的格子位数方便后续获取位置并更改

static bool[] Flags= new bool[2] {true,true };存储两位玩家的回合是否进行 后续暂停回合需要用到

主函数

  static void Main(string[] args)
  {
      GameShow();
      Console.WriteLine("请输入玩家1的名字:");
      PlayerName[0] = Console.ReadLine();
    
      while (PlayerName[0] == "")
      {
          Console.WriteLine("玩家A的姓名不能为空,请重新输入");
          PlayerName[0] = Console.ReadLine();
      }
      Console.WriteLine("请输入玩家2的名字:");
      PlayerName[1] = Console.ReadLine();
      while (PlayerName[1] == "")
      {
          Console.WriteLine("玩家B的姓名不能为空,请重新输入");
          PlayerName[0] = Console.ReadLine();
      }
      if ( PlayerName[0] == PlayerName[1] )
      {
          Console.WriteLine("玩家名字不能相同!请重新输入玩家B的名字");
          PlayerName[1] = Console.ReadLine();
      }
     
      Console.WriteLine("按回车键开始");
      Console.ReadLine();
      Console.Clear();

      GameShow();
      Console.WriteLine("玩家A的名字为:{0},玩家B的名字为:{1}", PlayerName[0], PlayerName[1]);
      InitializeTheMap();
      FinallyMap();
      while (PlayerScore [0] < 99 && PlayerScore [1] < 99)
      {
          for (int i = 0;  i < 2;  i++)
          {
              if (Flags[i])
              {
                  Play (i);
              }
              else
              {
                  Flags[i] = true;
              }
              if (PlayerScore[i] == 99)
              {
                  Console.ForegroundColor = ConsoleColor.Blue;
                  Console.WriteLine("玩家{0}赢了玩家{1}", PlayerName[i], PlayerName[1 - i]);
                  break;
              }
          }
      }
      Console.WriteLine("按回车键结束游戏");
      Console.ReadLine();

  }

GameShow();打印欢迎语

//

 Console.WriteLine("请输入玩家1的名字:");
 PlayerName[0] = Console.ReadLine();
          
 while (PlayerName[0] == "")
 {
     Console.WriteLine("玩家A的姓名不能为空,请重新输入");
     PlayerName[0] = Console.ReadLine();
 }
 Console.WriteLine("请输入玩家2的名字:");
 PlayerName[1] = Console.ReadLine();
 while (PlayerName[1] == "")
 {
     Console.WriteLine("玩家B的姓名不能为空,请重新输入");
     PlayerName[0] = Console.ReadLine();
 }
 if ( PlayerName[0] == PlayerName[1] )
 {
     Console.WriteLine("玩家名字不能相同!请重新输入玩家B的名字");
     PlayerName[1] = Console.ReadLine();
 }

 Console.WriteLine("按回车键开始");
 Console.ReadLine();
 Console.Clear();

// 这段代码是用来输入玩家姓名

InitializeTheMap();设置上面的Maps整数数组的格子类型

FinallyMap();方法 打印格子

//

  while (PlayerScore [0] < 99 && PlayerScore [1] < 99)
  {
      for (int i = 0;  i < 2;  i++) 两位玩家
      {
          if (Flags[i])// 上面的Flags已经默认了此回合执行true
          {
              Play (i);
          }
          else 上一回合不执行后,下次回合正常执行需要改为true
          {
              Flags[i] = true;
          }
          if (PlayerScore[i] == 99)
          {
              Console.ForegroundColor = ConsoleColor.Blue;
              Console.WriteLine("玩家{0}赢了玩家{1}", PlayerName[i], PlayerName[1 - i]);
              break;
          }
      }
  }

// 这段代码用来判断回合是否暂停

欢迎语方法代码

 static void GameShow()
 {
     //Console.ForegroundColor控制台字体颜色
     //Console.BackgroundColor控制台背景颜色
     Console.ForegroundColor = ConsoleColor.Yellow;
     Console.WriteLine("************************************");
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine("************************************");
     Console.ForegroundColor = ConsoleColor.Green;
     Console.WriteLine("************************************");
     Console.ForegroundColor = ConsoleColor.White;
     Console.WriteLine("*************飞行棋游戏1.4*************");
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.WriteLine("************************************");
     Console.ForegroundColor = ConsoleColor.DarkMagenta;
     Console.WriteLine("************************************");
     Console.ForegroundColor = ConsoleColor.DarkBlue;
     Console.WriteLine("************************************");
     Console.ForegroundColor = ConsoleColor.White;
 }

static Random random = new Random(); 这是用来重置每次的地图每次地图需要不同

初始化地图

 static void InitializeTheMap()
 {
     int[] LuckDong = new int[5]; // 幸运◎
     int[] landMine = new int[5];// 地雷★
     int l = 0;
     int[] bomb = new int[5]; // 暂停▲
     int b = 0;
     int[] TimeTunnel = new int[5]; // 时光隧穿卍
     int t = 0;
     for (int i = 0; i < 20; i++)
     {
         int num = random.Next(1, 101);

         Thread.Sleep(15);


         if (i < 5)
         {
             LuckDong[i] = num;
         }
         else if (i < 10)
         {
             landMine[l++] = num;
         }
         else if (i < 15)
         {
             bomb[b++] = num;
         }
         else if (i < 20)
         {
             TimeTunnel[t++] = num;
         }
     }
     for (int i = 0; i < LuckDong .Length; i++)
         Maps[LuckDong [i]] = 1;
     for (int i = 0; i < landMine.Length; i++)
         Maps[landMine[i]] = 2;
     for (int i = 0; i < bomb.Length; i++)
         Maps[bomb[i]] = 3;
     for (int i = 0; i < TimeTunnel.Length; i++)
         Maps[TimeTunnel[i]] = 4;
 }

//

int[] LuckDong = new int[5]; // 幸运◎
int[] landMine = new int[5];// 地雷★
int l = 0;
int[] bomb = new int[5]; // 暂停▲
int b = 0;
int[] TimeTunnel = new int[5]; // 时光隧穿卍
int t = 0;
for (int i = 0; i < 20; i++)
{
    int num = random.Next(1, 101);

    Thread.Sleep(15);


    if (i < 5)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值