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

C#控制臺(tái)實(shí)現(xiàn)飛行棋小游戲

 更新時(shí)間:2021年07月21日 10:40:23   作者:王大瑜  
這篇文章主要為大家詳細(xì)介紹了C#控制臺(tái)實(shí)現(xiàn)飛行棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#控制臺(tái)實(shí)現(xiàn)飛行棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下

游戲標(biāo)題

static void ShowTitle()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("*****************飛行棋*****************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("****************************************");
        }

游戲規(guī)則說(shuō)明

1、玩家A和玩家B必須輸入一個(gè)非純數(shù)字的非空用戶(hù)名
2、玩家A先擲骰子,AB玩家輪流投擲骰子
3游戲中“□”代表普通格子,“◎”代表幸運(yùn)輪盤(pán),“★”代表地雷,“▲”代表暫停,“卍”代表時(shí)空隧道
4、“□”對(duì)于玩家沒(méi)有任何獎(jiǎng)懲!
5、“◎”玩家具有兩種選擇:a.選擇與對(duì)方交換位置;b.選擇轟炸對(duì)方使對(duì)方倒退6步
6、“★”對(duì)于玩家懲罰使玩家倒退6步
7、“▲”懲罰玩家下一輪暫停操作
8、“卍”獎(jiǎng)勵(lì)玩家直接前進(jìn)10步
9、果踩到對(duì)方則懲罰對(duì)方直接倒退6步

游戲的地圖

地圖共由100個(gè)格子組成,Z型實(shí)現(xiàn),第一行從左往右30個(gè)格子,第一列往下5個(gè),第二行從右往左30個(gè)格子,第二列往下5個(gè)第三行從左往右30個(gè)

1、聲明一個(gè)int類(lèi)型的一維數(shù)組,長(zhǎng)度是100,每個(gè)位置的默認(rèn)值是0
2、“□”普通格子也就是默認(rèn)的值0代表
3、“◎”幸運(yùn)輪盤(pán)使用數(shù)字1代表 {6,23,40,55,69,83}
4、“★”地雷使用數(shù)字2代表 {5,13,17,33,38,50,64,80,94}
5、“▲”暫停使用數(shù)字3代表 {9,27,60,93}
6、“卍”時(shí)空隧道使用數(shù)字4代表 {20,25,45,63,88,90}

繪制游戲規(guī)則

static void ShowRule()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("------------------------------------------------------------");
            Console.WriteLine("玩家【1】使用A表示");
            Console.WriteLine("玩家【2】使用B表示");
            Console.WriteLine("規(guī)則說(shuō)明:");
            Console.WriteLine("1.玩家A先擲骰子,A、B玩家輪流投擲骰子");
            Console.WriteLine("2.“□”對(duì)于玩家沒(méi)有任何獎(jiǎng)懲!");
            Console.WriteLine("3.“◎”玩家具有兩種選擇:a.選擇與對(duì)方交換位置;b.選擇轟炸對(duì)方使對(duì)方倒退6步");
            Console.WriteLine("4.“★”對(duì)于玩家懲罰使玩家倒退6步");
            Console.WriteLine("5.“▲”懲罰玩家下一輪暫停操作");
            Console.WriteLine("6.“卍”獎(jiǎng)勵(lì)玩家直接前進(jìn)10步");
            Console.WriteLine("7.如果踩到對(duì)方則懲罰對(duì)方直接倒退6步");
            Console.WriteLine("------------------------------------------------------------");
        }

初始化地圖

static void InitialMap()
        {
            //確定“◎”=1
            int[] lunckturn = { 6, 23, 40, 55, 69, 83 };
            for (int i = 0; i < lunckturn.Length; i++)
            {
                int index = lunckturn[i];
                Maps[index] = 1;
            }
            //確定“★”=2
            int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
            for (int i = 0; i < landmine.Length; i++)
            {
                Maps[landmine[i]] = 2;
            }
            //確定“▲”=3
            int[] pause = { 9, 27, 60, 93 };
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            //確定“卍”=4
            int[] timeTunnel = { 20, 25, 45, 63, 88, 90 };
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }

繪制地圖

/// <summary>
        /// 繪制地圖
        /// </summary>
        static void DrawMap()
        {
            //第一行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
            //第一列
            for (int i = 30; i < 35; i++)
            {
                for (int k = 0; k < 29; k++)
                {
                    Console.Write(" ");
                }
                Console.Write(DrawString(i));
                Console.WriteLine();
            }
            //第二行
            for (int i = 64; i > 34; i--)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
            //第二列
            for (int i = 65; i <70; i++)
            {
                Console.WriteLine(DrawString(i));
            }
            //第三行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
        }

        /// <summary>
        /// 根據(jù)地圖數(shù)組每個(gè)位置的值選擇繪制的圖案
        /// </summary>
        /// <param name="type">地圖數(shù)組中位置數(shù)字代表地圖位置索引</param>
        /// <returns>返回最終選擇的圖案</returns>
        static string DrawString(int index)
        {
            string str = "";
            //兩名玩家的位置重合,并且保證兩個(gè)玩家的位置都在地圖中
            if (playerPosition[0]==playerPosition[1]&&playerPosition[0]==index)
            {
                Console.ForegroundColor = ConsoleColor.White;
                str = "<>";
            }
            else if (playerPosition[0]==index)
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                str = "A";
            }
            else if (playerPosition[1]==index)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                str = "B";
            }
            else
            {
                switch (Maps[index])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "★";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "卍";
                        break;
                    default:
                        break;
                }
            }
            return str;
        }

實(shí)現(xiàn)用戶(hù)注冊(cè)

 /// <summary>
        /// 地圖
        /// </summary>
        static int[] Maps = new int[100];
        /// <summary>
        /// 用戶(hù)姓名
        /// </summary>
        static string[] playerName = new string[2];
        /// <summary>
        /// 兩名玩家的位置
        /// </summary>
        static int[] playerPosition = new int[2];
        static void Main(string[] args)
        {
            //游戲標(biāo)題
            ShowTitle();
            #region///用戶(hù)名注冊(cè)
            Console.WriteLine("請(qǐng)兩名玩家先進(jìn)行用戶(hù)注冊(cè)!");
            Console.WriteLine("注冊(cè)規(guī)則:不能為空,并且不能是純數(shù)字,玩家AB姓名不能相同");
            Console.WriteLine("請(qǐng)輸入玩家A的姓名:");
            string pattern = @"^\d+$";
            bool isok = true;
            while (isok)
            {
                playerName[0] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[0]))
                {
                    Console.WriteLine("玩家A的姓名不能為空,請(qǐng)重新輸入");
                }
                else if (Regex.IsMatch(playerName[0], pattern))
                {
                    Console.WriteLine("玩家A的姓名不能是純數(shù)字,請(qǐng)重新輸入");
                }
                else
                {
                    isok = false;
                }
            }
            isok = true;
            Console.WriteLine("請(qǐng)輸入玩家B的姓名:");
            while (isok)
            {
                playerName[1] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[1]))
                {
                    Console.WriteLine("玩家B的姓名不能為空,請(qǐng)重新輸入");
                }
                else if(Regex.IsMatch(playerName[1], pattern))
                {
                    Console.WriteLine("玩家B的姓名不能是純數(shù)字,請(qǐng)重新輸入");
                }
                else if (playerName[0]==playerName[1])
                {
                    Console.WriteLine("玩家B的姓名已被占用,請(qǐng)重新輸入");
                }
                else
                {
                    isok = false;
                }
            }
            #endregion
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            Console.ReadLine();
        }

游戲邏輯

1、玩家A按下任意鍵先手?jǐn)S骰子
2、擲完骰子出現(xiàn)隨機(jī)的1-6步
3、按下任意鍵進(jìn)行移動(dòng)相對(duì)應(yīng)的步數(shù)

 /// <summary>
        /// 地圖
        /// </summary>
        static int[] Maps = new int[100];
        /// <summary>
        /// 用戶(hù)姓名
        /// </summary>
        static string[] playerName = new string[2];
        /// <summary>
        /// 兩名玩家的位置
        /// </summary>
        static int[] playerPosition = new int[2];
        /// <summary>
        /// 標(biāo)記兩個(gè)玩家誰(shuí)該進(jìn)行投擲篩子
        /// </summary>
        static bool[] playerFlag = new bool[2];
        static void Main(string[] args)
        {
            //游戲標(biāo)題
            ShowTitle();
            #region///用戶(hù)名注冊(cè)
            Console.WriteLine("請(qǐng)兩名玩家先進(jìn)行用戶(hù)注冊(cè)!");
            Console.WriteLine("注冊(cè)規(guī)則:不能為空,并且不能是純數(shù)字,玩家AB姓名不能相同");
            Console.WriteLine("請(qǐng)輸入玩家A的姓名:");
            string pattern = @"^\d+$";
            bool isok = true;
            while (isok)
            {
                playerName[0] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[0]))
                {
                    Console.WriteLine("玩家A的姓名不能為空,請(qǐng)重新輸入");
                }
                else if (Regex.IsMatch(playerName[0], pattern))
                {
                    Console.WriteLine("玩家A的姓名不能是純數(shù)字,請(qǐng)重新輸入");
                }
                else
                {
                    isok = false;
                }
            }
            isok = true;
            Console.WriteLine("請(qǐng)輸入玩家B的姓名:");
            while (isok)
            {
                playerName[1] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[1]))
                {
                    Console.WriteLine("玩家B的姓名不能為空,請(qǐng)重新輸入");
                }
                else if(Regex.IsMatch(playerName[1], pattern))
                {
                    Console.WriteLine("玩家B的姓名不能是純數(shù)字,請(qǐng)重新輸入");
                }
                else if (playerName[0]==playerName[1])
                {
                    Console.WriteLine("玩家B的姓名已被占用,請(qǐng)重新輸入");
                }
                else
                {
                    isok = false;
                }
            }
            #endregion
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            //開(kāi)始進(jìn)入游戲
            //判斷兩名玩家都未到達(dá)終點(diǎn)則游戲繼續(xù)
            while (playerPosition[0]<99&&playerPosition[1]<99)
            {
                if (playerFlag[0]==false)
                {
                    PlayGame(0);
                }
                else
                {
                    playerFlag[0] = false;
                }

                if (playerFlag[1]==false)
                {
                    PlayGame(1);
                }
                else
                {
                    playerFlag[1] = false;
                }

                if (playerPosition[0]>=99)
                {
                    Console.WriteLine("恭喜玩家【{0}】獲勝!",playerName[0]);
                    break;
                }
                else if (playerPosition[1] >= 99)
                {
                    Console.WriteLine("恭喜玩家【{0}】獲勝!", playerName[1]);
                    break;
                }
            }
            Console.ReadLine();
        }

4、移動(dòng)完成之后判斷這個(gè)格子的功能,并實(shí)現(xiàn)特殊格子的功能
5、按下任意鍵完成界面

/// <summary>
        /// 實(shí)現(xiàn)游戲功能的方法
        /// </summary>
        /// <param name="playerIndex">通過(guò)玩家索引判斷本次游戲操作是由哪個(gè)玩家發(fā)起的</param>
        static void PlayGame(int playerIndex)
        {
            #region ///為了實(shí)現(xiàn)玩家擲骰子算出移動(dòng)步數(shù)
            Random r = new Random();
            Console.WriteLine("本回合由玩家【{0}】按下任意鍵擲骰子!",playerName[playerIndex]);
            Console.ReadKey(true);
            int number=r.Next(1, 7);
            Console.WriteLine("玩家【{0}】擲出<{1}>點(diǎn)",playerName[playerIndex],number);
            Console.WriteLine("玩家【{0}】按下任意鍵開(kāi)始移動(dòng)!",playerName[playerIndex]);
            Console.ReadKey(true);
            playerPosition[playerIndex] += number;
            Console.WriteLine("玩家【{0}】移動(dòng)完成!",playerName[playerIndex]);
            #endregion
            CheckPosition();
            #region///進(jìn)行對(duì)移動(dòng)結(jié)果的格子獎(jiǎng)懲判斷
            //先判斷兩個(gè)玩家是否踩到對(duì)方
            if (playerPosition[0]==playerPosition[1])
            {
                Console.WriteLine("玩家【{0}】踩到玩家【{1}】,玩家【{1}】退6格!",playerName[playerIndex],playerName[1-playerIndex]);
                playerPosition[1-playerIndex] -= 6;
            }
            else
            {
                switch (Maps[playerPosition[playerIndex]])
                {
                    //踩到普通格子
                    case 0:
                        Console.WriteLine("玩家【{0}】踩到安全地帶!無(wú)獎(jiǎng)懲!按下任意鍵刷新界面!",playerName[playerIndex]);
                        Console.ReadKey(true);
                        break;
                    //踩到幸運(yùn)輪盤(pán)
                    case 1:
                        Console.WriteLine("玩家【{0}】踩到幸運(yùn)輪盤(pán)!請(qǐng)選擇:a--交換位置,b--轟炸對(duì)方",playerName[playerIndex]);
                        string type = Console.ReadLine();
                        while (true)
                        {
                            if (type=="a")
                            {
                                Console.WriteLine("玩家【{0}】選擇與玩家【{1}】交換位置",playerName[playerIndex],playerName[1-playerIndex]);
                                int temp = playerPosition[0];
                                playerPosition[0] = playerPosition[1];
                                playerPosition[1] = temp;
                                Console.WriteLine("玩家【{0}】與玩家【{1}】交換位置完成,按下任意鍵刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]);
                                Console.ReadKey(true);
                                break;
                            }
                            else if (type=="b")
                            {
                                Console.WriteLine("玩家【{ 0}】選擇轟炸玩家【{ 1}】", playerName[playerIndex], playerName[1 - playerIndex]);
                                playerPosition[1-playerIndex] -= 6;
                                Console.WriteLine("玩家【{ 0}】轟炸玩家【{ 1}】完成,按下任意鍵刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("請(qǐng)輸入正確的指令!請(qǐng)選擇:a--交換位置,b--轟炸對(duì)方");
                                type = Console.ReadLine();
                            }
                        }
                        break;
                        //踩到地雷
                    case 2:
                        Console.WriteLine("玩家【{0}】踩到地雷,退6格!按下任意鍵刷新界面",playerName[playerIndex]);
                        playerPosition[playerIndex] -= 6;
                        Console.ReadKey(true);
                        break;
                        //踩到暫停
                    case 3:
                        Console.WriteLine("玩家【{0}】踩到暫停,下一回合暫停!按下任意鍵刷新界面",playerName[playerIndex]);
                        playerFlag[playerIndex] = true;
                        break;
                        //踩到時(shí)空隧道
                    case 4:
                        Console.WriteLine("玩家【{0}】踩到時(shí)空隧道,前進(jìn)10步!按下任意鍵刷新界面", playerName[playerIndex]);
                        playerPosition[playerIndex] += 10;
                        Console.ReadKey(true);
                        break;
                    default:
                        break;
                }
            }
            CheckPosition();
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            #endregion
        }
        /// <summary>
        /// 斷是否超越終點(diǎn)或者超出起點(diǎn)
        /// </summary>
        static void CheckPosition()
        {
            if (playerPosition[0] < 0)
            {
                playerPosition[0] = 0;
            }
            if (playerPosition[0] > 99)
            {
                playerPosition[0] = 99;
            }
            if (playerPosition[1] < 0)
            {
                playerPosition[1] = 0;
            }
            if (playerPosition[1] > 99)
            {
                playerPosition[1] = 99;
            }
        }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Avalonia實(shí)現(xiàn)自定義彈窗的示例詳解

    基于Avalonia實(shí)現(xiàn)自定義彈窗的示例詳解

    對(duì)于使用avalonia的時(shí)候某些功能需要到一些提示,比如異?;蛘叱晒Χ夹枰獙?duì)用戶(hù)進(jìn)行提示,所以需要單獨(dú)實(shí)現(xiàn)彈窗功能,并且可以自定義內(nèi)部組件,這一期將手動(dòng)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的小彈窗,并且很容易自定義,希望大家喜歡
    2023-02-02
  • C#調(diào)用Python腳本程序的兩種方法

    C#調(diào)用Python腳本程序的兩種方法

    本文主要介紹了C#調(diào)用Python腳本程序的兩種方法,包含介紹了通過(guò)C#IronPython開(kāi)源庫(kù)和通過(guò)Process類(lèi)來(lái)運(yùn)行python解釋器這兩種,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 詳解C# 網(wǎng)絡(luò)編程系列:實(shí)現(xiàn)類(lèi)似QQ的即時(shí)通信程序

    詳解C# 網(wǎng)絡(luò)編程系列:實(shí)現(xiàn)類(lèi)似QQ的即時(shí)通信程序

    本篇主要介紹了c#實(shí)現(xiàn)類(lèi)似QQ的即時(shí)通信程序 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • c#使用xamarin編寫(xiě)撥打電話(huà)程序

    c#使用xamarin編寫(xiě)撥打電話(huà)程序

    Xamarin是一個(gè)行動(dòng)App開(kāi)發(fā)平臺(tái),提供跨平臺(tái)開(kāi)發(fā)能力,開(kāi)發(fā)人員透過(guò)Xamarin開(kāi)發(fā)工具與程序語(yǔ)言,即可開(kāi)發(fā)出iOS、Android 與Windows 等平臺(tái)的原生(Native) App 應(yīng)用程序,不須個(gè)別使用各平臺(tái)的開(kāi)發(fā)工具與程序語(yǔ)言,
    2015-05-05
  • C#操作數(shù)據(jù)庫(kù)中存取圖片文件的方法

    C#操作數(shù)據(jù)庫(kù)中存取圖片文件的方法

    這篇文章主要介紹了C#操作數(shù)據(jù)庫(kù)中存取圖片文件的方法,以實(shí)例形式分析了C#將圖片存入數(shù)據(jù)庫(kù)及從數(shù)據(jù)庫(kù)讀取圖片文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • C# Winform 實(shí)現(xiàn)TCP發(fā)消息

    C# Winform 實(shí)現(xiàn)TCP發(fā)消息

    這篇文章主要介紹了C# Winform 實(shí)現(xiàn)TCP發(fā)消息的示例,幫助大家更好的理解和學(xué)習(xí)使用c#技術(shù),感興趣的朋友可以了解下
    2021-03-03
  • 解讀在C#中winform程序響應(yīng)鍵盤(pán)事件的詳解

    解讀在C#中winform程序響應(yīng)鍵盤(pán)事件的詳解

    本篇文章是對(duì)在C#中winform程序響應(yīng)鍵盤(pán)事件的詳細(xì)介紹,需要的朋友參考下
    2013-05-05
  • Unity實(shí)現(xiàn)游戲存檔框架

    Unity實(shí)現(xiàn)游戲存檔框架

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)游戲存檔框架,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的示例詳解

    C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的示例詳解

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2023-03-03
  • C#仿Windows XP自帶的掃雷游戲

    C#仿Windows XP自帶的掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C#仿Windows XP自帶的掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評(píng)論

威海市| 靖安县| 上虞市| 历史| 康乐县| 青河县| 乌鲁木齐县| 绥宁县| 新巴尔虎左旗| 阿拉善右旗| 千阳县| 克什克腾旗| 探索| 永丰县| 南华县| 黄浦区| 临武县| 嵊州市| 中方县| 陵川县| 黄骅市| 唐海县| 兴和县| 康保县| 嵩明县| 黔西县| 南丰县| 东明县| 青铜峡市| 谢通门县| 云龙县| 永修县| 宜川县| 罗平县| 宝兴县| 玉林市| 罗定市| 肇庆市| 绥滨县| 会同县| 乡城县|