/
问题描述:开发一款Java小游戏——骑士飞行棋
游戏规则和传统的飞行棋一样,支持两人对战
采用100格小型游戏棋盘
游戏规则:对战双方轮流掷骰子控制自己的骑兵前进或后退,在游戏棋盘上设置有关卡
普通
地雷
暂停
时空隧道
幸运轮盘(提供两种运气:交换位置和轰炸)
棋盘上的关卡只在骑兵第一次移动遇到时有效
开发一款Java小游戏——骑士飞行棋
游戏规则和传统的飞行棋一样,支持两人对战
采用100格小型游戏棋盘
游戏规则:对战双方轮流掷骰子控制自己的骑兵前进或后退,在游戏棋盘上设置有关卡
普通
地雷
暂停
时空隧道
幸运轮盘(提供两种运气:交换位置和轰炸)
棋盘上的关卡只在骑兵第一次移动遇到时有效
开发一款Java小游戏——骑士飞行棋
游戏规则和传统的飞行棋一样,支持两人对战
采用100格小型游戏棋盘
游戏规则:对战双方轮流掷骰子控制自己的骑兵前进或后退,在游戏棋盘上设置有关卡
普通
地雷
暂停
时空隧道
幸运轮盘(提供两种运气:交换位置和轰炸)
棋盘上的关卡只在骑兵第一次移动遇到时有效
@@ 两人重合时
A 玩家1
B 玩家2
¤ 幸运轮盘
★ 地雷
■ 暂停
〓 时空隧道
∷ 普通格
地图样式
∷∷∷∷∷★¤∷∷■∷∷∷★∷∷∷★∷∷〓∷∷¤∷〓∷■∷∷∷
∷
∷
★
∷
∷★〓∷∷■∷∷∷∷∷∷∷∷∷★∷∷∷∷〓∷∷∷∷¤∷★∷∷∷
∷
∷
∷
¤∷∷〓∷∷∷∷∷∷∷★∷∷¤∷∷∷∷〓∷〓∷∷■★∷∷∷∷∷
/
Map类的实现:
public class Map
{
int map[]=new int[100];//对战地图
int LandMine[]={6,14,18,34,39,51,65,81,95};//地雷位置
int LuckTurn[]={7,24,41,56,70,84};//幸运轮盘
int Stop[]={10,28,61,95};//暂停
int TimeTunne[]={21,26,46,64,73,89,91};//时空隧道
public void CreateMap()
{
int i=0;
//在对战地图上设置幸运轮盘
for(i = 0; i < LuckTurn.length; i++)
{
map[LuckTurn[i]] = 1;
}
// 实现在对战地图上设置地雷
for(i = 0; i < LandMine.length; i++)
{
map[LandMine[i]] = 2;
}
// 实现在对战地图上设置暂停
for(i = 0; i< Stop.length; i++)
{
map[Stop[i]] = 3;
}
// 实现在对战地图上设置时空隧道
for(i = 0; i < TimeTunne.length; i++)
{
map[TimeTunne[i]] = 4;
}
}
public String GetGraph(int i,int index,int playerA,int playerB)
{
String graph="";
if(playerA == index && playerB == index)
{
graph = "@@";
}
else if(playerA == index)
{
graph = "A";
}
else if(playerB == index)
{
graph = "B";
}
else
{
switch(i)
{
case 0:
graph = "∷";
break;
case 1:
graph = "◆";
break;
case 2:
graph = "★";
break;
case 3:
graph = "■";
break;
case 4:
graph = "〓";
break;
}
}
return graph;
}
public void ShowLine_odd(int start,int end,int playerA,int playerB)
{
for(int i = start; i < end; i++ )
{ //输出奇数(odd)行(第1、3行)。
System.out.print(GetGraph(map[i], i,playerA , playerB));
}
}
public void ShowLine_even(int start, int end, int playerA, int playerB)
{ //输出偶数(even)行 (第2行)
for(int i = end-1; i >= start; i-- )
{
System.out.print(GetGraph(map[i], i,playerA, playerB));
}
}
public void ShowRLine(int start, int end, int playerA, int playerB)
{ // 输出地图的右竖列
for(int i = start; i< end; i++)
{
for(int j =25; j > 0; j--)
{ //输出29个空格
System.out.print(" ");
}
System.out.print(GetGraph(map[i], i,playerA, playerB));
System.out.println();
}
}
public void ShowLLine(int start, int end, int playerA, int playerB)
{//输出地图的左竖列
for(int i = start; i< end; i++)
{
System.out.print(GetGraph(map[i], i,playerA, playerB));
System.out.println();
}
}
public void showMap(int playerA,int playerB)
{//作战地图
ShowLine_odd(0, 30,playerA,playerB);
//换行
System.out.println();
//显示地图右竖行
ShowRLine(30,35,playerA,playerB);
//显示地图第二行
ShowLine_even(35,66,playerA,playerB);
//换行
System.out.println();
//显示地图左竖行
ShowLLine(66,69,playerA,playerB);
//显示地图第3行
ShowLine_odd(69, 99,playerA,playerB);
}
}
//
Game类的实现:
public class Game
{
Map map =new Map();
int playerA;//声明对战中玩家1的当前位置
int playerB;//声明对战中玩家2的当前位置
String[] goAndStop = new String[2]; //声明走或停标识设置
String[] playerName = new String[2];//声明对战角色
//初始化游戏第一局
public void init()
{ //创建Map对象 生成地图
map.CreateMap();
playerA = 0;//设置玩家1起始位置
playerB = 0;//设置玩家2起始位置
goAndStop[0] = "on"; //记录玩家1下一次走或停
goAndStop[1] = "on"; //设置玩家2下一次走或停
}
// 开始游戏
public void start()
{ //调用初始化方法
init();
//显示游戏界面
map.showMap(playerA, playerB);
System.out.println("/n");
System.out.println("1、曹操 2、诸葛亮 3、张飞 4、司马懿 /n");
//角色设置
Scanner input = new Scanner(System.in);
System.out.print("1P选择人物: ");
int role = input.nextInt();
setRole(1,role);
boolean judge = true;
do {
System.out.println();
System.out.print("2P选择人物: ");
int role2 = input.nextInt();
if(role2 == role) {
System.out.println("角色重复,请重新选择人物!");
}
else {
setRole(2,role2);
}
}while(judge == false);
//开始游戏
play();
}
//设置对战角色 , role 角色代号
public void setRole(int no, int role){
switch(role){
case 1:
playerName[no-1] = "曹操";
break;
case 2:
//设置玩家名称为"艾森豪威尔"
playerName[no-1] = "诸葛亮";
break;
case 3:
//设置玩家名称为"司马懿"
playerName[no-1] = "司马懿";
break;
case 4:
//设置玩家名称为"张飞"
playerName[no-1] = "张飞";
break;
default:
break;
}
}
//两人对战玩法
public void play(){
System.out.println("/n/n/n/n");
System.out.print("/n/n▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼/n");
System.out.print(" 开始游戏 /n");
System.out.print("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲/n/n");
//显示对战双方士兵样式
System.out.println(" ~_~" + playerName[0] + "的士兵: A");
System.out.println("*_*" + playerName[1] + "的士兵: B/n");
System.out.println("/n图例: " + "■ 暂停 ◆幸运轮盘 ★ 地雷 〓 时空隧道 ∷ 普通/n"); //显示对战地图
map.showMap(playerA, playerB);
//游戏开始
int step; //存储骰子数目
while(playerA < 99 && playerB< 99)
{ //有任何一方走到终点,跳出循环
//轮流掷骰子
if(goAndStop[0].equals("on")){
//玩家1掷骰子
step = throwShifter(1); //掷骰子
System.out.println("/n-----------------"); //显示结果信息
System.out.println("骰子数: "+ step);
playerA = getCurPos(1, playerA, step); //计算这一次移动后的当前位置
System.out.println("/n您当前位置: "+ playerA);
System.out.println("对方当前位置:"+ playerB);
System.out.println("-----------------/n");
map.showMap(playerA, playerB); //显示当前地图
if(playerA == 99){ //如果走到终点
break; //退出
}
}else{
System.out.println("/n" + playerName[0] +"停掷一次!/n"); //显示此次暂停信息
goAndStop[0] = "on"; //设置下次可掷状态
}
System.out.println("/n/n/n/n");
if(goAndStop[1].equals("on")){
//玩家2掷骰子
step = throwShifter(2); //掷骰子
System.out.println("/n-----------------"); //显示结果信息
System.out.println("骰子数: "+ step);
playerPos2 = getCurPos(2, playerB, step); //计算这一次移动后的当前位置
System.out.println("/n您当前位置: "+ playerb);
System.out.println("对方当前位置:"+ playera);
System.out.println("-----------------/n");
map.showMap(playerA, playerb);
if(playerB == 99){ //如果走到终点
break; //退出
}
}else{
System.out.println("/n" + playerName[1] + "停掷一次!/n"); //显示此次暂停信息
goAndStop[1] = "on"; //设置下次可掷状态
}
System.out.println("/n/n/n/n");
}
//游戏结束
System.out.println("/n/n/n/n");
System.out.print("▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼/n");
System.out.print(" 游戏结束 /n");
System.out.print("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲/n/n");
judge();
}
//掷骰子部分
public int throwShifter(int no)
{
//定义变量存储骰子数目
int step = 0;
//提示玩家启动掷骰子
System.out.println("/n");
System.out.print("请输入任意键开掷骰子!");
Scanner input = new Scanner(System.in);
input.nextInt();
//模拟掷骰子:产生一个1~6的数字作为玩家掷的骰子数目
Random random = new Random();
step = random.nextInt(5) + 1;
return step;
}
// 计算玩家此次移动后的当前位置
//no 玩家次序
//position 移动前位置
//step 掷的骰子数目
//return position 移动后的位置
public int getCurPos(int no, int position, int step){
position = position + step; //第一次移动后的位置
if(position >= 99){
return 99;
}
Scanner input = new Scanner(System.in);
switch(map.map[position])
{ //根据地图中的关卡代号进行判断
case 0: //走到普通格
if(position == playerB)
{ //添加条件:玩家1与对方骑兵相遇
//添加代码实现:踩到对方,对方回到起点
playerB = 0;
System.out.println("哈哈 追上你啦!");
}
if (position == playerA)
{ //添加条件:玩家2与对方骑兵相遇
//添加代码实现:踩到对方,对方回到起点
playerA = 0;
System.out.println(":-D 哈哈哈哈...踩到了!");
}
break;
case 1: //幸运轮盘
System.out.println("/n◆◇◆◇◆恭喜您进入轮盘 祝您好运◆◇◆◇◆");
System.out.println(" 请选择 :");
System.out.println(" 1. 交换位置 2. 轰炸");
System.out.println("………………………………………………………………………………/n");
int choice = input.nextInt();
int temp; //交换时的临时变量
switch(choice){
case 1: //交换位置
if(no == 1){
//添加代码实现交换:position与playerPos2数值互换
temp = playerB;
playerB = position;
position = temp;
}else if(no == 2){
//添加代码实现交换:position与playPos1数值互换
temp = playerA;
playerA = position;
position = temp;
}
break;
case 2: //轰炸
if(no == 1){ //no为1并且玩家2位置小于6
//添加代码实现:计算玩家2当前位置
if(playerB < 6) {
playerB = 0;
}
else {
//添加代码实现:计算玩家2当前位置
playerB -= 6;
}
}
if(no == 2){ //no为2并且玩家1位置小于6
//添加代码实现: 计算玩家1当前位置
if(playerA < 6)
{
playerA = 0;
}
else
{
//添加代码实现:计算玩家1当前位置
playerA-= 6;
}
}
break;
}
break;
case 2: //踩到地雷
//添加代码实现:踩到地雷退6步
position -= 6;
System.out.println("~:-( " + "呜呜 踩地雷了...");
break;
case 3: //下一次暂停一次
//添加代码实现:设置下次暂停掷骰子
goAndStop[no-1] = "off";
System.out.println("~~>_<~~ 要停战一局了。");
break;
case 4: //时空隧道
// 实现:进入时空隧道,加走10步
position += 10;
System.out.println("|-P " + "进入时空隧道, 不错!");
break;
}
//返回此次掷骰子后玩家的位置坐标
if(position < 0){
return 0;
}else if(position > 99){
return 99;
}else{
return position;
}
}
//显示战果
public void judge(){
if(playerPos1 >playerPos2) {
System.out.println(playerName[0] + "获得了胜利!");
}
else {
System.out.println(playerName[1] + "获得了胜利!");
}
}
}