zoukankan      html  css  js  c++  java
  • 骑士飞行棋第三版(上色)

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace 骑士飞行棋
      8 {
      9     class Program
     10     {
     11 
     12         //在下面的数组存储我们游戏地图各个关卡
     13         //数组的小标为0的元素对应地图上的第一格   下标为1的元素对应第二格。。。下标为n的元素对应n+1格
     14         //在数组中用 1.表格幸运轮盘◎
     15         //           2.地雷☆
     16         //           3.暂停▲
     17         //           4.时空隧道※
     18         //           0.表示普通□
     19         static int[] Map = new int[100];//设置地图格数并为每格赋值初始值为0
     20         static int[] playerPos = { 0, 0 };//playerPos[0]存玩家A的坐标,playerPos[1]存玩家B的坐标 ps:注意这里等于号后面的是{},并不是[],只表示数组长度为2,分别都为零→ static int[] playerPos = new int[1];
     21         static string[] names = new string[2]; //names[0]存玩家A的姓名,names[1]存玩家B的姓名
     22         static bool[] isStop = { false, false };//isStop[0]表示A是否上一次走到了暂停,isStop[1]表示B上一次是否走到了暂停,如果走到暂停则设置其值为true
     23         #region//主程序
     24         static void Main(string[] args)
     25         {
     26 
     27 
     28 
     29 
     30 
     31 
     32             ShowUI();//显示游戏名称
     33             Console.WriteLine("请输入玩家A的姓名:");//输出提示
     34             names[0] = Console.ReadLine();//把玩家A的姓名赋值给names[0]
     35             //判断用户输入的内容是否为空,如果为空,则让用户重新输入
     36             while (names[0] == "")//只要用户输入为空则执行下面的循环
     37             {
     38                 Console.WriteLine("姓名不能为空,请重新输入玩家A的姓名:");//提示不能输入空值
     39                 names[0] = Console.ReadLine();//把玩家A的姓名赋值给names[0]
     40 
     41             }
     42             Console.WriteLine("请输入玩家B的姓名:"); //输出提示
     43             names[1] = Console.ReadLine();//把玩家B的姓名赋值给names[1]
     44             while ((names[1] == "") || (names[0] == names[1]))//只要用户输入空值或者与玩家A的姓名重复则执行下面的循环
     45             {
     46                 if (names[1] == "")//如果玩家B的姓名为空则
     47                 {
     48                     Console.WriteLine("姓名不能为空,请重新输入玩家B的姓名:");
     49                 }
     50                 else//如果玩家B的姓名与玩家A的姓名重复则
     51                 {
     52                     Console.WriteLine("姓名不能重复,请重新输入玩家B的姓名:");
     53                 }
     54                 names[1] = Console.ReadLine();//把玩家B的姓名赋值给names[1]
     55             }
     56             Console.Clear();//清屏
     57             ShowUI();//显示游戏名称
     58             Console.WriteLine("对战开始......");//输出提示
     59             Console.WriteLine("{0}用A来表示", names[0]);//输出玩家A的姓名
     60             Console.WriteLine("{0}用B来表示", names[1]);//输出玩家B的姓名
     61             Console.WriteLine("如果AB同一位置,用<>来表示");//输出提示
     62             InitialMap();//初始化地图
     63             DrawMap();//绘制地图
     64             Console.WriteLine("开始游戏......");
     65 
     66             //这个循环中让玩家A和玩家B轮流掷骰子    当玩家A或者玩家B的坐标>=99时,则结束循环
     67             //那循环条件就是
     68             //产生一个m到n之间的随机数
     69             //Random r = new Random();
     70             //int i = r.Next(m,n+1);
     71             while (playerPos[0] < 99 && playerPos[1] < 99)//判断胜利条件,如果没有胜利,程序继续循环运行
     72             {
     73                 if (isStop[0] == false)
     74                 {
     75 
     76                     Action(0);//玩家A掷骰子
     77                 }
     78                 else
     79                 {
     80                     //说明 isStop == true
     81                     isStop[0] = false;
     82                 }
     83 
     84                 if (playerPos[0] >= 99)//如果玩家A坐标为最后一格,则退出循环,胜利
     85                 {
     86                     break; //退出循环
     87                 }
     88 
     89                 if (isStop[1] == false)
     90                 {
     91                     Action(1);//玩家B掷骰子
     92                 }
     93                 else
     94                 {
     95                     //说明 isStop == true
     96                     isStop[1] = false;
     97                 }
     98 
     99             }
    100 
    101             //判断谁胜利,谁失败
    102             Console.Clear();//清屏
    103             ShowUI();//绘制游戏开头画面
    104             if (playerPos[0] >= 99)
    105             {
    106                 Console.BackgroundColor = ConsoleColor.White; //设置背景色
    107                 Console.ForegroundColor = ConsoleColor.DarkGreen; //设置前景色,即字体颜色
    108                 Console.WriteLine();
    109                 Console.WriteLine("************ {0} 胜利了!!!!************", names[0]);//提示玩家A胜利
    110                 Console.ResetColor(); //将控制台的前景色和背景色设为默认值
    111             }
    112             else
    113             {
    114                 Console.BackgroundColor = ConsoleColor.White; //设置背景色
    115                 Console.ForegroundColor = ConsoleColor.DarkGreen; //设置前景色,即字体颜色
    116                 Console.WriteLine();
    117                 Console.WriteLine("************ {0} 胜利了!!!!************", names[1]);//提示玩家B胜利
    118                 Console.ResetColor(); //将控制台的前景色和背景色设为默认值
    119             }
    120 
    121             Console.ReadKey();//等待
    122         }
    123         #endregion
    124 
    125         /// <summary>
    126         /// A或B掷骰子的方法
    127         /// </summary>
    128         /// <param name="playerNumber">A掷骰子传0过来,B掷骰子传1过来</param>
    129         static void Action(int playerNumber)
    130         {
    131             //playerNumber中存的就是当前玩家 姓名 坐标 是否暂停 这三个数组的下标
    132             //1-playerNumber就是对方的 姓名 坐标 是否暂停 的下标 
    133             //当前0,对方1
    134             //当前1,对方0
    135 
    136             #region 掷骰子
    137             Random r = new Random();//r是产生随机数用的
    138             int step = 0;//用于存放临时产生的随机数
    139             string msg = "";//用于存储用户踩到某关卡,输出的话
    140             Console.WriteLine("{0}按任意键开始掷骰子......", names[playerNumber]);
    141             //Console.ReadKey(true);//不显示输入的按键并等待
    142             //step = r.Next(1, 7);//产生一个1-6之间的随机整数(掷骰子结果)
    143             //取消上面的正常代码,改作弊代码:
    144             ConsoleKeyInfo rec = Console.ReadKey(true);//ConsoleKeyInfo类似于类型(即int,string等等的类型),这里是ReadKey的类型,此行代码意思:把键盘动作赋值给rec
    145             step = r.Next(1, 7);//产生一个1-6之间的随机整数(掷骰子结果)
    146             if (rec.Key == ConsoleKey.Tab && rec.Modifiers == (ConsoleModifiers.Control | ConsoleModifiers.Shift))//如果键盘动作为组合键:Ctrl+Shift+Tab
    147             {
    148                 ConsoleKeyInfo cc = Console.ReadKey();//ConsoleKeyInfo类似于类型(即int,string等等的类型),这里是ReadKey的类型,此行代码意思:把键盘动作赋值给cc
    149                 if (cc.Key == ConsoleKey.F1)//如果键盘动作为F1
    150                 {
    151                     step = ReadInt(1, 100);//选择1至100间的数为掷骰子结果
    152                 }
    153             }
    154             else
    155             {
    156 
    157             }
    158 
    159             Console.WriteLine("{0}掷出了:{1}", names[playerNumber], step);//输出当前玩家(names[playerNumber])掷骰子结果(随机数step)
    160             Console.WriteLine("按任意键开始行动......");
    161             Console.ReadKey(true);//不显示输入的按键并等待
    162             playerPos[playerNumber] = playerPos[playerNumber] + step;//注意,一旦坐标发生改变,就要判断,坐标值是否>99或者<0
    163             CheckPos();// 进行玩家A和玩家B坐标越界的判断
    164 
    165             if (playerPos[0] == playerPos[1])//当前玩家踩到对方玩家
    166             {
    167                 playerPos[1-playerNumber] = 0;//对方玩家坐标归零
    168                 msg = string.Format("{0}踩到了{1},{1}退回原点", names[playerNumber], names[1 - playerNumber]);//string.Format方法提供了不需即时输出又能实现占位符功能
    169             }
    170             else
    171             {
    172                 //没踩到,要判断当前玩家现在所在的位置是否有其他关卡
    173                 switch (Map[playerPos[playerNumber]])
    174                 {
    175                     case 0:
    176                         //普通,没有效果
    177                         msg = "";
    178                         break;
    179                     case 1:
    180                         //走到了幸运轮盘关卡
    181                         Console.Clear();//清屏
    182                         DrawMap();//绘制地图
    183                         Console.WriteLine("{0}走到了幸运轮盘,请选择运气?", names[playerNumber]);
    184                         Console.WriteLine("1--交换位置 2--轰炸对方");
    185                         int userSelect = ReadInt(1, 2);
    186                         if (userSelect == 1)//选择1,要与对方交换位置
    187                         {
    188                             int temp = playerPos[0];
    189                             playerPos[0] = playerPos[1];
    190                             playerPos[1] = temp;
    191                             msg = string.Format("{0}选择了与对方交换位置!", names[playerNumber]);//string.Format方法提供了不需即时输出又能实现占位符功能
    192                         }
    193                         else//选择2,轰炸对方
    194                         {
    195                             playerPos[1 - playerNumber] = playerPos[1 - playerNumber] - 6;//对方玩家坐标退6格
    196                             CheckPos();//检测退六格后有没有坐标越界,有则纠正
    197                             msg = string.Format("{0}轰炸了{1},{1}退6格!", names[playerNumber], names[1 - playerNumber]);//string.Format方法提供了不需即时输出又能实现占位符功能
    198                         }
    199                         break;
    200                     case 2:
    201                         //踩到了地雷
    202                         playerPos[playerNumber] = playerPos[playerNumber] - 6;//退六格
    203                         CheckPos();//检测退六格后有没有坐标越界,有则纠正
    204                         msg = string.Format("{0}踩到了地雷,退六格", names[playerNumber]);//string.Format方法提供了不需即时输出又能实现占位符功能
    205                         break;
    206                     case 3:
    207                         //暂停一次
    208                         isStop[playerNumber] = true;//赋值给isStop[playerNumber]为true,让下回合暂停
    209                         msg = string.Format("{0}走到红灯,下次暂停一次!", names[playerNumber]);//string.Format方法提供了不需即时输出又能实现占位符功能
    210                         break;
    211                     case 4:
    212                         //时空隧道
    213                         playerPos[playerNumber] = playerPos[playerNumber] + 10;//前进10格
    214                         CheckPos();//检测前进10格后有没有坐标越界,有则纠正
    215                         msg = string.Format("{0}进入时空隧道,爽死了,进10步!", names[playerNumber]);//string.Format方法提供了不需即时输出又能实现占位符功能
    216                         break;
    217                 }
    218             }
    219 
    220             Console.Clear();//清屏
    221             DrawMap();//绘制地图
    222             if (msg != "")//如果msg不为空,则输出msg
    223             {
    224                 Console.WriteLine(msg);
    225             }
    226             Console.WriteLine("{0}掷出了{1},行动完成!", names[playerNumber], step);//step掷骰子结果
    227             Console.WriteLine("****************玩家A和玩家B的位置如下****************");
    228             Console.WriteLine("{0}的位置为:{1}", names[0], playerPos[0] + 1);
    229             Console.WriteLine("{0}的位置为:{1}", names[1], playerPos[1] + 1);
    230             Console.WriteLine("按任意键继续......");
    231             Console.ReadKey();
    232             #endregion
    233         }
    234 
    235         #region//绘制飞行棋的开头画面
    236         /// <summary>
    237         /// 用于绘制飞行棋的名称
    238         /// </summary>
    239         static void ShowUI()//显示游戏名称的方法 
    240         {
    241             Console.BackgroundColor = ConsoleColor.Blue; //设置背景色
    242             Console.ForegroundColor = ConsoleColor.White; //设置前景色,即字体颜色
    243             Console.WriteLine();
    244             Console.WriteLine("******************************************");
    245             Console.WriteLine("*                                        *");
    246             Console.WriteLine("*         骑  士   飞   行  棋           *");
    247             Console.WriteLine("*                                        *");
    248             Console.WriteLine("******************************************");
    249             Console.ResetColor(); //将控制台的前景色和背景色设为默认值
    250         }
    251         #endregion
    252 
    253         /// <summary> 
    254         /// 进行玩家A和玩家B坐标越界的判断
    255         /// </summary>
    256         static void CheckPos()//进行玩家A和玩家B坐标越界的判断
    257         {
    258             for (int i = 0; i <= 1; i++)
    259             {
    260                 if (playerPos[i] > 99)
    261                 {
    262                     playerPos[i] = 99;
    263                 }
    264                 if (playerPos[i] < 0)
    265                 {
    266                     playerPos[i] = 0;
    267                 }
    268             }
    269         }
    270 
    271         #region//对地图中的关卡进行初始化
    272         /// <summary>
    273         /// 对地图中的关卡进行初始化
    274         /// </summary>
    275         static void InitialMap()
    276         {
    277             //用于存储在地图中为地雷的下标
    278             int[] luckyTurn = { 6, 23, 40, 55, 69, 83, 98 };//幸运轮盘1
    279             int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷2
    280             int[] pause = { 9, 27, 60, 93 };//暂停的坐标3
    281             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道※4
    282 
    283             for (int i = 0; i < luckyTurn.Length; i++)//循环,从零开始/到luckyTurn的数组长度结束/每循环运行一次i增加1
    284             {
    285                 int pos = luckyTurn[i];//赋值给pos
    286                 Map[pos] = 1;// 地图数组[下标]=1,把地雷的下标赋值给地图
    287             }
    288             for (int i = 0; i < landMine.Length; i++)//循环,从零开始/到landMine的数组长度结束/每循环运行一次i增加1
    289             {
    290                 Map[landMine[i]] = 2;//把地雷的下标赋值给地图
    291             }
    292             for (int i = 0; i < pause.Length; i++)//循环,从零开始/到pause的数组长度结束/每循环运行一次i增加1
    293             {
    294                 Map[pause[i]] = 3;//把暂停的下标赋值给地图
    295             }
    296             for (int i = 0; i < timeTunnel.Length; i++)//循环,从零开始/到timeTunnel的数组长度结束/每循环运行一次i增加1
    297             {
    298                 Map[timeTunnel[i]] = 4;//把时空隧道的下标赋值给地图
    299             }
    300         }
    301         #endregion
    302 
    303         /// <summary>
    304         /// 获得第pos坐标上应该绘制的图案
    305         /// </summary>
    306         /// <param name="pos">要绘制的坐标</param>
    307         /// <returns></returns>
    308         static string GetMapString(int pos)//获得第pos坐标上应该绘制的图案 ps:DrawMap()方法需要用到该方法来判断并输出图案
    309         {
    310             string result = "";
    311             //判断A和B是否在当前要画的第i格上,是则先绘制A和B的位置
    312             if (playerPos[0] == pos && playerPos[1] == pos)
    313             {
    314                 Console.ForegroundColor = ConsoleColor.Yellow;//更改输出的字符颜色为黄色
    315                 result = "<>";
    316             }
    317             else if (playerPos[0] == pos)//判断A在当前画的格上
    318             {
    319                 Console.ForegroundColor = ConsoleColor.Yellow;//更改输出的字符颜色为黄色
    320                 result = "A";
    321             }
    322             else if (playerPos[1] == pos)//判断B在当前画的格上
    323             {
    324                 Console.ForegroundColor = ConsoleColor.Yellow;//更改输出的字符颜色为黄色
    325                 result = "B";
    326             }
    327             else
    328             {
    329                 //绘制除开A和B位置的地图
    330                 switch (Map[pos])//以外层循环到的地图格数为判定
    331                 {
    332                     case 0:
    333                         Console.ForegroundColor = ConsoleColor.White;
    334                         result = "";//如果为0则绘制□,正常地图格
    335                         break;//退出循环
    336                     case 1:
    337                         Console.ForegroundColor = ConsoleColor.Red;
    338                         result = "";//如果为1则绘制◎,幸运轮盘
    339                         break;//退出循环
    340                     case 2:
    341                         Console.ForegroundColor = ConsoleColor.Green;
    342                         result = "";//如果为2则绘制☆,地雷
    343                         break;//退出循环
    344                     case 3:
    345                         Console.ForegroundColor = ConsoleColor.Blue;
    346                         result = "";//如果为3则绘制▲,暂停
    347                         break;//退出循环
    348                     case 4:
    349                         Console.ForegroundColor = ConsoleColor.Magenta;
    350                         result = "";//如果为4则绘制※,时空隧道
    351                         break;//退出循环
    352                 }
    353             }
    354             return result;
    355         }
    356 
    357         static void DrawMap()
    358         {
    359             Console.BackgroundColor = ConsoleColor.DarkYellow; //设置背景色
    360             Console.ForegroundColor = ConsoleColor.White; //设置前景色,即字体颜色
    361             //string value = "图例: 幸运轮盘:◎  地雷:☆  暂停:▲  时空隧道:※";
    362             //Console.WriteLine(value.PadRight(Console.WindowWidth - value.Length)); //设置一整行的背景色
    363             Console.WriteLine("图例: 幸运轮盘:◎  地雷:☆  暂停:▲  时空隧道:※");
    364             Console.ResetColor(); //将控制台的前景色和背景色设为默认值
    365             //画第一行 绘制下标从0-29格的地图
    366             for (int i = 0; i <= 29; i++)
    367             {
    368                 //判断A和B是否在当前要画的第i格上,是则先绘制A和B的位置
    369                 Console.Write(GetMapString(i));
    370             }
    371             //绘制第一行完毕
    372             Console.Write("
    ");//换行
    373             //绘制第一列
    374             for (int i = 30; i <= 34; i++)
    375             {
    376                 //绘制29个双空格
    377                 for (int j = 0; j < 29; j++)
    378                 {
    379                     Console.Write("  ");
    380                 }
    381                 Console.WriteLine(GetMapString(i));
    382             }
    383             //绘制第二行
    384             for (int i = 64; i >= 35; i--)
    385             {
    386                 Console.Write(GetMapString(i));
    387             }
    388             Console.Write("
    ");//换行
    389             //绘制第二列
    390             for (int i = 65; i <= 69; i++)
    391             {
    392                 Console.WriteLine(GetMapString(i));
    393             }
    394             //绘制第三行
    395             for (int i = 70; i <= 99; i++)
    396             {
    397                 Console.Write(GetMapString(i));
    398             }
    399             Console.WriteLine();
    400             Console.ResetColor();
    401         }
    402 
    403         #region//产生随机数方法???判断是否为一或者二的整数
    404         static int ReadInt()//产生一个整数
    405         {
    406             int i = ReadInt(int.MaxValue, int.MinValue);
    407             return i;
    408         }
    409 
    410         static int ReadInt(int min, int max)//产生min--max 之间的数
    411         {
    412             while (true)
    413             {
    414                 try
    415                 {
    416                     int number = Convert.ToInt32(Console.ReadLine());
    417                     if (number < min || number > max)
    418                     {
    419                         Console.WriteLine("只能输入{0}--{1}之间的数字,请重新输入", min, max);
    420                         continue;//跳出本次循环,继续下次循环
    421                     }
    422                     return number;//退出循环,并返回number值
    423                 }
    424                 catch
    425                 {
    426                     Console.WriteLine("只能输入数字,请重新输入!");
    427                 }
    428             }
    429         }
    430         #endregion
    431 
    432     }
    433 }
  • 相关阅读:
    day09
    初识socket
    java正则表达式
    Servlet 3.0 新特性详解
    spring利用PropertiesFactoryBean管理属性配置文件properties
    MyBatis获取插入记录的自增主键
    深入学习理解java-ThreadLocal
    Mybatis批量执行语句
    MyBatis使用二级缓存
    编码的理解
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/5041853.html
Copyright © 2011-2022 走看看