版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenhaiming123/article/details/82082259
一、前言
本文记录了在学习C#视频中的骑士飞行棋实例。记录了数组的使用,方法的表达和调用,相似事件逻辑判断和方法封装,传值的使用,随机数的使用等。
二、正文
namespace _骑士飞行棋
{
class Program
{
#region 声明各种变量
public static int[] Map = new int[100];
//类里面,方法外面,声明一个长度为100的数组,用来放地图,这个数组可能很多方法都用
public static int[] PlayerPos = new int[2] { 0, 0 };//声明一个数组用来存玩家A和玩家B,用来存放坐标,PlayerPos[0]为玩家A
public static string[] PlayerNames = new string[2];
public static bool[] flag = new bool[] { false, false };//声明一个flag的布尔变量,用来表示玩家暂停判断,CheckPos,又如:case2--
#endregion
public static void ShowUI()
#region 先写一个方法,显示界面图形
{
Console.WriteLine("*****************************************");
Console.WriteLine("* *");
Console.WriteLine("* 终极骑士飞行棋 12.00 *");
Console.WriteLine("* *");
Console.WriteLine("*****************************************");
}
#endregion
static void Main(string[] args)
{
#region 调用方法:游戏头和地图
ShowUI();
//调用方法,游戏头
InitMap();
//初始化地图
#region 玩家A和玩家B输入姓名
Console.WriteLine("请输入玩家A的姓名");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.WriteLine("玩家A的姓名不能为空,请重新输入");
PlayerNames[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
PlayerNames[1] = Console.ReadLine();
while (PlayerNames[1] == PlayerNames[0] || PlayerNames[1] == "")
//玩家B的姓名等于玩家A的姓名或玩家B的姓名为空
{
if (PlayerNames[1] == PlayerNames[0])
{
Console.WriteLine("玩家B的姓名和玩家A的姓名[{0}]不能相同相同", PlayerNames[0]);
}
else
{
Console.WriteLine("玩家B的姓名为空,请重新输入");
}
PlayerNames[1] = Console.ReadLine();
}
#endregion
Console.Clear();
//清空输入玩家姓名,界面游戏表头上移
ShowUI();//游戏头,重载
Console.WriteLine("对战开始......");
Console.WriteLine("{0}的士兵用A表示......", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B表示......", PlayerNames[1]);
DrawMap();//画地图
#endregion
while (PlayerPos[0] <= 99 && PlayerPos[1] <= 99)//不成立,两个玩家坐标比较
{
#region 掷骰子的代码冗余,已被写成Rowtouzi方法
// Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[0]);
// Console.ReadKey(true);//获取用户按下的下一个字符或功能键,按下的键显示在控制台窗口中,ReadKey有两个方法,检查
// Console.WriteLine("{0}掷出了{1}", PlayerNames[0],4);
// Console.WriteLine("{0}按任意键开始行动.....", PlayerNames[0]);
// PlayerPos[0] += 4;
// if (PlayerPos[0] == PlayerPos[1])
// {
// Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[], PlayerNames[], PlayerNames[]);
// PlayerPos[1] -= 6;
// }
// else
// {
// switch (Map[PlayerPos[0]]) //玩家A的坐标位置
// {
// case 0:Console .WriteLine("行动完了");break ;
// case 1:Console .WriteLine("{0}走到了幸运轮盘,请选择 1----交换位置,2----轰炸对方"");break ;
//要写方法实现 ReadInt。然后就是到定义变量msg,接着写成Rowtouzi
// case 2:Console .WriteLine("踩到地雷了");break ;
// PlayerPos[0]-=6;
// case 3:Console .WriteLine("踩到暂停了");break ;
// case 4:Console .WriteLine("踩到时空隧道了");break ;
// PlayerPos[0] += 10;
// Console.Clear();//刷新地图
// DrawMap();
//}
#endregion
#region 玩家A掷骰子
if (flag[0] == false)
{
RowTouZi(0);//调用玩家A掷骰子的方法
}
else
{
flag[0] = false;
}
#endregion
if (PlayerPos[0] == 99)
{
Console.WriteLine("恭喜玩家A胜利了");
break;
}
#region 玩家B掷骰子
if (flag[1] == false)//声明一个flag的布尔变量,用来表示玩家暂停判断,CheckPos
{
RowTouZi(1);//调用玩家B掷骰子的方法
}
else
{
flag[1] = false;
}
#endregion
if (PlayerPos[1] == 99)
{
Console.WriteLine("恭喜玩家B胜利了");
break;
}
Console.WriteLine("行动完毕...");
}
Win();
Console.ReadKey();
//画图
//如何行走-----逻辑判断
}
/// <summary>
/// 显示游戏头
/// </summary>
public static void InitMap()
{//初始化地图
//我用0表示普通,显示给用户就是□
//。。。。1.。。幸运转轮,显示组用户就@
//。。。。2.。。地雷,显示给用户就是☆
//。。。。3.。。暂停,显示给用户就是▲
//。。。。4.。。时空隧道,显示给用户就是卐
int[] luckturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘@
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷,显示给用户就是☆
int[] pause = { 9, 27, 60, 93 };//暂停,显示给用户就是▲
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道,显示给用户就是卐
//把数组中下标为6,23,40,55,69,83的地方的值改为1
for (int i = 0; i < luckturn.Length; i++)//幸运转轮
{
//int temp = luckyturn[i];
Map[luckturn[i]] = 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]] = 4;
}
}
/// <summary>
/// 画地图的
/// </summary>
public static void DrawMap()
{
//图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐
Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐");
#region 画地图第一行
DrawMapLeftToRight(0, 29);
#endregion
Console.WriteLine();
#region 画地图第一竖行
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
//Console.Write();
Console.WriteLine(DrawStringMap(i));
}
#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));
}
#endregion
#region 画第三横行
DrawMapLeftToRight(70, 99);
#endregion
Console.WriteLine(); //使输入的玩家开始掷骰子进行换行
}
/// <summary>
/// 画地图的逻辑
/// </summary>
/// <param name="pos">传过来的坐标</param>
/// <returns></returns>
public static string DrawStringMap(int pos)
{
string temp = "";
#region 画第一行的逻辑,封装后的代码如下
//如果玩家A和玩家B在一起并且在地图上就画<>
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "<>";
}
else if (PlayerPos[0] == pos)//如果玩家A在地图上就画A
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "A";
}
else if (PlayerPos[1] == pos)//如果玩家B在地图上就画B
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "B";
}
else
{
switch (Map[pos])//如果玩家A和玩家B不在一起也不在这个坐标上就画该显示的地图图标
{
case 0: Console.ForegroundColor = ConsoleColor.Gray;
temp = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
temp = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Blue;
temp = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
temp = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
temp = "卐";
break;
}//end switch
}//end else
return temp;
#endregion
}
/// <summary>
/// 从某个坐标到另一个坐标画地图
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
public static void DrawMapLeftToRight(int left, int right)
{
for (int i = left; i <= right; i++)
{
Console.Write(DrawStringMap(i));
}
}
/// <summary>
/// 根据传过来的数字,返回一个数字
/// </summary>
/// <param name="msg"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static int ReadInt(string msg, int min, int max)//传过来一个字符串和两个数字;这个方法是int类型
{
while (true)
{
try
{
Console.WriteLine(msg); //显示传过来的字符串,msg来自RowTouZi的方法
int number = Convert.ToInt32(Console.ReadLine());//把用户输入转换成数字
if (number >= min && number <= max)
{
return number;
}
else
{
Console.WriteLine("你的输入不合法!只能输入{0}到{1}之间的数字!", min, max);
continue;
}
}
catch
{
Console.WriteLine("输入有误,请重新输入!");
}
}
//程序绝对走不到这里
}
public static void RowTouZi(int playerPos)//playerPos用于传值
{
Random r = new Random();//随机数生成器
int num = r.Next(1, 7);//返回一个指定范围的随机数
string msg = "";//定义一个字符串msg为空
Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerPos]);
ConsoleKeyInfo coninfo = Console.ReadKey(true);//你懂的飞行
if (coninfo.Key == ConsoleKey.Q)
{
coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.A)
{
coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.Z)
{
num = 50;
}
}
}
//Console.ReadKey(true);
Console.WriteLine("{0}掷出了{1}", PlayerNames[playerPos], num);
Console.WriteLine("{0}按任意键开始行动.....", PlayerNames[playerPos]);
Console.ReadKey(true);
PlayerPos[playerPos] += num;
CheckPos();
if (PlayerPos[playerPos] == PlayerPos[1 - playerPos])
{
msg = string.Format("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerPos], PlayerNames[1 - playerPos], PlayerNames[1 - playerPos]);
PlayerPos[1 - playerPos] -= 6;
CheckPos();
}
else
{
switch (Map[PlayerPos[playerPos]]) //玩家A的坐标位置
{
case 0: msg = "行动完了"; break;
case 1:
msg = string.Format("{0}走到了幸运轮盘,请选择 1----交换位置,2----轰炸对方", PlayerNames[playerPos]);
int number = ReadInt(msg, 1, 2);
if (number == 1)
{
int temp = 0;//两个位置交换,与前面学习变量交换的一样
temp = PlayerPos[playerPos];//玩家位置已经进行封装
PlayerPos[playerPos] = PlayerPos[1 - playerPos];
PlayerPos[1 - playerPos] = temp;
msg = string.Format("玩家{0}选择了与玩家{1}交换位置", PlayerNames[playerPos], PlayerNames[1 - playerPos]);
}
else
{
PlayerPos[1 - playerPos] = 0;//被轰炸回到起点
msg = string.Format("玩家{0}选择轰炸玩家{1}", PlayerNames[playerPos], PlayerNames[1 - playerPos]);
}
break;
case 2:
//踩到地雷了
msg = "恭喜你,能踩到地雷,百年不遇,退6格";
PlayerPos[playerPos] -= 6;
CheckPos();
break;
case 3:
msg = "踩到暂停了";
flag[playerPos] = true;
break;
case 4:
msg = "恭喜你,这个猥琐家伙竟然穿越了10步";
PlayerPos[playerPos] += 10;
CheckPos();
break;
}
}
Console.Clear();
DrawMap();
Console.WriteLine(msg);
}
public static void CheckPos()
{
if (PlayerPos[0] > 99)//玩家A的位置判断,最终只能在99的位置
{
PlayerPos[0] = 99;
}
if (PlayerPos[1] > 99)
{
PlayerPos[1] = 99;
}
if (PlayerPos[0] < 0)
{
PlayerPos[0] = 0;
}
if (PlayerPos[1] < 0)
{
PlayerPos[1] = 0;
}
}