---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
曾经刚学习Java的时候做的第一程序有点成就感的程序就是骑士飞行棋,应该很多学程序的朋友都有做过吧!今天再用C#做了一次,也算是对基本知道的巩固应用吧!
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 骑士飞行棋
{
class Program
{
static int[] map = new int[100];
static int[] playerPos = { 0, 0 };
static string[] names = new string[2];
static bool[] isStop = {false,false};
static int step;
static Random r = new Random();
static void Main(string[] args)
{
Player();
Map();
Console.WriteLine("请按任意键进入游戏!");
Console.ReadKey();
Console.Clear();
DrawMap();
StartPlay();
}
//初始化界面
public static void ShowUI()
{
Console.WriteLine("********************************************************");
Console.WriteLine("* *");
Console.WriteLine("* 骑 士 飞 行 棋 *");
Console.WriteLine("* *");
Console.WriteLine("********************************************************");
}
//判断坐标是否越界
public static void JudgePos(int i)
{
if (playerPos[i] > 99)
{
playerPos[i] = 99;
}
else if (playerPos[i] < 0)
{
playerPos[i] = 0;
}
}
public static int ReadInt(int min, int max)
{
int num = 0;
while (true)
{
try
{
num = Convert.ToInt32(Console.ReadLine());
if (num < min || num > max)
{
Console.WriteLine("你的输入不正确,请重新输入");
continue;
}
return num;
}
catch
{
Console.WriteLine("你只能输入数字,请重新输入");
}
}
}
#region 定义两个玩家
public static void Player()
{
ShowUI();
Console.WriteLine("请输入玩家1的姓名:");
names[0] = Console.ReadLine();
while (names[0] == "")
{
Console.WriteLine("你的输入不能为空,请重新输入");
names[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家2的姓名:");
names[1] = Console.ReadLine();
while (names[1] == ""||names[1]==names[0])
{
if (names[1] == "")
{
Console.WriteLine("你的输入不能为空,请重新输入");
}
else
{
Console.WriteLine("你输入的姓名已存在,请重新输入");
}
names[1] = Console.ReadLine();
}
Console.Clear();
ShowUI();
Console.WriteLine("对战开始……");
Console.WriteLine("{0}的士兵用字母A表示!", names[0]);
Console.WriteLine("{0}的士兵用字母B表示!", names[1]);
}
#endregion
#region 主地图
/// <summary>
/// 主地图
/// </summary>
public static void Map()
{
/**
* 在下面的数组存储我们游戏地图各个关卡
* 数组的下标为0的元素对应地图上的第1格 下标为1的元素对应第2格……下标为n的元素对应n+1格
* 在数组中用 0:表示普通□
* 1:表示幸运轮盘◎
* 2:表示地雷☆
* 3:表示时空隧道※
*/
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[] timeTunne = { 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 < timeTunne.Length; i++)
{
map[timeTunne[i]] = 4;
}
}
#endregion
#region 在相应的坐标处绘制相应的图标
/// <summary>
/// 在相应的坐标处绘制相应的图标
/// </summary>
/// <param name="pos">坐标</param>
/// <returns>返回对应的图标</returns>
public static string GetMap(int pos)
{
string result="";
if (playerPos[0] == pos && playerPos[0] == playerPos[1])
{
Console.ForegroundColor = ConsoleColor.Yellow;
result ="◇";
}
else if (playerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result ="A";
}
else if (playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result ="B";
}
else
{
switch (map[pos])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
result ="□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Cyan;
result ="◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Red;
result ="☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
result ="▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Blue;
result ="※";
break;
}
}
return result;
}
#endregion
#region 绘制地图
/// <summary>
/// 绘制地图
/// </summary>
public static void DrawMap()
{
Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:※");
//string[0]表示玩家1所在的坐标,string[1]表示玩家2所在的坐标
for (int i = 0; i <= 29; i++)
{
string result=GetMap(i);
Console.Write(result);
}
Console.WriteLine();
for (int i = 30; i <=34; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
string result = GetMap(i);
Console.WriteLine(result);
}
for (int i = 64; i >= 35; i--)
{
string result = GetMap(i);
Console.Write(result);
}
Console.WriteLine();
for (int i = 65; i <= 69; i++)
{
string result = GetMap(i);
Console.WriteLine(result);
}
for (int i = 70; i <= 99; i++)
{
string result = GetMap(i);
Console.Write(result);
}
Console.WriteLine();
Console.ResetColor();
}
#endregion
public static void Action(int playerNunber)
{
if (isStop[playerNunber] == false)
{
Console.WriteLine("{0}按任意键开始掷骰子", names[playerNunber]);
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.F1)
{
step = 20;
}
else
{
step = r.Next(1, 7);
}
Console.WriteLine("{0}掷出了一个:{1}", names[playerNunber], step);
Console.WriteLine("按任意键开始行动!");
Console.ReadKey(true);
Console.Clear();
playerPos[playerNunber] = playerPos[playerNunber] + step;
JudgePos(playerNunber);
Console.Clear();
DrawMap();
Console.WriteLine("{0}的坐标是:{1}", names[0], playerPos[0]);
Console.WriteLine("{0}的坐标是:{1}", names[1], playerPos[1]);
if (playerPos[playerNunber] == playerPos[1 - playerNunber])//玩家1踩到了玩家2,玩家2则被踩回老家
{
playerPos[1 - playerNunber] = 0;
}
else
{
switch (map[playerPos[playerNunber]])
{
case 0:
break;
case 1:
Console.WriteLine("{0}走到了幸运轮盘,请选择运气", names[playerNunber]);
Console.WriteLine("1、和对方交换位置 2、轰炸对方");
int num = ReadInt(1, 2);
if (num == 1)
{
int temp = 0;
temp = playerPos[playerNunber];
playerPos[playerNunber] = playerPos[1 - playerNunber];
playerPos[1 - playerNunber] = temp;
Console.WriteLine("{0}和{1}交换了位置", names[playerNunber], names[1 - playerNunber]);
}
else
{
playerPos[1 - playerNunber] = playerPos[1 - playerNunber] - 6;
JudgePos(1 - playerNunber);
Console.WriteLine("{0}轰炸了{1},{2}退了6格", names[playerNunber], names[1 - playerNunber], names[1 - playerNunber]);
}
break;
case 2:
Console.WriteLine("{0}踩到了地雷,后退6格", names[playerNunber]);
playerPos[playerNunber] = playerPos[playerNunber] - 6;
JudgePos(playerNunber);
break;
case 3:
isStop[playerNunber] = true;
Console.WriteLine("{0}踩到了暂停,要暂停一次", names[playerNunber]);
break;
case 4:
Console.WriteLine("{0}踩到了时空隧道,向前飞一会儿,前进了10格,爽死了", names[playerNunber]);
playerPos[0] = playerPos[playerNunber] + 10;
JudgePos(playerNunber);
break;
}
}
Console.WriteLine("按任意键开始行动!");
Console.ReadKey(true);
Console.Clear();
DrawMap();
Console.WriteLine("{0}的坐标是:{1}", names[0], playerPos[0]);
Console.WriteLine("{0}的坐标是:{1}", names[1], playerPos[1]);
}
else
{
isStop[playerNunber] = false;
}
}
#region 玩家开始游戏
public static void StartPlay()
{
while (playerPos[0] < 99 && playerPos[1] < 99)
{
//玩家1的操作
Action(0);
if (playerPos[0] >= 99)
{
break;
}
// 玩家2的操作
Action(1);
}
if (playerPos[0] >= 99)
{
Console.Clear();
ShowUI();
Console.WriteLine();
Console.WriteLine("*****************恭喜{0}获得了本局的胜利*****************", names[0]);
}
else
{
Console.Clear();
ShowUI();
Console.WriteLine();
Console.WriteLine("*****************恭喜{0}获得了本局的胜利*****************", names[1]);
}
Console.ReadKey();
}
#endregion
}
}
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://net.itheima.com/