黑马程序员——骑士飞行棋C#代码

来源:互联网 发布:sql查询分析器建立 编辑:程序博客网 时间:2024/05/16 23:33

------- <a href="http://www.itheima.com" target="blank">Windows Phone 7手机开发</a>、<a href="http://www.itheima.com" target="blank">.Net培训</a>、期待与您交流! -------

using System;

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]);
}
}


}
}
原创粉丝点击