这是在CSDN的第一篇博客,写博客的目的是记录学习C#和Unity引擎的经验。

已经在北京学习了2周多了,期间做过2次周作业:

第一次做了一个文字游戏Survive,技术含量不高,主要是用心做了剧情。

第二次做的是4人飞行棋,这篇博客主要总结做飞行棋的经验和教训。

火星学习C#第二周作业-四人飞行棋



总结分为:  教训、思路、代码三个部分。

一.教训

第二周的周二就开始着手画棋盘,经历下面阶段:

1.用做习题打印图形的办法,在控制台做了棋盘外圈,内圈一个个找点画出来(只想先画出来);

2.因为不知道外圈每个点坐标,又全部用手一个一个点画出来(想知道每个点的坐标);

3.设置一个起始点,以地图每个边的数量按不同方向走出一个地图,存入一维数组(if里有很长的判断条件);

4.周五听了老师用算法画回形地图,然后才发现一直没弄清楚标准的地图是怎样的,其实标准的地图是有规律的每个能被3整除的边是5格,其他都是4格,方向的改变也是有规律的,完全可以用算法解决,于是又研究算法寸入一维数组;

5.画完一维数组才发现没办法让4个玩家在不同起点开始,不同终点结束,咨询过李老师发现必须用2维数组画地图,然后用4个一维数组记录玩家的路径,于是周日上午一直在想二维数组怎么实现,时间就1天很焦虑,中午的时候和同学交流这个疑问时,发现可以先用EXCEL表格形象的画出二维数组再去研究算法,于是下午用之前算法的变体画出了地图。

火星学习C#第二周作业-四人飞行棋

在画棋盘的过程中真是把能踩的坑都踩了,造成这样的原因是一开头没有想明白结构和做法就埋头写代码,造成时间的浪费,同时也没有和同学老师及时交流设计思路,今后要先着眼于总体结构,然后再做好细节,勤于交流。

除了画地图,写代码中也有很多教训,其中最费时间找BUG的错误是写if else时忘记了写else,还有就是处理超过索引最大值得方法没有放在使用索引的第一个位置之前。这2个问题归根结底还是写代码的时候脑子里对逻辑结构没有清晰的思路。于是这个游戏很多想做的功能没有做出来,只做了跳步,一步一步走,越过终点退回三个功能,不过最重要的是有4个玩家可以一起玩。

还有一个重点,以后遇到问题一定要拆分问题,寻找规律,而且不能懒,以图形化的方式来帮助理解,这条真的挺重要。


二.思路

设计思路是先用一个算法走出地图并存入一个二维数组,然后通过不同4个切入点和起始方向记录4个玩家的行走路线,存入一维数组。用枚举做状态,都存在地图上,同一个状态按颜色名区分属于哪个玩家,以便后面判断玩家到达此位置该做什么操作。采用回合制让4个玩家轮流丢筛子前进,用一个每个回合加1的count记录回合数字,在最开始判断count%4,如果等于0是玩家1,以此类推。在所有判断结束后使用for循环来一步一步打印玩家的位置,for循环的次数是增加的步数。返回循环的开始用2个for循环打印地图和玩家位置。


三.代码

做完这个游戏最大的收获是对算法和二维数组的进一步理解。

下面是方向的算法,根据不同的起始方向进行判断。

newLoopCount是画地图框的次数,这里是12次。

startDirection是一个结构体,包含x和y轴。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void CalDirection(int newLoopCount,Vector2 startDirection)//根据起始方向,画的次数来计算方向
        {
            if (newLoopCount <= 6 && newLoopCount % 2 != 0)
            {
                direction.x = startDirection.x;//1  0
                direction.y = startDirection.y;//0  1
            }
            else if (newLoopCount <= 6 && newLoopCount != 2 && newLoopCount % 2 == 0 || newLoopCount > 6 && newLoopCount != 10 && newLoopCount != 12 && newLoopCount % 2 == 0)
            {
                direction.x = -startDirection.y;//0  -1
                direction.y =  startDirection.x;//1   0
            }
            else if (newLoopCount > 6 && newLoopCount % 2 != 0)
            {
                direction.x = -startDirection.x;//-1  0
                direction.y = -startDirection.y;//0   -1
            }
            else if (newLoopCount == 2 || newLoopCount == 10|| newLoopCount == 12)
            {
                direction.x = startDirection.y;//0    1
                direction.y = -startDirection.x;//-1  0
            }

        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

下面是设置地图数据的算法:

map是二维数组,存地图数据;

a,b是屏幕打印的起始点坐标;

indexX,indexY是起始点对应的二维数组的位置坐标;

startDirection是起始方向,可以是4个方向,例如从左至右是x=1,y=0;从上至下是x=0,y=1,从下至上是x=0,y=-1,从右至左是x=-1,y=0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void SetMapData(MapCell[,] map, int a, int b, int indexX, int indexY, Vector2 startDirection)//设置二维数组数据
        {
            int index = 0;
            map[indexX, indexY].mapPosition.x = a;
            map[indexX, indexY].mapPosition.y = b;




            for (int i = 1; i <= loopCount; i++)
            {
                this.CalDirection(i, startDirection);
                if (i % 3 == 0)//如果路径数能被3整除则画5个地图格
                {
                    for (int j = 0; j < 5; j++)
                    {
                        int calState = index % 4;
                        switch (calState.ToString())
                        {
                            case "0":
                                map[indexX, indexY].mapState = MapState.GreenJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundGreen;
                                break;
                            case "1":
                                map[indexX, indexY].mapState = MapState.YellowJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundYellow;
                                break;
                            case "2":
                                map[indexX, indexY].mapState = MapState.BlueJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundBlue;
                                break;
                            case "3":
                                map[indexX, indexY].mapState = MapState.RedJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundRed;
                                break;
                        }
                        if (!(i == loopCount && j == 4))//当是最后一次循环的时候,索引不能再增加
                        {
                            index++;
                            indexX += direction.y;
                            indexY += direction.x;
                            map[indexX, indexY].mapPosition.x = map[indexX - direction.y, indexY - direction.x].mapPosition.x + direction.x;
                            map[indexX, indexY].mapPosition.y = map[indexX - direction.y, indexY - direction.x].mapPosition.y + direction.y;
                            
                        }
                        
                    }
                }
                else
                {
                    for (int k = 0; k < 4; k++)
                    {
                        int calState = index % 4;
                        switch (calState.ToString())
                        {
                            case "0":
                                map[indexX, indexY].mapState = MapState.GreenJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundGreen;
                                break;
                            case "1":
                                map[indexX, indexY].mapState = MapState.YellowJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundYellow;
                                break;
                            case "2":
                                map[indexX, indexY].mapState = MapState.BlueJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundBlue;
                                break;
                            case "3":
                                map[indexX, indexY].mapState = MapState.RedJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundRed;
                                break;
                        }
                        index++;
                        indexX += direction.y;
                        indexY += direction.x;
                        map[indexX, indexY].mapPosition.x = map[indexX - direction.y, indexY - direction.x].mapPosition.x + direction.x;
                        map[indexX, indexY].mapPosition.y = map[indexX - direction.y, indexY - direction.x].mapPosition.y + direction.y;


                        
                    }
                }

            }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

下面是设置4个玩家路径的方法,其实就是设置地图的方法。

在每次画一个点时,一维数组的索引+1,不过也是根据不同方向绘制的,所以4个玩家都是不同的路径。

由于玩家的一维数组只要存路径上每个点的二维数组地址坐标即可,所以参数里只有int indexX, int indexY这2个二维数组的坐标 players[index].mapPos.x = indexX; players[index].mapPos.y = indexY; 

存玩家路径的一维数组是Players[] players。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void GetPlayerLoop(MapCell[,] map, Players[] players, int indexX, int indexY, Vector2 startDirection)
        {
            int index = 0;
            players[index].mapPos.x = indexX;
            players[index].mapPos.y = indexY;
                    
            for (int i = 1; i <= loopCount; i++)
            {
                this.CalDirection(i, startDirection);
                if (i % 3 == 0)//如果路径数能被3整除则画5个地图格
                {
                    for (int j = 0; j < 5; j++)
                    {
                        if (!(i == loopCount && j == 4))//当是最后一次循环的时候,索引不能再增加
                        {
                            index++;
                            indexX += direction.y;
                            indexY += direction.x;
                            players[index].mapPos.x = indexX;
                            players[index].mapPos.y = indexY;
                        }
                    }
                                     
                }
                else
                {
                    for (int k = 0; k < 4; k++)
                    {
                        index++;
                        indexX += direction.y;
                        indexY += direction.x;
                        players[index].mapPos.x = indexX;
                        players[index].mapPos.y = indexY;
                    }  
                                     
                }
                
            }

        }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

写到这基本也就总结完这次飞行棋作业,第三周作业是做2048,希望做完后能有更多经验积累。

PS. 下午老师按例展示每周优秀作品,北京地区着重介绍了我的游戏,极大的鼓励了我,再接再厉!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

下面写上全部代码,主要是2个类:Program和SetData。

Program:

 struct Vector2
    {
        public int x;
        public int y;
        public Vector2(int a, int b)
        {
            x = a;
            y = b;
        }
    }


    enum MapState
    {
        Empty,
        Normal,
        GreenJump,
        YellowJump,
        BlueJump,
        RedJump,
        StartPoint,
        EndPoint
    }
    enum Color
    {
        BackGroundGreen,
        ForeGroundGreen,
        BackGroundYellow,
        ForeGroundYellow,
        BackGroundRed,
        ForeGroundRed,
        BackGroundBlue,
        ForeGroundBlue,
        BackGroundWhite,
        ForeGroundWhite,
        BackGroundBlack,
        ForeGroundBlack,
    }


    class Program
    {
        static void Main(string[] args)
        {
            //设计思路
            //先用一个二维数组用算法打印出地图,然后再从这个二维数组中提取出2个玩家的独自路径,放入一个一维数组


            SetData setData = new SetData();//实例化SetData
            Players[] PlayerOneLoop = new Players[setData.CalArrayLength(setData.loopCount)];//建立玩家1的路径数组
            Players[] PlayerTwoLoop = new Players[setData.CalArrayLength(setData.loopCount)];//建立玩家2的路径数组
            Players[] PlayerThreeLoop = new Players[setData.CalArrayLength(setData.loopCount)];//建立玩家3的路径数组
            Players[] PlayerFourLoop = new Players[setData.CalArrayLength(setData.loopCount)];//建立玩家4的路径数组
            MapCell[,] Map = new MapCell[14, 14];
            Vector2 startDirectionOne = new Vector2(1, 0);//玩家1的起始方向,用于CalDirection
            Vector2 startDirectionTwo = new Vector2(0, 1);//玩家2的起始方向,用于CalDirection
            Vector2 startDirectionThree = new Vector2(-1, 0);//玩家3的起始方向,用于CalDirection
            Vector2 startDirectionFour = new Vector2(0, -1);//玩家4的起始方向,用于CalDirection


            Random roll = new Random();
            int randomNum = 0;
            int indexNumOne = 0;
            int indexNumTwo = 0;
            int indexNumThree = 0;
            int indexNumFour = 0;
            int count = 0;
            bool isGameOver = false;
            bool isJump = false;
            bool isOverFlow = false;




            #region 初始化


            //初始化二维数组里每个元素
            for (int j = 0; j < Map.GetLength(1); j++)
            {
                for (int i = 0; i < Map.GetLength(0); i++)
                {
                    Map[i, j] = new MapCell(0, 0, MapState.Empty, Color.ForeGroundWhite);
                }
            }
            //初始化玩家1的一维数组
            for (int i = 0; i < PlayerOneLoop.Length; i++)
            {
                PlayerOneLoop[i] = new Players("玩家1", 0, 0, 0, Color.ForeGroundWhite);
            }
            //初始化玩家2的一维数组
            for (int i = 0; i < PlayerTwoLoop.Length; i++)
            {
                PlayerTwoLoop[i] = new Players("玩家2", 1, 0, 0, Color.ForeGroundWhite);
            }
            //初始化玩家3的一维数组
            for (int i = 0; i < PlayerOneLoop.Length; i++)
            {
                PlayerThreeLoop[i] = new Players("玩家3", 2, 0, 0, Color.ForeGroundWhite);
            }
            //初始化玩家4的一维数组
            for (int i = 0; i < PlayerTwoLoop.Length; i++)
            {
                PlayerFourLoop[i] = new Players("玩家4", 3, 0, 0, Color.ForeGroundWhite);
            }


            //设置玩家1的路径参数           
            setData.GetPlayerLoop(Map, PlayerOneLoop, 4, 0, startDirectionOne);//从二维数组中提取出玩家1的路径


            ////设置玩家2的路径参数
            setData.GetPlayerLoop(Map, PlayerTwoLoop, 0, 9, startDirectionTwo);//从二维数组中提取出玩家2的路径


            //设置玩家3的路径参数           
            setData.GetPlayerLoop(Map, PlayerThreeLoop, 9, 13, startDirectionThree);//从二维数组中提取出玩家3的路径


            ////设置玩家4的路径参数
            setData.GetPlayerLoop(Map, PlayerFourLoop, 13, 4, startDirectionFour);//从二维数组中提取出玩家4的路径


            //设置游戏地图
            setData.SetMapData(Map, 13, 9, 4, 0, startDirectionOne);
            //PlayerOneLoop屏幕打印点的x,y(13,9),二维数组的起始点坐标x,y(4.0)
            //PlayerTwoLoop屏幕打印点的x,y(22,5),二维数组的起始点坐标x,y(0.9) 
            #endregion


            Console.Clear();
            Console.SetCursorPosition(17 * 2, 9);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Welcome!");
            Console.SetCursorPosition(15 * 2, 11);
            Console.WriteLine("四人飞行棋初代版");
            Console.SetCursorPosition(12 * 2, 14);
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("既然是下棋,按任意键直接开始");
            Console.ForegroundColor = ConsoleColor.White;
            Console.ReadKey(true);
            //回合开始~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


            #region 游戏回合
            while (!isGameOver)
            {


                // 打印二维数组里的地图,4个玩家位置,4个终点标记


                Console.Clear();
                PrintMap(Map);
                PrintPlayer(PlayerOneLoop, indexNumOne, Map);
                PrintPlayer(PlayerTwoLoop, indexNumTwo, Map);
                PrintPlayer(PlayerThreeLoop, indexNumThree, Map);
                PrintPlayer(PlayerFourLoop, indexNumFour, Map);
                PrintEndPos();
                Console.ReadKey(true);


                //状态判断           
                randomNum = roll.Next(1, 7);


                #region 状态判断
                int round = count % 4;
                switch (round.ToString())
                {
                    case "0":
                        PrintPlayerOneLetter(randomNum);
                        indexNumOne += randomNum;
                        indexNumOne = HandleOverFlow(indexNumOne, PlayerOneLoop.Length, randomNum, isOverFlow);//处理溢出
                        int tempX1 = PlayerOneLoop[indexNumOne].mapPos.x;
                        int tempY1 = PlayerOneLoop[indexNumOne].mapPos.y;
                        switch (Map[tempX1, tempY1].mapState)
                        {
                            case MapState.GreenJump: indexNumOne += 3; isJump = true; break;//踩到绿色环跳3步
                        }                  
                        PrintPlayer(PlayerOneLoop, indexNumOne, Map, randomNum, isJump, isOverFlow);
                        break;
                    case "1":
                        PrintPlayerTwoLetter(randomNum);
                        indexNumTwo += randomNum;
                        indexNumTwo = HandleOverFlow(indexNumTwo, PlayerTwoLoop.Length, randomNum, isOverFlow);//处理溢出
                        int tempX2 = PlayerTwoLoop[indexNumTwo].mapPos.x;
                        int tempY2 = PlayerTwoLoop[indexNumTwo].mapPos.y;                       
                        switch (Map[tempX2, tempY2].mapState)
                        {
                            case MapState.YellowJump: indexNumTwo += 3; isJump = true; break;//踩到黄色环跳3步
                        }                       
                        PrintPlayer(PlayerTwoLoop, indexNumTwo, Map, randomNum, isJump, isOverFlow);
                        break;
                    case "2":
                        PrintPlayerThreeLetter(randomNum);
                        indexNumThree += randomNum;
                        indexNumThree = HandleOverFlow(indexNumThree, PlayerThreeLoop.Length, randomNum, isOverFlow);//处理溢出
                        int tempX3 = PlayerThreeLoop[indexNumThree].mapPos.x;
                        int tempY3 = PlayerThreeLoop[indexNumThree].mapPos.y;
                        switch (Map[tempX3, tempY3].mapState)
                        {
                            case MapState.BlueJump: indexNumThree += 3; isJump = true; break;//踩到蓝色环跳3步                               
                        }                  
                        PrintPlayer(PlayerThreeLoop, indexNumThree, Map, randomNum, isJump, isOverFlow);
                        break;
                    case "3":
                        PrintPlayerFourLetter(randomNum);
                        indexNumFour += randomNum;
                        indexNumFour = HandleOverFlow(indexNumFour, PlayerFourLoop.Length, randomNum, isOverFlow);//处理溢出 
                        int tempX4 = PlayerFourLoop[indexNumFour].mapPos.x;
                        int tempY4 = PlayerFourLoop[indexNumFour].mapPos.y;
                        switch (Map[tempX4, tempY4].mapState)
                        {
                            case MapState.RedJump: indexNumFour += 3; isJump = true; break;//踩到红色环跳3步
                        }                                             
                        PrintPlayer(PlayerFourLoop, indexNumFour, Map, randomNum, isJump,isOverFlow);
                        break;
                }
                isJump = false;
                isOverFlow = false;


                #endregion
                //判断游戏结束,超过终点退回
                #region 判断游戏结束
                if (indexNumOne == PlayerOneLoop.Length - 1)
                {
                    Console.SetCursorPosition(29, 0);
                    Console.WriteLine("玩家1获得胜利,游戏结束。");
                    isGameOver = true;
                }
                if (indexNumTwo == PlayerTwoLoop.Length - 1)
                {
                    Console.SetCursorPosition(29, 0);
                    Console.WriteLine("玩家2获得胜利,游戏结束。");
                    isGameOver = true;
                }
                if (indexNumThree == PlayerThreeLoop.Length - 1)
                {
                    Console.SetCursorPosition(29, 0);
                    Console.WriteLine("玩家3获得胜利,游戏结束。");
                    isGameOver = true;
                }
                if (indexNumFour == PlayerFourLoop.Length - 1)
                {
                    Console.SetCursorPosition(29, 0);
                    Console.WriteLine("玩家4获得胜利,游戏结束。");
                    isGameOver = true;
                }
                #endregion




                //回合计数
                count++;


            }
            Console.ReadKey(true);
        }
        #endregion


        //回合结束~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


        //下面都是静态方法
            #region 静态方法
        public static int HandleOverFlow(int indexNum, int length, int randomNum,bool isOverFlow)
        {
                if (indexNum > length - 1)
                {
                    indexNum -= randomNum;
                    indexNum = (length - 1) - (randomNum - ((length - 1) - indexNum));
                    isOverFlow = true;
                }          
                return indexNum;
        }


        public static void PrintPlayer(Players[] players, int newIndex, MapCell[,] mapCell, int randomNum, bool isJump,bool isOverFlow)
        {
                int count = 0;
                int loop = 0;
            if (isOverFlow)
            {
                if (isJump)
                {
                    count = newIndex- 3;
                    loop = 3;
                }
                else
                {
                    loop = 1;
                    count = newIndex-1;
                }
                for (int i = 0; i < loop; i++)
                {
                    count++;
                    Console.SetCursorPosition(mapCell[players[count].mapPos.x, players[count].mapPos.y].mapPosition.x * 2, mapCell[players[count].mapPos.x, players[count].mapPos.y].mapPosition.y);
                    switch (players[count].id.ToString())
                    {
                        case "0": Console.ForegroundColor = ConsoleColor.Green; break;
                        case "1": Console.ForegroundColor = ConsoleColor.Yellow; break;
                        case "2": Console.ForegroundColor = ConsoleColor.Blue; break;
                        case "3": Console.ForegroundColor = ConsoleColor.Red; break;
                    }
                    Console.WriteLine("☆");
                    Thread.Sleep(300);
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
            else
            {
                if (isJump)
                {
                    count = newIndex - randomNum - 3;
                    loop = randomNum + 3;
                }
                else
                {
                    loop = randomNum;
                    count = newIndex - randomNum;
                }
                for (int i = 0; i < loop; i++)
                {
                    count++;
                    Console.SetCursorPosition(mapCell[players[count].mapPos.x, players[count].mapPos.y].mapPosition.x * 2, mapCell[players[count].mapPos.x, players[count].mapPos.y].mapPosition.y);
                    switch (players[count].id.ToString())
                    {
                        case "0": Console.ForegroundColor = ConsoleColor.Green; break;
                        case "1": Console.ForegroundColor = ConsoleColor.Yellow; break;
                        case "2": Console.ForegroundColor = ConsoleColor.Blue; break;
                        case "3": Console.ForegroundColor = ConsoleColor.Red; break;
                    }
                    Console.WriteLine("☆");
                    Thread.Sleep(300);
                    Console.ForegroundColor = ConsoleColor.White;
                }
            }
                
        }


        public static void PrintPlayer(Players[] players, int newIndex, MapCell[,] mapCell)
        {
            Console.SetCursorPosition(mapCell[players[newIndex].mapPos.x, players[newIndex].mapPos.y].mapPosition.x * 2, mapCell[players[newIndex].mapPos.x, players[newIndex].mapPos.y].mapPosition.y);
            switch (players[newIndex].id.ToString())
            {
                case "0": Console.ForegroundColor = ConsoleColor.Green; break;
                case "1": Console.ForegroundColor = ConsoleColor.Yellow; break;
                case "2": Console.ForegroundColor = ConsoleColor.Blue; break;
                case "3": Console.ForegroundColor = ConsoleColor.Red; break;
            }
            Console.WriteLine("☆");
            Console.ForegroundColor = ConsoleColor.White;
        }


        public static void PrintMap(MapCell[,] mapCell)
        {
            for (int j = 0; j < mapCell.GetLength(1); j++)
            {
                for (int i = 0; i < mapCell.GetLength(0); i++)
                {
                    Console.SetCursorPosition(mapCell[i, j].mapPosition.x * 2, mapCell[i, j].mapPosition.y);


                    switch (mapCell[i, j].mapColor)
                    {
                        case Color.BackGroundGreen: Console.BackgroundColor = ConsoleColor.Green; break;
                        case Color.ForeGroundGreen: Console.ForegroundColor = ConsoleColor.Green; break;
                        case Color.BackGroundYellow: Console.BackgroundColor = ConsoleColor.Yellow; break;
                        case Color.ForeGroundYellow: Console.ForegroundColor = ConsoleColor.Yellow; break;
                        case Color.BackGroundRed: Console.BackgroundColor = ConsoleColor.Red; break;
                        case Color.ForeGroundRed: Console.ForegroundColor = ConsoleColor.Red; break;
                        case Color.BackGroundBlue: Console.BackgroundColor = ConsoleColor.Blue; break;
                        case Color.ForeGroundBlue: Console.ForegroundColor = ConsoleColor.Blue; break;
                        case Color.BackGroundWhite: Console.BackgroundColor = ConsoleColor.White; break;
                        case Color.ForeGroundWhite: Console.ForegroundColor = ConsoleColor.White; break;
                        case Color.BackGroundBlack: Console.BackgroundColor = ConsoleColor.Black; break;
                        case Color.ForeGroundBlack: Console.ForegroundColor = ConsoleColor.Black; break;
                    }
                    switch (mapCell[i, j].mapState)
                    {
                        case MapState.Normal: Console.Write("□"); break;
                        case MapState.BlueJump:
                        case MapState.RedJump:
                        case MapState.GreenJump:
                        case MapState.YellowJump: Console.Write("□"); break;
                        case MapState.StartPoint: Console.Write("●"); break;
                        case MapState.EndPoint: Console.Write("★"); break;
                    }
                    Console.ForegroundColor = ConsoleColor.White;//改回默认前景色颜色
                    Console.BackgroundColor = ConsoleColor.Black;//改回默认背景色颜色
                }
            }
        }


        public static void PrintEndPos()
        {
            //玩家1的结束位置
            Console.SetCursorPosition(12 * 2, 10);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("→");
            Console.ForegroundColor = ConsoleColor.White;
            //玩家2的结束位置
            Console.SetCursorPosition(21 * 2, 4);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("↓");
            Console.ForegroundColor = ConsoleColor.White;
            //玩家3的结束位置
            Console.SetCursorPosition(27 * 2, 13);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("←");
            Console.ForegroundColor = ConsoleColor.White;
            //玩家4的结束位置
            Console.SetCursorPosition(18 * 2, 19);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("↑");
            Console.ForegroundColor = ConsoleColor.White;
        }


        public static void PrintPlayerOneLetter(int randomNum)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(2, 2);
            Console.WriteLine("这是玩家1的回合");
            Console.SetCursorPosition(2, 3);
            Console.WriteLine("请丢筛子");
            Console.ReadKey(true);
            Console.SetCursorPosition(2, 4);
            Console.WriteLine($"筛子的点数是{randomNum}");
            Console.ReadKey(true);
            Console.SetCursorPosition(2, 5);
            Console.WriteLine($"玩家1走{randomNum}步");
            Console.ReadKey(true);
        }
        public static void PrintPlayerTwoLetter(int randomNum)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.SetCursorPosition(63, 2);
            Console.WriteLine("这是玩家2的回合");
            Console.SetCursorPosition(63, 3);
            Console.WriteLine("请丢筛子");
            Console.ReadKey(true);
            Console.SetCursorPosition(63, 4);
            Console.WriteLine($"筛子的点数是{randomNum}");
            Console.ReadKey(true);
            Console.SetCursorPosition(63, 5);
            Console.WriteLine($"玩家2走{randomNum}步");
            Console.ReadKey(true);
        }


        public static void PrintPlayerThreeLetter(int randomNum)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(63, 18);
            Console.WriteLine("这是玩家3的回合");
            Console.SetCursorPosition(63, 19);
            Console.WriteLine("请丢筛子");
            Console.ReadKey(true);
            Console.SetCursorPosition(63, 20);
            Console.WriteLine($"筛子的点数是{randomNum}");
            Console.ReadKey(true);
            Console.SetCursorPosition(63, 21);
            Console.WriteLine($"玩家3走{randomNum}步");
            Console.ReadKey(true);
        }




        public static void PrintPlayerFourLetter(int randomNum)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(2, 18);
            Console.WriteLine("这是玩家4的回合");
            Console.SetCursorPosition(2, 19);
            Console.WriteLine("请丢筛子");
            Console.ReadKey(true);
            Console.SetCursorPosition(2, 20);
            Console.WriteLine($"筛子的点数是{randomNum}");
            Console.ReadKey(true);
            Console.SetCursorPosition(2, 21);
            Console.WriteLine($"玩家4走{randomNum}步");
            Console.ReadKey(true);
        }


        #endregion

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SetData:

 class SetData
    {
        public Vector2 direction = new Vector2(0, 0);//用于CalDirection,SetPlayerMap,存计算后的方向
        public int loopCount = 12;//用于SetPlayerMap
        public int indexX = 4;//用于SetPlayerMap
        public int indexY = 0;//用于SetPlayerMap
        
        #region 计算方向
        public void CalDirection(int newLoopCount,Vector2 startDirection)//根据起始方向,画的次数来计算方向
        {
            if (newLoopCount <= 6 && newLoopCount % 2 != 0)
            {
                direction.x = startDirection.x;//1  0
                direction.y = startDirection.y;//0  1
            }
            else if (newLoopCount <= 6 && newLoopCount != 2 && newLoopCount % 2 == 0 || newLoopCount > 6 && newLoopCount != 10 && newLoopCount != 12 && newLoopCount % 2 == 0)
            {
                direction.x = -startDirection.y;//0  -1
                direction.y =  startDirection.x;//1   0
            }
            else if (newLoopCount > 6 && newLoopCount % 2 != 0)
            {
                direction.x = -startDirection.x;//-1  0
                direction.y = -startDirection.y;//0   -1
            }
            else if (newLoopCount == 2 || newLoopCount == 10|| newLoopCount == 12)
            {
                direction.x = startDirection.y;//0    1
                direction.y = -startDirection.x;//-1  0
            }
        }
        #endregion


        #region 设置地图参数
        public void SetMapData(MapCell[,] map, int a, int b, int indexX, int indexY, Vector2 startDirection)//设置二维数组数据
        {
            int index = 0;
            map[indexX, indexY].mapPosition.x = a;
            map[indexX, indexY].mapPosition.y = b;




            for (int i = 1; i <= loopCount; i++)
            {
                this.CalDirection(i, startDirection);
                if (i % 3 == 0)//如果路径数能被3整除则画5个地图格
                {
                    for (int j = 0; j < 5; j++)
                    {
                        int calState = index % 4;
                        switch (calState.ToString())
                        {
                            case "0":
                                map[indexX, indexY].mapState = MapState.GreenJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundGreen;
                                break;
                            case "1":
                                map[indexX, indexY].mapState = MapState.YellowJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundYellow;
                                break;
                            case "2":
                                map[indexX, indexY].mapState = MapState.BlueJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundBlue;
                                break;
                            case "3":
                                map[indexX, indexY].mapState = MapState.RedJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundRed;
                                break;
                        }
                        if (!(i == loopCount && j == 4))//当是最后一次循环的时候,索引不能再增加
                        {
                            index++;
                            indexX += direction.y;
                            indexY += direction.x;
                            map[indexX, indexY].mapPosition.x = map[indexX - direction.y, indexY - direction.x].mapPosition.x + direction.x;
                            map[indexX, indexY].mapPosition.y = map[indexX - direction.y, indexY - direction.x].mapPosition.y + direction.y;
                            
                        }
                        
                    }
                }
                else
                {
                    for (int k = 0; k < 4; k++)
                    {
                        int calState = index % 4;
                        switch (calState.ToString())
                        {
                            case "0":
                                map[indexX, indexY].mapState = MapState.GreenJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundGreen;
                                break;
                            case "1":
                                map[indexX, indexY].mapState = MapState.YellowJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundYellow;
                                break;
                            case "2":
                                map[indexX, indexY].mapState = MapState.BlueJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundBlue;
                                break;
                            case "3":
                                map[indexX, indexY].mapState = MapState.RedJump;
                                map[indexX, indexY].mapColor = Color.ForeGroundRed;
                                break;
                        }
                        index++;
                        indexX += direction.y;
                        indexY += direction.x;
                        map[indexX, indexY].mapPosition.x = map[indexX - direction.y, indexY - direction.x].mapPosition.x + direction.x;
                        map[indexX, indexY].mapPosition.y = map[indexX - direction.y, indexY - direction.x].mapPosition.y + direction.y;


                        
                    }
                }
            }




        }
        #endregion


        #region 提取玩家路径
        public void GetPlayerLoop(MapCell[,] map, Players[] players, int indexX, int indexY, Vector2 startDirection)
        {
            int index = 0;
            players[index].mapPos.x = indexX;
            players[index].mapPos.y = indexY;
                    
            for (int i = 1; i <= loopCount; i++)
            {
                this.CalDirection(i, startDirection);
                if (i % 3 == 0)//如果路径数能被3整除则画5个地图格
                {
                    for (int j = 0; j < 5; j++)
                    {
                        if (!(i == loopCount && j == 4))//当是最后一次循环的时候,索引不能再增加
                        {
                            index++;
                            indexX += direction.y;
                            indexY += direction.x;
                            players[index].mapPos.x = indexX;
                            players[index].mapPos.y = indexY;
                        }
                    }
                                     
                }
                else
                {
                    for (int k = 0; k < 4; k++)
                    {
                        index++;
                        indexX += direction.y;
                        indexY += direction.x;
                        players[index].mapPos.x = indexX;
                        players[index].mapPos.y = indexY;
                    }  
                                     
                }
                
            }


        }
 
        #endregion


        #region 计算一维数组长度
        public int CalArrayLength(int loopCount)
        {
            int arrayLength = 0;
            for (int i = 0; i < loopCount / 3; i++)
            {
                arrayLength += 5;
            }
            for (int i = 0; i < loopCount - loopCount / 3; i++)
            {
                arrayLength += 4;
            }
            return arrayLength;
        } 
        #endregion
    }

    }


相关文章:

  • 2022-01-20
  • 2021-11-29
  • 2021-06-18
  • 2022-12-23
  • 2021-12-02
  • 2021-12-05
猜你喜欢
  • 2021-09-10
  • 2021-08-18
  • 2021-10-30
  • 2021-12-28
  • 2021-04-29
  • 2021-06-23
  • 2022-01-09
相关资源
相似解决方案