欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# 騎士飛行棋的源碼(分享)

 更新時間:2013年05月08日 11:53:21   作者:  
以下是騎士飛行棋的源碼,需要的朋友可以拿去用

代碼如下所示:

復制代碼 代碼如下:

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


namespace 騎士飛行棋
{
    class Program
    {
        //在下面的數(shù)組存儲我們游戲地圖各各關(guān)卡
        //數(shù)組的下標為0的元素對應地圖上的第1格    下標為1的元素對應元素第2格...下標為n的元素對應n+1格
        //在數(shù)組中   1:表示幸運輪盤  ◎
        //           2: 表示地雷 ☆
        //           3: 表示暫停 ▲
        //           4: 表示時空隧道 卍
        //           0: 表示普通  □
        static int[] map = new int[100];

        static string[] names = new string[2]; //names[0]存儲玩家A的姓名    name[1]存玩家B的姓名

        static int[] playerPos = { 0, 0 };//playPos[0]存玩家A的位置,playPos[1]存玩家B的位置

        static int step = 0; //用于存放產(chǎn)生的隨機數(shù)

        static string input = ""; //用戶存儲用戶的輸入

        static string msg = ""; //用于存儲用戶踩到某個關(guān)卡,輸出的話

        static bool[] isStop = { false, false };//isStop[0]表示玩家A是否上次一走到暫停,似的話為true,不是為false

        static Random r = new Random();//r是產(chǎn)生的隨機數(shù)

        static void Main( string[] args)
        {
           


            ShowUI(); //顯示游戲
            InitialName();
            Console.Clear();
            ShowUI();
            Console.WriteLine("對戰(zhàn)開始......");
            Console.WriteLine("{0}用A來表示", names[0]);
            Console.WriteLine("{0}用B來表示", names[1]);
            Console.WriteLine("如果AB在同一位置,用<>表示");
            InitialMap();//初始化地圖
            drawMap();//繪制地圖
            Console.WriteLine("開始游戲......");

            //這個循環(huán)中讓玩家A和玩家B輪流擲骰子  當玩家A或者玩家B的坐標>=99時,則循環(huán)結(jié)束
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                Action(0);//A擲篩子
                Action(1);//B擲篩子 
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 用于繪制飛行棋的名稱
        /// </summary>
        static void ShowUI()
        {
            Console.WriteLine("*******************************************************");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*         騎     士     飛     行      棋             *");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*******************************************************");
        }

        static void InitialName()
        {
            Console.WriteLine("請輸入玩家A的姓名");
            names[0] = Console.ReadLine();
            //判斷用書輸入的內(nèi)容是否為空,如果為空,則讓用戶重新輸入
            while (names[0] == "")
            {
                Console.WriteLine("玩家A的姓名不能為空,請重新輸入!");
                names[0] = Console.ReadLine();
            }
            Console.WriteLine("請輸入玩家B的姓名");
            names[1] = Console.ReadLine();
            //判斷用戶輸入的內(nèi)容是否為空,如果為空,則讓用戶重新輸入
            while (names[1] == "" || names[1] == names[0])
            {
                if (names[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能為空,請重新輸入!");
                    names[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("你輸入的姓名與玩家A的姓名{0}相同,請重新輸入", names[0]);
                    names[1] = Console.ReadLine();
                }

            }
        }

        static void InitialMap()
        {
            //用于存儲在地圖中為地雷的下標
            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸運輪盤 1
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 2
            int[] pause = { 9, 27, 60, 93 };//暫停的坐標 3
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//時空隧道  4

            for (int i = 0; i < 100; i++) // 初始化map數(shù)組的數(shù)據(jù)
                map[i] = 0;

            //把幸運輪盤位置填入map中
            for (int i = 0; i < luckyTurn.Length; i++)
                map[luckyTurn[i]] = 1;
            //把地雷填入map中
            for (int i = 0; i < landMine.Length; i++)
                map[landMine[i]] = 2;
            //把暫停填入map中
            for (int i = 0; i < pause.Length; i++)
                map[pause[i]] = 3;
            //把時空隧道填入map中
            for (int i = 0; i < timeTunnel.Length; i++)
                map[timeTunnel[i]] = 4;
        }

        /// <summary>
        ///  獲得第pos坐標上應該繪制的圖案
        /// </summary>
        /// <param name="pos">要繪制的坐標</param>
        /// <returns></returns>
        static string getMapString(int pos)
        {
            string result = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "<>";
            }
            else if (playerPos[0] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "A";
            }
            else if (playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "B";
            }
            else
            {
                switch (map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        result = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        result = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        result = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        result = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        result = "卍";
                        break;
                }
            }
            return result;
        }

        static void drawMap()
        {
            Console.WriteLine("圖例:幸運輪盤:◎   地雷:☆   暫停:▲     時空隧道:卍  ");
            //畫第一行
            for (int i = 0; i < 30; i++)
                Console.Write(getMapString(i));
            Console.WriteLine();

            //左邊的第一列
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                    Console.Write(" ");
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

            //第二行
            for (int i = 64; i >= 35; i--)
                Console.Write(getMapString(i));
            Console.WriteLine();

            //右邊的列
            for (int i = 65; i < 70; i++)
            {
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

            //第三行
            for (int i = 70; i < 100; i++)
                Console.Write(getMapString(i));
            Console.ResetColor();
            Console.WriteLine();
        }

        static void checkPos()
        {
            for (int i = 0; i <= 1; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }
                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }

        static int ReadInt()//產(chǎn)生一個整數(shù)
        {
            int i = ReadInt(int.MaxValue, int.MinValue);
            return i;
        }

        static int ReadInt(int min, int max)//產(chǎn)生min--max 之間的數(shù)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(Console.ReadLine());
                    if (number < min || number > max)
                    {
                        Console.WriteLine("只能輸入{0}--{1}之間的數(shù)字,請重新輸入", min, max);
                        continue;
                    }
                    return number;
                }
                catch
                {
                    Console.WriteLine("只能輸入數(shù)字,請重新輸入!");
                }

            }

        }

        /// <summary>
        /// A或者B擲篩子的方法
        /// </summary>
        /// <param name="playerNumber">A擲篩子傳0過來   B擲篩子傳1過來</param>
        static void Action(int playerNumber)
        {
            if (isStop[playerNumber] == false)
            {
                Console.WriteLine("{0}按任意鍵開始擲篩子......", names[playerNumber]);
                ConsoleKeyInfo sec = Console.ReadKey(true);
                step = r.Next(1, 7);//產(chǎn)生一個1到6之間的隨機數(shù)
                if (sec.Key == ConsoleKey.Tab)
                {
                    ConsoleKeyInfo sec1 = Console.ReadKey(true);
                    if (sec1.Key == ConsoleKey.F1)
                    {
                        step = ReadInt(1, 100);
                    }
                }

                Console.WriteLine("{0}擲出了{1}", names[playerNumber], step);
                Console.WriteLine("{0}按任意鍵開始行動......", names[playerNumber]);
                Console.ReadKey(true);
                playerPos[playerNumber] += step; //注意,一旦坐標發(fā)生改變,就要判斷坐標值是否>99||<0
                checkPos();//檢查坐標是否越界

                if (playerPos[playerNumber] == playerPos[1 - playerNumber]) //玩家A采到玩家B
                {
                    playerPos[1 - playerNumber] = 0;
                    msg = string.Format("{0}踩到了{1},{1}退回了原點", names[playerNumber], names[1 - playerNumber]);
                }
                else
                {//沒踩到,要判斷玩家A現(xiàn)在所在的位置是否有其他關(guān)卡
                    switch (map[playerPos[playerNumber]])
                    {
                        case 0:
                            //普通,沒有效果
                            msg = "";
                            break;
                        case 1:
                            //走到了 幸運輪盤關(guān)卡
                            Console.Clear();
                            Console.WriteLine("你走到了幸運輪盤,請選擇運氣?");
                            Console.WriteLine("1 ---交換位置  2---轟炸對方");
                            int userSelect = ReadInt(1, 2);
                            if (userSelect == 1)
                            {//要與對方交換位置
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - playerNumber] = temp;
                                msg = string.Format("{0}選了與對方交換位置", names[playerNumber]);
                            }
                            else
                            {//轟炸對方
                                playerPos[1 - playerNumber] -= 6;
                                msg = string.Format("{0}轟炸了{1},{1}退回了6格", names[playerNumber], names[1 - playerNumber]);
                                checkPos();
                            }
                            break;
                        case 2:
                            //踩到了地雷
                            playerPos[playerNumber] -= 6;
                            checkPos();
                            msg = string.Format("{0}踩到了地雷,{0}退了6格", names[playerNumber]);
                            break;
                        case 3:
                            //暫停一次
                            isStop[playerNumber] = true;
                            msg = string.Format("{0}走到了紅燈,下次暫停一次啊", names[playerNumber]);
                            break;
                        case 4:
                            //踩到時空隧道
                            playerPos[playerNumber] += 10;
                            msg = string.Format("{0}進入了時空隧道,爽死了,進了10格", names[playerNumber]);
                            break;
                    }

                }
            }
            else
            {
                isStop[playerNumber] = false;
            }

            if (playerPos[playerNumber] >= 99)
            {
                //判斷誰勝利,誰失敗
                Console.Clear();
                if (playerPos[0] >= 99)
                {
                    Console.WriteLine("{0}勝利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
                }
                else
                {
                    Console.WriteLine("{0}勝利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
                }
            }

            Console.Clear();
            drawMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0}擲出了{1},行動完成!", names[playerNumber], step);
            Console.WriteLine("*************玩家A和玩家B的位置*********");
            Console.WriteLine("{0}的位置為:{1}", names[0], playerPos[0] + 1);
            Console.WriteLine("{0}的位置為:{1}", names[1], playerPos[1] + 1);

        }
    }
}

相關(guān)文章

  • C#在DataTable中根據(jù)條件刪除某一行的實現(xiàn)方法

    C#在DataTable中根據(jù)條件刪除某一行的實現(xiàn)方法

    我們通常的方法是把數(shù)據(jù)源放在DataTable里面,但是偶爾也會需要把不要的行移除,怎么實現(xiàn)呢,下面通過代碼給大家介紹c# atatable 刪除行的方法,需要的朋友一起看下吧
    2016-05-05
  • C#中如何使用 XmlReader 讀取XML文件

    C#中如何使用 XmlReader 讀取XML文件

    本文介紹了C#中使用XmlReader,只讀、向前、循環(huán)讀取XML節(jié)點的方法,并為我們列出、XmlReader類的方法、屬性、枚舉等成員,希望對大家學習有所幫助。
    2016-05-05
  • C#實現(xiàn)單例模式的多種方式

    C#實現(xiàn)單例模式的多種方式

    這篇文章介紹了C#實現(xiàn)單例模式的多種方式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • C#實現(xiàn)餐飲管理系統(tǒng)完整版

    C#實現(xiàn)餐飲管理系統(tǒng)完整版

    這篇文章主要為大家詳細介紹了C#實現(xiàn)餐飲管理系統(tǒng)的完整版,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 基于C#編寫一個操作XML的簡單類庫XMLHelper

    基于C#編寫一個操作XML的簡單類庫XMLHelper

    這篇文章主要為大家詳細介紹了如何基于C#編寫一個操作XML的簡單類庫——XMLHelper,文中的示例代碼講解詳細,需要的小伙伴可以參考一下
    2023-06-06
  • C# 8.0中的范圍類型(Range Type)示例詳解

    C# 8.0中的范圍類型(Range Type)示例詳解

    這篇文章主要給大家介紹了關(guān)于C# 8.0中范圍類型(Range Type)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)

    C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)

    這篇文章介紹了C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn),文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • WPF實現(xiàn)控件拖動的示例代碼

    WPF實現(xiàn)控件拖動的示例代碼

    這篇文章主要介紹了WPF實現(xiàn)控件拖動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • C#計算兩個文件的相對目錄算法的實例代碼

    C#計算兩個文件的相對目錄算法的實例代碼

    現(xiàn)在已知兩個文件相對于網(wǎng)站根目錄的路徑,如何計算相對路徑呢,有需要的朋友可以參考一下
    2013-09-09
  • C#調(diào)用WinRar執(zhí)行rar、zip壓縮的方法

    C#調(diào)用WinRar執(zhí)行rar、zip壓縮的方法

    這篇文章主要介紹了C#調(diào)用WinRar執(zhí)行rar、zip壓縮的方法,涉及C#針對winrar的判斷與調(diào)用技巧,需要的朋友可以參考下
    2015-05-05

最新評論