注意:
在每一次Console.WriteLine()后要接Console.ReadKey(true)已防止输出阻塞在缓冲区而没有被打印出来;
Console.ReadKey()默认为Console.ReadKey(false),会将输入信息显示在控制台;
而Console.ReadKey(true)则不会将输入信息显示在控制台。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FeiXingQi
{
class Program
{
//游戏规则:
//如果玩家A踩到玩家B,玩家B后退6格
//踩到幸运轮盘:◎ 1--交换位置 2--轰炸对方,使对方退6格
//踩到地雷:☆ 退6格
//踩到暂停:▲ 暂停一回合
//踩到时空隧道:卐 前进10格
//踩到方块:□ 什么都不做
public static int[] map = new int[100];
static int[] playerPos = new int[2];
static string[] playerName = new string[2];
static int[] playNumber = new int[2];
static bool[] flag = new bool[2];
static void Main(string[] args)
{
//游戏名称
GameShow();
#region 输入玩家姓名
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("请输入玩家A的姓名");
playerName[0] = Console.ReadLine();
while (playerName[0] == "")
{
Console.WriteLine("玩家姓名不能为空");
playerName[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
playerName[1] = Console.ReadLine();
while (playerName[1] == "" || playerName[1] == playerName[0])
{
if (playerName[1] == "")
{
Console.WriteLine("玩家姓名不能为空");
playerName[1] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家B姓名不能与玩家A姓名相同");
playerName[1] = Console.ReadLine();
}
}
#endregion
Console.Clear();
GameShow();
Console.WriteLine("{0}的士兵用A表示", playerName[0]);
Console.WriteLine("{0}的士兵用B表示", playerName[1]);
//初始化地图
InitialMap();
DrawMap();
while (playerPos[0] < 99 || playerPos[1] < 99)
{
if (flag[0] == false)
{
PlayGame(0);
}
else
{
flag[0] = false;
}
if (playerPos[0] >= 99)
{
Console.WriteLine("玩家{0}胜利", playerName[0]);
break;
}
if (flag[1] == false)
{
PlayGame(1);
}
else
{
flag[1] = false;
}
if (playerPos[1] >= 99)
{
Console.WriteLine("玩家{0}胜利", playerName[1]);
break;
}
}
}
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("*********飞 行 棋*********");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("****************************");
Console.ForegroundColor = ConsoleColor.Black;
}
public static void InitialMap()
{
int[] second = { 3, 8, 13, 18, 23, 46, 56, 67, 78, 89 };
for (int i = 0; i < second.Length; i++)
{
map[second[i]] = 1;
}
int[] third = { 2, 5, 12, 14, 25, 35, 48, 55, 68, 79 };
for (int i = 0; i < third.Length; i++)
{
map[third[i]] = 2;
}
int[] four = { 4, 7, 17, 27, 37, 47, 57, 77, 87, 97 };
for (int i = 0; i < four.Length; i++)
{
map[four[i]] = 3;
}
int[] five = { 9, 19, 29, 39, 49, 59, 69, 99 };
for (int i = 0; i < five.Length; i++)
{
map[five[i]] = 4;
}
}
public static void DrawMap()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐");
#region
//第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(DrawStringMap(i));
}//for
#endregion
Console.WriteLine();
//第一竖行
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
Console.WriteLine(DrawStringMap(i));
}
//第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
//第二竖行
for (int i = 65; i < 70; i++)
{
Console.WriteLine(DrawStringMap(i));
}
//第三横行
for (int i = 70; i < 100; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
}
public static string DrawStringMap(int i)
{
string str = "";
if (playerPos[0] == playerPos[1] && playerPos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Cyan;
str = "<>";
}
else if (playerPos[0] == i)
{
//shift+空格
str = "A";
}
else if (playerPos[1] == i)
{
//shift+空格
str = "B";
}
else
{
switch (map[i])
{
case 0:
{
Console.ForegroundColor = ConsoleColor.Magenta;
str = "□";
break;
}
case 1:
{
Console.ForegroundColor = ConsoleColor.White;
str = "◎";
break;
}
case 2:
{
Console.ForegroundColor = ConsoleColor.Green;
str = "☆";
break;
}
case 3:
{
Console.ForegroundColor = ConsoleColor.DarkGray;
str = "▲";
break;
}
case 4:
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
str = "卐";
break;
}//case
}//switch
}//else
return str;
}
/// <summary>
/// 玩家玩游戏时,获取玩家位置,并重画地图
/// </summary>
/// <param name="playNumber"></param>
public static void PlayGame(int playNumber)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("请按任意键开始游戏");
Console.ReadKey(true);//true设置了屏幕上不显示所输入的任意键
Random r = new Random();
int rNumber = r.Next(1, 7);//左闭右开原则:1/2/3/4/5/6
Console.WriteLine("玩家{0}掷出了{1}", playerName[playNumber], rNumber);
Console.ReadKey(true);
//Console.WriteLine("玩家{0}开始行动", playerName[playNumber]);
playerPos[playNumber] += rNumber;
changePos();
Console.WriteLine("玩家{0}前进了{1}格", playerName[playNumber], rNumber);
Console.ReadKey(true);
if (playerPos[0] == playerPos[1])
{
Console.WriteLine("玩家{0}踩到了玩家{1}, 玩家{1}后退6格", playerName[playNumber], playerName[playNumber]);
Console.ReadKey(true);
playerPos[1 - playNumber] -= 6;
}
else
{
switch (map[playerPos[playNumber]])
{
case 0:
Console.WriteLine("玩家{0}踩到方格,什么事也不会发生", playerName[playNumber]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到幸运轮盘,可以选择:1--交换位置 2--轰炸对方", playerName[playNumber]);
Console.ReadKey(true);
string input = Console.ReadLine();
while (input == "" || (input != "1" && input != "2"))
{
Console.WriteLine("输入无效,请重新输入!");
input = Console.ReadLine();
}
if (input == "1")
{
Console.WriteLine("玩家{0}与玩家{1}交换位置", playerName[playNumber], playerName[1 - playNumber]);
Console.ReadKey(true);
int temp = playerPos[playNumber];
playerPos[playNumber] = playerPos[1 - playNumber];
playerPos[1 - playNumber] = temp;
}
else
{
Console.WriteLine("玩家{0}选择轰炸对方,玩家{1}需后退8格", playerName[playNumber], playerName[1 - playNumber]);
Console.ReadKey(true);
playerPos[1 - playNumber] -= 8;
}
break;
case 2:
Console.WriteLine("玩家{0}踩到地雷,需要后退6格", playerName[playNumber]);
Console.ReadKey(true);
playerPos[playNumber] -= 6;
break;
case 3:
Console.WriteLine("玩家{0}被暂停一次", playerName[playNumber]);
Console.ReadKey(true);
flag[playNumber] = true;
break;
case 4:
Console.WriteLine("玩家{0}进行时空穿梭,前进10格", playerName[playNumber]);
Console.ReadKey(true);
playerPos[playNumber] += 10;
break;
default:
break;
}
Console.Clear();
changePos();
DrawMap();
}
}
/// <summary>
/// 判断玩家是否出界
/// </summary>
public static void changePos()
{
for (int i = 0; i < 2; i++)
{
if (playerPos[i] <= 0)
{
playerPos[i] = 0;
}
if (playerPos[i] >= 99)
{
playerPos[i] = 99;
}
}
}
}
}