最近在学习c#在看:B站的这个视频 在学到飞行棋时先自己写了一个。下面是运行结果:
我也是新手代码还有很多不完善的地方,但目前功能还没发生什么错误,大家可以来玩一下。
using System;
namespace 飞行棋
{
class Program
{
public struct Player {
public int _playlocation;
public string _playerName;
public int _playValue;
}
static void Main(string[] args)
{
GameShow();
Console.ForegroundColor = ConsoleColor.White;
int[] map = new int[200];// □
int[] luckturn = {1,20,61,84,90,999};// ◎幸运转盘位置数组 因为自己写的方法的原因,这里几个数组得从大到小排列,最后一个起限制作用
int[] bomb = {6,24,27,44,80,98,999};//● 炸弹位置数组
int[] transfer = {5,11,30,36,77,83,999};//∩ 传送门位置数组 传送门比较特殊,相关代码就要调用该数组。而方法之间存在相互关联,因此有些方法也要传入该数组
int[] stop = { 12, 31, 59, 66, 88,999};//▽ 暂停位置数组
MapValue(map, luckturn, bomb, transfer, stop);
MapPaint(map);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("请输入玩家A的姓名");
Player playerA = PlayerIni();
Console.WriteLine("初始化成功!\n玩家A的姓名是:{0}\n\n请输入玩家B的姓名", playerA._playerName);
Player playerB = PlayerIni();
Console.WriteLine("玩家B的昵称是{0}\n按任意键开始游戏:", playerB._playerName);
Console.ReadKey();
Console.Clear();
playerB._playValue = 200;//方便区分玩家A、B
Player winner = GameStart(map,playerA,playerB,transfer);//主要运行的内容都在这个方法里了
Console.WriteLine("游戏结束玩家{0}胜利!", winner._playerName);
GameEnd(winner);
}
public static void playerActionMove(int[] map,ref Player x,ref Player y,int[] transfer)//玩家行动 x代表当前行动玩家 y代表另一位玩家
{
Console.ForegroundColor = ConsoleColor.White;
map[x._playlocation] -= x._playValue;
Console.WriteLine("按任意键,玩家{0}投色子",x._playerName);
Console.ReadKey();
Console.Clear();
Random r = new Random();
int rNumber = r.Next(1, 7);
Console.WriteLine("玩家{0}掷出了{1}向前移动了{2}格\n",x._playerName,rNumber,rNumber);
x._playlocation += rNumber;
map[x._playlocation] += x._playValue;
PlayerTread(map,ref x,ref y);
while (true)
{
if (map[x._playlocation] == x._playValue + 1)//这边本来打算用switch——case的 但不能 case x.playlocation(我也不知道为啥)就没用。
{
MapFunctionLuck(map,ref x, ref y);
continue;
}
if (map[x._playlocation] == x._playValue + 2)
{
MapFunctionBomb(map, ref x);
continue;
}
break;
}
MapFunctionTransfer(map, ref x,transfer);
}
public static Player GameStart(int[] map,Player x,Player y,int[] transfer)
{
map[0] = 300;
MapPaint(map);
while (true)
{
if(!MapFuctionStop(map,x))
{
playerActionMove(map, ref x, ref y, transfer);
if (x._playlocation >= 99)
{
return x;
}
MapPaint(map);
}
if (!MapFuctionStop(map,y))
{
playerActionMove(map, ref y, ref x, transfer);
if (y._playlocation >= 99)
{
return y;
}
MapPaint(map);
}
}
}//游戏开始
public static void MapFunctionLuck(int[] map, ref Player x,ref Player y)
{
Console.WriteLine("恭喜你踩到了幸运转盘,你可以选择\n1.让自己前进随机1-6格\n2.让对方后退随机1-6格");
string s = Console.ReadLine();
Random r = new Random();
int rNumber = r.Next(1, 7);
if (s == "1")
{
map[x._playlocation] -= x._playValue;
Console.WriteLine("干得漂亮,你向前移动了{0}格",rNumber);
x._playlocation += rNumber;
map[x._playlocation] += x._playValue;
}
else if (s == "2")
{
map[y._playlocation] -= (y._playValue+1);
Console.WriteLine("芜湖~对方向后移动了{0}", rNumber);
y._playlocation -= rNumber;
map[y._playlocation] += y._playValue;
}
else if (s == "李杨太帅了")
{
x._playlocation += 100;
Console.WriteLine("说的好!一看就是赢的料!");
}
else
{
Console.WriteLine("输入错误,请重新输入");
MapFunctionLuck(map,ref x,ref y);
}
}//幸运轮盘代码 让玩家选择 1.自己前进1-6格 2.让对方后退1-6格
public static void MapFunctionBomb(int[] map,ref Player x)//炸弹代码 让踩到的玩家后退三格
{
map[x._playlocation] -= x._playValue;
x._playlocation -= 3;
map[x._playlocation] += x._playValue;
Console.WriteLine("玩家{0}被炸弹炸退了三格", x._playerName);
}
public static void MapFunctionTransfer(int[] map, ref Player x, int[] transfer)//传送门代码 让玩家传送到下一个传送门上 传送门记得一个玩家不能踩一个来回移动
{
if (map[x._playlocation] == x._playValue + 3)
{
for (int i = 0; i < transfer.Length - 1; i++)
{
if (transfer[i] == x._playlocation)
{
if (i == 5)
{
map[x._playlocation] -= x._playValue;
x._playlocation = transfer[i - 1];
map[x._playlocation] += x._playValue;
Console.WriteLine("一股神秘力量将你带回了上一个传送门");
break;
}
map[x._playlocation] -= x._playValue;
x._playlocation = transfer[i + 1];
map[x._playlocation] += x._playValue;
Console.WriteLine("你成功传送到了下一个传送门");
break;
}
}
}
}
public static bool MapFuctionStop(int[] map,Player x)//暂停代码 让玩家暂停一回合 目前还没想清楚怎么让它跳过这一回合,只能改为踩过之后暂停这个方块就消失了
{
bool isStop = false;
if (map[x._playlocation] == x._playValue + 4)
{
isStop = true;
map[x._playlocation] -= 4;
}
return isStop;
Console.WriteLine("你被暂停啦");
}
public static void PlayerTread(int[] map, ref Player x,ref Player y)//一名玩家踩到另一名玩家导致其后退6格
{
if(x._playlocation == y._playlocation)
{
map[y._playlocation] -= y._playValue;
y._playlocation -= 6;
if(y._playlocation < 0) //防止在前六格踩到超出地图边界
{
y._playlocation = 0;
}
map[y._playlocation] += y._playValue;
Console.WriteLine("漂亮!{0}踩到了{1},使{2}后退了六格", x._playerName, y._playerName, y._playerName);
}
}
public static Player PlayerIni()
{
Player player = new Player();
player._playlocation = 0;
player._playerName = Console.ReadLine();
player._playValue = 100;
return player;
}//初始化玩家
public static void MapPaint(int[] map)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("□:表示空格 ◎:表示幸运转盘 ●:表示炸弹 ∩:表示传送门 ▽:表示暂停 A、B:表示玩家 ‖:表示两玩家在同一位置");
Console.ForegroundColor = ConsoleColor.Cyan;
string s1 = "";
for(int i = 0;i < 30; i++)//这里我还不知道Console.write()可以不自动换行输出,因此改变了我整个代码的结构,这里我分了五段分别赋给s1让它输出五遍得到完整地图
{
switch (map[i]){
case 0:s1 += "□"; break;
case 1:s1 += "◎"; break;
case 2:s1 += "●"; break;
case 3:s1 += "∩"; break;
case 4:s1 += "▽"; break;
case 100: case 101:case 102:case 103:case 104: s1 += "A";break;
case 200: case 201:case 202:case 203:case 204: s1 += "B";break;
case 300: case 302:case 304:case 306:case 308: s1 += "‖";break;
}
}
Console.WriteLine(s1);
s1 = "";
for(int i = 30; i < 35; i++)
{
switch (map[i])
{
case 0: s1 = "□"; break;
case 1: s1 = "◎"; break;
case 2: s1 = "●"; break;
case 3: s1 = "∩"; break;
case 4: s1 = "▽"; break;
case 100: case 101: case 102: case 103: case 104: s1 = "A"; break;
case 200: case 201: case 202: case 203: case 204: s1 = "B"; break;
case 300: case 302: case 304: case 306: case 308: s1 = "‖"; break;
}
Console.WriteLine(" {0}",s1);
}
s1 = "";
for (int i = 64; i > 34; i--)
{
switch (map[i])
{
case 0: s1 += "□"; break;
case 1: s1 += "◎"; break;
case 2: s1 += "●"; break;
case 3: s1 += "∩"; break;
case 4: s1 += "▽"; break;
case 100: case 101: case 102: case 103: case 104: s1 += "A"; break;
case 200: case 201: case 202: case 203: case 204: s1 += "B"; break;
case 300: case 302: case 304: case 306: case 308: s1 += "‖"; break;
}
}
Console.WriteLine(s1);
s1 = "";
for (int i = 65; i < 70; i++)
{
switch (map[i])
{
case 0: s1 = "□"; break;
case 1: s1 = "◎"; break;
case 2: s1 = "●"; break;
case 3: s1 = "∩"; break;
case 4: s1 = "▽"; break;
case 100: case 101: case 102: case 103: case 104: s1 = "A"; break;
case 200: case 201: case 202: case 203: case 204: s1 = "B"; break;
case 300: case 302: case 304: case 306: case 308: s1 = "‖"; break;
}
Console.WriteLine("{0}", s1);
}
s1 = ""; for (int i = 70; i < 100; i++)
{
switch (map[i])
{
case 0: s1 += "□"; break;
case 1: s1 += "◎"; break;
case 2: s1 += "●"; break;
case 3: s1 += "∩"; break;
case 4: s1 += "▽"; break;
case 100: case 101: case 102: case 103: case 104: s1 += "A"; break;
case 200: case 201: case 202: case 203: case 204: s1 += "B"; break;
case 300: case 302: case 304: case 306: case 308: s1 += "‖"; break;
}
}
Console.WriteLine(s1);
}//地图绘制
public static void MapValue(int[] map,int[] luckturn,int[] bomb,int[] transfer,int[] stop)//给map赋值用
{
int l = 0, b = 0, t = 0, s = 0;
for(int i =0;i < map.Length; i++)
{
if(i == luckturn[l])
{
map[i] = 1;
l++;
}
if(i == bomb[b])
{
map[i] = 2;
b++;
}
if(i == transfer[t])
{
map[i] = 3;
t++;
}
if(i == stop[s])
{
map[i] = 4;
s++;
}
}
}
public static void GameShow()//游戏头
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("*****飞行棋游戏,芜湖!*******");
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("******************************");
}
public static void GameEnd(Player winner)//游戏尾
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("*****恭喜{0}获得胜利!********",winner._playerName);
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("******************************");
}
}
}