[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Text;
namespace 飞行棋项目
{
class Program
{ //用静态字段 模仿全局变量
public static int[] Maps = new int[100];
//声明一个静态字段 存放玩家 坐标;
public static int[] PlayerPos = new int[2];
//存储两个玩家的姓名;
public static string[] PlayerNames = new string[2];
//两个玩家的标记: Flags[0] 是玩家A的标记 Flags[1]是玩家B的标记
public static bool[] Flags = new bool[2];
static void Main(string[] args)
{ //游戏开始 打印游戏头;
GameShow();
#region //判断玩家A与玩家B的不能为空 2017年8月3日
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("请输入玩家A的姓名:");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("玩家姓名不能为,空请重新输入:");
PlayerNames[0] = Console.ReadLine();
}
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("请输入玩家B的姓名:");
PlayerNames[1] = Console.ReadLine();
while (PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1])
{
if (PlayerNames[1] == "")
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("玩家姓名不能为空,请重新输入:");
PlayerNames[1] = Console.ReadLine();
}
else if (PlayerNames[1] == PlayerNames[0])
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("玩家B的姓名与玩家A姓名重复,请重新输入:");
PlayerNames[1] = Console.ReadLine();
}
}
#endregion
Console.WriteLine("{0}玩家用A表示", PlayerNames[0]);
Console.WriteLine("{0}玩家用B表示", PlayerNames[1]);
InitailMap();
DrawMap();
//当玩家A跟玩家B 没有一个人在终点的时候 一直玩游戏;
while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
{
if (Flags[0] == false)
{
PlayGame(0);
}
else
{
Flags[0] = false;
}
if (PlayerPos[0] >= 99)
{
Console.WriteLine("玩家{0}赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
break;
}
if (Flags[1] == false)
{
PlayGame(1);
}
else
{
Flags[1] = false;
}
if (PlayerPos[1] >= 99)
{
Console.WriteLine("玩家{0}赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
break;
}
}//while
Console.ReadKey();
}
/// <summary>
/// 游戏头 打印的图案;
/// </summary>
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Blue; //不知道值可以·一下 或者空格 会自动匹配 值的类型;
Console.WriteLine("*******************************");
Console.WriteLine("*******************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("*******************************");
Console.WriteLine("**********飞行棋项目***********");
Console.WriteLine("*******************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("*******************************");
Console.WriteLine("*******************************");
Console.WriteLine("*******************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(" 2017年8月1日 ");
}
/// <summary>
/// 地图特殊值;
/// </summary>
public static void InitailMap()
{
int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; //幸运轮盘 ◎
for (int i = 0; i < luckyturn.Length; i++)
{
// int index = luckyturn;
Maps[luckyturn] = 1;
}
int[] landMine = { 5, 13, 17, 33, 50, 64, 80, 94 };//地雷 ☆
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine] = 2;
}
int[] pause = { 9, 27, 60, 93 };//暂停▲
for (int i = 0; i < pause.Length; i++)
{
Maps[pause] = 3;
}
int[] timeTunel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道 卍
for (int i = 0; i < timeTunel.Length; i++)
{
Maps[timeTunel] = 4;
}
}
//画地图中分离出来的方法:判断i坐标的值
/// <summary>
/// 画关卡判断
/// </summary>
/// <param name="i">map 坐标</param>
/// <returns>返回图形</returns>
public static string DrawStringMap(int i)
{
string str = "";
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)//保证A/B都在一行 所以playerpos[]=i;
{
str = "<>";
}
else if (PlayerPos[0] == i)
{
str = "A";
}
else if (PlayerPos[1] == i)
{
str = "B";
}
else
{
switch (Maps)
{
case 0:
Console.ForegroundColor = ConsoleColor.Gray;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
str = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.DarkCyan;
str = "卍";
break;
}//switch 的括号
}//else 的括号
return str;
}
/// <summary>
/// 画地图
/// </summary>
public static void DrawMap()
{
#region //画地图第一横行;
Console.WriteLine("图例:幸运转盘: ◎ 地雷:☆ 暂停:▲ 时空隧道: 卍");
for (int i = 0; i < 30; i++)
{
#region //打印前判断AB的值;
//if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)//保证A/B都在一行 所以playerpos[]=i;
//{
// Console.Write("<>");
//}
//else if (PlayerPos[0] == i)
//{
// Console.Write("A");
//}
//else if (PlayerPos[1] == i)
//{
// Console.Write("B");
//}
//else {
// switch(Maps)
// {
// case 0:
// Console.ForegroundColor = ConsoleColor.Gray;
// Console.Write("□");
// break;
// case 1:
// Console.ForegroundColor = ConsoleColor.Red;
// Console.Write("◎");
// break;
// case 2:
// Console.ForegroundColor = ConsoleColor.Green;
// Console.Write("☆");
// break;
// case 3:
// Console.ForegroundColor = ConsoleColor.Blue;
// Console.Write("▲");
// break;
// case 4:
// Console.ForegroundColor = ConsoleColor.DarkCyan;
// Console.Write("卍");
// break;
// }//switch 的括号
//}//else 的括号
#endregion
//调用判断 封装成的方法;
Console.Write(DrawStringMap(i));
}//for 的括号
Console.WriteLine(); //画完一行之后 换行;
#endregion
#region //第一竖行;
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
#region //画关卡 判断
//if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)//保证A/B都在一行 所以playerpos[]=i;
//{
// Console.Write("<>");
//}
//else if (PlayerPos[0] == i)
//{
// Console.Write("A");
//}
//else if (PlayerPos[1] == i)
//{
// Console.Write("B");
//}
//else
//{
// switch (Maps)
// {
// case 0:
// Console.ForegroundColor = ConsoleColor.Gray;
// Console.Write("□");
// break;
// case 1:
// Console.ForegroundColor = ConsoleColor.Red;
// Console.Write("◎");
// break;
// case 2:
// Console.ForegroundColor = ConsoleColor.Green;
// Console.Write("☆");
// break;
// case 3:
// Console.ForegroundColor = ConsoleColor.Blue;
// Console.Write("▲");
// break;
// case 4:
// Console.ForegroundColor = ConsoleColor.DarkCyan;
// Console.Write("卍");
// break;
// }//switch 的括号
//}//else 的括号
#endregion
Console.Write(DrawStringMap(i));
Console.WriteLine();
}
#endregion
#region 第二横行;
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
//打印完毕之后换行
Console.WriteLine();
#endregion
#region //第二竖行;
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawStringMap(i));
}
#endregion
#region //第三横行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawStringMap(i));
}
#endregion
Console.WriteLine();//地图画完之后换一行;
}
/// <summary>
/// 玩游戏
/// </summary>
/// <param name="playerNumber">传入0还是1</param>
public static void PlayGame(int playerNumber)
{
Random r = new Random();
int rNumber = r.Next(1, 7);
//改颜色
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}掷骰子,掷除了{1}", PlayerNames[playerNumber], rNumber);
PlayerPos[playerNumber] += rNumber;
ChangePos();
Console.ReadKey(true);
Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}行动完了", PlayerNames[playerNumber]);
Console.ReadKey(true);
//玩家A有可能踩到了玩家B 踩到□ 踩到 幸运转盘 踩到 暂停 踩到地雷 踩到 时空隧道
if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber])
{
Console.WriteLine("{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]);
PlayerPos[1 - playerNumber] -= 6;
Console.ReadKey(true);
}
else //踩到了关卡 跟玩家的坐标有关系、
{
switch (Maps[PlayerPos[playerNumber]])
{
case 0: Console.WriteLine("玩家{0}踩到了方块,安全 什么都没发生", PlayerNames[playerNumber]);
Console.ReadKey(true);
break;
case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择:1 交换位置 2 轰炸对方", PlayerNames[playerNumber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}选择与玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
Console.WriteLine(true);
int temp = PlayerPos[playerNumber];
PlayerPos[playerNumber] = PlayerPos[1 - playerNumber];
PlayerPos[1 - playerNumber] = temp;
Console.WriteLine("交换完成 按任意键继续游戏");
Console.ReadKey(true);
break;// 完成之后 得跳出循环
}
else if (input == "2")
{
Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber], PlayerNames[1 - playerNumber]);
Console.ReadKey(true);
PlayerPos[1 - playerNumber] -= 6;
Console.WriteLine("玩家{0}退了6格", PlayerNames[playerNumber]);
Console.ReadKey(true);
break;// 完成之后 得跳出循环
}
else
{
Console.WriteLine("只能输入1或者2 1交换位置 2轰炸对地方");
input = Console.ReadLine();
}
}
break;
case 2: Console.WriteLine("玩家{0}踩到了地雷退6格", PlayerNames[playerNumber]);
PlayerPos[playerNumber] -= 6;
Console.ReadKey(true);
break;
case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一个回合玩家{1}继续玩", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
Flags[playerNumber] = true;
Console.ReadKey(true);
break;
case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerNumber]);
PlayerPos[playerNumber] += 10;
Console.ReadKey(true);
break;
}//switch括号
}//else 括号
ChangePos();
Console.Clear();//a/b的图形改变之后地图重新画;
DrawMap();
}
/// <summary>
/// 当玩家坐标改变的时候调用
/// </summary>
public static void ChangePos()
{
if (PlayerPos[0] < 0)
{
PlayerPos[0] = 0;
}
if (PlayerPos[0] >= 99)
{
PlayerPos[0] = 99;
}
if (PlayerPos[1] < 0)
{
PlayerPos[1] = 0;
}
if (PlayerPos[1] >= 99)
{
PlayerPos[1] = 99;
}
}
}
}