C#第八天

118 阅读3分钟

1、画游戏头 2、初始化地图(加载地图所需要的资源) 将整数数组中的数字编程控制台中显示的特殊字符串的这个过程 就是初始化地图 3、画地图

4、玩游戏

游戏规则: 如果玩家A踩到了玩家B  玩家B退6格  踩到了地雷 退6格 踩到了时空隧道 进10格 踩到了幸运轮盘 1交换位置  2 轰炸对方 使对方退6格 踩到了暂停  暂停一回合  踩到了方块  神马都不干

Map[50] if(map[40]==1) {  Console.WriteLine("◎"); }

//初始化地图   Map[6]=1             //我用0表示普通,显示给用户就是 □             //....1...幸运轮盘,显示组用户就◎             //....2...地雷,显示给用户就是 ☆             //....3...暂停,显示给用户就是 ▲             //....4...时空隧道,显示组用户就 卐             int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎             int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆             int[] pause = { 9, 27, 60, 93 };//暂停▲             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐    for(int i=0;i<luckyturn.Length;i++)    {     int n=luckyturn[i];     Map[n]=1;    }    for(int i=0;i<landMine.Length;i++)    {     int n=landMine[i];     Map[n]=1;    }

代码实现

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

namespace 飞行棋
{
    class Program
    {

        static int[] Maps = new int[100];
        static int[] PlayerPos = new int[2];
        static string[] PlayerNames = new string[2];
        static bool[] Flags = new bool[2];

        static void Main(string[] args)
        {
            GameShow();
            #region 输入玩家姓名
            Console.WriteLine("请输入玩家A的姓名");
            PlayerNames[0] = Console.ReadLine();
            while(PlayerNames[0] == "")
            {
                    Console.WriteLine("玩家姓名不能为空,请重新输入!");
                    PlayerNames[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B的姓名");
            PlayerNames[1] = Console.ReadLine();
            while (PlayerNames[1] == "" || PlayerNames[1] == PlayerNames[0])
            {
                if (PlayerNames[1] == "")
                {
                    Console.WriteLine("玩家姓名不能为空,请重新输入!");
                    PlayerNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩家B姓名不能和玩家A姓名一样,请重新输入!");
                    PlayerNames[1] = Console.ReadLine();
                }
            }
            #endregion
            Console.Clear();
            GameShow();
            Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
            Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
            InitialMap();
            DrawMap();
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flags[0]=false;
                }
                if (PlayerPos[0] >= 99)
                {
                    Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
                    break;
                }
                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {
                    Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
                    break;
                }
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("************************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("*******飞行棋游戏*******");
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine("************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("************************");
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitialMap()
        {
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
            for (int i = 0; i < luckyturn.Length; i++)
            {
                Maps[luckyturn[i]] = 1;
            }
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
            for (int i = 0; i < landMine.Length; i++)
            {
                Maps[landMine[i]] = 2;
            }
            int[] pause = { 9, 27, 60, 93};//暂停▲
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }

        /// <summary>
        /// 画地图
        /// </summary>
        public static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲   时空隧道:卐");
            #region 第一行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion
            Console.WriteLine();
            #region 第一列
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(DrawStringMap(i));
            }
            #endregion
            #region 第二行
            for (int i = 64; i > 34; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion
            Console.WriteLine();
            #region 第二列
            for (int i = 65; i < 70; i++)
            {
                Console.WriteLine(DrawStringMap(i));
            }
            #endregion
            #region 第三行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion
            Console.WriteLine();
        }

        /// <summary>
        /// 从画地图的方法中抽象出来的一个方法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string DrawStringMap(int i)
        {
            string str = "";
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
                str = "A";
            }
            else if (PlayerPos[1] == i)
            {
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        str = "卐";
                        break;
                }
            }
            return str;
        }

        /// <summary>
        /// 玩游戏
        /// </summary>
        public static void PlayGame(int playerNumber)
        {
            Random r = new Random();
            int rNumber = r.Next(1, 7);
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", PlayerNames[playerNumber], rNumber);
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            PlayerPos[playerNumber] += rNumber;
            Console.WriteLine("{0}行动完了", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            ChangePos();
            Console.Clear();
            DrawMap();
            if (PlayerPos[playerNumber] == PlayerPos[1-playerNumber])
            {
                PlayerPos[1-playerNumber] -= 6;
                Console.ReadKey(true);
            }
            else
            {
                switch (Maps[PlayerPos[playerNumber]])
                {
                    case 0: Console.WriteLine("玩家{0}踩到了方块,安全", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择 1--交换位置 2--轰炸对方", PlayerNames[playerNumber]);
                        string input=Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}选择跟对方交换位置", PlayerNames[playerNumber]);
                                int temp = PlayerPos[playerNumber];
                                PlayerPos[playerNumber] = PlayerPos[1-playerNumber];
                                PlayerPos[1-playerNumber] = temp;
                                Console.WriteLine("交换完成,请按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸对方", PlayerNames[playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1-playerNumber] -= 6;
                                Console.WriteLine("玩家{0}遭到玩家{1}轰炸,退6格", PlayerNames[1-playerNumber], PlayerNames[playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("无效选择,请重新选择1--交换位置 2--轰炸对方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 2: Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] -= 6;
                        break;
                    case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]);
                        Flags[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerNumber]);
                        PlayerPos[playerNumber] += 10;
                        Console.ReadKey(true);
                        break;
                }
            }
            ChangePos();
            Console.Clear();
            DrawMap();

        }

        /// <summary>
        /// 当玩家坐标发生改变的时候调用
        /// </summary>
        public static void ChangePos()
        {
            if (PlayerPos[0] < 0)
            {
                PlayerPos[0] = 0;
            }
            if (PlayerPos[0] > 99)
            {
                PlayerPos[0] = 99;
            }
            if (PlayerPos[1] < 0)
            {
                PlayerPos[1] = 0;
            }
            if (PlayerPos[1] > 99)
            {
                PlayerPos[1] = 99;
            }
        }
    }
}