看基础课程后,到了飞行棋这个项目.开始的时候要看看能不能自己做出来,做的时候才发现没什么思路,做到画地图的时候就卡住了,我想着一次性就把地图给画全了,结果发现很难,看了一视频才知道,要一步一步来.最后看一段视频做了一段的情况下,终于完成了飞行棋.
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 飞行棋
{
class Program
{
static int[] map = new int[100];//地图上每一格所代表的变量
static int[] playpos = { 0, 0 };
static string[] name=new string[2];
static bool[] stop = { false, false };//用于判断谁暂停掷骰子
static void Main(string[] args)
{
ShowUI();//显示界面
Console.WriteLine("请输入玩家A的姓名:");
name[0] = Name();//玩家A的姓名
Console.WriteLine("请输入玩家B的姓名:");
name[1] = Name();//玩家B的姓名
while (name[0] == name[1])//判断A,B姓名是否重名
{
Console.WriteLine("输入的姓名不能和玩家{0}同名,请再输入玩家B的姓名:", name[0]);
name[1] = Name();
}
Console.Clear();//清屏
ShowUI();//显示界面
Console.WriteLine("对站开始.....");
Console.WriteLine("如果AB在同一位置用<>表示");
Initialmap();//初始化地图
Drawmap();//画出地图
Console.WriteLine("对战开始...");
//这个循环让玩家A和玩家B轮流掷骰子,当玩家A或者玩家B其中一位的坐标大于等99时,结束.
while (playpos[0] < 99 && playpos[1] < 99)
{
if (stop[0] == false)
{
action(0);
}
else
{
stop[0] = false;
}
if (playpos[0] >= 99)
{
break;
}
if (stop[1] == false)
{
action(1);
}
else
{
stop[1] = false;
}
}
Console.Clear();
ShowUI();
if (playpos[0] >= 99)
{
Console.WriteLine("{0}恭喜你胜利了!!!", name[0]);
}
else
{
Console.WriteLine("{0}恭喜你胜利了!!!", name[1]);
}
Console.ReadKey();
}
/// <summary>
/// 用于绘制飞行旗名称
/// </summary>
static void ShowUI()
{
Console.WriteLine(" ***************************************");
Console.WriteLine(" * *");
Console.WriteLine(" * 伟 哥 飞 行 棋 *");
Console.WriteLine(" * *");
Console.WriteLine(" ***************************************");
}
/// <summary>
/// 返回一个用户输入的姓名,并且判断姓名不能为空
/// </summary>
/// <returns></returns>
static string Name()
{
do
{
string name = Console.ReadLine();
if (name == "")
{
Console.WriteLine("不能输入空的名字,请重新输入!");
}
else
{
return name;
}
}
while (true);
}
/// <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 };//时空剖隧道 4表示
for (int i = 0; i < luckyTurn.Length; i++)
{
map[luckyTurn[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>
static void Drawmap()
{
Console.WriteLine(" {0}为A玩家,{1}为B玩家", name[0], name[1]);
Console.WriteLine("图例: 幸运轮盘:◎ 地雷:★ 暂停:▲ 时空隧道:※");
for (int i = 0; i <= 29; i++)//画第一行
{
Console.Write(Outputmap(i));//调用判断该画的图标
}
Console.WriteLine();//换行,从下一行开始画
for (int i = 30; i <= 34; i++)//画第一坚
{
for (int j = 0; j < 29; j++)//前面的空格
{
Console.Write(" ");
}
Console.WriteLine(Outputmap(i));
}
for (int i = 64; i >= 35; i--)//第二行
{
Console.Write(Outputmap(i));
}
Console.WriteLine();
for (int i = 65; i <= 69; i++)//画第二坚
{
Console.WriteLine(Outputmap(i));
}
for (int i = 70; i <= 99; i++)//画第三行
{
Console.Write(Outputmap(i));
}
Console.WriteLine();
Console.ResetColor();
}
/// <summary>
/// 判断需要输出的形状
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
static string Outputmap(int i)
{
string result = "";
if (i == playpos[0] && i == playpos[1])
{
Console.ForegroundColor = ConsoleColor.DarkGray;
result = "<>";
}
else if (playpos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Cyan;
result = "A";
}
else if (playpos[1] == i)
{
Console.ForegroundColor = ConsoleColor.Cyan;
result = "B";
}
else
{
switch (map[i])
{
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.Magenta;
result = "※";
break;
}
}
return result;
}
/// <summary>
/// 判断是否越界
/// </summary>
static void checkmap()
{
for (int i = 0; i <= 1; i++)
{
if (playpos[i] > 99)
{
playpos[i] = 99;
}
if (playpos[i] < 0)
{
playpos[i] = 0;
}
}
}
static int Reint(int min,int max)
{
int temp = 0;
do
{
try
{
temp = Convert.ToInt32(Console.ReadLine());
if (temp < min || temp > max)
{
Console.WriteLine("你输入的数不是{0}到{1}之间的数请重新输入",min,max);
}
}
catch
{
Console.WriteLine("你输入的不是数字,请重新输入");
}
} while (temp < min || temp > max);
return temp;
}
/// <summary>
/// 掷骰子 并且判断是否踩到特殊格子 画出地图 显示结果
/// </summary>
/// <param name="playname"></param>
static void action(int playname)
{
string msg = "";
Random r = new Random();//产生随机数
int setp;//存随机数
Console.WriteLine("{0}按任意键开始掷骰子...", name[playname]);
ConsoleKeyInfo rec = Console.ReadKey(true);//返回一个用户输入的按键
if (rec.Key == ConsoleKey.F10&&rec.Modifiers==(ConsoleModifiers.Alt|ConsoleModifiers.Control))//作弊器,判断是否输入的是F10
{
setp = Reint(1,100);//是否掷骰子25
}
else
{
setp = r.Next(1, 7);//产生一个1-6的随机数
}
Console.WriteLine("{0}掷出了:{1}", name[playname], setp);
Console.WriteLine("{0}按任意键开始行动...",name[playname]);
Console.ReadKey(true);//按任意键,加true则不会显示按的键的值
playpos[playname] = playpos[playname] + setp;//玩家name1的新位置 ;
checkmap();//查检是否会跳出地图
if (playpos[0] == playpos[1])//判断玩家A是否踩到玩家B
{
playpos[1-playname] = 0;//是则让其回到起点
msg = string.Format("{0}踩到了{1},{1}回到原点!", name[playname], name[1-playname], name[1-playname]);
}
else
{
switch (map[playpos[playname]])//判断是否踩到特殊格子
{
case 0:
msg = "";
break;
case 1://幸福轮盘.
int xuanzhe;
Console.Clear();
Drawmap();
Console.WriteLine("恭喜{0}踩到幸运轮盘啦!", name[playname]);
Console.WriteLine("1,与{0}交换位置 2,轰炸对方", name[1-playname]);
xuanzhe = Reint(1, 2);//返加一个1到2之内的整数
if (xuanzhe == 1) //1则交换位置
{
int temp = playpos[playname];
playpos[playname] = playpos[1-playname];
playpos[1-playname] = temp;
msg = string.Format("{0}选择与{1}了交换位置", name[playname], name[1-playname]);
}
else if (xuanzhe == 2)//否则轰炸对方
{
playpos[1-playname] = playpos[1-playname] - 6;
checkmap();
msg = string.Format("{0}轰炸了{1},{1}退6格", name[playname], name[1-playname]);
}
break;
case 2://地雷
playpos[playname] = playpos[playname] - 6;
msg = string.Format("{0}踩到地雷啦,后退6步", name[playname]);
checkmap();
break;
case 3://暂停
stop[playname] = true;
msg = string.Format("{0}踩到暂停,需要休息一次", name[playname]);
break;
case 4://时空隧道
playpos[playname] = playpos[playname] + 10;
checkmap();
msg = string.Format("{0}进入了时空隧道,进10步,爽暴了!", name[playname]);
break;
}
}
Console.Clear();
Drawmap();
if (msg != "")
{
Console.WriteLine(msg);
}
Console.WriteLine("{0}掷出了{1},行动完成!", name[playname], setp);
Console.WriteLine("{0}的位置为{1},{2}的位置为{3}", name[playname], playpos[0] + 1, name[1-playname], playpos[1] + 1);
}
}
}