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

來源:互聯網
上載者:User

代碼如下所示:

複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 騎士飛行棋
{
class Program
{
//在下面的數組儲存我們遊戲地圖各各關卡
//數組的下標為0的元素對應地圖上的第1格 下標為1的元素對應元素第2格...下標為n的元素對應n+1格
//在數組中 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; //用於存放產生的隨機數

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

static string msg = ""; //用於儲存使用者踩到某個關卡,輸出的話

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

static Random r = new Random();//r是產生的隨機數

static void Main( string[] args)
{

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

//這個迴圈中讓玩家A和玩家B輪流擲骰子 當玩家A或者玩家B的座標>=99時,則迴圈結束
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();
//判斷用書輸入的內容是否為空白,如果為空白,則讓使用者重新輸入
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的姓名不可為空,請重新輸入!");
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數組的資料
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()//產生一個整數
{
int i = ReadInt(int.MaxValue, int.MinValue);
return i;
}

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

}

}

/// <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);//產生一個1到6之間的隨機數
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; //注意,一旦座標發生改變,就要判斷座標值是否>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現在所在的位置是否有其他關卡
switch (map[playerPos[playerNumber]])
{
case 0:
//普通,沒有效果
msg = "";
break;
case 1:
//走到了 幸運輪盤關卡
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);

}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.