1. 程式人生 > >黑馬程式設計師——騎士飛行棋C#程式碼

黑馬程式設計師——騎士飛行棋C#程式碼

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


namespace 騎士飛行棋
{
class Program
{
#region 定義靜態變數
static int[] Map = new int[100];
static int[] playerPos = { 0, 0 };//playerPos[0]存玩家A的位置,playerPos[1]存玩家B的位置
static string[] names = new string[2];//names[1]存玩家A的姓名,names[2]存玩家B的姓名
static string msg = "";//用於儲存使用者踩到某關卡輸出的話
static int step = 0;//用於存放產生的隨機數
static bool[] isStop = { false, false };//如果走到暫停,則設定為true
#endregion


static void Main(string[] args)
{

ShowUI();
GetNames();
Entrance();
InitialMap();
DrawMap();


//這個迴圈讓玩家輪流擲骰子,當A或B的座標不小於99時,結束迴圈
int i = 1;
do
{
i = 1 - i;
RunAction(i);
} while (playerPos[i] < 99);


Finish();
}




/// <summary>玩家
/// 輸入兩個玩家的名字
/// </summary>
static void GetNames()
{
Console.WriteLine("請輸入玩家A的姓名?");
names[0] = Console.ReadLine();
//判斷使用者輸入是否為空,若是則重新輸入
while (names[0] == "")
{
Console.WriteLine("玩家A的姓名不能為空,請重新輸入!");
names[0] = Console.ReadLine();
}
Console.WriteLine("請輸入玩家B的姓名?");
names[1] = Console.ReadLine();
//判斷使用者輸入是否為空或重名,若是則重新輸入
while (names[1] == "" || names[1] == names[0])
{
if (names[1] == "")
{
Console.WriteLine("玩家B的姓名不能為空,請重新輸入!");
}
if (names[1] == names[0])
{
Console.WriteLine("該姓名已被玩家A佔用,請重新輸入!");
}
names[1] = Console.ReadLine();
}
}


/// <summary>入口
/// 進入遊戲準備以及提示
/// </summary>
static void Entrance()
{
Console.WriteLine("輸入任意鍵進入遊戲。");
Console.ReadKey();
Console.Clear();
ShowUI();
Console.WriteLine("對戰開始。。。。。。");
Console.WriteLine("{0}用A表示", names[0]);
Console.WriteLine("{0}用B表示", names[1]);
Console.WriteLine("如果AB在同一位置,用<>來表示");
}


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


/// <summary>初始化
/// 用於儲存在地圖中為地雷的下標
/// </summary>
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, 99 };//時空隧道卐 4


for (int i = 0; i < luckyTurn.Length; i++)
{
int pos = luckyTurn[i];
Map[pos] = 1;
}
for (int i = 0; i < landMine.Length; i++)
{
Map[landMine[i]] = 2;
}
for (int i = 0; i < pause.Length; i++)
{
Map[pause[i]] = 3;
}
for (int i = 0; i < timeTunnel.Length; i++)
{
Map[timeTunnel[i]] = 3;
}


}


/// <summary>畫圖
/// 用字元畫圖
/// </summary>
static void DrawMap()
{
Lengend();
int i;
//第一行
for (i = 0; i < 30; i++)
{
Console.Write(GetMapString(i));
}
//第一列
for (i = 30; i < 35; i++)
{
Console.WriteLine();
for (int j = 0; j < 29; j++)
{
Console.Write("  ");
}
Console.Write(GetMapString(i));
}
//第二行
Console.WriteLine();
for (i = 64; i > 34; i--)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
//第二列
for (i = 65; i < 70; i++)
{
Console.WriteLine(GetMapString(i));
}
//第三行
for (i = 70; i < 100; i++)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();


Console.ResetColor();
Console.WriteLine("開始遊戲");
}


/// <summary>畫字元
/// 畫出對應位置上的字元
/// </summary>
/// <param name="i"></param>整數
static string GetMapString(int pos)
{
string result = "";


if (playerPos[0] == pos && playerPos[1] == pos)//both A and B is at pos;
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "<>";
}
else if (playerPos[0] == pos)//A is at pos;
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "A ";
}
else if (playerPos[1] == pos)//B is at 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;
}


/// <summary>圖例
/// 輸出符號以及它們的意思
/// </summary>
static void Lengend()
{
Console.WriteLine("圖例:幸運輪盤◎    地雷☆    暫停▲    時空隧道卐");
}


/// <summary>隨機數
/// 獲取[left, right)之間的隨機數
/// </summary>
/// <returns>void</returns>int
static int GetRandomInt(int left, int right)
{
Random r = new Random();//r是產生隨機數用的
return r.Next(left, right);
}


/// <summary>越界
/// 進行玩家座標越界的判斷
/// </summary>
static void CheckPos()
{
for (int i = 0; i < 2; i++)
{
if (playerPos[i] > 99)
{
playerPos[i] = 99;
}
if (playerPos[i] < 0)
{
playerPos[i] = 0;
}
}
}

/// <summary>閉區間
/// 得到一個輸入的在閉區間內的整數
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
static int ReadInt(int min, int max)
{
int result;
do
{
try
{
result = int.Parse(Console.ReadLine());
if (result >= min && result <= max)
{
return result;
}
else
{
Console.WriteLine("{0}不在{1}與{2}的閉區間內", result, min, max);
Console.WriteLine("請重新輸入!");
}
}
catch
{
Console.WriteLine("不是數字,請重新輸入!");
}
} while (true);
}


/// <summary>幸運輪盤
/// names[i]走到了幸運輪盤關卡
/// </summary>
static void LuckyWheel(string[] names, int i)
{
int j = (i + 1) % 2;


Console.Clear();
DrawMap();
Console.WriteLine("{0}走到了幸運輪盤,請選擇運氣?", names[i]);
Console.WriteLine("1——交換位置    2——轟炸對方");
int userSelect = ReadInt(1, 2);
if (userSelect == 1)
{//交換位置
playerPos[0] ^= playerPos[1];
playerPos[1] ^= playerPos[0];
playerPos[0] ^= playerPos[1];
msg = string.Format("{0}選擇了與對方交換位置!", names[i]);
}
else
{//轟炸對方
playerPos[j] -= 6;
CheckPos();
msg = string.Format("{0}轟炸了{1},{1}退六步!", names[i],names[j]);
}
}


/// <summary>顯示變化
/// 清屏重畫顯示
/// </summary>
static void ClearDrawWrite(string[] names, int i)
{
int j = (i + 1) % 2;


Console.Clear();
DrawMap();
if (msg != "")
{
Console.WriteLine(msg);
}
msg = "";
Console.WriteLine("{0}擲出了{1},行動完成!", names[i], step);
Console.WriteLine("********玩家A的位置與玩家B的位置如下********");
Console.WriteLine("{0}的位置為{1}", names[i], playerPos[i] + 1);
Console.WriteLine("{0}的位置為{1}", names[j], playerPos[j] + 1);
Console.ReadKey();
}


/// <summary>擲骰子並移動
/// 產生隨機數,並按照隨機數移動
/// </summary>
/// <param name="names"></param>
/// <param name="i"></param>
static void DiceMove(string[] names, int i)
{


int j = 1 - i;
//int j = (i + 1) % 2;
//int j = Math.Abs(i - 1);


Console.WriteLine("{0}按任意鍵開始擲骰子。。。", names[i]);
FraudStep(ref i);
Console.WriteLine("按任意鍵開始行動。。。");
Console.ReadKey(true);
playerPos[i] += step;//一旦座標發生改變,就要判斷是否>99或者<0
CheckPos();//檢測座標越界並移動
}


/// <summary>地點與相對位置
/// 移動後的行為
/// </summary>
/// <param name="names"></param>
/// <param name="i"></param>
static void Geography(string[] names, int i)
{
int j = (i + 1) % 2;


if (playerPos[i] == playerPos[j])//玩家i踩到玩家j
{
playerPos[i] = 0;
msg = string.Format("{0}踩到了{1},{1}退回原點", names[i], names[j]);
}
else
{//沒踩到,要判斷玩家i現在所在的位置是否有其他關卡
#region 輪盤 地雷 暫停 隧道
switch (Map[playerPos[i]])
{
case 0:
//普通,沒有效果
break;
case 1:
//幸運輪盤
LuckyWheel(names, i);
break;
case 2:
//踩到了地雷
playerPos[i] -= 6;
CheckPos();
msg = string.Format("{0}踩到了地雷,退6格!", names[i]);
break;
case 3:
isStop[i] = true;
msg = string.Format("{0}走到紅燈,暫停一次", names[i]);
//暫停一次
break;
case 4:
//時空隧道
playerPos[i] += 10;
CheckPos();
msg = string.Format("{0}進入了時空隧道,Cool,進十步", names[i]);
break;
}
#endregion
}
}


/// <summary>作弊
/// Tab+F1可以前進1~100之間的步數
/// </summary>
static void FraudStep(ref int i)
{
ConsoleKeyInfo rec = Console.ReadKey(true);
if (rec.Key == ConsoleKey.Tab)
{
ConsoleKeyInfo cc = Console.ReadKey(true);
if (cc.Key == ConsoleKey.F1)
{
step = ReadInt(1, 100);
}
}
else
{
step = GetRandomInt(1, 7);//產生一個1~6之間的隨機整數
Console.WriteLine("{0}擲出了:{1}", names[i], step);
}
}


/// <summary>擲走寫
/// A或B擲骰子的方法
/// </summary>
/// <param name="playerNumber"></param>
static void Action(int playerNumber)
{
DiceMove(names, playerNumber);
Geography(names, playerNumber);
ClearDrawWrite(names, playerNumber);
}


/// <summary>判斷並執行
/// 是否遇到了暫停
/// </summary>
/// <param name="playerNumber"></param>
static void RunAction(int playerNumber)
{
if (isStop[playerNumber] == false)
{
Action(playerNumber);
}
else
{//說明 isStop[playerNumber] == true;
isStop[playerNumber] = false;
}
}


/// <summary>遊戲結果
/// 
/// </summary>
static void Finish()
{
Console.Clear();
ShowUI();
if (playerPos[0] >= 99)
{
Console.WriteLine("{0}勝利了!!!", names[0]);
}
else
{
Console.WriteLine("{0}勝利了!!!", names[1]);
}
}


}
}