---------------------- ASP.Net+Android+IOS開發、.Net培訓、期待與您交流! ----------------------
本項目使用的是面向過程的程序設計思想,用到的主要知識點有,用數組存放同類型的數據,類,方法的聲明和調用,循環結構,選擇結構等。代碼是我一句句敲出來的,可能會有小的bug和需要改善的地方,如有發現請指出以便改正,謝謝。不多說了,上代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 騎士飛行棋
{
#region
class Program
{
static string[] playerName;//存放玩家姓名
static int[] Map = new int[100];//存入地圖上的100個格兒
static int[] playerPos = { 0, 0 };//存放玩家當前的位置座標
static bool[] isStop = { false, false };//控制是否允許玩家擲骰子
static void Main(string[] args)
{
ShowUI();
playerName = InputPlayerName();//調用讓玩家輸入姓名的方法
Console.Clear();//清屏
ShowUI();//顯示遊戲名稱界面
Console.WriteLine("對戰開始......");
Console.WriteLine("玩家[{0}]用A表示...", playerName[0]);
Console.WriteLine("玩家[{0}]用B表示...", playerName[1]);
Console.WriteLine("如果A、B在同一位置上,則用<>來表示...");
SetGatePos();//設置關卡的座標位置
DrawMap();//繪製地圖
Console.WriteLine("開始遊戲......");
//玩家A、B輪流擲骰子
while (playerPos[0] < 99 & playerPos[1] < 99)
{ // isStop爲false玩家可以擲骰子,爲true則不能擲骰子
if (isStop[0] == false)
{
Start(0);//玩家A調用擲骰子方法
}
else
{ //說明isStop==true
isStop[0] = false;
}
if (playerPos[0] >= 99)
{
Console.WriteLine("玩家A獲勝!");
break;
}
if (isStop[1] == false)
{
Start(1);//玩家B調用擲骨子方法
}
else
{
isStop[1] = false;
}
}
if (playerPos[0] >= 99)
{
Console.WriteLine("玩家A勝利了!!");
}
else if(playerPos[1]>=99)
{
Console.WriteLine("玩家B勝利了!!");
}
Console.ReadKey();
}
#endregion
/// <summary>
/// 顯示遊戲名稱
/// </summary>
static void ShowUI()
{
Console.WriteLine("*******************************************");
Console.WriteLine("* *");
Console.WriteLine("* 騎 士 飛 行 棋 *");
Console.WriteLine("* *");
Console.WriteLine("*******************************************");
}
/// <summary>
/// 輸入玩家姓名
/// </summary>
static string[] InputPlayerName()
{
string[] names = new string[2];//存放玩家A、B的姓名,names[0]是玩家A,names[1]是玩家B
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();
}
}
return names;
}
/// <summary>
/// 定義各關卡的座標
/// </summary>
static void SetGatePos()
{
//定義各關卡的座標
int[] LuckyTurn = { 15, 23, 44, 56, 69, 78, 83, 97 };//幸運輪盤
int[] TimeTunnel = { 10, 30, 37, 45, 63, 72 };//時空隧道
int[] Pause = { 8, 17, 27, 40, 60, 88, 92 };//暫停一次
int[] LandMine = { 4, 19, 33, 50, 68, 80, 95 };//地雷
//把定義的關卡的座標應用到遊戲地圖中,分別用0、1、2、3、4表示。0表示普通,1幸運輪盤,2表示時空隧道,3表示暫停一次,4表示地雷
for (int i = 0; i < LuckyTurn.Length; i++)
{
//int pos = LuckyTurn[i];
//Map[pos] = 1;
Map[LuckyTurn[i]] = 1;
}
for (int i = 0; i < TimeTunnel.Length; i++)
{
Map[TimeTunnel[i]] = 2;
}
for (int i = 0; i < Pause.Length; i++)
{
Map[Pause[i]] = 3;
}
for (int i = 0; i < LandMine.Length; i++)
{
Map[LandMine[i]] = 4;
}
}
/// <summary>
/// 判斷某個座標上應該畫什麼圖案
/// </summary>
static string GetMapString(int pos)
{
string mapString = "";
if (playerPos[0] == pos && playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
mapString = "<>";
}
else if (playerPos[0] == pos)
{
mapString = "Α";
}
else if (playerPos[1] == pos)
{
mapString = "Β";
}
else
{
switch (Map[pos])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
mapString = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Yellow;
mapString = "◎";//幸運輪盤
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
mapString = "※";//時空隧道
break;
case 3:
Console.ForegroundColor = ConsoleColor.Red;
mapString = "▲";//暫停一次
break;
case 4:
Console.ForegroundColor = ConsoleColor.Cyan;
mapString = "☆";//地雷
break;
}
}
return mapString;
}
/// <summary>
/// 繪製飛行棋地圖
/// </summary>
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.WriteLine(GetMapString(i));
}
for (int i = 64; i >= 35; i--)//第二行
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
for (int i = 65; i < 70; i++)//第二列
{
Console.WriteLine(GetMapString(i));
}
for (int i = 70; i < 100; i++)//第三行
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// 擲骰子,對玩家所在的關卡進行判斷
/// </summary>
/// <param name="playerID"></param>
static void Start(int playerID)
{
int step;
string msg = "";
Random r = new Random();
Console.WriteLine("{0}按任意鍵開始擲骰子...", playerName[playerID]);
Console.ReadKey(true);
step = r.Next(1, 7);
//小竅門1
//ConsoleKeyInfo cki = Console.ReadKey(true);
//step = r.Next(1, 7);
//if (cki.Key == ConsoleKey.Tab)
//{
// ConsoleKeyInfo cki2 = Console.ReadKey(true);
// if (cki2.Key == ConsoleKey.F1)
// {
// step = ReadInt(1, 100);
// }
//}
//小竅門2
//if (cki.Key == ConsoleKey.Tab)
//{
// step = 10;
//}
//else
//{
// step = r.Next(1, 7);
//}
Console.WriteLine("{0}擲出了:{1}", playerName[playerID], step);
Console.WriteLine("按任意鍵開始行動...");
Console.ReadKey(true);
playerPos[playerID] += step;
CheckPos(playerID);//檢查玩家座標是否越界
if (playerPos[playerID] == playerPos[1 - playerID])
{
playerPos[1 - playerID] = 0;
msg = string.Format("{0}踩到了{1},{1}後退到起點!", playerName[playerID], playerName[1 - playerID]);
}
else
{
switch (Map[playerPos[playerID]])
{
case 0: break;
case 1:
Console.Clear();
DrawMap();
Console.WriteLine("{0}踩到了幸運輪盤,請選擇一個幸運數字:", playerName[playerID]);
Console.WriteLine("1、與對方交換位置\t2、轟炸對方");
int select = ReadInt(1,2);
if (select == 1)
{
int temp = playerPos[playerID];
playerPos[playerID] = playerPos[1 - playerID];
playerPos[1 - playerID] = temp;
msg = string.Format("{0}選擇了與對方交換位置!", playerName[playerID]);
}
else
{
playerPos[1 - playerID] -= 6;
CheckPos(playerID);
msg = string.Format("{0}轟炸了{1},{1}退後6格!", playerName[playerID], playerName[1 - playerID]);
}
break;
case 2:
playerPos[playerID]+=10;
CheckPos(playerID);
msg = string.Format("{0}踩到了時空隧道,前進10格",playerName[playerID]);
break;
case 3:
isStop[playerID] = true;
msg = string.Format("{0}踩到了暫停一次!", playerName[playerID]);
break;
case 4:
playerPos[playerID] -= 6;
CheckPos(playerID);
msg = string.Format("{0}踩到地雷,退後6格!", playerName[playerID]);
break;
}
}
Console.Clear();
DrawMap();
if (msg != "")
{
Console.WriteLine(msg);
}
Console.WriteLine("{0}擲出了:{1},行動完成!", playerName[playerID], step);
Console.WriteLine("==================玩家A、B的最新位置如下====================");
Console.WriteLine("{0}當前的位置爲:{1}", playerName[0], playerPos[0] + 1);
Console.WriteLine("{0}當前的位置爲:{1}", playerName[1], playerPos[1] + 1);
Console.WriteLine("============================================================");
}
/// <summary>
/// 檢查玩家當前座標是否越界
/// </summary>
static void CheckPos(int playerID)
{
if (playerPos[playerID] < 0)
{
playerPos[playerID] = 0;
}
if (playerPos[playerID] > 99)
{
playerPos[playerID] = 99;
Console.WriteLine("{0}先到達終點,取得勝利!",playerName[playerID]);
}
}
/// <summary>
/// 讀取用戶的輸入並做出判斷
/// </summary>
/// <returns></returns>
static int ReadInt(int min,int max)
{
int input;
while (true)
{
try
{
input = Convert.ToInt32(Console.ReadLine());
if (input < min || input > max)
{
Console.WriteLine("只能輸入{0}~{1}之間的數,請重新輸入!",min,max);
continue;
}
else
{
return input;
}
}
catch
{
Console.WriteLine("只能輸入數字,請重新輸入!");
}
}
}
}
}
---------------------- ASP.Net+Android+IOS開發、.Net培訓、期待與您交流! ----------------------
詳細請查看:http://edu.csdn.net