骑士飞行棋笔记

 主函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 骑士飞行棋
{
    class Program
    {
        public static int[] map = new int[100];//初始化地图,大小为100
        public static int[] person = new int[2] { 0, 0 };//设置角色,角色A和B其地图位置为他的值
        public static string[] playerName = new string[2];//初始化一个大小为2数组,用于存放用户A和B的名字
        public static bool[] result = new bool[2] { false, false };

        static void Main(string[] args)
        {

            ShowUI();
            ShowMap();
            Console.WriteLine("请输入玩家A的姓名");
            playerName[0] = Console.ReadLine();//把玩家A的姓名放在数组playerName下标为0中
            while (playerName[0] == "")//判断玩家A的姓名是否为空
            {
                Console.WriteLine("玩家A姓名不得为空,请重新输入");//如果玩家姓名为空,提示用户重新输入
                playerName[0] = Console.ReadLine();//把用户输入的玩家A姓名重新放在playerName[0] 中
            }
            Console.WriteLine("请输入玩家B的姓名");
            playerName[1] = Console.ReadLine();
            while (playerName[1] == "" || playerName[1] == playerName[0])
            {
                if (playerName[1] == "")
                {
                    Console.WriteLine("玩家B姓名不得为空,请重新输入");
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能与玩家A[{0}]的姓名相同,请重新输入", playerName[0]);
                }

                playerName[1] = Console.ReadLine();
            }
            Console.Clear();.//当玩家输入完可用的用户名后,清屏
            ShowUI();//显示游戏头
            Console.WriteLine("对战开始......");
            Console.WriteLine("{0}的士兵用A表示", playerName[0]);//显示玩家的名字
            Console.WriteLine("{0}的士兵用B表示", playerName[1]);
            DrawMap();
            while (person[0] <= 99 && person[1] <= 99)//判断用户是否在地图上
            {
                
                #endregion
                if (result[0] == false)//当result[0]为false玩家A掷骰子
                {
                    ZhiShaiZi(0);
                }
                else    //当result[0]为false玩家A掷骰子暂停掷骰子,暂停操作
                {
                    result[0] = false;
                }
                if (person[0]==99)//判断玩家A的坐标为99
                {
                    Console.Clear();//如果为99清屏
                    Win();//调用win方法,显示胜利
                    break;//跳出掷骰子
                }
                if (result[1] == false)//玩家B掷骰子
                {
                    ZhiShaiZi(1);
                }
                else
                {
                    result[1] = false;
                }
                if (person[1] == 99)
                {
                    Console.Clear();
                    Win();
                    break;
                }


            }
        }

初始化地图头ShowUI()方法:

        /// <summary>
        /// 出事画地图的头
        /// </summary>
        public static void ShowUI()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("*****************************************");
            Console.WriteLine("*                                       *");
            Console.WriteLine("*               骑士飞行棋              *");
            Console.WriteLine("*                                       *");
            Console.WriteLine("*****************************************");
            Console.ForegroundColor = ConsoleColor.White;
        }

改变地图数组的值,每一种值代表一种符号的ShowMap()方法:
            1...幸运轮盘,显示组用户就 ◎
            2...地雷,显示给用户就是 ☆
            3...暂停,显示给用户就是 ▲
            4...时空隧道,显示组用户就 卐

        /// <summary>
        /// 改变数组map的值,每一种值代表一种符号
        /// </summary>
        public static void ShowMap()
        {
            //....1...幸运轮盘,显示组用户就 ◎
            //....2...地雷,显示给用户就是 ☆
            //....3...暂停,显示给用户就是 ▲
            //....4...时空隧道,显示组用户就 卐
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运转盘
            for (int i = 0; i < luckyturn.Length; i++)
            {
                map[luckyturn[i]] = 1;
            }
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
            for (int i = 0; i < landMine.Length; i++)
            {
                map[landMine[i]] = 2;
            }
            int[] pause = { 9, 27, 60, 93 };//暂停
            for (int i = 0; i < pause.Length; i++)
            {
                map[pause[i]] = 3;
            }
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                map[timeTunnel[i]] = 4;
            }
        }

根据数组map的值得不同的画地图上的符号方法:

        /// <summary>
        /// 画地图上的符号
        /// </summary>
        /// <param name="pos">传过来的坐标</param>
        /// <returns></returns>
        public static string DrawHang(int pos)
        {
            string temp = "";//声明一个字符串temp,以便于存地图符号,返回给调用的方法
            if (person[0] == person[1] && person[0] == pos)//当A和B值相等并且在地图上
            {
                temp = "<>";//地图符号画<>
            }
            else if (person[0] == pos)//A的值如果等于传过来的坐标的值,就在此坐标画A
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                temp = "";
            }
            else if (person[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                temp = "";
            }
            else
            {
                switch (map[pos])
                {
                    case 0: Console.ForegroundColor = ConsoleColor.Gray;//当传过来的坐标等于0时,画□,颜色灰色
                        temp = ""; break;
                    case 1: Console.ForegroundColor = ConsoleColor.Red;//当传过来的坐标等于0时,画◎,颜色红色
                        temp = ""; break;
                    case 2: Console.ForegroundColor = ConsoleColor.Blue;
                        temp = ""; break;
                    case 3: Console.ForegroundColor = ConsoleColor.Magenta;
                        temp = ""; break;
                    case 4: Console.ForegroundColor = ConsoleColor.Green;
                        temp = ""; break;

                }
            }
            return temp;//返回符号
        }

画地图的方法:(要调用话符号方法)

第一行有30个符号:坐标0-29

第一竖行有5个符号:坐标:30-34

第二行有30个符号:坐标35-64//应从64开始遍历到35:

第二竖行有5个符号:坐标:65-69

第三行有30个符号:坐标70-99

/// <summary>
        /// 画地图
        /// </summary>
        /// <returns></returns>
        public static string DrawHang(int pos)
        public static void DrawMap()
        {
            //画第一行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            Console.WriteLine();
            //第一画竖行
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            //画第二行
            for (int i = 64; i > 34; i--)
            {
                Console.Write(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            Console.WriteLine();
            //画第二竖
            for (int i = 65; i < 70; i++)
            {
                Console.WriteLine(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            //画第三行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            Console.WriteLine();
        }

玩家踩到幸运轮盘时,选择请选择1----交换位置,2----轰炸对方的方法:

方法只返回数字1或则数字2

        /// <summary>
        /// 玩家踩到幸运转盘时判断选的是否为提示数字的方法
        /// </summary>
        /// <param name="pos">传过来的提示字符串</param>
        /// <returns></returns>
           public static int LucyPan(string msg)
        {
            while (true)
            {
                try
                {
                    Console.WriteLine(msg);
                    int num = int.Parse(Console.ReadLine());//转换用户输入的字符串
                    if (num == 1 || num == 2)//若用户输入的为1或则2,返回num
                    {

                        return num;
                    }
                    else
                    {
                        Console.WriteLine("您输入的数字不合法,请重新输入;");
                        Console.WriteLine(msg);
                        continue;
                    }
                }

                catch
                {
                    Console.WriteLine("输入有误,请重新输入...");
                }
            }

        }

掷骰子ZhiSaiZi(int plyerpos)方法:

注意:当玩家的下标为plyerpos时那么另一个玩家的下标为[1-plyerpos]

        /// <summary>
        /// 掷骰子方法
        /// </summary>
        /// <param name="plyerpos">传过来的玩家姓名的数组下标标</param>
        /// <returns></returns>
        public static void ZhiShaiZi(int plyerpos)
        {
            #region
            string msg = "";
            Random r=new Random();
            int posNumber=r.Next(1,7);
            Console.WriteLine("玩家{0}按任意开始掷骰子", playerName[plyerpos]);
            Console.ReadKey(true);
            Console.WriteLine("玩家{0}掷出了{1}", playerName[plyerpos], posNumber);
            Console.WriteLine("按任意键开始行动....");
            Console.ReadKey(true);
            person[plyerpos] += posNumber;
            CheckPos();
            if (person[plyerpos] == person[1 - plyerpos])
            {
                person[1 - plyerpos] = 0;
            }
            else
            {
                switch (map[person[plyerpos]])
                {
                    //正常行动,方块
                    case 0:
                        msg = string.Format( "{0}行动完毕", playerName[plyerpos]); break;
                    //幸运转盘,
                    case 1:
                        msg = string.Format("{0}走到了幸运轮盘,请选择1----交换位置,2----轰炸对方", 

playerName[plyerpos]);
                        int num = LucyPan(msg);//调用LucyPan的方法,把msg传给LucyPan,返回1或则2,
                        if (num == 1)//当返回1的时候,玩家调换位置
                        {
                            int temp = person[plyerpos];
                            person[plyerpos] = person[1 - plyerpos];
                            person[1 - plyerpos] = temp;
                            msg = string.Format("玩家{0}选择了与玩家{1}交换位置", playerName[plyerpos], 

playerName[1 - plyerpos]);
                        }
                        else //当num为2的时候,玩家的对家退6格
                        {
                            person[1 - plyerpos] -= 6;
                            CheckPos();//检查玩家是否在地图上
                            msg = string.Format("玩家{0}选择了轰炸对方,玩家{1}退6格", playerName[plyerpos], 

playerName[1 - plyerpos]);
                        }
                        break;
                    //地雷,返回6格
                    case 2:
                        person[plyerpos] -= 6;
                        CheckPos();//检查玩家是否在地图上
                        msg = string.Format("恭喜玩家{0}踩到了万年不遇的地雷,退6格", playerName[plyerpos]); 

break;
                    //暂停一次
                    case 3:
                        result[plyerpos] = true;//在主函数里面为true时,不调用ZhiSaiZi方法
                        msg = string.Format("玩家{0}暂停一次", playerName[plyerpos]); break;
                    //时空隧道,前进10格
                    case 4:
                        person[plyerpos] += 10;
                        CheckPos();//检查玩家是否在地图上
                        msg = string.Format("玩家{0}极其猥琐,竟然穿越了10格", playerName[plyerpos]); break;
                }
            }
            Console.Clear();
            DrawMap();
            Console.WriteLine(msg);

            #endregion

        }

检查用户是否在地图上CheckPos()方法:

        public static void CheckPos()
        {
            if (person[0]>99)//当玩家A的坐标大于99时
            {
                person[0] = 99;//把玩家A的坐标设为99
            }
            if (person[1] > 99)//当玩家B的坐标大于99时
            {
                person[1] = 99;//把玩家B的坐标设为99
            }
            if (person[0] <0)//当玩家A的坐标小于0时
            {
                person[0] = 0;//把玩家A的坐标设为0
            }
            if (person[1] <0)//当玩家B的坐标小于0时
            {
                person[1] = 0;//把玩家B的坐标设为0
            }
        }

胜利win()方法:

public static void Win()
        {
             Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("");
            Console.WriteLine("                    ■                  ◆               ■        ■");
            Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
            Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
            Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
            Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
            Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
            Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
            Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
            Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
            Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
            Console.ResetColor();
        
        }

 注意:随即产生1-6的数字方法

            Random r=new Random();
            int posNumber=r.Next(1,7);



原文地址:https://www.cnblogs.com/guohuiru/p/2769272.html