C#编程学习飞行棋小游戏

C#飞行棋代码,如果A玩家踩到地雷,向后退6格的时候,刚好碰到B玩家,两个玩家处同一单元格,算不算“A踩到B”,B玩家要不要也退后6格呢?

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

namespace _04_飞行棋游戏
{
class Program
{
//用静态字段模拟全局变量,因为可能会有很多方法需要用到Maps 数组。
public static int[] Maps = new int[100];
//声明一个静态数组用来存储玩家A和玩家B的坐标
public static int[] PlayerPos = new int[2];
//存储两个玩家的姓名
public static string[] PlayerNames = new string[2];

    public 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("玩家A的姓名不能为空,请重新输入");
            PlayerNames[0] = Console.ReadLine();
        }
        Console.WriteLine("请输入玩家B的姓名");
        PlayerNames[1] = Console.ReadLine();
        while (PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1])
        {
            if (PlayerNames[1] == "")
            {
                Console.WriteLine("玩家B的姓名不能为空,请重新输入");
                PlayerNames[1] = Console.ReadLine();
            }
            else
            {
                Console.WriteLine("玩家B的姓名不能和玩家A相同,请重新输入");
                PlayerNames[1] = Console.ReadLine();
            }
        }
        #endregion
        //玩家输入OK后,首先清屏,且重新输入游戏头
        Console.Clear();//清屏
        GameShow();
        Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
        Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);

        //绘制地图之前,首先必须初始化地图
        InitialMap();
        DrawMap();

        //当A玩家和B玩家没有一个人在终点的时候即playerPos<99的时候 两个玩家不停的去玩游戏
        while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
        {
            if (Flags[0] == false)
            {
                PlayGame(0);//A先玩
            }
            else
            {
                Flags[0] = false;
            }
            if (PlayerPos[0] >= 99)
            {
                Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
                break;
            }
            if (Flags[1] == false)
            {
                PlayGame(1);//B玩
            }
            else
            {
                Flags[1] = false;
            }
            if (PlayerPos[1] >= 99)
            {
                Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
                break;
            }
        }//while

        Win();

        Console.ReadKey();
    }
    /// <summary>
    /// 胜利
    /// </summary>
    public static void Win()//游戏尾
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("******************************");
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("******************************");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("**********获得胜利!***********");
        Console.ForegroundColor = ConsoleColor.DarkBlue;
        Console.WriteLine("******************************");
        Console.ForegroundColor = ConsoleColor.DarkGray;
        Console.WriteLine("******************************");
    }


    /// <summary>
    /// 画游戏头
    /// </summary>
    public static void GameShow()
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("**************************");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("**************************");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("**************************");
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("**********飞行棋**********");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("**************************");
        Console.ForegroundColor = ConsoleColor.Magenta;
        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));
        }//for
        #endregion
        //画完第一横行后换行
        Console.WriteLine();
        #region //第一竖行
        for (int i = 30; i < 35; i++)
        {
            for (int j = 0; j <= 28; j++)
            {
                Console.Write("  ");
            }
            Console.WriteLine(DrawStringMap(i));//直接使用writeline对最后一位换行
            //Console.WriteLine();//每画完一行换行
        }//for
        #endregion

        #region //第二横行
        for (int i = 64; i >= 35; 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();


    }//DrawMap方法的结尾

    /// <summary>
    /// 从画地图的方法中抽象出来的一个方法
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    public static string DrawStringMap(int i)
    {
        string str = "";
        #region//画图
        //如果玩家A和玩家B的坐标相同,并且都在这个地图上,画一个尖括号
        if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)//确保玩家在地图上playerpos==i
        {
            str = "<>";
        }
        else if (PlayerPos[0] == i)//确认A玩家的位置
        {
            str = "A";
        }
        else if (PlayerPos[1] == i)//确认B玩家的位置
        {
            str = "B";
        }
        else//画关卡
        {
            switch (Maps[i])
            {
                case 0:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    str = "□";
                    break;
                case 1:
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    str = "○";
                    break;
                case 2:
                    Console.ForegroundColor = ConsoleColor.Green;
                    str = "☆";
                    break;
                case 3:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    str = "△";
                    break;
                case 4:
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    str = "卐";
                    break;
            }//switch
        }//else
        return str;
        #endregion
    }

    /// <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);
        PlayerPos[PlayerNumber] += rNumber;
        ChangePos();

        Console.ReadKey(true);
        Console.WriteLine("{0}按任意键开始行动", PlayerNames[PlayerNumber]);
        Console.ReadKey(true);
        Console.WriteLine("{0}行动完了", PlayerNames[PlayerNumber]);
        Console.ReadKey(true);
        //玩家A有可能踩到玩家B 方块 幸运轮盘 地雷 暂停 时空隧道
        if (PlayerPos[PlayerNumber] == PlayerPos[1 - PlayerNumber])
        {
            Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[PlayerNumber], PlayerNames[1- PlayerNumber], PlayerNames[1- PlayerNumber]);
            PlayerPos[1- PlayerNumber] -= 6;
            ChangePos();
            Console.WriteLine(true);
        }
        else//踩到了关卡
        {
            //玩家的坐标
            switch (Maps[PlayerPos[PlayerNumber]]) //==0 1 2 3 4
            {
                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}选择跟玩家{1}交换位置", PlayerNames[PlayerNumber], PlayerNames[1- PlayerNumber]);
                            Console.ReadKey(true);
                            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}选择轰炸玩家{1},玩家{2}退后6格", PlayerNames[PlayerNumber], PlayerNames[1- PlayerNumber], PlayerNames[1- PlayerNumber]);
                            Console.ReadKey(true);
                            PlayerPos[1- PlayerNumber] -= 6;
                            ChangePos();
                            Console.WriteLine("玩家{0}退了6格",PlayerNames[1-PlayerNumber]);
                            Console.ReadKey(true);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("只能输入1或者2,1--交换位置  2--轰炸对方");
                            input = Console.ReadLine();
                        }
                    }
                    break;//case 1
                case 2:
                    Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[PlayerNumber]);
                    Console.ReadKey(true);
                    PlayerPos[PlayerNumber] -= 6;
                    ChangePos();
                    //Console.WriteLine("玩家{0}退了6格", PlayerPos[PlayerNumber]);
                    //Console.ReadKey(true);
                    break;
                case 3:
                    Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[PlayerNumber]);
                    Flags[PlayerNumber] = true;
                    Console.ReadKey(true);
                    break;
                case 4:
                    Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[PlayerNumber]);
                    Console.ReadKey(true);
                    PlayerPos[PlayerNumber] += 10;
                    ChangePos();
                    Console.ReadKey(true);
                    break;
            }//switch
        }//else
        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;
        }
    }
}

}