黑马程序员技术交流社区
标题:
C#飞行棋(类目优化,方便折叠)
[打印本页]
作者:
追溯客
时间:
2013-10-8 15:33
标题:
C#飞行棋(类目优化,方便折叠)
[csharp]
view plain
copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static string[] names = new string[2]; //names[0]为A名字,names[1]为B名字
public static int ReadInt(int min, int max)//读取数字
{
int number = 0;
do
{
try
{
number = Convert.ToInt32(Console.ReadLine());
return number;
}
catch
{
Console.WriteLine("您输入的有误,请重新输入!");
}
}
while (true);
}
static bool[] isStop = { false, false }; //判断是否暂停
作者:
追溯客
时间:
2013-10-8 15:33
static void Main(string[] args)
{
Show(); //显示游戏名称
#region 输入玩家A/B名称
Console.WriteLine("请输入玩家A的姓名:");
names[0] = Console.ReadLine();
while (names[0] == "") //判断玩家A的姓名,不能为空,为空则重新输入
{
Console.WriteLine("玩家A的姓名不能为空,请重新输入!");
Console.WriteLine("请输入玩家A的姓名:");
names[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名:"); //判断玩家B的姓名,不能为空,为空或与A相同则重新输入
names[1] = Console.ReadLine();
while (names[1] == "" || names[1] == names[0])
{
if (names[1] == "")
{
Console.WriteLine("玩家B的姓名不能为空!请重新输入!");
}
else
{
Console.WriteLine("玩家B的姓名不能与玩家A[{0}]想同!请重新输入!", names[0]);
}
names[1] = Console.ReadLine();
}
Console.Clear();
#endregion
#region 游戏开始
Show(); //显示游戏名称
Console.WriteLine("游戏启动......");
Console.WriteLine("玩家{0}用\"A\"表示", names[0]);
Console.WriteLine("玩家{0}用\"B\"表示", names[1]);
Console.WriteLine("如果 玩家A和玩家B在一格内,用\"<>\"表示");
NewMap(); //初始化地图
DrawMap(); //绘制地图
Console.WriteLine("按任意键开始游戏......");
Console.ReadKey(true);
#endregion
作者:
追溯客
时间:
2013-10-8 15:34
else
{
switch (Map[PlayerPos[0]])
{
case 0:
//普通,没有效果
msg = "";
break;
case 1:
//走到了幸运轮盘
Console.Clear();
Console.WriteLine("恭喜你来到了幸运轮盘,请选择运气!");
Console.WriteLine("按任意键进入!");
DrawMap();
Console.WriteLine("恭喜{0}来到了幸运轮盘,请选择运气!", names[PlayerNumber]);
Console.WriteLine("1--交换位置 2--轰炸对方");
int UserInput = ReadInt(1, 2);
if (UserInput == 1)
{//要与对方交换位置
int temp = PlayerPos[0];
PlayerPos[0] = PlayerPos[1];
PlayerPos[1] = temp;
CheckPos();
msg = string.Format("{0}选择与对方交换位置!", names[PlayerNumber]);
}
else//轰炸对方
{
PlayerPos[1 - PlayerNumber] = PlayerPos[1 - PlayerNumber] - 5;
CheckPos();
msg = string.Format("{0}选择轰炸对方,{1}退5格!", names[PlayerNumber], names[1 - PlayerNumber]);
}
break;
case 2:
//踩到地雷
PlayerPos[0] = PlayerPos[0] - 5;
CheckPos();
msg = string.Format("{0}踩到了地雷,退6格!", names[PlayerNumber]);
break;
case 3:
//暂停一次
isStop[0] = true;
msg = string.Format("{0}踩到暂停,暂停一次", names[PlayerNumber]);
break;
case 4:
//时空隧道
PlayerPos[0] = PlayerPos[0] + 10;
CheckPos();
msg = string.Format("{0}进入时空隧道,前进10格!", names[PlayerNumber]);
break;
}
}
Console.Clear();
DrawMap();
if (msg != "")
{
Console.WriteLine(msg);
}
Console.WriteLine("{0}掷出了{1},行动完成!", names[PlayerNumber], step);
Console.WriteLine("*******************玩家A和玩家B的位置如下*******************");
Console.WriteLine("{0}的位置为{1}", names[0], PlayerPos[0]);
Console.WriteLine("{0}的位置为{1}", names[1], PlayerPos[1]);
}
static void Show() //飞行器名称
{
Console.WriteLine("********************************************");
Console.WriteLine("* *");
Console.WriteLine("* 飞 行 棋 *");
Console.WriteLine("* *");
Console.WriteLine("********************************************");
}
static int[] Map = new int[100];
static void NewMap() //绘制新地图 存储字符下标
{
//转盘◎
//地雷※
//暂停▼
//隧道〓
int[] turn = { 5, 15, 25, 35, 75, 48 }; //轮盘
int[] mine = { 6, 16, 32, 24, 64, 52 }; //地雷
int[] stop = { 7, 51, 46, 83, 97, 69 }; //暂停
int[] time = { 9, 65, 95, 75, 84, 91 }; //隧道
for (int i = 0; i < turn.Length; i++)
{
int pos = turn[i];
Map[pos] = 1;
}
for (int i = 0; i < turn.Length; i++)
{
Map[mine[i]] = 2;
}
for (int i = 0; i < stop.Length; i++)
{
Map[stop[i]] = 3;
}
for (int i = 0; i < time.Length; i++)
{
Map[time[i]] = 4;
}
}
static string MapString(int pos) //获得第pos坐标上要绘制的图案
{
string result = "";
if (PlayerPos[0] == pos && PlayerPos[1] == pos) // ||两个都为真才能退出 &&两个中只要有一个为假就退出
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "<>"; //判断A和B是否都在第i格上
}
else if (PlayerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "A"; //判断A在当前格
}
else if (PlayerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "B"; //判断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.Magenta;
result = "〓";
break;
}
}
return result;
}
static int[] PlayerPos = { 0, 0 }; //playerPos[0]是玩家A坐标,PlaysPos[1]是玩家B的坐标
static void DrawMap() //绘制地图
{
Console.WriteLine("图例 转盘◎ 地雷※ 暂停▼ 隧道〓");
//绘制第一行,
for (int i = 0; i <= 29; i++)
{
Console.Write(MapString(i));
}
//第一行绘制完毕
Console.WriteLine();
//绘制第一列
for (int i = 30; i <= 34; i++)
{
//绘制29个双空格(半角)
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
//Console.WriteLine(MapString(i));
string str = MapString(i);
Console.WriteLine(str);
}
//绘制第二行
for (int i = 64; i >= 35; i--)
{
Console.Write(MapString(i));
}
Console.WriteLine();
//绘制第二列
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(MapString(i));
}
//绘制第三行
for (int i = 70; i <= 99; i++)
{
Console.Write(MapString(i));
}
Console.WriteLine();
}
static void CheckPos() //确认A.B位置及检查两玩家是否越界
{
for (int i = 0; i <= 1; i++)
{
if (PlayerPos[i] > 99)
{
PlayerPos[i] = 99;
}
if (PlayerPos[i] < 0)
{
PlayerPos[i] = 0;
}
}
}
}
}
作者:
追溯客
时间:
2013-10-8 15:35
因为字符限制无法一次性发完,这里可以直接复制全部http://blog.csdn.net/zk672756448/article/details/12395819
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2