C#——第1个:飞行棋项目

来源:互联网 发布:单片机自动门源程序 编辑:程序博客网 时间:2024/05/24 04:56

简单的飞行棋项目

游戏界面如下:
地图共100格

代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace AeroplaneChess{    class Program    {        public static int[] map = new int[100];  //地图的100个坐标数组,初值均为0;        static int[] playerPos = new int[2];  //两个玩家的坐标数组,初值均为0;        static string[] playerName = new string[2];  //两个玩家的姓名数组;        static bool[] Flags = new bool[2];  //玩家的两个标记,初值为false;        static void Main(string[] args)        {            GameShow();  //游戏头;          #region 输入玩家姓名            Console.WriteLine("请输入第一个玩家的姓名:");            playerName[0] = Console.ReadLine();            while (playerName[0] == "")            {                Console.WriteLine("玩家的姓名不能为空,请重新输入:");                playerName[0] = Console.ReadLine();            }            Console.WriteLine("请输入第二个玩家的姓名:");            playerName[1] = Console.ReadLine();            while (playerName[1] == "" || playerName[1] == playerName[0])            {                if (playerName[1] == "")                {                    Console.WriteLine("玩家的姓名不能为空,请重新输入:");                    playerName[1] = Console.ReadLine();                }                else                {                    Console.WriteLine("玩家的姓名不能相同,请重新输入:");                    playerName[1] = Console.ReadLine();                }            }            #endregion            Console.Clear(); //清屏;            GameShow();            Console.WriteLine("玩家{0}的士兵用♂表示", playerName[0]);            Console.WriteLine("玩家{0}的士兵用♀表示", playerName[1]);            InitailMap(); //游戏地图初始化;            DrawMap();  //画地图;            while (playerPos[0] < 99 && playerPos[1] < 99)            {                if (Flags[0] == false)                    PlayGame(0);                else                    Flags[0] = false;  //当玩家踩中暂停,需在下一回合将标记值改回false;                if (playerPos[0] >= 99)                {                    Console.WriteLine("玩家{0}赢了!", playerName[0]);                    break;                }                if(Flags[1]==false)                    PlayGame(1);                else                    Flags[1] = false;                if (playerPos[1] >= 99)                {                    Console.WriteLine("玩家{0}赢了!", playerName[1]);                    break;                }              }            Console.ReadKey();        }        /// <summary>        /// 游戏头        /// </summary>        public static void GameShow()        {            Console.ForegroundColor = ConsoleColor.Red;            Console.WriteLine("********************");            Console.ForegroundColor = ConsoleColor.Green;            Console.WriteLine("********************");            Console.ForegroundColor = ConsoleColor.Magenta;            Console.WriteLine("--------------------");            Console.WriteLine("-----飞--行--棋-----");            Console.WriteLine("--------------------");            Console.ForegroundColor = ConsoleColor.Green;            Console.WriteLine("********************");            Console.ForegroundColor = ConsoleColor.Red;            Console.WriteLine("********************");        }        /// <summary>        /// 初始化地图        /// </summary>        public static void InitailMap()        {            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };            for(int i = 0; i < luckyturn.Length; ++i)                map[luckyturn[i]] = 1;  //1表示幸运轮盘⊙;            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };            for (int i = 0; i < landMine.Length; ++i)                map[landMine[i]] = 2;  //2表示地雷☆;            int[] pause = { 9, 27, 60, 93 };            for (int i = 0; i < pause.Length; ++i)                map[pause[i]] = 3;  //3表示暂停▲;            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };            for (int i = 0; i < timeTunnel.Length; ++i)                map[timeTunnel[i]] = 4;  //4表示时空隧道※;        }        /// <summary>        /// 画地图通用方法        /// </summary>        /// <param name="i">最大坐标数</param>        public static string MapGrid(int i)        {            string str = "";            //如果玩家A和玩家B的坐标相同,且都在这个地图上,则画<>;            if (playerPos[0] == playerPos[1] && playerPos[1] == i)            {                str="<>";            }            else if (playerPos[0] == i)            {                str="♂";            }            else if (playerPos[1] == i)            {                str="♀";            }            else            {                switch (map[i])                {                    case 0:                        Console.ForegroundColor = ConsoleColor.White;                        str="□"; break;                    case 1:                        Console.ForegroundColor = ConsoleColor.DarkYellow;                        str="⊙"; break;                    case 2:                        Console.ForegroundColor = ConsoleColor.Red;                        str="☆"; break;                    case 3:                        Console.ForegroundColor = ConsoleColor.Blue;                        str="▲"; break;                    case 4:                        Console.ForegroundColor = ConsoleColor.DarkGray;                        str="※"; break;                }            }            return str;        }        /// <summary>        /// 画地图        /// </summary>        public static void DrawMap()        {            GameShow();            Console.WriteLine("{0}的士兵用♂表示", playerName[0]);            Console.WriteLine("{0}的士兵用♀表示", playerName[1]);            Console.WriteLine("图例:幸运轮盘:⊙     地雷:☆      暂停:▲      时空隧道:※");            //画地图的第一横线;            for (int i = 0; i < 30; ++i)            {                Console.Write(MapGrid(i));            }            //画地图的第一竖线;            Console.WriteLine();            for (int i = 30; i < 35; ++i)            {                for (int j = 0; j < 29; ++j)                {                    Console.Write("  ");                }                Console.WriteLine(MapGrid(i));            }            //画地图的第二横线;            for(int i = 64; i >= 35; i--)            {                Console.Write(MapGrid(i));            }            //画地图的第二竖线;            Console.WriteLine();            for (int i = 65; i < 70; i++)            {                Console.WriteLine(MapGrid(i));            }            //画地图的第三横线;            for (int i = 70; i < 100; ++i)            {                Console.Write(MapGrid(i));            }            Console.WriteLine();        }        /// <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;        }        /// <summary>        /// 游戏规则        /// </summary>        /// <param name="player">第一个玩家或者第二个玩家</param>        public static void PlayGame(int player)        {            Random r = new Random();            int rNumber = r.Next(1, 7);  //掷骰子在1-6里随机;            Console.WriteLine("{0}按任意键开始掷骰子", playerName[player]);            Console.ReadKey(true);            Console.WriteLine("{0}掷出了{1}", playerName[player], rNumber);            playerPos[player] += rNumber;            ChangePos();            Console.ReadKey(true);            Console.WriteLine("{0}按任意键开始行动", playerName[player]);            Console.ReadKey(true);            Console.WriteLine("{0}行动完了!", playerName[player]);            Console.ReadKey(true);            //玩家A有可能踩到玩家B;方块;幸运轮盘;地雷;暂停;时空隧道;            if (playerPos[player] == playerPos[1 - player])            {                Console.WriteLine("{0}踩到了{1},退6格", playerName[player], playerName[1 - player]);                playerPos[1 - player] -= 6;                Console.ReadKey(true);            }            else //踩到关卡;            {                switch (map[playerPos[player]])                {                    case 0:Console.WriteLine("{0}踩到了方块,什么也没发生。", playerName[player]);                        Console.ReadKey(true);                        break;                    case 1: Console.WriteLine("{0}踩到了幸运轮盘,请选择 1——交换位置 2——轰炸对方", playerName[player]);                        string input = Console.ReadLine();                        while (true)                        {                            if (input == "1")                            {                                Console.WriteLine("玩家{0}选择跟玩家{1}交换位置", playerName[player], playerName[1 - player]);                                Console.ReadKey(true);                                int temp = playerPos[player];                                playerPos[player] = playerPos[1 - player];                                playerPos[1 - player] = temp;                                Console.WriteLine("交换完成!按任意键继续游戏!");                                Console.ReadKey(true);                                break;                            }                            else if (input == "2")                            {                                Console.WriteLine("玩家{0}选择了轰炸玩家{1},退6格", playerName[player], playerName[1 - player]);                                Console.ReadKey(true);                                playerPos[1 - player] -= 6;                                Console.WriteLine("玩家{0}退了6格", playerName[1 - player]);                                Console.ReadKey(true);                                break;                            }                            else                            {                                Console.WriteLine("只能输入1或者2,请重新输入:");                                input = Console.ReadLine();                            }                        }                        break;                    case 2:                        Console.WriteLine("{0}踩到了地雷,退6格。", playerName[player]);                        playerPos[player] -= 6;                        Console.ReadKey(true);                        break;                    case 3:                        Console.WriteLine("{0}踩到了暂停,暂停一回合。", playerName[player]);                        Flags[player] = true;                        Console.ReadKey(true);                        break;                    case 4:                        Console.WriteLine("{0}踩到了时空隧道,前进10格。", playerName[player]);                        playerPos[player] += 10;                        Console.ReadKey(true);                        break;                }//switch            }//else            ChangePos();            Console.Clear();            DrawMap();        }    }}
原创粉丝点击