最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)飛行棋優(yōu)化版

 更新時間:2021年05月10日 14:17:12   作者:一天至少八杯水  
這篇文章主要為大家詳細介紹了C#實現(xiàn)飛行棋的優(yōu)化版,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C#實現(xiàn)飛行棋優(yōu)化版的具體代碼,供大家參考,具體內(nèi)容如下

代碼如下

using System;

namespace ConsoleApp1
{
    enum E_Gezi
    {
        Simple,
        Boom,
        Pause,
        TimeTravel,
    }
    enum E_PlayerType
    {
        MianPlayer,
        Computer,
    }
    enum E_SceneType
    {
        Begin,
        Game,
        End,
    }
    struct Vector2
    {
        public int x;
        public int y;
        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    struct Gezi
    {
        public E_Gezi type;
        public Vector2 pos;
        public Gezi(int x, int y, E_Gezi gezi)
        {
            pos = new Vector2(x, y);
            type = gezi;
        }
        public void Draw()
        {
            Console.SetCursorPosition(pos.x, pos.y);
            switch (type)
            {
                case E_Gezi.Simple:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("□");
                    break;
                case E_Gezi.Boom:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("●");
                    break;
                case E_Gezi.Pause:
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.Write("Ⅱ");
                    break;
                case E_Gezi.TimeTravel:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("D ");
                    break;
            }
        }
    }
    struct Map
    {
        public Gezi[] gezis;
        public Map(int x, int y, int num)
        {
            this.gezis = new Gezi[num];
            Random r = new();
            int ra;
            int stepNum = 2;
            int indexX = 0;
            int indexY = 0;
            for (int i = 0; i < gezis.Length; i++)
            {
                //隨機有物體生成
                ra = r.Next(1, 101);
                if (ra >= 1 && ra < 85 || i == 0 || i == gezis.Length - 1)
                {
                    gezis[i].type = E_Gezi.Simple;
                }
                else if (ra >= 85 && ra < 90)
                {
                    gezis[i].type = E_Gezi.Pause;
                }
                else if (ra >= 90 && ra < 95)
                {
                    gezis[i].type = E_Gezi.Boom;
                }
                else
                {
                    gezis[i].type = E_Gezi.TimeTravel;
                }
                gezis[i].pos = new Vector2(x, y);
                //一行有十個 然后讓y加2次 
                if (indexX == 10)
                {
                    y += 1;
                    ++indexY;
                    if (indexY == 2)
                    {
                        indexX = 0;
                        indexY = 0;
                        stepNum = -stepNum;
                    }
                }
                else
                {
                    //奇數(shù)次步長反向
                    x += stepNum;
                    ++indexX;
                }
            }
        }
        public void Draw()
        {
            for (int i = 0; i < gezis.Length; i++)
            {
                gezis[i].Draw();
            }
        }
    }
    struct Player
    {
        public E_PlayerType type;
        public int indexStep;
        //這里有一種優(yōu)化方案 就是直接把坐標變成索引 并且內(nèi)置一個bool變量判斷玩家是否被暫停
        //之后用索引配合map來繪制地圖
        public bool isPause;
        public Player(int index, E_PlayerType type)
        {
            indexStep = index;
            this.type = type;
            isPause = false;
        }
        public void Draw(Map map)
        {
            Console.SetCursorPosition(map.gezis[indexStep].pos.x, map.gezis[indexStep].pos.y);
            switch (type)
            {
                case E_PlayerType.MianPlayer:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("★");
                    break;
                case E_PlayerType.Computer:
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.Write("▲");
                    break;
            }
        }
    }
    class Program
    {
        static void Main()
        {
            //初始化設置
            Console.SetWindowSize(50, 40);
            Console.SetBufferSize(50, 40);
            Console.CursorVisible = false;
            E_SceneType type = E_SceneType.Begin;
            while (true)
            {
                switch (type)
                {
                    case E_SceneType.Begin:
                        Console.Clear();
                        GameScence(ref type);
                        break;
                    case E_SceneType.Game:
                        Console.Clear();
                        DrawWall();
                        Map map = new(12, 3, 80);
                        map.Draw();
                        //繪制玩家相關
                        Player player = new(0, E_PlayerType.MianPlayer);
                        Player computer = new(0, E_PlayerType.Computer);
                        DrawPlayer( player, computer, map);
                        while (true)
                        {
                            if(!InputSelection(ref player,ref computer,map,ref type))
                            {
                                break;
                            }
                            if(!InputSelection(ref computer, ref player, map, ref type))
                            {
                                break;
                            }
                        }
                        break;
                    case E_SceneType.End:
                        Console.Clear();
                        GameOverScence(ref type);
                        break;
                }
            }
        }
        static void GameScence(ref E_SceneType type)
        {
            //標題
            string title = "飛行棋";
            string startGame = "開始游戲";
            string quitGame = "結束游戲";
            //選擇的索引
            int selectIndex = 0;
            //選擇
            Console.SetCursorPosition(25 - title.Length, 10);
            Console.Write(title);
            //跳出while
            bool isquit = false;
            while (true)
            {
                if (isquit)
                {
                    break;
                }
                Console.SetCursorPosition(25 - startGame.Length, 13);
                Console.ForegroundColor = selectIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(startGame);
                Console.SetCursorPosition(25 - quitGame.Length, 15);
                Console.ForegroundColor = selectIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(quitGame);
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        selectIndex = 0;
                        break;
                    case ConsoleKey.S:
                        selectIndex = 1;
                        break;
                    case ConsoleKey.J:
                        if (selectIndex == 0)
                        {
                            type = E_SceneType.Game;
                            isquit = true;
                        }
                        else if (selectIndex == 1)
                        {
                            Environment.Exit(0);
                        }
                        break;
                }
            }
        }
        static void GameOverScence(ref E_SceneType type)
        {
            //標題
            string Endtitle = "游戲結束";
            string returnMain = "回到主菜單";
            string quitGame1 = "結束游戲";
            //選擇的索引
            int selectIndex = 0;
            Console.SetCursorPosition(25 - Endtitle.Length, 10);
            Console.Write(Endtitle);
            //跳出while
            bool isquit = false;
            while (true)
            {
                if (isquit)
                {
                    break;
                }
                Console.SetCursorPosition(25 - returnMain.Length, 13);
                Console.ForegroundColor = selectIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(returnMain);
                Console.SetCursorPosition(25 - quitGame1.Length, 15);
                Console.ForegroundColor = selectIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write(quitGame1);
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        selectIndex = 0;
                        break;
                    case ConsoleKey.S:
                        selectIndex = 1;
                        break;
                    case ConsoleKey.J:
                        if (selectIndex == 0)
                        {
                            type = E_SceneType.Begin;
                            isquit = true;
                        }
                        else if (selectIndex == 1)
                        {
                            Environment.Exit(0);
                        }
                        break;
                }
            }
        }
        static void DrawWall()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 0);
                Console.Write("■");
            }
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 39);
                Console.Write("■");
            }
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 34);
                Console.Write("■");
            }
            for (int i = 0; i < 50 - 2; i += 2)
            {
                Console.SetCursorPosition(i, 29);
                Console.Write("■");
            }
            for (int i = 0; i < 40; i++)
            {
                Console.SetCursorPosition(0, i);
                Console.Write("■");
            }
            for (int i = 0; i < 40; i++)
            {
                Console.SetCursorPosition(46, i);
                Console.Write("■");
            }
            Console.SetCursorPosition(2, 30);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("□: 普通格子");
            Console.SetCursorPosition(2, 31);
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.Write("Ⅱ: 暫停,一回合不動");
            Console.SetCursorPosition(24, 31);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("●: 炸彈,倒退5格");
            Console.SetCursorPosition(2, 32);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("D : 時空隧道,隨機倒退,暫停,換位置");
            Console.SetCursorPosition(2, 33);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("★:玩家");
            Console.SetCursorPosition(10, 33);
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("▲:電腦");
            Console.SetCursorPosition(18, 33);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("C :電腦玩家重合");
            Console.SetCursorPosition(2, 35);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("按任意鍵開始扔篩子");
        }
        //優(yōu)化二 專門寫一個函數(shù) 繪制玩家        //重合的時候 和 不重合的時候分別畫
        static void DrawPlayer(Player player, Player player1,Map map)
        {
            if(player.indexStep == player1.indexStep)
            {
                Console.SetCursorPosition(map.gezis[player.indexStep].pos.x, map.gezis[player.indexStep].pos.y);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("C ");
            }
            else
            {
                Console.SetCursorPosition(map.gezis[player.indexStep].pos.x, map.gezis[player.indexStep].pos.y);
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("★");
                Console.SetCursorPosition(map.gezis[player1.indexStep].pos.x, map.gezis[player1.indexStep].pos.y);
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.Write("▲");
            }
        }
        //優(yōu)化3 扔色子函數(shù)判斷是否游戲結束
        //優(yōu)化4 通過函數(shù)包裹來重用玩家輸入和電腦輸入
        static bool InputSelection(ref Player player,ref Player player1, Map map,ref E_SceneType type)
        {
            if (player.isPause)
            {
                player.isPause = false;
            }
            else
            {
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.A:
                    case ConsoleKey.B:
                    case ConsoleKey.C:
                    case ConsoleKey.D:
                    case ConsoleKey.E:
                    case ConsoleKey.F:
                    case ConsoleKey.G:
                    case ConsoleKey.H:
                    case ConsoleKey.I:
                    case ConsoleKey.J:
                    case ConsoleKey.K:
                    case ConsoleKey.L:
                    case ConsoleKey.M:
                    case ConsoleKey.N:
                    case ConsoleKey.O:
                    case ConsoleKey.P:
                    case ConsoleKey.Q:
                    case ConsoleKey.R:
                    case ConsoleKey.S:
                    case ConsoleKey.T:
                    case ConsoleKey.U:
                    case ConsoleKey.V:
                    case ConsoleKey.W:
                    case ConsoleKey.X:
                    case ConsoleKey.Y:
                    case ConsoleKey.Z:
                        ThrowShaiZi(ref player, ref player1, map, ref type);
                        break;
                }
            }
            if(type == E_SceneType.End)
            {
                return false;
            }
            return true;
        }
        static void ThrowShaiZi(ref Player player, ref Player computer, Map map,ref E_SceneType type)
        {
            Random r = new();
            player.indexStep += r.Next(1, 7);
            for (int i = 0; i < map.gezis.Length; i++)
            {
                if(player.indexStep >= map.gezis.Length - 1)
                {
                    player.indexStep = map.gezis.Length - 1;
                    type = E_SceneType.End;
                }
                if (player.indexStep == i)
                {
                    switch (map.gezis[i].type)
                    {
                        case E_Gezi.Simple:
                            break;
                        case E_Gezi.Boom:
                            player.indexStep -= 5;
                            break;
                        case E_Gezi.Pause:
                            player.isPause = true;
                            break;
                        case E_Gezi.TimeTravel:
                            if(r.Next(1,4) == 1)
                            {
                                player.indexStep -= 5;
                            }
                            else if(r.Next(1, 4) == 2)
                            {
                                player.isPause = true;
                            }
                            else if(r.Next(1, 4) == 3)
                            {
                                int temp = computer.indexStep;
                                computer.indexStep = player.indexStep;
                                player.indexStep = temp;
                            }
                            break;
                    }
                    break;
                }
            }
        }
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#泛型Dictionary的用法實例詳解

    C#泛型Dictionary的用法實例詳解

    這篇文章主要介紹了C#泛型Dictionary的用法,并以實例的形式講述了對鍵值對的填充、移除及遍歷等操作,需要的朋友可以參考下
    2014-09-09
  • C#使用二維數(shù)組模擬斗地主

    C#使用二維數(shù)組模擬斗地主

    這篇文章主要介紹了C#使用二維數(shù)組模擬斗地主的方法,通過C#的二維數(shù)組簡單實現(xiàn)撲克隨機發(fā)牌的功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#隨機數(shù)生成字母金字塔

    C#隨機數(shù)生成字母金字塔

    這篇文章主要為大家詳細介紹了C#隨機數(shù)生成字母金字塔,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 淺析C#線程本地存儲中為什么線程間值不一樣

    淺析C#線程本地存儲中為什么線程間值不一樣

    這篇文章主要想來和大家一起討論一下C#線程本地存儲中為什么線程間值不一樣,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-01-01
  • C#實現(xiàn)word和pdf格式互轉(zhuǎn)

    C#實現(xiàn)word和pdf格式互轉(zhuǎn)

    這篇文章主要為大家詳細介紹了如何通過C#實現(xiàn)word和pdf格式互轉(zhuǎn)功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-10-10
  • 淺談C#設計模式之工廠模式

    淺談C#設計模式之工廠模式

    這篇文章主要介紹了淺談C#設計模式之工廠模式,需要的朋友可以參考下
    2014-12-12
  • 基于WPF實現(xiàn)帶明細的環(huán)形圖表

    基于WPF實現(xiàn)帶明細的環(huán)形圖表

    這篇文章主要介紹了如何利用WPF繪制帶明細的環(huán)形圖表?,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,需要的可以參考一下
    2022-08-08
  • c#注冊表操作類分享

    c#注冊表操作類分享

    這篇文章主要介紹了c#注冊表操作類,主要包括創(chuàng)建注冊表項、讀取注冊表項、判斷注冊表項是否存在、刪除注冊表項、創(chuàng)建注冊表鍵值 、讀取注冊表鍵值、判斷注冊表鍵值是否存在、刪除注冊表鍵值等功能,需要的朋友可以參考下
    2014-03-03
  • C#使用開源驅(qū)動連接操作MySQL數(shù)據(jù)庫

    C#使用開源驅(qū)動連接操作MySQL數(shù)據(jù)庫

    這篇文章主要介紹了C#使用開源驅(qū)動連接操作MySQL數(shù)據(jù)庫,本文講解使用SourceForge上的mysqldrivercs驅(qū)動連接操作MySQL數(shù)據(jù)庫,需要的朋友可以參考下
    2015-02-02
  • C#讀寫文本文件(.txt)的方法實例

    C#讀寫文本文件(.txt)的方法實例

    讀寫文本文件其實是件很簡單的事情,這篇文章主要給大家介紹了關于C#讀寫文本文件(.txt)的相關資料,需要的朋友可以參考下
    2021-05-05

最新評論

图们市| 平度市| 琼结县| 通山县| 师宗县| 托里县| 清河县| 阳高县| 阿勒泰市| 兖州市| 江北区| 始兴县| 日照市| 抚松县| 柏乡县| 汶川县| 柘荣县| 桦甸市| 手游| 凤台县| 清河县| 仪陇县| 浦县| 平阳县| 涞水县| 腾冲县| 定南县| 右玉县| 老河口市| 四平市| 思茅市| 汾阳市| 麻城市| 富蕴县| 九江市| 陇南市| 宜兴市| 安化县| 锦屏县| 游戏| 社旗县|