ice_103 2021-08-29 03:31 采纳率: 100%
浏览 21
已结题

控制台飞行棋运行的死循环问题

在代码371行这边,代码封装后出现死循环,为黑屏状态,地图有的,但没有AB角色事件。感觉是传参int 0,1在传到嵌套方法下的问题。
谢谢解答

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

namespace _13飞行棋
{   
    class Program
    {
        //我们用静态字段模拟全局变量
        public static int[] Maps = new int[100];
        //声明一个静态数组来存储玩家A和B的坐标,0是A的坐标,1是B的坐标
        static int[] PlayerPos = new int[2];
        //存储两个玩家的姓名
        static string[] PlayerNames = new string [2];

        static void Main(string[] args)
        {
            GameShow();
            InputPlayerName();
            InitailMap();
            DrawMap();
            PlayerOperation();
        }
        #region 游戏开头图案
        //打印星号
        public static void GameShow_asterisk()
        {
            Console.WriteLine("***************************************");
        }

        //飞行棋开头
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            GameShow_asterisk();
            Console.ForegroundColor = ConsoleColor.Red;
            GameShow_asterisk();
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("**********飞行棋-控制台版**************");
            Console.ForegroundColor = ConsoleColor.Magenta;
            GameShow_asterisk();
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            GameShow_asterisk();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
        }
        #endregion

        #region 输入玩家信息
        public static void InputPlayerName()
        {
            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[1] == PlayerNames[0])
            {
                if (PlayerNames[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入");
                }
                else if(PlayerNames[1] == PlayerNames[0])
                {
                    Console.WriteLine("玩家A和B的姓名不能相同,请重新输入");
                }
                PlayerNames[1] = Console.ReadLine();
            }
            Console.Clear();
            Console.WriteLine();
            GameShow();
            Console.WriteLine("{0}的棋子用A表示\n{1}的棋子用B表示\n", PlayerNames[0], PlayerNames[1]);
        }
        #endregion
 
        #region 初始化地图
        /// <summary>
        /// 初始化地图
        /// </summary>
        /// <param name="Maps">静态地图</param>
        public static void InitailMap()
        {
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//抽奖◎,数组中下标为的6..这些数组存放的值都为1
            for(int i=0;i<luckyturn.Length;i++)
            {
                int index = luckyturn[i];
                Maps[index] = 1;
            }
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
            for (int i = 0; i < landMine.Length; i++)//地雷☆,数组中下标为的5..这些数组存放的值都为2
            {
                int index = landMine[i];
                Maps[index] = 2;
            }
            int[] pause = { 9, 27, 60, 93 };//暂停▲
            for (int i = 0; i < pause.Length; i++)//暂停▲,数组中下标为的9..这些数组存放的值都为3
            {
                int index = pause[i];
                Maps[index] = 3;
            }
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//快速通道卍
            for (int i = 0; i < timeTunnel.Length; i++)//快速通道卍,数组中下标为的20..这些数组存放的值都为4
            {
                int index = timeTunnel[i];
                Maps[index] = 4;
            }
            int[] end = { 99 };
            for (int i = 0; i <end.Length; i++)//终点★,数组中下标为的99..这些数组存放的值都为5
            {
                int index = end[i];
                Maps[index] = 5;
            }
        }
        #endregion

        #region 画地图
        
        #region 画各种图形



        /// <summary>
        /// 用于打印字符串
        /// </summary>
        public static void Str(string str)
        {
            Console.Write(str);
        }
        
        /// <summary>
        /// 画方块,地图的格子
        /// </summary>
        public static void Block()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Str("□");
            Console.BackgroundColor = ConsoleColor.Black;
        }
        
        /// <summary>
        /// 画圆圈,代表抽奖
        /// </summary>
        public static void Circle()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.BackgroundColor = ConsoleColor.White;
            Str("◎");
            Console.BackgroundColor = ConsoleColor.Black;
        }

        /// <summary>
        /// 画星号,代表地雷
        /// </summary>
        public static void Stars()
        {
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.BackgroundColor = ConsoleColor.Yellow;
            Str("☆");
            Console.BackgroundColor = ConsoleColor.Black;
        }

        /// <summary>
        /// 画三角形,代表暂停
        /// </summary>
        public static void Triangle()
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.BackgroundColor = ConsoleColor.Red;
            Str("▲");
            Console.BackgroundColor = ConsoleColor.Black;
        }

        /// <summary>
        /// 画卍字,代表快速通道
        /// </summary>
        public static void Thousandwords()
        {
            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.Magenta;
            Str("卍");
            Console.BackgroundColor = ConsoleColor.Black;
        }

        /// <summary>
        /// 画¤,代表重合点
        /// </summary>
        public static void CoincidencePoint()
        {
            //玩家AB坐标相同的话,并都在地图上,显示为绿色¤
            Console.ForegroundColor = ConsoleColor.Green;
            Str("¤");
        }

        /// <summary>
        /// 画★,代表终点
        /// </summary>
        public static void EndPoint()
        {
            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.Red;
            Str("★");
            Console.BackgroundColor = ConsoleColor.Black;
        }

        /// <summary>
        /// 画字符A
        /// </summary>
        /// //shift+空格全角
        public static void CharacterA()
        {
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Str("A");
        }

        /// <summary>
        /// 画字符B
        /// </summary>
        public static void CharacterB()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Str("B");
        }
        #endregion

        #region 逻辑判断,AB和图形符号
        /// <summary>
        /// 元素符号类型判断,判定AB坐标是否相同
        /// </summary>
        /// <param name="i">0-99号数组中的值</param>
        public static void ABDetermineANDGraphicSymbolJudgment(int i)
        {
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i && PlayerPos[1] == i)
            {
                CoincidencePoint();
            }
            else if (PlayerPos[0] == i)
            {
                CharacterA();
            }
            else if (PlayerPos[1] == i)
            {
                CharacterB();
            }
            else
            {
                //如果不是AB或者重合点则进行进行多条件的判断
                switch (Maps[i])
                {
                    case 0:
                        Block();
                        break;
                    case 1:
                        Circle();
                        break;
                    case 2:
                        Stars();
                        break;
                    case 3:
                        Triangle();
                        break;
                    case 4:
                        Thousandwords();
                        break;
                    case 5:
                        EndPoint();
                        break;
                }
            }
        }
        #endregion

        #region 画横竖行地图(画地图前,首先要求初始化)
        /// <summary>
        /// 画第一行横行
        /// </summary>
        public static void Rampant_1()
        {
            //第一横行
            for (int i = 0; i < 30; i++)
            {
                ABDetermineANDGraphicSymbolJudgment(i);
            }
            //画完第一横行后,进行换行
            Console.WriteLine();
        }
        
        /// <summary>
        /// 画第一竖列
        /// </summary>
        public static void Column_1()
        {
            for(int i = 30;i < 35;i++)
            {
                for(int j = 0;j<=28;j++)
                {
                    Console.Write("  ");
                }
                ABDetermineANDGraphicSymbolJudgment(i);
                //画完第一竖列后,进行换行
                Console.WriteLine();
            }
        }

        /// <summary>
        /// 画第二横行
        /// </summary>
        public static void Rampant_2()
        {
            for(int i =64;i>=35;i--)
            {
                ABDetermineANDGraphicSymbolJudgment(i);
            }
            //画完第二横行后,进行换行
            Console.WriteLine();
        }

        /// <summary>
        /// 画第二竖列
        /// </summary>
        public static void Column_2()
        {
            for(int i =65;i<=69;i++)
            {
                ABDetermineANDGraphicSymbolJudgment(i);
                Console.WriteLine();
            }
        }

        public static void Rampant_3()
        {
            for (int i = 70; i <= 99; i++)
            {
                ABDetermineANDGraphicSymbolJudgment(i);
            }
            //画完第三横行后,进行换行
            Console.WriteLine("\n");
        }

        public static void DrawMap()
        {
            //添加图例
            Console.WriteLine("图例:抽奖:◎\t地雷:☆\t暂停:▲\t快速通道:卍\n");
            //第一横行
            Rampant_1();
            //第一竖列
            Column_1();
            //第二横行
            Rampant_2();
            //第二竖列
            Column_2();
            //第三横行
            Rampant_3();
        }
        #endregion

        #endregion

        /// <summary>
        /// 游戏运行
        /// </summary>
        public static void PlayerOperation()
        {
            
            //当玩家A和玩家B没有一个人在终点的时候,可以一直进行游戏
            while (PlayerPos[0] <99 && PlayerPos[1] <99)
            {
                PlayGames(0);
                PlayGames(1);
            }
        }

        /// <summary>
        /// 封装踩到格子的几种情况
        /// </summary>
        /// <param name="playerNumber"></param>
        public static void PlayGames(int playerNumber)
        {
            //玩家A有可能踩到了玩家B,方块,轮盘,地雷,暂停,快速通道
            if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退后6步", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                PlayerPos[1] = PlayerPos[1 - playerNumber] - 6;
                Console.ReadKey(true);
            }
            else//踩到了关卡
            {
                PlayerEvent(playerNumber);
            }
            Console.Clear();
            DrawMap();
        }

        /// <summary>
        /// AB踩到各类特殊事件的情形
        /// </summary>
        public static void PlayerEvent(int playerNumber)
        {
            //玩家的坐标
            switch (Maps[PlayerPos[playerNumber]])// 0,1,2,3,4,5
            {
                        case 0:Console.WriteLine("玩家{0}踩到了方块,无事发生", PlayerNames[playerNumber]);
                    Console.ReadKey(true);
                    break;
                        case 1:Console.WriteLine("玩家{0}进行抽奖◎,进行以下事件\n1.交换位置\t2.轰炸对方", PlayerNames[playerNumber]);
                    string input = Console.ReadLine();
                    while (true || input == "2")
                    {
                        if (input == "1")
                        {
                            Console.WriteLine("玩家{0}选择和玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1- playerNumber]);
                            Console.ReadKey();
                            //交换玩家1和2的位置
                            int middle = 0;
                            middle = PlayerPos[playerNumber];
                            PlayerPos[playerNumber] = PlayerPos[1- playerNumber];
                            PlayerPos[1- playerNumber] = middle;
                            Console.WriteLine("交换完成!按任意键继续游戏!");
                            Console.ReadKey(true);
                            break;
                        }
                        else if (input == "2")
                        {
                            Console.WriteLine("玩家{0}选择轰炸{1},玩家{1}退4格", PlayerNames[playerNumber], PlayerNames[1- playerNumber]);
                            Console.ReadKey(true);
                            PlayerPos[1] = PlayerPos[1- playerNumber] - 4;
                            Console.WriteLine("玩家{0}退4格", PlayerNames[1- playerNumber]);
                            Console.ReadKey(true);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("请重新输入,只能输入1或2进行以下事件\n1.交换位置\t2.轰炸对方");
                            input = Console.ReadLine();
                        }
                    }
                    break;

                case 2: Console.WriteLine("玩家{0}踩到了地雷☆,玩家{0}退5格", PlayerNames[playerNumber]);
                    Console.ReadKey(true);
                    PlayerPos[playerNumber] = PlayerPos[playerNumber] - 5;
                    break;
                case 3: Console.WriteLine("玩家{0}踩到了暂停▲,玩家{0}暂停一回合", PlayerNames[playerNumber]);
                    Console.ReadKey(true);
                    break;
                case 4: Console.WriteLine("玩家{0}踩到了快速通道卍,玩家{0}暂停一回合", PlayerNames[playerNumber]);
                    Console.ReadKey(true);
                    PlayerPos[playerNumber] = PlayerPos[playerNumber] + 5;
                    break;
                case 5: Console.WriteLine("玩家{0}通关了!!!!", PlayerNames[playerNumber]);
                    Console.ReadKey(true);
                    break;
            }
        }

        /// <summary>
        /// AB踩到常规的情形
        /// </summary>
        public static void PlayerAction(int playerNumber)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            int r = RandomNumber();
            Console.WriteLine("{0}得到了{1}点", PlayerNames[playerNumber], r);
            PlayerPos[0] += r;
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
            for (int i = 0; i < r; i++)
            {
                Console.WriteLine("往前走了一步");
            }
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完了", PlayerNames[playerNumber]);
        }

        /// <summary>
        /// 取得随机数
        /// </summary>
        /// <returns>随机数1-6</returns>
        public static int RandomNumber()
        {
            Random random = new Random();
            return random.Next(1,6);
        }
    }

        
}




  • 写回答

1条回答 默认 最新

  • ice_103 2021-08-29 10:06
    关注

    想了很久,搞定了,不用麻烦大家了

     /// <summary>
            /// 游戏运行
            /// </summary>
            public static void PlayerOperation()
            {
                
                //当玩家A和玩家B没有一个人在终点的时候,可以一直进行游戏
                while (PlayerPos[0] <99 && PlayerPos[1] <99)
                {
                    int n1 = 0;
                    int n2 = 1;
                    PlayGames(ref n1);
                    PlayGames(ref n2);
                }
            }
    
            /// <summary>
            /// 封装踩到格子的几种情况
            /// </summary>
            /// <param name="playerNumber"></param>
            public static void PlayGames(ref int playerNumber)
            {
                PlayerAction(ref playerNumber);
                //玩家A有可能踩到了玩家B,方块,轮盘,地雷,暂停,快速通道
                if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
                {
                    Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退后6步", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                    PlayerPos[1] = PlayerPos[1 - playerNumber] - 6;
                    Console.ReadKey(true);
                }
                else 
                    //踩到了关卡
                {
                    PlayerEvent(ref playerNumber);
                }
                Console.Clear();
                DrawMap();
            }
    
            /// <summary>
            /// AB踩到各类特殊事件的情形
            /// </summary>
            public static void PlayerEvent(ref int playerNumber)
            {
                //玩家的坐标
                switch (Maps[PlayerPos[playerNumber]])// 0,1,2,3,4,5
                {
                            case 0:Console.WriteLine("玩家{0}踩到了方块,无事发生", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                            case 1:Console.WriteLine("玩家{0}进行抽奖◎,进行以下事件\n1.交换位置\t2.轰炸对方", PlayerNames[playerNumber]);
                        string input = Console.ReadLine();
                        while (true || input == "2")
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}选择和玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1- playerNumber]);
                                Console.ReadKey();
                                //交换玩家1和2的位置
                                int middle = 0;
                                middle = PlayerPos[playerNumber];
                                PlayerPos[playerNumber] = PlayerPos[1- playerNumber];
                                PlayerPos[1- playerNumber] = middle;
                                Console.WriteLine("交换完成!按任意键继续游戏!");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸{1},玩家{1}退4格", PlayerNames[playerNumber], PlayerNames[1- playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1] = PlayerPos[1- playerNumber] - 4;
                                Console.WriteLine("玩家{0}退4格", PlayerNames[1- playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("请重新输入,只能输入1或2进行以下事件\n1.交换位置\t2.轰炸对方");
                                input = Console.ReadLine();
                            }
                        }
                        break;
    
                    case 2: Console.WriteLine("玩家{0}踩到了地雷☆,玩家{0}退5格", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] = PlayerPos[playerNumber] - 5;
                        break;
                    case 3: Console.WriteLine("玩家{0}踩到了暂停▲,玩家{0}暂停一回合", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 4: Console.WriteLine("玩家{0}踩到了快速通道卍,玩家{0}暂停一回合", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playerNumber] = PlayerPos[playerNumber] + 5;
                        break;
                    case 5: Console.WriteLine("玩家{0}通关了!!!!", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                }
            }
    
            /// <summary>
            /// AB踩到常规的情形
            /// </summary>
            public static void PlayerAction(ref int playerNumber)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
                Console.ReadKey(true);
                int r = RandomNumber();
                Console.WriteLine("{0}得到了{1}点", PlayerNames[playerNumber], r);
                PlayerPos[0] += r;
                Console.ReadKey(true);
                Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
                for (int i = 0; i < r; i++)
                {
                    Console.WriteLine("往前走了一步");
                }
                Console.ReadKey(true);
                Console.WriteLine("{0}行动完了", PlayerNames[playerNumber]);
            }
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 9月6日
  • 创建了问题 8月29日

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?