----------------------
Windows Phones 7手机开发、
.Net培训、期待与您交流! ----------------------
看了传智播客最新的一个《飞行棋》的项目,加了自己学过C#就做了下,思路跟视频里所讲的老师差不多,只不过增加或者说修复了点功能:
1、A、B同时暂停改为都取消;
2、先判断到的点有没有被其它点占领,占领则踩掉,如果该点是特殊点再执行前进或者后退暂停等功能;
3、特殊点前进或者后退功能后,到达的点如果被占领,则踩掉;
4、源码:
(1)类:
/// <summary>
/// 飞行棋类
/// </summary>
public class ChessPieces
{
}
(2)类里面增加了方法和字段,这里我把属性也加上去了(暂无优化)
/// <summary>
/// 游戏姓名字段
/// </summary>
private string[] name = new string[2];
/// <summary>
/// 游戏姓名和图列字段
/// </summary>
private Dictionary<string, int> names = new Dictionary<string, int>(2);
/// <summary>
/// 游戏图列字段
/// </summary>
private Dictionary<int, MapExample> gamemapexample = new Dictionary<int, MapExample>(100);
/// <summary>
/// 游戏暂停字段
/// </summary>
private Dictionary<string, bool> namestops = new Dictionary<string, bool>(2);
/// <summary>
/// 游戏姓名属性
/// </summary>
public string[] Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// 游戏姓名和图列属性
/// </summary>
public Dictionary<string, int> Names
{
get { return names; }
set { names = value; }
}
/// <summary>
/// 游戏图列属性
/// </summary>
public Dictionary<int, MapExample> GameMapExample
{
get { return gamemapexample; }
set { gamemapexample = value; }
}
/// <summary>
/// 游戏暂停属性
/// </summary>
public Dictionary<string, bool> NameStops
{
get { return namestops; }
set { namestops = value; }
}
/// <summary>
/// 显示初始界面
/// </summary>
public void InitShowUi()
{
Console.WriteLine("******************************************************");
Console.WriteLine("* *");
Console.WriteLine("* 飞 行 棋 *");
Console.WriteLine("* *");
Console.WriteLine("******************************************************");
}
/// <summary>
/// 输入玩家的姓名
/// </summary>
public void InputName()
{
Console.Clear();
InitShowUi();
Console.WriteLine("请输入玩家A的姓名:");
string UserNameA = Console.ReadLine();
while (string.IsNullOrEmpty(UserNameA))
{
Console.WriteLine("玩家A姓名不能为空请重新输入!");
UserNameA = Console.ReadLine();
}
name[0] = UserNameA;
names.Add(UserNameA, 0);
namestops.Add(UserNameA, false);
Console.WriteLine("请输入玩家B的姓名:");
string UserNameB = Console.ReadLine();
while (string.IsNullOrEmpty(UserNameB) || UserNameA == UserNameB) //不能为空或者姓名相同
{
if (string.IsNullOrEmpty(UserNameB))
{
Console.WriteLine("玩家B姓名不能为空请重新输入!");
}
else
{
Console.WriteLine("玩家A姓名和玩家B姓名一样都为{0},请重新输入玩家B的姓名!", UserNameB);
}
UserNameB = Console.ReadLine();
}
name[1] = UserNameB;
names.Add(UserNameB, 0);
namestops.Add(UserNameB, false);
Console.WriteLine("玩家姓名输入完毕!");
}
/// <summary>
/// 开始游戏界面
/// </summary>
public void BeginGameUi()
{
Console.Clear();
InitShowUi();
Console.WriteLine("游戏开始");
Console.WriteLine("{0}用①来表示,{1}用②来表示", name[0], name[1]);
Console.WriteLine("如果AB重叠的时候用<>来表示");
Console.WriteLine("图列:◎:转盘 ¤:地雷 ∧:暂停 ∝:隧道 □:普通");
DefaultGame();
}
/// <summary>
/// 默认排序
/// </summary>
public void DefaultGame()
{
for (int i = 0; i < 100; i++)
{
gamemapexample.Add(i, MapExample.Common);
}
gamemapexample[5] = MapExample.Dial;
gamemapexample[15] = MapExample.Dial;
gamemapexample[26] = MapExample.Dial;
gamemapexample[42] = MapExample.Dial;
gamemapexample[63] = MapExample.Dial;
gamemapexample[87] = MapExample.Dial;
gamemapexample[8] = MapExample.Mine;
gamemapexample[33] = MapExample.Mine;
gamemapexample[50] = MapExample.Mine;
gamemapexample[71] = MapExample.Mine;
gamemapexample[93] = MapExample.Mine;
gamemapexample[2] = MapExample.Stop;
gamemapexample[22] = MapExample.Stop;
gamemapexample[45] = MapExample.Stop;
gamemapexample[77] = MapExample.Stop;
gamemapexample[62] = MapExample.Stop;
gamemapexample[98] = MapExample.Stop;
gamemapexample[6] = MapExample.Tube;
gamemapexample[19] = MapExample.Tube;
gamemapexample[38] = MapExample.Tube;
gamemapexample[57] = MapExample.Tube;
gamemapexample[83] = MapExample.Tube;
DrawUi();
}
/// <summary>
/// 画界面
/// </summary>
public void DrawUi()
{
for (int i = 0; i < 13; i++)
{
switch (i)
{
case 0: //第一行(0-29)
{
for (int j = 0; j < 30; j++)
{
Console.Write(GetMapExample(gamemapexample[j],j));
}
Console.WriteLine();
}
break;
case 1://第二行(30)
case 2://第三行(31)
case 3://第四行(32)
case 4://第五行(33)
case 5://第六行(34)
{
for (int j = 0; j < 30; j++)
{
if (j != 29)
{
Console.Write(" ");
}
else
{
Console.Write(GetMapExample(gamemapexample[29 + i], 29 + i));
}
}
Console.WriteLine();
}
break;
case 6://第七行(35-64)
{
for (int j = 0; j < 30; j++)
{
Console.Write(GetMapExample(gamemapexample[64 - j], 64 - j));
}
Console.WriteLine();
}
break;
case 7://第八行(65)
case 8://第九行(66)
case 9://第十行(67)
case 10://第十一行(68)
case 11://第十二行(69)
{
for (int j = 0; j < 30; j++)
{
if (j == 0)
{
Console.Write(GetMapExample(gamemapexample[65 + i - 7], 65 + i - 7));
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
break;
case 12://第十二行(70-99)
{
for (int j = 0; j < 30; j++)
{
Console.Write(GetMapExample(gamemapexample[70 + j], 70 + j));
}
Console.WriteLine();
}
break;
}
}
Console.ResetColor();
}
/// <summary>
/// 游戏中
/// </summary>
public void GamePlay()
{
Random r = new Random();
while (names[name[0]] < 99 && names[name[1]] < 99)
{
if (namestops[name[0]] == false) //无暂停
{
Console.WriteLine("{0}请按任意键开始掷骨子", name[0]);
Console.ReadKey(true);
int ra = r.Next(1, 7);
Console.WriteLine("{0}掷出了{1}", name[0], ra);
Console.WriteLine("{0}请按任意键开始行动", name[0]);
Console.ReadKey(true);
names[name[0]] = names[name[0]] + ra;
CheckMapIndex();
switch (gamemapexample[names[name[0]]])
{
case MapExample.Common: //普通
{
if (names[name[0]] == names[name[1]])
{
names[name[1]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到{1}了,{1}回到起点", name[0], name[1]);
}
else
{
Console.Clear();
InitShowUi();
DrawUi();
}
}
break;
case MapExample.Dial: //转盘
{
Console.WriteLine("{0}走到转盘,请选择1或者2", name[0]);
Console.WriteLine("1--交换位置 2--轰炸对方");
string temp = Console.ReadLine();
while (temp != "1" && temp != "2")
{
Console.WriteLine("{0}走到转盘,请选择1或者2", name[0]);
Console.WriteLine("1--交换位置 2--轰炸对方");
temp = Console.ReadLine();
}
if (temp == "1")
{
int tempindex = names[name[1]];
names[name[1]] = names[name[0]];
names[name[0]] = tempindex;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("你们已经交换了位置");
}
else if (temp == "2")
{
names[name[1]] = names[name[1]] - 6;
CheckMapIndex();
if (names[name[1]] != names[name[0]])
{
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}已经退了6格", name[1]);
}
else
{
names[name[0]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}倒霉,{1}退了6格,{0}正好被踩了", name[0], name[1]);
}
}
}
break;
case MapExample.Mine:
{
names[name[0]] = names[name[0]] - 6;
CheckMapIndex();
if (names[name[1]] != names[name[0]])
{
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到地雷了,{0}退了6格", name[0]);
}
else
{
names[name[1]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到地雷了,{0}退了6格,正好踩到了{1}", name[0], name[1]);
}
}
break;
case MapExample.Stop:
{
namestops[name[0]] = true;
if (names[name[0]] == names[name[1]])
{
names[name[1]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到{1}了,{1}回到起点", name[0], name[1]);
}
else
{
Console.Clear();
InitShowUi();
DrawUi();
}
Console.WriteLine("{0}暂停一次了", name[0]);
if (namestops[name[0]] == true && namestops[name[1]] == true)
{
namestops[name[0]] = false;
namestops[name[1]] = false;
Console.WriteLine("两人都暂停状态所以都取消");
}
}
break;
case MapExample.Tube:
{
names[name[0]] = names[name[0]] + 6;
CheckMapIndex();
if (names[name[1]] != names[name[0]])
{
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}进入了隧道了,{0}前进6格", name[0]);
}
else
{
names[name[1]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}进入了隧道了,{0}前进6格,并踩到了{1}", name[0], name[1]);
}
}
break;
}
if (names[name[0]] >= 99)
{
Console.Clear();
Console.WriteLine("{0}胜利了!", name[0]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("{0}的位置:{1},{2}的位置:{3}", name[0], names[name[0]] + 1, name[1], names[name[1]] + 1);
}
}
else
{
namestops[name[0]] = false;
}
if (namestops[name[1]] == false) //无暂停
{
Console.WriteLine("{0}请按任意键开始掷骨子", name[1]);
Console.ReadKey(true);
int ra = r.Next(1, 7);
Console.WriteLine("{0}掷出了{1}", name[1], ra);
Console.WriteLine("{0}请按任意键开始行动", name[1]);
Console.ReadKey(true);
names[name[1]] = names[name[1]] + ra;
CheckMapIndex();
switch (gamemapexample[names[name[1]]])
{
case MapExample.Common: //普通
{
if (names[name[0]] == names[name[1]])
{
names[name[0]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到{1}了,{1}回到起点", name[1], name[0]);
}
else
{
Console.Clear();
InitShowUi();
DrawUi();
}
}
break;
case MapExample.Dial: //转盘
{
Console.WriteLine("{0}走到转盘,请选择1或者2", name[1]);
Console.WriteLine("1--交换位置 2--轰炸对方");
string temp = Console.ReadLine();
while (temp != "1" && temp != "2")
{
Console.WriteLine("{0}走到转盘,请选择1或者2", name[1]);
Console.WriteLine("1--交换位置 2--轰炸对方");
temp = Console.ReadLine();
}
if (temp == "1")
{
int tempindex = names[name[1]];
names[name[1]] = names[name[0]];
names[name[0]] = tempindex;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("你们已经交换了位置");
}
else if (temp == "2")
{
names[name[0]] = names[name[0]] - 6;
CheckMapIndex();
if (names[name[1]] != names[name[0]])
{
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}已经退了6格", name[0]);
}
else
{
names[name[0]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}倒霉,{1}退了6格,{0}正好被踩了", name[1], name[0]);
}
}
}
break;
case MapExample.Mine:
{
names[name[1]] = names[name[1]] - 6;
CheckMapIndex();
if (names[name[1]] != names[name[0]])
{
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到地雷了,{0}退了6格", name[1]);
}
else
{
names[name[0]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到地雷了,{0}退了6格,正好踩到了{1}", name[1], name[0]);
}
}
break;
case MapExample.Stop:
{
namestops[name[1]] = true;
if (names[name[0]] == names[name[1]])
{
names[name[0]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}踩到{1}了,{1}回到起点", name[1], name[0]);
}
else
{
Console.Clear();
InitShowUi();
DrawUi();
}
Console.WriteLine("{0}暂停一次了", name[1]);
if (namestops[name[0]] == true && namestops[name[1]] == true)
{
namestops[name[0]] = false;
namestops[name[1]] = false;
Console.WriteLine("两人都暂停状态所以都取消");
}
}
break;
case MapExample.Tube:
{
names[name[1]] = names[name[1]] + 6;
CheckMapIndex();
if (names[name[1]] != names[name[0]])
{
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}进入了隧道了,{0}前进6格", name[1]);
}
else
{
names[name[0]] = 0;
Console.Clear();
InitShowUi();
DrawUi();
Console.WriteLine("{0}进入了隧道了,{0}前进6格,并踩到了{1}", name[1], name[0]);
}
}
break;
}
if (names[name[1]] >= 99)
{
Console.Clear();
Console.WriteLine("{0}胜利了!", name[1]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("{0}的位置:{1},{2}的位置:{3}", name[0], names[name[0]] + 1, name[1], names[name[1]] + 1);
}
}
else
{
namestops[name[1]] = false;
}
}
}
/// <summary>
/// 超出地图范围的判断
/// </summary>
public void CheckMapIndex()
{
for(int i=0;i<name.Length;i++)
{
if(names[name[i]]>99)
{
names[name[i]] = 99;
}
if (names[name[i]] < 0)
{
names[name[i]] = 0;
}
}
}
/// <summary>
/// 根据图列枚举得到图列
/// </summary>
/// <param name="mapexample">图列枚举</param>
/// <returns></returns>
public string GetMapExample(MapExample mapexample,int index)
{
string value = "□";
if (index == names[name[0]] && index == names[name[1]])
{
Console.ForegroundColor = ConsoleColor.Yellow;
value = "<>";
}
else if (index == names[name[0]])
{
Console.ForegroundColor = ConsoleColor.Gray;
value = "①";
}
else if (index == names[name[1]])
{
Console.ForegroundColor = ConsoleColor.Magenta;
value = "②";
}
else
{
switch (mapexample)
{
case MapExample.Dial:
Console.ForegroundColor = ConsoleColor.Blue;
value = "◎";
break;
case MapExample.Mine:
Console.ForegroundColor = ConsoleColor.Red;
value = "¤";
break;
case MapExample.Stop:
Console.ForegroundColor = ConsoleColor.Green;
value = "∧";
break;
case MapExample.Tube:
Console.ForegroundColor = ConsoleColor.DarkMagenta;
value = "∝";
break;
case MapExample.Common:
Console.ForegroundColor = ConsoleColor.White;
value = "□";
break;
}
}
return value;
}
(3)枚举
/// <summary>
/// 图例枚举
/// </summary>
public enum MapExample
{
/// <summary>
/// 转盘
/// </summary>
Dial=0,
/// <summary>
/// 地雷
/// </summary>
Mine=1,
/// <summary>
/// 暂停
/// </summary>
Stop=2,
/// <summary>
/// 隧道
/// </summary>
Tube=3,
/// <summary>
/// 普通
/// </summary>
Common=4
}
(4)示意图: