using System;數組
namespace Game
{
class Program
{
//用靜態字段模擬全局變量
public static int[] Maps = new int[100];
//申明一個數組來存儲玩家A和B
public static int[] PlayerPos = new int[2];
//存儲玩家姓名
public static string[] PlayNames = new string[2];
//兩個玩家的標記
static bool[] Flags = new bool[2];//默認是false
static void Main(string[] args)
{
GameShow();
#region 輸入玩家姓名
Console.WriteLine("請輸入玩家A的姓名");
PlayNames[0] = Console.ReadLine();
while (PlayNames[0] == "")
{
Console.WriteLine("不能爲空,請從新輸入");
PlayNames[0] = Console.ReadLine();
}
Console.WriteLine("請輸入玩家B的姓名");
PlayNames[1] = Console.ReadLine();
while (PlayNames[1] == PlayNames[0] || PlayNames[1] == "")
{
if (PlayNames[1] == "")
{
Console.WriteLine("不能爲空,請從新輸入");
PlayNames[1] = Console.ReadLine();
}
else
{
Console.WriteLine("不能和玩家A的姓名相同,請從新輸入");
PlayNames[1] = Console.ReadLine();
}
}
#endregion
Console.Clear();
GameShow();
Console.WriteLine("{0}的士兵用A表示", PlayNames[0]);
Console.WriteLine("{0}的士兵用A表示", PlayNames[1]);
InitailMap();
DrawMap();
//讓玩家AB同時開始玩遊戲並結束
while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
{
if (Flags[0]== false)
{
PlayGame(0);
}
else
{
Flags[0] = false;
}
if (PlayerPos[0] >=99)
{
Console.WriteLine("玩家{0}的贏了玩家{1}", PlayNames[0], PlayNames[1]);break;
}
if (Flags[1]==false)
{
PlayGame(1);
}
else
{
Flags[1] = false;
}
if (PlayerPos[1] >= 99)
{
Console.WriteLine("玩家{0}的贏了玩家{1}", PlayNames[1], PlayNames[0]); break;
}
}
Win();
Console.ReadKey();
}
/// <summary>
/// 遊戲頭
/// </summary>
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("**********************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("**********************");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("*****飛行棋***********");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("**********************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("**********************");
}dom
/// <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 = { 9, 27, 60, 93,4,2,3,7,8 };//暫停▲
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//時空隧道卐
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
}spa
public static void DrawMap()
{
Console.WriteLine("圖例:幸運輪盤:◎ 地雷:☆ 暫停:▲ 時空隧道:卐");
#region 第一橫行
for (int i = 0; i < 30; i++)
{
//若是玩家A跟玩家B的座標相同,而且都在這個地圖上,畫一個尖括號
Console.Write(DrawStringMap(i));
}
#endregion
//換行繼續畫豎着
Console.WriteLine();
#region 第一豎行
for (int i = 30; i < 35; 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.WriteLine(DrawStringMap(i));
}
#endregioninput
#region 第三橫行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawStringMap(i));
}string
#endregion
Console.WriteLine();
}
/// <summary>
/// 畫圖中提取的一個抽象的方法
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string DrawStringMap(int i)
{
string str = "";
//若是玩家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.Green; str = "□"; break;
case 1: Console.ForegroundColor = ConsoleColor.Blue; str = "◎"; break;
case 2: Console.ForegroundColor = ConsoleColor.Yellow; str = "☆"; break;
case 3: Console.ForegroundColor = ConsoleColor.Magenta; str = "▲"; break;
case 4: Console.ForegroundColor = ConsoleColor.Green; str = "卐"; break;
}
}
return str;
}
/// <summary>
/// 玩遊戲
/// </summary>
/// <param name="playNumber"></param>
public static void PlayGame(int playNumber)
{
Random r = new Random();
int rNumber = r.Next(1, 7);
Console.WriteLine("{0}按任意鍵開始擲骰子", PlayNames[playNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}擲出了{1}", PlayNames[playNumber], rNumber);
PlayerPos[playNumber] += rNumber;
Console.ReadKey(true);
Console.WriteLine("{0}按任意鍵開始行動", PlayNames[playNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}行動完了", PlayNames[playNumber]);
Console.ReadKey(true);
//玩家A有可能踩到了玩家B 方塊 幸運輪盤 地雷 暫停 時空隧道
if (PlayerPos[playNumber] == PlayerPos[1 - playNumber])
{
Console.WriteLine("{0}踩到了{1}並使{1}退6格", PlayNames[playNumber], PlayNames[1 - playNumber], PlayNames[1 - playNumber]);
PlayerPos[playNumber] -= 6;
Console.ReadKey(true);
}
else
{
switch (Maps[PlayerPos[playNumber]])
{
case 0: Console.WriteLine("{0}踩到了方塊 遊戲繼續", PlayNames[playNumber]); Console.ReadKey(true); break;
case 1:
Console.WriteLine("{0}踩到了幸運輪盤,請選擇 1--交換位置 2--轟炸對方", PlayNames[playNumber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("{0}和{1}交換位置", PlayNames[playNumber], PlayNames[1 - playNumber]);
Console.ReadKey(true);
int temp = PlayerPos[playNumber];
PlayerPos[playNumber] = PlayerPos[1 - playNumber];
PlayerPos[1 - playNumber] = temp;
Console.WriteLine("交換完成!!!按任意鍵繼續遊戲!!!");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("{0}轟炸{1} 使{2}退6格", PlayNames[playNumber], PlayNames[1 - playNumber],PlayNames[1-playNumber]);it
Console.ReadKey(true);
PlayerPos[1-playNumber] -= 6;
ChangePos();
Console.WriteLine("{0}退了6格",PlayNames[playNumber]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("只能輸入1或者2 1--交換位置 2--轟炸對方");
input = Console.ReadLine();
}io
}
break;
case 2: Console.WriteLine("{0}踩到了地雷退6格", PlayNames[playNumber]); Console.ReadKey(true); PlayerPos[playNumber] -= 6; ChangePos(); break;
case 3: Console.WriteLine("{0}踩到了暫停 暫停一回合", PlayNames[playNumber]); Flags[playNumber] = true; Console.ReadKey(true); break;
case 4: Console.WriteLine("{0}踩到了時空隧道前進10格", PlayNames[playNumber]); PlayerPos[playNumber] += 10; Console.ReadKey(true); ChangePos(); break;
}
}
ChangePos();
Console.Clear();
DrawMap();
}
//發送座標移動時使用 防止AB跑出數組外
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;
}
}
/// <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();
}
}
}class