300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 黑马程序员_骑士飞行棋

黑马程序员_骑士飞行棋

时间:2023-10-20 09:47:42

相关推荐

黑马程序员_骑士飞行棋

---------------------- Windows Phone 7手机开发、 .Net培训、期待与您交流! ----------------------

骑士飞行棋代码: 来自传智播客 视频!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace 骑士飞行棋01

{

class Program

{

//用下面的整数数组存储我们的游戏地图关卡

//数组的下标为0的元素对应地图上的第1格,下标为n的元素对应第n+1格

//在数组中用 1:表示幸运轮盘◎

// 2:地雷☆

// 3:暂停▲

// 4:时空隧道卐

// 0:表示普通

static int[] Map = new int[100];

static int[] playerPos = { 0, 0 };//playerPos[0]存储玩家A的坐标 playerPos[1]存储玩家B的坐标

static string[] names = new string[2]; //names[0]存储玩家A的姓名 names[1]存储B的姓名

//stringinput = ""; //存储用户的输入,输入1或者2的时候

static bool[] isStop = { false,false };//isStop[0]表示A上次是否走到了暂停 isStop[1]表示B的

//如果走到暂停,设为True下次要设为false

static void Main(string[]args)

{

ShowUI(); //显示游戏名称

Console.WriteLine("请输入玩家A的姓名:");

names[0] = Console.ReadLine();

//判断用户输入是否为空,如果为空,重新输入

while(names[0] == "")

{

Console.WriteLine("玩家A的姓名不能为空,请重新输入:");

names[0] = Console.ReadLine();

}

Console.WriteLine("请输入玩家B的姓名:");

names[1] = Console.ReadLine();

//玩家B的姓名为空或者与A重名要重新输入

while(names[1] == "" || names[1] ==names[0])

{

if(names[1] == "") //玩家B姓名为空

{

Console.WriteLine("玩家B的姓名不能为空,请重新输入:");

}

if(names[1] == names[0])//玩家B的姓名与A重名

{

Console.WriteLine("该用户名已存在,请重新输入玩家B的姓名:");

}

names[1] = Console.ReadLine();

}

Console.Clear();

ShowUI();

Console.WriteLine("激情对战开始......");

Console.WriteLine("{0}用A来表示", names[0]);

Console.WriteLine("{0}用B来表示", names[1]);

Console.WriteLine("如果AB在同一位置,用<>来表示");

InitialMap(); //初始化地图

DrawMap(); //画地图

Console.WriteLine("激情游戏开始...");

//玩家A和B轮流掷骰子,当玩家A或者B的坐标>=99时,循环结束

while(playerPos[0] <99 && playerPos[1] <99)

{

if(isStop[0] == false)

{

Action(0); //A调用掷骰子的方法

}

else

{ //说明isStop==true

isStop[0] = false;

}

if(playerPos[0] >= 99)

{

break;

}

if(isStop[1] == false)

{

Action(1); //B调用掷骰子的方法

}

else

{

isStop[1] = false;

}

}

//判断谁胜利谁失败

Console.Clear();

ShowUI();

if(playerPos[0] >= 99)

{

Console.WriteLine("{0}胜利了!!!", names[0]);

}

else

{

Console.WriteLine("{0}胜利了!!!", names[1]);

}

Console.ReadKey();

}

///<summary>

/// A或B掷骰子的方法

///</summary>

///<paramname="playerNumber">A掷骰子传0过来 B掷骰子传1过来</param>

public static void Action(int playerNumber)

{

//playerNumber中存储的就是当前玩家姓名,坐标,是否暂停这三个数组的下标

//当前0对方1

//当前1对方0

//1-playerNumber就是对方的

Randomr = new Random();// r是产生随机数用的

stringmsg = ""; //用于存储用户踩到某关卡输出的话

intstep = 0; //用于存放产生的随机数

Console.WriteLine("{0}按任意键开始掷骰子......", names[playerNumber]);

ConsoleKeyInforec =Console.ReadKey(true);//不显示按键

step = r.Next(1, 7); //产生一个1-6的随机整数

if(rec.Key == ConsoleKey.Tab &&rec.Modifiers == (ConsoleModifiers.Control |ConsoleModifiers.Shift))

{

ConsoleKeyInfocc =Console.ReadKey();

if(cc.Key == ConsoleKey.F1)

{

step = ReadInt(1, 100);

}

}

//if(rec.Key == ConsoleKey.F11)

//{

// step = 20;

//}

//else

//{

// step = r.Next(1, 7); //产生一个1-6的随机整数

//}

Console.WriteLine("玩家{0}掷出了{1}", names[playerNumber], step);

Console.WriteLine("按任意键开始行动......");

Console.ReadKey(true);

playerPos[playerNumber] += step;//一旦坐标发生改变,就要判断坐标值是否>99或<0

CheckPos(); //检测坐标是否越界

if(playerPos[0] == playerPos[1])//玩家A踩到玩家B

{

playerPos[1-playerNumber] =0; //对方设置为0

msg = string.Format("{0}踩到了{1},{1}退回原点!", names[playerNumber],names[1-playerNumber]); //{1}都是取的第二个变量

}

else

{ //没踩到,要判断玩家A所在的位置是否有其他机关

switch(Map[playerPos[playerNumber]])

{

case0://普通,没有效果

msg = "";

break;

case1://走到了幸运轮盘

Console.Clear();

DrawMap();

Console.WriteLine("{0}很幸运到了幸运轮盘,请选择运气:", names[playerNumber]);

Console.WriteLine("1.交换位置 2.向对方丢一个高爆手雷");

int userSelect = ReadInt(1, 2);

if (userSelect == 1)

{ //要与对方交换位置

int temp = playerPos[0];

playerPos[0] =playerPos[1];

playerPos[1] =temp;

msg = string.Format("{0}选择了与对方交换位置!", names[playerNumber]);

}

else

{ //轰炸对方

playerPos[1] -= 6;

CheckPos();

msg = string.Format("{0}无情的轰炸了{1},{1}退6步!", names[playerNumber],names[1-playerNumber]);

}

break;

case2://踩到了地雷

playerPos[playerNumber] -= 6;

CheckPos();

msg = string.Format("{0}踩到了地雷,退6步!", names[playerNumber]);

break;

case3://暂停一次

isStop[playerNumber] = true;

msg = string.Format("{0}走到红灯,下次暂停一次!", names[playerNumber]);

break;

case4://时空隧道

playerPos[playerNumber]+= 10;

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] + 1);

Console.WriteLine("{0}的位置为:{1}", names[1], playerPos[1] + 1);

}

///<summary>

///输出游戏界面

///</summary>

public static voidShowUI()

{

Console.WriteLine("********************************************");

Console.WriteLine("* *");

Console.WriteLine("* 骑士飞行棋 *");

Console.WriteLine("* *");

Console.WriteLine("********************************************");

}

///<summary>

///进行玩家A和B坐标进行越界的判断

///</summary>

public static voidCheckPos()

{

for(int count = 0; count <= 1; count++)

{

if(playerPos[count] > 99)

{

playerPos[count] = 99;

}

if(playerPos[count] < 0)

{

playerPos[count] = 0;

}

}

}

///<summary>

///初始化地图

///</summary>

public static voidInitialMap()

{

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 count = 0; count < luckyTurn.Length;count++)

{

intpos = luckyTurn[count];//取下标

Map[pos] = 1; //设置相应的元素

}

for(int count = 0; count < landMine.Length;count++)

{

Map[landMine[count]] = 2;

}

for(int count = 0; count < pause.Length;count++)

{

Map[pause[count]] = 3;

}

for(int count = 0; count < timeTunnel.Length;count++)

{

Map[timeTunnel[count]] = 4;

}

}

///<summary>

///获得第pos坐标上应该画的图

///</summary>

///<paramname="pos">要画的坐标</param>

///<returns></returns>

public static stringGetMapString(int pos)

{

stringresult = "";

//判断A和B是否在当前要画的第count格上

if(playerPos[0] == pos && playerPos[1] == pos)

{

Console.ForegroundColor=ConsoleColor.Yellow;

result = "<>";

}

elseif (playerPos[0] == pos)//A在当前格上

{

Console.ForegroundColor=ConsoleColor.Yellow;

result = "A";//全角A

}

elseif (playerPos[1] == pos)//B在当前格上

{

Console.ForegroundColor=ConsoleColor.Yellow;

result = "B";

}

else

{

switch(Map[pos])//正常

{

case0:

Console.ForegroundColor =ConsoleColor.White;

result = "□";

break;

case1:

Console.ForegroundColor =ConsoleColor.Blue;

result = "◎";

break;

case2:

Console.ForegroundColor =ConsoleColor.DarkRed;

result = "☆";

break;

case3:

Console.ForegroundColor =ConsoleColor.Red;

result = "▲";

break;

case4:

Console.ForegroundColor =ConsoleColor.Green;

result = "卐";

break;

}

}

returnresult;

}

///<summary>

///画地图

///</summary>

public static void DrawMap()

{

Console.WriteLine("图例:幸运轮盘:◎地雷:☆暂停:▲时空隧道:卐");

//画第一行

for(int count = 0; count <= 29; count++)

{

Console.Write(GetMapString(count));

}

//第一行画完换行

Console.WriteLine();

//画第一列

for(int count = 30; count <= 34; count++)

{

for(int var1 = 0; var1 < 29; var1++)

{

Console.Write(" ");//二个空格*29

}

Console.WriteLine(GetMapString(count));//换行

}

//画第二行

for(int count = 64; count >= 35; count--)

{

Console.Write(GetMapString(count));

}

Console.WriteLine();//换行

//画第二列

for(int count = 65; count <= 69; count++)

{

Console.WriteLine(GetMapString(count));//画完每一个都要换行

}

//画第三行

for(int count = 70; count <= 99; count++)

{

Console.Write(GetMapString(count));//不用换行

}

Console.WriteLine();

Console.ResetColor();//如果99用了颜色,后面的也会使用这个颜色所以重置

}

//publicstatic int ReadInt()

//{

// int i = ReadInt(int.MinValue,int.MaxValue);

// return i;

//}

///<summary>

///测试输入的数字

///</summary>

///<paramname="min"></param>

///<paramname="max"></param>

///<returns></returns>

public static int ReadInt(int min,int max)

{

while(true)

{

try

{

intnumber = int.Parse(Console.ReadLine());

if(number < min || number > max)

{

Console.WriteLine("只能输入{0}-{1}之间的数字,请重新输入!", min, max);

continue;

}

returnnumber;

}

catch

{

Console.WriteLine("只能输入数字,请重新输入");

}

}

}

}

}

---------------------- Windows Phone 7手机开发、 .Net培训、期待与您交流! ----------------------

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。