C#基礎入門第八天(飛行棋)

這個練手的項目,能很好的總結前八天所學的東西,而且還能培養必定的編程的思惟。建議先手打一遍,而後試着寫一遍拍錯,在寫一遍以求理解!本身第二遍寫時候錯了不少錯,好比在玩遊戲上調用,因爲flasg數組的else賦值不對,致使只要碰到暫停,就沒法繼續下去。還有將console.readkey(true)寫成了console.writeLine(true),小小的兩個錯誤排查了將近兩小時。編程

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

namespace 飛行棋
{
class Program
{
    //2.1因爲遊戲有不少個方法,並且多個方法都有可能用到地圖的數字,因此將這個數字模擬成全局變量
    public static int[] Maps = new int[100];
    //3.2使用靜態字段數組,存儲玩家座標
    public static int[] PlayerPos = new int[2];
    //4.1聲明一個全局靜態數組,存儲玩家姓名
    public static string[] PlayerName = new string[2];
    //7.2暫停至關於讓踩到的玩家少執行一次玩遊戲的方法或者讓沒踩到的玩家多執行一遍,這樣就是
    //    知足某些條件才執行某些代碼,能夠用bool類型來判斷
    static bool[] Flags = new bool[2]; //bool默認值是false
    static void Main(string[] args)
    {
        //1.因爲遊戲頭是會被隨時調用的,因此建立一個遊戲頭的方法並調用
        GameShow();
        //4.讓玩家輸入姓名
        #region 讓玩家輸入姓名
        Console.WriteLine("輸入玩家A的姓名,不能爲空");
        PlayerName[0] = Console.ReadLine();
        while (PlayerName[0] == "")
        {
            Console.WriteLine("玩家A姓名不能爲空,請從新輸入");
            PlayerName[0] = Console.ReadLine();
        }
        Console.WriteLine("輸入玩家B的姓名,不能爲空,不能跟A重複");
        PlayerName[1] = Console.ReadLine();
        while (PlayerName[1] == "" || PlayerName[1] == PlayerName[0])
        {
            if (PlayerName[1] == "")
            {
                Console.WriteLine("玩家B姓名不能爲空,請從新輸入");
                PlayerName[1] = Console.ReadLine();
            }
            else
            {
                Console.WriteLine("玩家B姓名不能跟玩家A重複,請從新輸入");
                PlayerName[1] = Console.ReadLine();
            }
        }
        #endregion
        //5.清屏
        Console.Clear();
        //6.從新調用遊戲頭
        GameShow();
        Console.WriteLine("{0}的士兵用A表示", PlayerName[0]);
        Console.WriteLine("{0}的士兵用B表示", PlayerName[1]);
        //2.建立初始化地圖方法並調用(加載地圖所需的資源,爲畫地圖作準備)
        //將整數數組中的數字編程控制檯中顯示的特殊字符串的這個過程 就是初始化地圖
        InitailMap();
        //3.建立畫地圖方法並調用
        DrawMap();
        //7.玩遊戲的過程
        while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
        {
            //先在這裏模擬寫玩遊戲的方法,而後將這個方法封裝成方法來給玩家A和B不聽的調用

            //7.1傳遞玩家進去
            if (Flags[0] == false)
            {
                PlayGame(0);
            }
            else
            {
                Flags[0] = false;
            }
            if (PlayerPos[0] >= 99)
            {
                Console.WriteLine("玩家{0}無恥的贏了玩家{1}", PlayerName[0], PlayerName[1]);
                break;
            }
            if (Flags[1] == false)
            {
                PlayGame(1);
            }
            else
            {
                Flags[1] = false;
            }
            if (PlayerPos[1] >= 99)
            {
                Console.WriteLine("玩家{0}無恥的贏了玩家{1}", PlayerName[1], PlayerName[0]);
                break;
            }
        }
        Win();

        Console.ReadKey();
    }

    #region 建立遊戲頭的方法
    /// <summary>
    /// 建立遊戲頭
    /// </summary>
    public static void GameShow()
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("**********************************");
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("**********************************");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("********C#基礎入門之飛行棋********");
        Console.ForegroundColor = ConsoleColor.Magenta;
        Console.WriteLine("**********************************");
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("**********************************");
    }
    #endregion

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

    #region 畫地圖
    public static void DrawMap()
    {
        Console.WriteLine("圖例:幸運輪盤:◎   地雷:¤   暫停:▲   時空隧道:卐");
        #region 第一橫行
        //3.1畫第一橫
        for (int i = 0; i < 30; i++)
        {
            //3.3若是玩家A的座標跟玩家B的座標相同,而且都在地圖上,畫一對尖括號,寫好的代碼拿去封裝了
            Console.Write(PanDuan(i));
        }
        #endregion
        Console.WriteLine();//畫完第一橫後換行
        #region 第一豎行
        for (int i = 30; i < 35; i++)
        {
            for (int j = 0; j <= 28; j++)
            {
                Console.Write("  ");
                //3.4當地圖畫到這裏,咱們又得開始畫關卡和玩家位置,這時候,咱們就能夠直接把第一橫行的判斷代碼
                //封裝成方法,在之後的話地圖中直接調用
            }
            Console.Write(PanDuan(i));
            Console.WriteLine();//豎行比較特殊,每畫完一個關卡就該換行
        }
        #endregion
        #region 第二橫行
        for (int i = 64; i >= 35; i--)
        {
            Console.Write(PanDuan(i));
        }
        #endregion
        Console.WriteLine();//畫完第二橫後換行
        #region 第二豎行
        for (int i = 65; i <= 69; i++)
        {
            Console.WriteLine(PanDuan(i));
        }
        #endregion
        #region 第三橫行
        for (int i = 70; i <= 99; i++)
        {
            Console.Write(PanDuan(i));
        }
        #endregion
        Console.WriteLine();//畫完最後一橫後換行
    }
    #endregion

    #region 經過畫完第一橫行後,在畫第一豎行時,咱們發現第一橫的判斷是被從新調用的代碼,因此封裝成一個方法
    /// <summary>
    /// 畫地圖關卡,玩家位置的判斷
    /// </summary>
    public static string PanDuan(int i)
    {
        //避免方法裏面使用console.write和console.readline
        string str = "";
            //3.3若是玩家A的座標跟玩家B的座標相同,而且都在地圖上,畫一對尖括號
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
            str = "A";
            }
            else if (PlayerPos[1] == i)
            {
            str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                    str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                    str = "¤";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                    str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                    str = "卐";
                        break;
                }
        }
        return str;
    }
    #endregion

    #region 玩遊戲的過程
    /// <summary>
    /// 玩遊戲的過程
    /// </summary>
    public static void PlayGame(int playerNumber)
    {
        Random r = new Random();
        int rNumber = r.Next(1, 7);
        Console.WriteLine("{0}按任意鍵開始擲骰子", PlayerName[playerNumber]);
        Console.ReadKey(true);
        Console.WriteLine("{0}擲出了{1}", PlayerName[playerNumber], rNumber);
        PlayerPos[playerNumber] += rNumber;
        ChangePos();
        Console.ReadKey(true);
        Console.WriteLine("{0}按任意鍵開始行動", PlayerName[playerNumber]);
        Console.ReadKey(true);
        Console.WriteLine("{0}行動完了", PlayerName[playerNumber]);
        Console.ReadKey(true);
        //玩家A有可能踩到玩家B 方塊 幸運輪盤  地雷  暫停  時空隧道
        if (PlayerPos[0] == PlayerPos[1])
        {
            Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}倒退6格", PlayerName[playerNumber], PlayerName[1- playerNumber], PlayerName[1- playerNumber]);
            PlayerPos[1- playerNumber] -= 6;
            ChangePos();
            Console.ReadKey(true);
        }
        else //踩到關卡
        {
            //玩家的座標
            switch (Maps[PlayerPos[playerNumber]])//1 2 3 4 0
            {
                case 0:
                    Console.WriteLine("玩家{0}踩到了方塊,安全!", PlayerName[playerNumber]);
                    Console.ReadKey(true);
                    break;
                case 1:
                    Console.WriteLine("玩家{0}踩到了幸運輪盤,請輸入1--跟玩家B交換位置,2--轟炸對方,使對方倒退6格");
                    while (true)
                    {
                        string input = Console.ReadLine();
                        if (input == "1")
                        {
                            Console.WriteLine("玩家{0}選擇與玩家{1}交換位置", PlayerName[playerNumber], PlayerName[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}", PlayerName[playerNumber], PlayerName[1- playerNumber]);
                            Console.ReadKey(true);
                            PlayerPos[1- playerNumber] -= 6;
                            ChangePos();
                            Console.WriteLine("轟炸完成,請按任意鍵繼續!");
                            Console.ReadKey(true);
                            break;
                        }
                        else
                        {
                            Console.WriteLine("只能選擇1,2,請從新輸入!");
                            input = Console.ReadLine();
                        }
                    }
                    break;
                case 2:
                    Console.WriteLine("玩家{0}踩到地雷,倒退6格", PlayerName[playerNumber]);
                    Console.ReadKey(true);
                    PlayerPos[playerNumber] -= 6;
                    ChangePos();
                    break;
                //因爲暫停的邏輯最難,放到最後寫
                case 3:
                    Console.WriteLine("玩家{0}踩到暫停,遊戲暫停一回合", PlayerName[playerNumber]);
                    Flags[playerNumber] = true;
                    Console.ReadKey(true);
                    break;
                case 4:
                    Console.WriteLine("玩家{0}踩到時空隧道,前進10格", PlayerName[playerNumber]);
                    Console.ReadKey(true);
                    PlayerPos[playerNumber] += 10;
                    ChangePos();
                    break;
            }
        }
        //玩家A玩完一個回合,清屏從新畫
        //ChangePos();  也能夠在最後這裏調用,這樣都在地圖內
        Console.Clear();
        DrawMap();
    }
    #endregion

    #region 限定玩家座標一直在地圖內
    /// <summary>
    /// 當玩家座標發生變更時調用
    /// </summary>
    public static void ChangePos()
    {
        if (PlayerPos[0] < 0)
        {
            PlayerPos[0] = 0;
        }
        if (PlayerPos[0] >= 99)
        {
            PlayerPos[0] = 99;
        }
        if (PlayerPos[1] < 0)
        {
            PlayerPos[1] = 0;
        }
        if (PlayerPos[1] >= 99)
        {
            PlayerPos[1] = 99;
        }
    }
    #endregion

    #region 勝利
    /// <summary>
    /// 玩家勝利時,調用
    /// </summary>
    public static void Win()
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("                                          ◆                      ");
        Console.WriteLine("                    ■                  ◆               ■        ■");
        Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
        Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
        Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
        Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
        Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
        Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
        Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
        Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
        Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
        Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
        Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
        Console.ResetColor();
    }
    #endregion 
}

}數組

相關文章
相關標籤/搜索