//游戏类
import java.util.Scanner;
public class Game {
Map map;
int player1Pos, player2Pos;
String[] goAndbreak;
String[] playerName;
Scanner input = new Scanner(System.in);
[align=center][/align]
// 游戏初始化
public void initial() {
playerName = new String[3];
goAndbreak = new String[2];
map = new Map();
map.initial();
player1Pos = 0;
player2Pos = 0;
goAndbreak[0] = "on";
goAndbreak[1] = "on";
System.out
.println("\t\t\t----------------骑士飞行棋------------------\t\t\t\n\n");
System.out.println("\t\t\t\t\t游戏开始\n\n");
System.out
.println("\t\t\t----------------骑士飞行棋------------------\t\t\t\n\n");
choicRole();
}
// 角色选择并判断
public void choicRole() {
// 玩家1选择
System.out.println("请选择角色:1.张三 2.李四 3.王五");
System.out.println("请玩家1选择角色");
int choice = input.nextInt();
int choice1;
do {
System.out.println("请玩家2选择角色");
choice1 = input.nextInt();
} while (choice == choice1);
getRole(0, choice);
getRole(1, choice1);
}
// 方法多用
public void getRole(int index, int c) {
switch (c) {
case 1:
playerName[index] = "张三";
break;
case 2:
playerName[index] = "李四";
break;
case 3:
playerName[index] = "王五";
break;
}
}
public void start() {
/**
* 判定骰子数 游戏规则 比赛结果
*/
initial();// 初始化
// map.showMaps(player1Pos, player2Pos);// 显示初始化地图
while (player1Pos < 99 && player2Pos < 99) {
// 对玩家1进行判断
if (goAndbreak[0].equals("on")) {
int step = throwShift(1);// 把下面返回的骰子数赋值给step
player1Pos += step;
System.out.println("骰子数:" + step);
System.out.println(playerName[0] + "当前位置" + player1Pos);
System.out.println(playerName[1] + "当前位置" + player2Pos);
player1Pos = getCurPos(1, player1Pos, step);// 计算这一次移动后的当前位置
map.showMaps(player1Pos, player2Pos);
if (player1Pos > 99) {
player1Pos = 99;
break;
}
} else {
System.out.println("玩家:" + playerName[0] + "暂停投掷一次");
goAndbreak[0] = "on";
}
// 对玩家2进行判断
if (goAndbreak[1] == "on") {
int step = throwShift(2);// 把下面返回的骰子数赋值给step
player1Pos += step;
System.out.println("骰子数:" + step);
System.out.println(playerName[1] + "当前位置" + player2Pos);
System.out.println(playerName[0] + "当前位置" + player1Pos);
player2Pos = getCurPos(1, player2Pos, step);// 计算下一次当前位置
map.showMaps(player1Pos, player2Pos);
if (player2Pos > 99) {
player2Pos = 99;
break;
}
} else {
System.out.println("玩家:" + playerName[1] + "暂停投掷一次");
goAndbreak[0] = "on";
}
}
// 结束游戏
// 判定
judeg();
System.out.println("\t\t\t\tGAME OVER");
// judge();最终判断
}
// 掷骰子并返回一个值给各位玩家,num骰子数,刚开始赋值为0
// no代表玩家*,如玩家1,玩家2,answer只进行了赋值未使用,因为随便按哪个键都要进行随机产生
public int throwShift(int no) {
int num = 0;// 骰子开始置为0
System.out.print("请玩家" + playerName[no - 1] + "掷骰子");
String answer = input.next();
num = (int) (Math.random() * 6) + 1;
return num;
}
// 每次其中一位玩家执行后当前的位置方法getCurPos,并返回给游戏每个玩家每次执行,传递过来不可能《0
public int getCurPos(int no, int playerPs, int step) {
playerPs = playerPs + step;
if (playerPs >= 99) {
playerPs = 99;
}
switch (map.maps[playerPs]) {
// 普通格
case 0:
if (no == 1 && playerPs == player2Pos) {
player2Pos = 0;// 玩家2回到原点
System.out.println("踩到您了");
} else if (no == 2 && playerPs == player1Pos) {
player1Pos = 0;// 玩家1回到原点
System.out.println("踩到人家了");
}
break;
// 地雷
case 1:
playerPs -= 6;
System.out.println("踩到地雷了……哈哈!");
break;
// 幸运
case 2:
System.out.println("真幸运,你进入了幸运,请选择\n\t1.和对方交换位置 2.轰炸\n");
Scanner input = new Scanner(System.in);
int choiceXYNum = input.nextInt();
switch (choiceXYNum) {
case 1:// 位置交换
System.out.println("你选择了和对方交换位置");
if (no == 1) {
int temp = playerPs;// playerPs相当于玩家no对应的位置
playerPs = player2Pos;
player2Pos = temp;
System.out.println(playerName[0] + "当前位置" + player2Pos);
System.out.println(playerName[1] + "当前位置" + player1Pos);
} else if (no == 2) {
int temp = playerPs;// playerPs相当于玩家no对应的位置
playerPs = player1Pos;
player1Pos = playerPs;
System.out.println(playerName[1] + "当前位置" + player1Pos);
System.out.println(playerName[0] + "当前位置" + player2Pos);
}
break;
case 2:// 选择轰炸,两玩家分别进行判断是否到最前面的位置0
if (no == 1 && playerPs < 6) {
playerPs = 0;
} else {
playerPs -= 6;
}
if (no == 2 && playerPs < 6) {
playerPs = 0;
} else {
playerPs -= 6;
}
break;
}
break;
// 暂停
case 3:
goAndbreak[no - 1] = "pause";// 玩家数组定义的,固要-1
System.out.println("嘿嘿!稍微休息一局!");
break;
// 时空
case 4:
playerPs += 10;
System.out.println("运气真好^-^ 希望下次再来一个时空遂道");
break;
}
if (playerPs < 0) {
playerPs = 0;
return playerPs;
} else if (playerPs > 99) {
playerPs = 99;
return playerPs;
} else {
return playerPs;
}
}
// judge最终判断
// 不能等,因为只要其中一位到了99就退出了,不可能相等,注意下面的else if
public void judeg() {
if (player1Pos > player2Pos) {
System.out.println("\n\n\t\t\t恭喜" + playerName[0] + "大侠你赢得了比赛");
} else if (player1Pos < player2Pos) {
System.out.println("\n\n\t\t\t恭喜" + playerName[1] + "大侠你赢得了比赛");
}
}
}
地图类
public class Map {
/**
* 创建并显示地图
*/
int maps[];
// 初始化
int dl[] = { 5, 16, 27, 38, 49, 60, 71, 82, 66, 93 };// 地雷出现位置-6
int xy[] = { 8, 19, 25, 65, 43, 92 };// 幸运出现位置
int zt[] = { 21, 54, 65, 83, 67 };// 暂停出现位置
int sk[] = { 11, 56 };// 时空遂道出现位置+10
public void initial() {
maps = new int[100];
// 踩到地雷
for (int i = 0; i < dl.length; i++) {
int num = dl[i];// 取出数组里面的数
maps[num] = 1;
}
// 幸运宝义为2
for (int i = 0; i < xy.length; i++) {
int num = xy[i];// 取出数组里面的数
maps[num] = 2;
}
// 暂停定义为3
for (int i = 0; i < zt.length; i++) {
int num = zt[i];// 取出数组里面的数
maps[num] = 3;
}
// 时空定义为4
for (int i = 0; i < sk.length; i++) {
int num = sk[i];// 取出数组里面的数
maps[num] = 4;
}
// 按要求显示
}
// 显示地图,player1Ps表示第一个人的位置,player2Ps表示第二个人的位置
public String getGraph(int player1Ps, int player2Ps, int index) {
String result = "∷";
if (player1Ps == index && player2Ps == index) {
result = "@@";
} else if (player1Ps == index) {
result = "A";
} else if (player2Ps == index) {
result = "B";
} else {
switch (maps[index]) {
case 1:
result = "★";
break;
case 2:
result = "◎";
break;
case 3:
result = "■";
break;
case 4:
result = "〓";
break;
case 0:
result = "∷";
break;
}
}
return result;
}
// 显示第一行
public void showLine1(int startps, int endps, int player1Ps, int player2Ps) {
for (int i = startps; i < endps; i++) {
System.out.print(getGraph(player1Ps, player2Ps, i));
}
System.out.println();
}
// 显示第二次
public void showRightLine1(int startps, int endps, int player1Ps,
int player2Ps) {
for (int i = startps; i < endps; i++) {
for (int j = 0; j < 30; j++) {
System.out.print(" ");// 显示空格
}
System.out.println(getGraph(player1Ps, player2Ps, i));// 显示最后一个符号,并且换行
}
}
// 显示第次
public void showsecondLine1(int startps, int endps, int player1Ps,
int player2Ps) {
for (int i = endps - 1; i >= startps; i--) {
System.out.print(getGraph(player1Ps, player2Ps, i));
}
System.out.println();
}
// 显示第四次
public void showLeftLine(int startIndex, int endIndex, int player1Position,
int player2Position) {
for (int i = startIndex; i < endIndex; i++) {
System.out.println(getGraph( player1Position, player2Position,i));
}
}
// 设定A与B两位置,查看全图
public void showMaps(int player1Ps, int player2Ps) {
showLine1(0, 31, player1Ps, player2Ps);
showRightLine1(31, 35, player1Ps, player2Ps);
showsecondLine1(35, 66, player1Ps, player2Ps);
showLeftLine(66, 69, player1Ps, player2Ps);
showLine1(69, 100, player1Ps, player2Ps);
}
}
测试类
public class Test {
/**
* @测试
*/
public static void main(String[] args) {
Map map= new Map();
Game game = new Game();
game.start();
}
}