zoukankan      html  css  js  c++  java
  • C# 飞行棋源代码

      1  using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 
      6 namespace 飞行棋
      7 {
      8 
      9     class Program
     10     {
     11         public static int[] maps = new int[100]; //声明一个静态数组 用来存储玩家A跟玩家B的坐标
     12         //两个玩家的坐标
     13         static int[] Players = new int[2];
     14         //存储两个玩家的姓名
     15         static string[] playerNames = new string[2];
     16         //两个玩家的标记,拥有某一个玩家暂停 
     17         static bool[] GamePause = new bool[2];
     18         static void Main(string[] args)
     19         {
     20             GameShow();//调用游戏提示信息
     21             #region 提示用户输入玩家姓名及姓名的判断
     22             Console.WriteLine("请输入玩家A的姓名:");
     23             playerNames[0] = Console.ReadLine();
     24             while (playerNames[0] == "")
     25             {
     26                 Console.WriteLine("玩家A的名字不能为空,请重新输入");
     27                 playerNames[0] = Console.ReadLine();
     28             }
     29             Console.WriteLine("请输入玩家B的姓名:");
     30             playerNames[1] = Console.ReadLine();
     31             while (playerNames[1] == "" || playerNames[1] == playerNames[0])
     32             {
     33                 if (playerNames[1] == "")
     34                 {
     35                     Console.WriteLine("玩家B的名字不能为空,请重新输入");
     36                     playerNames[1] = Console.ReadLine();
     37                 }
     38                 else
     39                 {
     40                     Console.WriteLine("玩家B的名字不能与玩家A的名字相同,请重新输入");
     41                     playerNames[1] = Console.ReadLine();
     42                 }
     43             }
     44             #endregion
     45             Console.Clear();//玩家姓名输入OK 清屏
     46             GameShow();
     47             Console.WriteLine("{0}的士兵用A表示", playerNames[0]);
     48             Console.WriteLine("{0}的士兵用B表示", playerNames[1]);
     49             GameMap();//调用地图
     50             ShowMap();//画图
     51             #region 玩游戏 
     52             while (Players[0] < 99 && Players[1] < 99)
     53             // 当玩家A 和玩家B 么有一个人到达终点的时候,两个玩家继续玩游戏
     54             {
     55                 if (GamePause[0] == false)
     56                 {
     57                     PlayGame(0);
     58                 }
     59                 else
     60                 {
     61                     GamePause[0] = false;
     62                 }
     63                 if (Players[0] >= 99)
     64                 {
     65                     Console.WriteLine("玩家{0}无耻的赢了玩家{1}", playerNames[0], playerNames[1]);
     66                     break;
     67                 }
     68                 if (GamePause[1] == false)
     69                 {
     70                     PlayGame(1);
     71                 }
     72                 else
     73                 {
     74                     GamePause[1] = false;
     75                 }
     76                 if (Players[1] >= 99)
     77                 {
     78                     Console.WriteLine("玩家{0}无耻的赢了玩家{1}", playerNames[1], playerNames[0]);
     79                     break;
     80                 }
     81             }//while
     82             #endregion
     83             Console.ReadKey();//等待
     84         }
     85         /// <summary>
     86         /// 两个玩家玩游戏
     87         /// </summary>
     88         public static void PlayGame(int playNumber)
     89         {
     90             Random r = new Random();
     91             int rNumber = r.Next(1, 7);
     92             Console.WriteLine("玩家{0}按任意键开始郑骰子", playerNames[playNumber]);
     93             Console.ReadKey(true);
     94             Console.WriteLine("玩家{0}掷骰子,掷出了{1}", playerNames[playNumber], rNumber);
     95             Players[playNumber] += rNumber;
     96             Console.ReadKey(true);
     97             Console.WriteLine("玩家{0}:请按任意键开始移动!!!", playerNames[playNumber]);
     98             Console.ReadKey(true);
     99             Console.WriteLine("玩家{0}:移动完了", playerNames[playNumber]);
    100             Console.ReadKey(true);
    101             // 玩家A有可能踩到了B ,空格,幸运轮盘,地雷,暂停,时空隧道
    102             if (Players[playNumber] == Players[1 - playNumber])
    103             {
    104                 Console.WriteLine("{0}玩家踩到了{1}玩家,{2}玩家退6格", playerNames[playNumber], playerNames[1 - playNumber], playerNames[1 - playNumber]);
    105                 Players[1 - playNumber] -= 6;
    106                 Console.ReadKey(true);
    107             }
    108             else // 玩家A踩到了关卡
    109             {
    110                 // 玩家的坐标
    111                 changePots();
    112                 switch (maps[Players[playNumber]])
    113                 {
    114                     case 0:
    115                         Console.WriteLine("玩家{0}踩到了方块,安全", playerNames[playNumber]);
    116                         Console.ReadKey(true);
    117                         break;
    118 
    119                     case 1:
    120                         Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择:1--交换位置 2--轰炸对方", playerNames[playNumber]);
    121                         string input = Console.ReadLine();
    122                         while (true)
    123                         {
    124                             if (input == "1")
    125                             {
    126                                 Console.WriteLine("玩家{0}选择与玩家{1}交换位置", playerNames[playNumber], playerNames[1 - playNumber]);
    127                                 Console.ReadKey(true);
    128                                 int temp = Players[playNumber];
    129                                 Players[playNumber] = Players[1 - playNumber];
    130                                 Players[1 - playNumber] = temp;
    131                                 Console.WriteLine("位置交换完成!!!按任意键继续游戏!!!");
    132                                 Console.ReadKey(true);
    133                                 break;
    134                             }
    135                             else if (input == "2")
    136                             {
    137                                 Console.WriteLine("玩家{0}选择了轰炸玩家{1},玩家{2}倒退6格", playerNames[playNumber], playerNames[1 - playNumber], playerNames[1 - playNumber]);
    138                                 Console.ReadKey(true);
    139                                 Players[1 - playNumber] -= 6;
    140                                 Console.WriteLine("玩家B倒退6格", playerNames[1 - playNumber]);
    141                                 break;
    142                             }
    143                             else
    144                             {
    145                                 Console.WriteLine("只能输入1或者2 1--交换位置 2--轰炸对方");
    146                                 input = Console.ReadLine();
    147                             }
    148                         }
    149                         break;
    150                     case 2:
    151                         Console.WriteLine("玩家{0}踩到了地雷,退6格", playerNames[playNumber]);
    152                         Console.ReadKey(true);
    153                         Players[playNumber] -= 6;
    154                         break;
    155                     case 3:
    156                         Console.WriteLine("玩家{0}猜到了暂停,暂停一回合", playerNames[playNumber]);
    157                         GamePause[playNumber] = true;
    158                         break;//暂停一回合是整个游戏最复杂的逻辑,需要最后再写
    159                     case 4:
    160                         Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", playerNames[playNumber]);
    161                         Players[playNumber] += 10;
    162                         Console.ReadKey(true);
    163                         break;
    164                 }//switch
    165             }//else
    166             changePots();
    167             Console.Clear();// 清屏 
    168             ShowMap();
    169         }
    170 
    171         /// <summary>
    172         /// 当玩家坐标发生改变的时候
    173         /// 限定玩家不能跳出地图的界限
    174         /// </summary>
    175         public static void changePots()
    176         {
    177             if (Players[0] < 0)
    178             {
    179                 Players[0] = 0;
    180             }
    181             if (Players[0] >= 99)
    182             {
    183                 Players[0] = 99;
    184             }
    185             if (Players[1] < 0)
    186             {
    187                 Players[1] = 0;
    188             }
    189             if (Players[1] >= 99)
    190             {
    191                 Players[1] = 99;
    192             }
    193         }
    194 
    195         /// <summary>
    196         /// 画游戏开头提示信息
    197         /// </summary>
    198         /// 
    199         public static void GameShow()
    200         {
    201             Console.ForegroundColor = ConsoleColor.Yellow;
    202             Console.WriteLine("*****************************");
    203             Console.ForegroundColor = ConsoleColor.Red;
    204             Console.WriteLine("*****************************");
    205             Console.ForegroundColor = ConsoleColor.Green;
    206             Console.WriteLine("**** Weiterli 飞行棋V1.0 ****");
    207             Console.ForegroundColor = ConsoleColor.Red;
    208             Console.WriteLine("*****************************");
    209             Console.ForegroundColor = ConsoleColor.Yellow;
    210             Console.WriteLine("*****************************");
    211         }
    212 
    213 
    214         //int[] luckyturn={6,23,40,55,69,83};//幸运轮盘
    215         //int[] lanmine ={5,13,17,33,38,50,64,80,94};//地雷☆
    216         //int[]pause={9,27,60,93};//暂停
    217         //int[] timeTunnel={20,25,45,63,72,88,90}//时空隧道
    218         /// <summary>
    219         ///初始化游戏地图
    220         ///(数组长度100,索引 0-99)
    221         ///<para>luckyturn幸运轮盘lanMine地雷 pause暂停 timeTunnel时空隧道</para>
    222         ///<para>注意:把数组当中的数字转换成控制台中显示的特殊字符串的过程就是初始化地图</para>
    223         /// </summary>
    224         public static void GameMap()
    225         {
    226             int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘
    227             for (int i = 0; i < luckyturn.Length; i++)
    228             {
    229                 //    int index = luckyturn[i];
    230                 maps[luckyturn[i]] = 1;
    231             }
    232             int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
    233             for (int i = 0; i < landmine.Length; i++)
    234             {
    235                 maps[landmine[i]] = 2;
    236             }
    237             int[] pause = { 9, 27, 60, 93 };//暂停
    238             for (int i = 0; i < pause.Length; i++)
    239             {
    240                 maps[pause[i]] = 3;
    241             }
    242             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道
    243             for (int i = 0; i < timeTunnel.Length; i++)
    244             {
    245                 maps[timeTunnel[i]] = 4;
    246             }
    247         }
    248 
    249 
    250         /// <summary>
    251         /// 画地图
    252         /// </summary>
    253         public static void ShowMap()
    254         {
    255             Console.WriteLine("图例:幸运轮盘:◎  地雷:☆  暂停:▲  时空隧道:卐");
    256             #region 第一横行
    257             for (int i = 0; i < 30; i++)
    258             {
    259                 Console.Write(DrawMaps(i));// 画关卡
    260             }//for
    261             #endregion
    262             Console.WriteLine();
    263             #region 画第一个竖行 
    264             //第一竖行30-35
    265             for (int i = 30; i < 35; i++)
    266             {
    267                 for (int j = 0; j <= 28; j++)
    268                 {
    269                     Console.Write("  ");
    270                 }
    271                 Console.Write(DrawMaps(i));
    272                 ;// 画关卡
    273 
    274                 Console.WriteLine();
    275 
    276             }
    277             #endregion
    278             #region 画第二横行
    279             for (int i = 64; i >= 35; i--)
    280             {
    281                 Console.Write(DrawMaps(i));
    282             }
    283             Console.WriteLine();//画完第二横行应当换行
    284             #endregion
    285             #region 画第二竖行
    286             for (int i = 65; i <= 69; i++)
    287             {
    288                 Console.WriteLine(DrawMaps(i));
    289             }
    290             #endregion 
    291             #region 画第三横行
    292             for (int i = 70; i <= 99; i++)
    293             {
    294                 Console.Write(DrawMaps(i));
    295             }
    296             Console.WriteLine();//画完第三横行应当换行
    297             #endregion
    298 
    299         }
    300 
    301 
    302         /// <summary>
    303         /// 画关卡的方法
    304         /// </summary>
    305         /// <param name="i"></param>
    306         /// <returns></returns>
    307         public static string DrawMaps(int i)
    308         {
    309             string str = "";
    310             #region 画关卡
    311             if (Players[0] == Players[1] && Players[0] == i)
    312             {
    313                 str = "<>";
    314             }
    315             else if (Players[0] == i)
    316             {
    317                 str = "";
    318             }
    319             else if (Players[1] == i)
    320             {
    321                 str = "";
    322             }
    323             else
    324             {
    325                 switch (maps[i])
    326                 {
    327                     case 0:
    328 
    329                         Console.ForegroundColor = ConsoleColor.White;
    330                         str = "";
    331                         break;
    332                     case 1:
    333 
    334                         Console.ForegroundColor = ConsoleColor.Red;
    335                         str = "";
    336                         break;
    337                     case 2:
    338 
    339                         Console.ForegroundColor = ConsoleColor.Green;
    340                         str = "";
    341                         break;
    342                     case 3:
    343                         Console.ForegroundColor = ConsoleColor.Yellow;
    344                         str = "";
    345                         break;
    346                     case 4:
    347                         Console.ForegroundColor = ConsoleColor.Blue;
    348                         str = "";
    349                         break;
    350                 }//swicth
    351             }//if
    352             #endregion
    353             return str;
    354         }
    355 
    356     }
    357 }
    C#飞行棋
  • 相关阅读:
    Mybatis学习--spring和Mybatis整合
    MyBatis学习--查询缓存
    MyBatis学习--延迟加载
    MyBatis学习--高级映射
    Mybatis学习--Mapper.xml映射文件
    java文件上传和下载
    【计算机视觉】Object Proposal之BING理解
    【计算机视觉】Object Proposal之BING++
    【计算机视觉】Object Proposal之BING++
    【计算机视觉】Objectness算法(一)---总体理解,整理及总结
  • 原文地址:https://www.cnblogs.com/weiterli/p/8214269.html
Copyright © 2011-2022 走看看