C#——飛行棋

小水瓶與大水瓶發表於2020-12-10

看的B站視訊裡的,跟著老師敲的,就後面結尾自己改了下,其他大致一樣,有興趣的可以玩一玩,專案地址在末尾

B站視訊連結

執行截圖:

C#——飛行棋
C#——飛行棋

原始碼:

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

namespace 飛行棋遊戲
{
    class Program
    {
        //static 修飾的變數只能放在類中,不能放在方法中
        static int[] Maps = new int[100];
        //宣告一個靜態陣列用來儲存玩家A和玩家B的座標
        static int[] PlayerPos = new int[2];
        //儲存兩個玩家的姓名
        static string[] PlayerNames = new string[2];
        //儲存兩個暫停確認標誌
        static bool[] Flag = new bool[2];   //bool值預設是false
        static void Main(string[] args)
        {
            GameShow();
            InputName();
            Console.Clear();    //清屏
            GameShow();
            Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
            Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
            //在畫地圖之前要初始化地圖
            InitailMap();
            DrawMap();

            //當玩家A跟玩家B沒有一個人在終點的時候,兩個玩家不停的去玩遊戲
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                if (Flag[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    Flag[0] = true;
                }

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

            }
            Console.ReadKey();
        }

        /// <summary>
        /// 畫遊戲頭
        /// </summary>
        public static void GameShow()
        {
            //設定背景色     ctrl+k+c註釋    ctrl+k+u取消
            //Console.BackgroundColor = ConsoleColor.Yellow;
            //設定前景色
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("*****************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("*****************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("*****************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("*********飛行器遊戲**********");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("*****************************");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("*****************************");
        }

        /// <summary>
        /// 初始化地圖
        /// </summary>
        public static void InitailMap()
        {
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };    //幸運輪盤☢ 
            for (int i = 0; i < luckyturn.Length; i++)
            {
                int index = luckyturn[i];
                Maps[index] = 1;
            }
            int[] landMine = { 5, 37, 17, 33, 38, 50, 64, 80, 94 };    //地雷 ✺
            for (int i = 0; i < landMine.Length; i++)
            {
                int index = landMine[i];
                Maps[index] = 2;
            }
            int[] pause = { 9, 27, 60, 93 };    //暫停 ㊡
            for (int i = 0; i < pause.Length; i++)
            {
                int index = pause[i];
                Maps[index] = 3;
            }
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };    //時空隧道 卐
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                int index = timeTunnel[i];
                Maps[index] = 4;
            }
        }

        /// <summary>
        /// 畫地圖
        /// </summary>
        public static void DrawMap()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("圖例:幸運輪盤⊙    地雷 ★    暫停 ㊡    時空隧道 卐");
            //第一橫行
            #region 第一橫行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));
            }//for
            #endregion

            //畫完第一橫行需要換行
            Console.WriteLine();

            //第一豎行
            #region 第二豎行
            for (int i = 30; i <= 34; i++)
            {
                for (int j = 0; j <= 28; j++)
                {
                    Console.Write("  ");
                }
                Console.Write(DrawStringMap(i));
                //列印完一行後也要換行
                Console.WriteLine();
            }
            #endregion

            //第二橫行
            #region 第二橫行
            for (int i = 64; i >= 35; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion

            //列印前要換行
            Console.WriteLine();

            //第二豎行
            #region 第二豎行
            for (int i = 65; i <= 69; i++)
            {
                Console.Write(DrawStringMap(i));
                //列印後也要換行
                Console.WriteLine();
            }
            #endregion

            畫完第二豎橫行 不需要 換行
            //Console.WriteLine();

            //第三橫行
            #region 第三橫行
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion

            //列印完最後一行地圖後要換行
            Console.WriteLine();
        }

        /// <summary>
        /// 從地圖的方法中抽象出來一個方法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string DrawStringMap(int i)
        {
            string str = " ";
            // 如果玩家一和玩家二的座標一樣,玩家還在地圖上,畫一個尖括號
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
                //shift+空格切換全形
                str = "A";
            }
            else if (PlayerPos[1] == i)
            {
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "□";
                        break;

                    case 1:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "⊙";
                        break;

                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "★";
                        break;

                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "㊡";
                        break;

                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        str = "卐";
                        break;
                }//switch
            }//else
            return str;
        }

        /// <summary>
        /// 玩家姓名輸入
        /// </summary>
        public static void InputName()
        {
            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[0] == PlayerNames[1])
            {
                if (PlayerNames[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能為空,請重新輸入");
                }
                if (PlayerNames[0] == PlayerNames[1])
                {
                    Console.WriteLine("玩家B的姓名不能和玩家A一樣,請重新輸入");
                }
                PlayerNames[1] = Console.ReadLine();
            }
        }

        /// <summary>
        /// 玩遊戲
        /// </summary>
        public static void PlayGame(int playerNumber)
        {
            Random r = new Random();
            int rNumber = r.Next(1, 7);
            Console.WriteLine("{0}按任意鍵開始擲骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}擲出了{1}", PlayerNames[playerNumber], rNumber);
            PlayerPos[playerNumber] += rNumber;
            Console.WriteLine("{0}按任意鍵開始行動", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行動完了", PlayerNames[playerNumber]);
            Console.ReadKey(true);

            //PlayerPos[playerNumber] += 99;    執行除錯

            ChangePos();    //新增預防陣列超出範圍
                            //ReadKey:從鍵盤讀取一個字元顯示在控制檯中  ReadLine:從鍵盤讀取多個字元返回 
                            //Write:將字元寫到控制檯不換行   WriteLine:將字元寫到控制檯並換行

            //玩家A有可能菜刀了玩家B 方塊 幸運輪盤 地雷 暫停 時空隧道
            if (PlayerPos[0] == PlayerPos[1])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退六格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]);
                PlayerPos[1 - playerNumber] -= 6;
                Console.ReadKey(true);
            }
            else//踩到了關卡
            {
                //玩家的座標
                switch (Maps[PlayerPos[playerNumber]])  //0 1 2 3 4 
                {
                    case 0:
                        Console.WriteLine("玩家{0}踩到了方塊,安全。", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("玩家{0}踩到了幸運輪盤,請選擇 1——交換位置 2——轟炸對方退六格。", PlayerNames[playerNumber]);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}選擇跟玩家{1}交換位置", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                int temp = PlayerPos[playerNumber];
                                PlayerPos[playerNumber] = PlayerPos[1 - playerNumber];
                                PlayerPos[1 - playerNumber] = temp;
                                Console.WriteLine("交換完成,按任意鍵繼續!");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}選擇轟炸玩家{1},玩家{2}退六格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playerNumber] -= 6;
                                Console.WriteLine("玩家{0}退了六格", PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能輸入1或者輸入2 1——交換位置 2——轟炸對方");
                                input = Console.ReadLine();
                            }
                        }//while
                        break;

                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷,退六格", PlayerNames[playerNumber]);
                        PlayerPos[playerNumber] -= 6;
                        Console.ReadKey(true);
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暫停,暫停一回合", PlayerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了時空隧道,前進10格", PlayerNames[playerNumber]);
                        PlayerPos[playerNumber] += 10;
                        Console.ReadKey(true);
                        break;
                }//switch
            }//else
            ChangePos();
            DrawMap();

        }

        /// <summary>
        /// 當玩家座標發生改變後呼叫
        /// </summary>
        public static void ChangePos()
        {
            if (PlayerPos[0] <= 0)
            {
                PlayerPos[0] = 0;
            }
            else if (PlayerPos[0] >= 99)
            {
                PlayerPos[0] = 99;
                DrawMap();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("             ~~恭喜勝利~~");
                Console.WriteLine("玩家{0}戰勝了玩家{1},恭喜恭喜", PlayerNames[0], PlayerNames[1]);
                Console.WriteLine("             ~~恭喜勝利~~");
                Console.ReadKey(true);
                IsAgain();
                //Environment.Exit(0);
            }

            if (PlayerPos[1] <= 0)
            {
                PlayerPos[1] = 0;
            }
            else if (PlayerPos[1] >= 99)
            {
                PlayerPos[1] = 99;
                DrawMap();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("             ~~恭喜勝利~~");
                Console.WriteLine("玩家{0}戰勝了玩家{1},恭喜恭喜", PlayerNames[1], PlayerNames[0]);
                Console.WriteLine("             ~~恭喜勝利~~");
                Console.ReadKey(true);
                IsAgain();
                //Environment.Exit(0);
            }

        }

        /// <summary>
        /// 判斷玩家是否再來一局
        /// </summary>
        public static void IsAgain()
        {
            while(true)
            {
                Console.WriteLine("請問還要再來一局嗎?是請輸入 Yes/No");
                String againFlag = Console.ReadLine();
                while (againFlag != "Yes" && againFlag != "yes" && againFlag != "No" && againFlag != "no")
                {
                    Console.WriteLine("輸入錯誤,請重輸!");
                    againFlag = Console.ReadLine();
                }
                if (againFlag == "Yes" || againFlag == "yes")
                {
                    PlayerPos[0] = 0;
                    PlayerPos[1] = 0;
                    DrawMap();
                    while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
                    {
                        if (Flag[0] == false)
                        {
                            PlayGame(0);
                        }
                        else
                        {
                            Flag[0] = true;
                        }

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

                    }//while

                }
                else if (againFlag == "No" || againFlag == "no")
                {
                    Environment.Exit(0);
                }
            }
           
        }
    }
} 

專案地址 密碼:331y

相關文章