C#(经典)新手项目《骑士飞行棋 》

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 骑士飞行棋
{
    class Program
    {
        static string msg = "";
        static bool[] isStop = { false, false };
        static int[] Map = new int[100];
        static string[] playerName = new string[2] { " ", " " };//用来存储玩家姓名 playerName[0]用来存储玩家A的姓名 playerName[1]存储玩家B的姓名
        static int[] playerPos = new int[] { 0, 0 };// playerPos[0] playerPos[1]玩家AB的坐标
        static void Main(string[] args)
        {
            //数组的下标为0的元素对应地图上的第1格,下标为1的元素对应第二格...下标为n的元素对应n+1格。
            //在数组中用:          1.表示幸运轮盘◎1  白色
            //                     2.地雷☆2 红色
            //                     3.暂停▲3 黄色
            //                     4.时空隧道卐4 绿色
            //                     0.表示普通
            //                    <>:表示AB在同一个坐标
            Random r = new Random();
            int step = 0;

            ShowUI();
            
            Console.WriteLine("请输入玩家A的姓名:");
            playerName[0] = Console.ReadLine();
            while (playerName[0] == "")
            {
                Console.WriteLine("玩家姓名不能为空,请重新输入!");
                playerName[0] = Console.ReadLine();

            }
            Console.WriteLine("请输入玩家B的姓名:");
            playerName[1] = Console.ReadLine();
            while (playerName[1] == "" || playerName[0] == playerName[1])
            {
                if (playerName[1] == "")
                {
                    Console.WriteLine("玩家姓名不能为空,请重新输入!");
                    playerName[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("该玩家姓名已存在,请重新输入!");
                    playerName[1] = Console.ReadLine();
                }
            }
            Console.WriteLine("按任意键开始游戏。。。。。。");
            Console.ReadKey(true);
            Console.Clear();
            ShowUI();
            Console.WriteLine("对战开始......");
            Console.WriteLine("{0}的士兵用A表示", playerName[0]);
            Console.WriteLine("{0}的士兵用B表示", playerName[1]);
            InitialMap();

            DrawMap();
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                if (isStop[0] == false)
                {
                    #region 玩家A开始掷骰子


                    Console.WriteLine("{0}开始掷骰子", playerName[0]);
                    step = r.Next(1, 7);
                    Console.WriteLine("{0}掷出了{1}", playerName[0], step);
                    Console.WriteLine("按任意键开始行动。。。。。。");
                    Console.ReadKey(true);
                  
                    playerPos[0] = playerPos[0] + step;
                    CheckPos();
                    if (playerPos[0] == playerPos[1])
                    {//如果A踩到了B B 退回原点
                        playerPos[1] = 0;
                        msg = string.Format("{0}玩家踩到了{1},{1}退回原点", playerName[0], playerName[1]);
                    }
                    else
                    {
                        switch (Map[playerPos[0]])
                        {
                            case 0://走到0 什么也不做
                                msg = "";
                                break;
                            case 1://走到了1 幸运轮盘 
                                Console.Clear();
                                DrawMap();
                                msg = string.Format("太好了,{0}走到了幸运轮盘。。。。。", playerName[0]);
                                Console.WriteLine("请选择你要的幸运 1——交换位置。2——轰炸对方");
                                int userSelect = SelectStr(1, 2);
                                if (userSelect == 1)
                                {
                                    msg = string.Format("{0}玩家选择了和{1}交换位置", playerName[0], playerName[1]);
                                    int temp = playerPos[0];
                                    playerPos[0] = playerPos[1];
                                    playerPos[1] = temp;

                                }
                                else
                                {
                                    playerPos[1] = playerPos[1] - 6;

                                    CheckPos();
                                    msg = string.Format("{0}玩家选择了轰炸{1},{1}退六格", playerName[0], playerName[1]);
                                }
                                break;
                            case 2://踩到了地雷
                                playerPos[0] = playerPos[0] - 6;

                                CheckPos();
                                msg = string.Format("{0}玩家踩到了地雷,{0}退六格", playerName[0]);
                                break;
                            case 3://暂停
                                isStop[0] = true;
                                msg = string.Format("{0}走到红灯,暂停一次掷骰子", playerName[0]);
                                break;
                            case 4://时空隧道
                                playerPos[0] = playerPos[0] + 10;

                                CheckPos();
                                msg = string.Format("太爽了,{0}走到了时空隧道,前进十格", playerName[0]);
                                break;
                        }

                    }
                    Console.Clear();
                    DrawMap();
                    if (msg != "")
                    {
                        Console.WriteLine(msg);
                    }
                    Console.WriteLine("{0}前进了{1}步,行动结束", playerName[0], step);
                    Console.WriteLine("**************************玩家AB的当前位置**************************");
                    Console.WriteLine("{0}的士兵的当前位置为:{1}", playerName[0], playerPos[0] + 1);
                    Console.WriteLine("{0}的士兵的当前位置为:{1}", playerName[1], playerPos[1] + 1);

                    #endregion
                }
                else
                {
                    isStop[0] = false;
                }


                if (playerPos[0] >= 99)//判断A是否已在终点位置 如果再终点位置 B不需要再掷骰子了
                {
                    break;
                }

                if (isStop[1] == false)
                {
                    #region 玩家B开始掷骰子
                    {


                        Console.WriteLine("{0}开始掷骰子", playerName[1]);
                        step = r.Next(1, 7);
                        Console.WriteLine("{0}掷出了{1}", playerName[1], step);
                        Console.WriteLine("按任意键开始行动。。。。。。");
                        Console.ReadKey(true);

                        playerPos[1] = playerPos[1] + step;

                        CheckPos();
                        if (playerPos[0] == playerPos[1])
                        {//如果B踩到了A A 退回原点
                            msg = string.Format("{0}玩家踩到了{1},{1}退回原点", playerName[1], playerName[0]);
                            playerPos[0] = 0;

                        }
                        else
                        {
                            switch (Map[playerPos[1]])
                            {
                                case 0://走到0 什么也不做
                                    msg = "";
                                    break;
                                case 1://走到了1 幸运轮盘 
                                    Console.Clear();
                                    DrawMap();
                                    msg = string.Format("太好了,{0}走到了幸运轮盘。。。。。", playerName[1]);
                                    Console.WriteLine("请选择你要的幸运 1——交换位置。2——轰炸对方");
                                    int userSelect = SelectStr(1, 2);
                                    if (userSelect == 1)
                                    {
                                        msg = string.Format("{0}玩家选择了和{1}交换位置", playerName[1], playerName[0]);
                                        int temp = playerPos[0];
                                        playerPos[0] = playerPos[1];
                                        playerPos[1] = temp;

                                    }
                                    else
                                    {
                                        playerPos[0] = playerPos[0] - 6;

                                        CheckPos();
                                        msg = string.Format("{0}玩家选择了轰炸{1},{1}退六格", playerName[1], playerName[0]);
                                    }
                                    break;
                                case 2://踩到了地雷
                                    playerPos[1] = playerPos[1] - 6;

                                    CheckPos();
                                    msg = string.Format("{0}玩家踩到了地雷,{0}退六格", playerName[1]);
                                    break;
                                case 3://暂停
                                    isStop[1] = true;
                                msg = string.Format("{0}走到红灯,暂停一次掷骰子",playerName[1]);
                                    break;
                                case 4://时空隧道
                                    playerPos[1] = playerPos[1] + 10;

                                    CheckPos();
                                    msg = string.Format("太爽了,{0}走到了时空隧道,前进十格", playerName[1]);
                                    break;
                            }

                        }
                        Console.Clear();
                        DrawMap();
                        if (msg != "")
                        {
                            Console.WriteLine(msg);
                        }
                        Console.WriteLine("{0}前进了{1}步,行动结束", playerName[1], step);
                        Console.WriteLine("**************************玩家AB的当前位置**************************");
                        Console.WriteLine("{0}的士兵的当前位置为:{1}", playerName[0], playerPos[0] + 1);
                        Console.WriteLine("{0}的士兵的当前位置为:{1}", playerName[1], playerPos[1] + 1);
                    #endregion
                }
                   
                
                
                
                }
                else
                {
                    isStop[1] = false;
                }
                

                
                
                
            }
            Console.Clear();
            ShowUI();
            if (playerPos[0] >= 99)
            {
                Console.WriteLine("{0}胜利了!!!!!!!!!!", playerName[0]);
            }
            else
            {
                Console.WriteLine("{0}胜利了!!!!!!!!!!", playerName[1]);
            }
            Console.ReadKey();
        }
        


        static string GetMapGuanQia(int temp)//返回地图中的关卡的方法
        {
            string result = "";

            if (playerPos[0] == temp && playerPos[1] == temp)
            {
                result = "<>";
            }
            else if (playerPos[0] == temp)//判断A的位置
            {
                result = "A";
            }
            else if (playerPos[1] == temp)//判断B的位置
            {
                result = "B";
            }
            else
            {

                switch (Map[temp])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        result = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.White;
                        result = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        result = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        result = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        result = "卐";
                        break;
                }
            }
            return result;
        }

        static void DrawMap()
        {

            Console.WriteLine("图例:<>表示AB在同一个坐标,◎表示幸运轮盘,☆表示地雷,▲表示暂停,卐表示时空隧道,□表示普通");
            for (int i = 0; i <= 29; i++)
            {
                Console.Write(GetMapGuanQia(i));//第一行地图
            }
            Console.WriteLine();
            for (int i = 30; i <= 34; i++)//第一列
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");//因为不规则符号都是全角的 一个空格对不起 占两位
                }
                Console.WriteLine(GetMapGuanQia(i));
            }
            for (int i = 64; i >= 35; i--)
            {
                Console.Write(GetMapGuanQia(i));//第二行地图

            }
            Console.WriteLine();
            for (int i = 65; i <= 69; i++)
            {
                Console.WriteLine(GetMapGuanQia(i));
            }
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(GetMapGuanQia(i));//第三行
            }
            Console.ResetColor();
            Console.WriteLine();

        }

        /// 
        /// 检查AB坐标是否过界 
        /// 
        static void CheckPos()
        {
            for (int i = 0; i < 2; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }

                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }
        /// 
        /// 选择 1 或者2 两个数字的方法
        /// 
        static int SelectStr(int min, int max)
        {
            int value;
            while (true)
            {
                try
                {
                    value = Convert.ToInt32(Console.ReadLine());
                    if (value < 1 || value > 2)
                    {
                        Console.WriteLine("只能输入{0}-{1}之间的数", min, max);
                        continue;
                    }
                    return value;

                }
                catch
                {
                    Console.WriteLine("输入数据不合法,请重新输入!");
                }
            }

        }
        /// 
        /// 画地图有重复代码 把他放在函数中 因为以后要重复的画图 重复调用
        /// 
        /// 
        /// 
        /// 
        static void InitialMap()//初始化地图 把各个关卡设置到地图中
        {

            int[] luckyTurn = { 6, 23, 40, 55, 69, 83, 98 };//幸运轮盘◎ 1
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ 2// 用于存储在地图中为地雷的下标
            int[] pause = { 9, 27, 60, 93 };//暂停的坐标▲ 3
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐 4
            for (int i = 0; i < luckyTurn.Length; i++)
            {
                Map[luckyTurn[i]] = 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 < timeTunnel.Length; i++)
            {
                Map[timeTunnel[i]] = 4;
            }


        }
        static void ShowUI()
        {
            Console.WriteLine("*************************************************************");
            Console.WriteLine("*                                                           *");
            Console.WriteLine("*            骑     士     飞     行     棋                 *");
            Console.WriteLine("*                                                           *");
            Console.WriteLine("*************************************************************");

        }
    }
}

你可能感兴趣的:(总结)