• 飞行棋代码


    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text;
    
    namespace 飞行棋 
    { 
        class Program 
        { 
           static int[] Map = new int[100];//在不赋值的情况下,默认值为0. 
           static int[] playerpost = { 0, 0 };//playerpost[0]存A的坐标,playerpost[1]村B的坐标
    
            static void Main(string[] args) 
            { 
                Random r = new Random();//r用于产生随机数 
                int step = 0;//用于存放产生的随机数 
                string mag = ""; //用于用户踩到某关卡输出的话。 
                bool[] isStop = { false,false};//isStop[0]为A的标签,isStop[1]为B的标签。
    
                showUI(); 
                
                string[] name = new string[2]; 
                Console.WriteLine("请输入一名玩家的名字"); 
                name[0] = Console.ReadLine(); 
                while (name[0] == "") 
                { 
                    Console.WriteLine("不能为空,请重新输入玩家的名字"); 
                    name[0] = Console.ReadLine(); 
                } 
                Console.WriteLine("请输入另一玩家的名字"); 
                name[1] = Console.ReadLine(); 
                while (name[1] == "" || name[1] == name[0]) 
                { 
                    if (name[1] == "") 
                    { 
                        Console.WriteLine("不能为空,请重新输入玩家的名字"); 
                    } 
                    if (name[1] == name[0]) 
                    { 
                        Console.WriteLine("两位玩家的名字不能相同,请重新输入"); 
                    } 
                    name[1] = Console.ReadLine(); 
                } 
                Console.Clear();      //在此屏幕清空。 
                showUI(); 
                Console.WriteLine("对战开始......"); 
                Console.WriteLine("玩家{0}的士兵用A表示",name[0]); 
                Console.WriteLine("玩家{0}的士兵用B表示",name[1]); 
                Console.WriteLine("当AB走到同一位置时,用<>表示");
    
                initialmap();//初始化地图 
                drawMap(); //绘制地图 
                Console.WriteLine("开始游戏......"); 
                
                //让玩家轮流掷色子,当一方坐标大于等于99时,结束循环。 
                while (playerpost[0] < 99 && playerpost[1] < 99) 
                { 
                    if (isStop[0] == false) 
                    { 
                        #region A 掷色子 
                        Console.WriteLine("{0}按任意键开始掷色子...", name[0]); 
                        ConsoleKeyInfo sec = Console.ReadKey(true); 
                        if (sec.Key == ConsoleKey.F7) 
                        { 
                            step = 20; 
                        } 
                        else if (sec.Key == ConsoleKey.Insert) 
                        { 
                            step =99; 
                        } 
                        else 
                        { 
                            step = r.Next(1, 7); 
                        } 
                        Console.WriteLine("{0}掷出了:{1}", name[0], step); 
                        Console.WriteLine("按任意键开始行动..."); 
                        Console.ReadKey(true); 
                        playerpost[0] = playerpost[0] + step; 
                        checkpos(); 
                        if (playerpost[0] == playerpost[1]) 
                        { 
                            playerpost[1] = 0; 
                            mag = string.Format("{0}踩到了{1},{1}退回起点", name[0], name[1]); 
                            // 在赋值字符串时可以使用占位符的方法 
                        } 
                        else 
                        { 
                            switch (Map[playerpost[0]]) 
                            { 
                                case 0://普通 
                                    mag = ""; 
                                    break; 
                                case 1://幸运轮盘 
                                    Console.Clear(); 
                                    drawMap(); 
                                    Console.WriteLine("{0}到达幸运轮盘,请选择下面的运气",name[0]); 
                                    Console.WriteLine("1---与对方互换位置  2---轰炸对方"); 
                                    int userselect = ReadInt(1, 2); 
                                    if (userselect == 1) 
                                    { 
                                        int temp = playerpost[0]; 
                                        playerpost[0] = playerpost[1]; 
                                        playerpost[1] = temp; 
                                        mag = string.Format("在幸运轮盘中,{0}选择了与{1}交换位置。", name[0], name[1]); 
                                    } 
                                    else 
                                    { 
                                        playerpost[1] = playerpost[1] - 6; 
                                        checkpos(); 
                                        mag = string.Format("在幸运轮盘中,{0}选择了轰炸{1},{1}退6步。", name[0], name[1]); 
                                    } 
                                    break;
    
                                case 2://地雷 
                                    playerpost[0] = playerpost[0] - 6; 
                                    checkpos(); 
                                    mag = string.Format("{0}踩中地雷,退6步", name[0]); 
                                    break;
    
                                case 3://暂停 
                                    mag = string.Format("{0}被暂停一次", name[0]); 
                                    isStop[0] = true; 
                                    break;
    
                                case 4://时空隧道 
                                    playerpost[0] = playerpost[0] + 10; 
                                    checkpos(); 
                                    mag = string.Format("{0}遇到时空隧道,前进10步", name[0]); 
                                    break; 
                            } 
                        }
    
                        Console.Clear(); 
                        drawMap(); 
                        if (mag != "") 
                        { 
                            Console.WriteLine(mag); 
                        } 
                        Console.WriteLine("{0}掷出了{1},行动完成!", name[0], step); 
                        Console.WriteLine("****************A和B的位置**************"); 
                        Console.WriteLine("{0}的位置是{1}", name[0], playerpost[0] + 1); 
                        Console.WriteLine("{0}的位置是{1}", name[1], playerpost[1] + 1); 
                        Console.WriteLine("****************************************");
    
                        #endregion 
                    } 
                    else 
                    { 
                        isStop[0] = false; 
                    }
    
    
                    if (playerpost[0] >= 99) 
                    { 
                        break; 
                    }
    
                    if (isStop[1] == false) 
                    { 
                        #region B 掷色子 
                        Console.WriteLine("{0}按任意键开始掷色子...", name[1]);
    
                        ConsoleKeyInfo rec = Console.ReadKey(true); 
                        if (rec.Key == ConsoleKey.F8) 
                        { 
                            step = 20; 
                        } 
                        else if (rec.Key == ConsoleKey.Insert) 
                        { 
                            step = 99; 
                        } 
                        else 
                        { 
                            step = r.Next(1, 7); 
                        } 
                        Console.WriteLine("{0}掷出了:{1}", name[1], step); 
                        Console.WriteLine("按任意键开始行动..."); 
                        Console.ReadKey(true); 
                        playerpost[1] = playerpost[1] + step; 
                        checkpos(); 
                        if (playerpost[1] == playerpost[0]) 
                        { 
                            playerpost[0] = 0; 
                            mag = string.Format("{0}踩到了{1},{1}退回起点", name[1], name[0]); 
                            // 在赋值字符串时可以使用占位符的方法 
                        } 
                        else 
                        { 
                            switch (Map[playerpost[1]]) 
                            { 
                                case 0://普通 
                                    mag = ""; 
                                    break; 
                                case 1://幸运轮盘 
                                    Console.Clear(); 
                                    drawMap(); 
                                    Console.WriteLine("{0}到达幸运轮盘,请选择下面的运气",name[0]); 
                                    Console.WriteLine("1---与对方互换位置  2---轰炸对方"); 
                                    int userselect = ReadInt(1, 2); 
                                    if (userselect == 1) 
                                    { 
                                        int temp = playerpost[0]; 
                                        playerpost[0] = playerpost[1]; 
                                        playerpost[1] = temp; 
                                        mag = string.Format("在幸运轮盘中,{0}选择了与{1}交换位置。", name[1], name[0]); 
                                    } 
                                    else 
                                    { 
                                        playerpost[0] = playerpost[0] - 6; 
                                        checkpos(); 
                                        mag = string.Format("在幸运轮盘中,{0}选择了轰炸{1},{1}退6步。", name[1], name[0]); 
                                    } 
                                    break;
    
                                case 2://地雷 
                                    playerpost[1] = playerpost[1] - 6; 
                                    checkpos(); 
                                    mag = string.Format("{0}踩中地雷,退6步", name[1]); 
                                    break;
    
                                case 3://暂停 
                                    mag = string.Format("{0}被暂停一次", name[1]); 
                                    isStop[1] = true; 
                                    break;
    
                                case 4://时空隧道 
                                    playerpost[1] = playerpost[1] + 10; 
                                    checkpos(); 
                                    mag = string.Format("{0}遇到时空隧道,前进10步", name[1]); 
                                    break; 
                            } 
                        }
    
                        Console.Clear(); 
                        drawMap(); 
                        if (mag != "") 
                        { 
                            Console.WriteLine(mag); 
                        } 
                        Console.WriteLine("{0}掷出了{1},行动完成!", name[1], step); 
                        Console.WriteLine("****************A和B的位置**************"); 
                        Console.WriteLine("{0}的位置是{1}", name[0], playerpost[0] + 1); 
                        Console.WriteLine("{0}的位置是{1}", name[1], playerpost[1] + 1); 
                        Console.WriteLine("****************************************"); 
                        #endregion 
                    } 
                    else 
                    { 
                        isStop[1] = false; 
                    }
    
                }
    
                Console.Clear(); 
                showUI(); 
                    
                if (playerpost[0] > playerpost[1]) 
                { 
                    Console.WriteLine("{0}在本次对战中获胜!", name[0]); 
                } 
                else 
                { 
                    Console.WriteLine("{0}在本次对战中获胜!", name[1]); 
                } 
                Console.WriteLine("按任意键游戏结束!"); 
                Console.ReadKey();
    
    
    
            }
    
    
    
    
     
          /// <summary>         /// 判断玩家坐标的位置,防止超出棋盘。         /// </summary>         static void checkpos()         {             for (int i = 0; i <= 1; i++)             {                 if (playerpost[i] > 99)                 {                     playerpost[i] = 99;                 }                 if (playerpost[i] < 0)                 {                     playerpost[i] = 0;                 }             }         }
    
            /// <summary> 
            /// 用于绘制飞行棋的名称 
            /// </summary> 
             public static void showUI() 
            { 
                Console.WriteLine("*****************************************"); 
                Console.WriteLine("*                                       *"); 
                Console.WriteLine("*        骑   士  飞    行   棋        *"); 
                Console.WriteLine("*                                      *"); 
                Console.WriteLine("****************************************"); 
            }
    
            /// <summary> 
            /// 对地图中的关卡进行初始化 
            /// </summary> 
             static void initialmap() 
             { 
                 //存储地雷下标 
                 int[] luckturn = { 6, 23, 40, 55, 69, 83 }; //幸运轮盘 
                 int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; //地雷 
                 int[] pause = { 9, 27, 60, 93 }; //暂停 
                 int[] timetunle = { 20, 25, 45, 63, 72, 88, 90 }; //时空隧道
    
                 for (int i = 0; i < luckturn.Length; i++) 
                 { 
                     int pos = luckturn[i]; 
                     Map[pos] = 1; 
                 } 
                 for (int i = 0; i < landmine.Length; i++) 
                 { 
                     Map[landmine[i]] = 2; 
                 } 
                 for (int i = 0; i < pause.Length; i++) 
                 { 
                     Map[pause[i]] = 3; 
                 } 
                 for (int i = 0; i < timetunle.Length; i++) 
                 { 
                     Map[timetunle[i]] = 4; 
                 } 
             }
    
            /// <summary> 
            /// 获得第Pos坐标上应绘制的图案 
            /// </summary> 
            /// <param name="pos">要绘制的坐标</param> 
            /// <returns></returns> 
             static string getmap(int pos) 
             { 
                 string result = "";
    
                 if (playerpost[0] == pos && playerpost[1] == pos) 
                 { 
                     Console.ForegroundColor = ConsoleColor.Yellow; 
                     result="<>"; 
                 } 
                 else if (playerpost[0] == pos) 
                 { 
                     Console.ForegroundColor = ConsoleColor.Yellow; 
                     result = "A"; 
                 } 
                 else if (playerpost[1] == pos) 
                 { 
                     Console.ForegroundColor = ConsoleColor.Yellow; 
                     result = "B"; 
                 } 
                 else 
                 { 
                     switch (Map[pos]) 
                     { 
                         case 0: Console.ForegroundColor = ConsoleColor.White; 
                             result = "□"; 
                             break; 
                         case 1: Console.ForegroundColor = ConsoleColor.DarkMagenta; 
                             result = "◎"; 
                             break; 
                         case 2: Console.ForegroundColor = ConsoleColor.DarkBlue; 
                             result ="★"; 
                             break; 
                         case 3: Console.ForegroundColor = ConsoleColor.DarkRed; 
                             result = "▲"; 
                             break; 
                         case 4: Console.ForegroundColor = ConsoleColor.Cyan; 
                             result = "※"; 
                             break; 
                     } 
                 } 
                 return result;
    
             }
    
            /// <summary> 
            /// 画地图 
            /// </summary> 
             static void drawMap() 
             { 
                 Console.WriteLine("图例:幸运轮盘:◎ ,地雷:★ ,暂停:▲ ,时空隧道:※"); 
                 //绘制第一行 
                 for (int i = 0; i < 30; i++) 
                 { 
                    Console.Write( getmap(i)); 
                 } 
                 Console.WriteLine();
    
                 //画第二条 
                 for (int i = 30; i < 35; i++) 
                 { 
                     for (int j = 0; j < 29; j++) 
                     { 
                         Console.Write("  "); 
                     } 
                    Console.Write( getmap(i)); 
                    Console.WriteLine(); 
                 }
    
                 //画第三条 
                 for (int i = 64; i>34; i--) 
                 { 
                    Console.Write( getmap(i)); 
                 } 
                 Console.WriteLine();
    
                 //画第四条 
                 for (int i = 65; i < 70; i++) 
                 { 
                     Console.WriteLine(getmap(i)); 
                 } 
                 //画第五条 
                 for (int i = 70; i <100; i++) 
                 { 
                     Console.Write(getmap(i)); 
                 }
    
                 Console.WriteLine(); 
                 Console.ResetColor(); 
             }
    
    
    
            /// <summary> 
            ///  只允许用户输入从最小值到最大值的方法   *** 
            /// </summary> 
            /// <returns></returns> 
             static int ReadInt() 
             { 
                 int i= ReadInt(int.MinValue, int.MaxValue); 
                 return i; 
             } 
             static int ReadInt(int min, int max) 
             { 
                 while (true) 
                 { 
                     try 
                     { 
                         int number = Convert.ToInt32(Console.ReadLine()); 
                         if (number < min || number > max) 
                         { 
                             Console.WriteLine("只能输入{0}-{1}之间的数,请重新输入", min, max); 
                             continue; 
                         } 
                         return number; 
                     } 
                     catch 
                     { 
                         Console.WriteLine("只能输入数字,请重新输入"); 
                     } 
                 } 
                     
             }
    
    
        } 
    } 
    
    
  • 相关阅读:
    下一代工具链「GitHub 热点速览 v.22.43」
    Vue3学习笔记(五)——路由,Router
    Node.js学习笔记(四)——NodeJS访问MongoDB与MySQL数据库
    Vue3学习笔记(一)——MVC与vue3概要、模板、数据绑定与综合示例
    Node.js学习笔记(二)——Node.js模块化、文件读写、环境变量
    Node.js学习笔记(一)——Node.js概要、NPM与package.json
    TypeScript学习笔记(三)—— 编译选项、声明文件、WebPack打包、Babel转换
    TypeScript学习笔记(四)—— TypeScript提高
    Vue3学习笔记(六)—— 作业
    Vue3学习笔记(二)——组合式API(Composition API)
  • 原文地址:https://www.cnblogs.com/HuXiaoxia/p/3392704.html
Copyright © 2020-2023  润新知