📄 game.java
字号:
package com.hlfly.dao;
import java.util.Random;
import java.util.Scanner;
import com.hlfly.entity.Map;
public class Game {
/**
* m 声明地图
* post1 声明玩家1起始位置
* post2 声明玩家2起始位置
* goAndStop 玩家走或停标识设置
* playName 声明对战角色名,放置在数组中
*
*/
Map m;
int post1,post2;
String goAndStop[]=new String[2];
String playName[]=new String[2];
int stopAlarm=0;//表示那一个玩家会停战一局
/**
* 初始化游戏第一局
*
*/
public void initi(){
m=new Map();//创建Map对象,调用无参的构造方法将地图初始化
post1=0;
post2=0;
goAndStop[0]="on";
goAndStop[1]="on";
}
int role1,role2;//玩家1、2的角色代号
/**
* 开始游戏
*/
public void start(){
initi();//调用初始化方法
m.getMap(post1, post2);//显示游戏界面
// 角色设置
System.out.println("\n\n"+"【请输入数字1~4选择玩家 玩家1:戴高乐、玩家2:艾森豪威尔、玩家3:麦克阿瑟、玩家4:巴顿】");
System.out.print("======请选择玩家1:");
role1=isNum(4);//调用函数,选择玩家1;
while(!judgeNumBound(role1,4)){
System.out.print("请输入数字1~4选择玩家:");
role1=isNum(4);
}
setRole(1,role1);
System.out.print("***************************玩家1是:"+playName[0]);
System.out.print("\n"+"======请选择玩家2:");
role2=isNum(4);//调用函数,选择玩家2;
while(!judgeNumBound(role2,4)){
System.out.print("请输入数字1~4选择玩家:");
role2=isNum(4);
}
while(role2==role1){
System.out.print("玩家2和玩家1不能相同,请重新选择:");
role2=isNum(4);
}
setRole(2,role2);
System.out.println("***************************玩家2是:"+playName[1]+"\n");
//System.out.println("【玩家1: "+playName[0]+" 执 A 旗】"+"【玩家2: "+playName[1]+" 执 B 旗】"+"\n");
}
/**
* 判读键盘输入的是否为整数,如是整数则返回这个输入的整数
* @return
*/
public int isNum(int c){
boolean b=true;
int num=0;
try {
while (b) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
for(int i=0;i<s.length();i++){
char chr=s.charAt(i);
if(chr<48||chr>57){
System.out.print("选择有误,请以1~"+c+"整数选择:");
b=true;
break;
}
else
{
b=false;
}
}
if(!b){
num=Integer.parseInt(s);
}
}
}catch(Exception e){
e.printStackTrace();
}
return num;
}
/**
* 判断数字是否是1~4之间的数
* @param num
* @return
*/
public boolean judgeNumBound(int num,int c){
boolean b=false;
if(num<1||num>c)
b=false;
else if(num>=1&&num<=c)
b=true;
return b;
}
/**
* 设置玩家
* @param no 玩家次序 1:玩家1, 2:玩家2, 3:玩家3,4:玩家4
* @param role 角色代号
*/
public void setRole(int no,int role){
switch(role){
case 1:
playName[no-1]="戴高乐";
break;
case 2:
playName[no-1]="艾森豪威尔";
break;
case 3:
playName[no-1]="麦克阿瑟";
break;
case 4:
playName[no-1]="巴顿";
break;
default:
break;
}
}
/**
* 两人对战玩法
*
*/
public void play(){
System.out.println("\n\n\n\n");
System.out.print("\n\n****************************************************\n");
System.out.print(" Game Start \n");
System.out.print("****************************************************\n\n");
//显示对战双方士兵样式
System.out.println("【^_^" + playName[0] + "的士兵: A】");
System.out.println("【^_^" + playName[1] + "的士兵: B】\n");
//显示对战地图
System.out.println("\n图例: " + "■ 暂停 ¤ 幸运轮盘 ★ 地雷 〓 时空隧道 ∷ 普通\n");
m.getMap(0, 0);
//游戏开始
int step;
while(post1<99&&post2<99){//有任何一方走到终点,跳出循环
if(stopAlarm==0){
//轮流掷骰子
if(goAndStop[0].equals("on")){
step=throwShifter(1);//玩家1掷骰子
System.out.println("\n-----------------"); //显示结果信息
System.out.println("骰子数: "+ step);
post1 = getCurrentPost(1, post1, step); //计算这一次移动后的当前位置
System.out.println("\n您当前位置: "+ post1);
System.out.println("对方当前位置:"+ post2);
System.out.println("-----------------\n");
m.getMap(post1, post2); //显示当前地图
if(post1 == 99){ //如果走到终点
break; //退出
}else{
//System.out.println("\n" + playName[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);
post2 = getCurrentPost(2, post2, step); //计算这一次移动后的当前位置
System.out.println("\n您当前位置: "+ post2);
System.out.println("对方当前位置:"+ post1);
System.out.println("-----------------\n");
m.getMap(post1, post2);
if(post2 == 99){ //如果走到终点
break; //退出
}
}else{
//System.out.println("\n" + playName[1] + "停掷一次!\n"); //显示此次暂停信息
goAndStop[1] = "on"; //设置下次可掷状态
}
}else{
int p=stopAlarm;
if(p==1){
goAndStop[0]="off";
goAndStop[1]="on";
stopAlarm=0;
}else if(p==2)
goAndStop[0]="on";
goAndStop[1]="off";
stopAlarm=0;
}
System.out.println("\n\n\n\n");
}
//游戏结束
System.out.println("\n\n\n\n");
System.out.print("****************************************************\n");
System.out.print(" Game Over \n");
System.out.print("****************************************************\n\n");
showResult();
}
/**
* 掷骰子
* @param no 玩家次序
* @return step 掷出的骰子数目
*/
public int throwShifter(int no){
//定义变量存储骰子数目
int step = 0;
//提示玩家启动掷骰子
Scanner sc=new Scanner(System.in);
System.out.println("\n");
System.out.println("玩家"+no+"请按任意键掷骰子:");
String p1=sc.next();
//模拟掷骰子:产生一个1~6的数字作为玩家掷的骰子数目
if(p1!=null){
Random random=new Random();
int r=(Math.abs(random.nextInt()))%6+1;
step=r;
}
return step;
}
/**
* 得到玩家此次移动后的当前位置
* @param no 玩家序号
* @param position 玩家当前位置
* @param step 玩家移动步数
* @return position 玩家移动后的位置
*/
public int getCurrentPost(int no,int position, int step){
position=position+step;
if(position>99)
return 99;
//Scanner input = new Scanner(System.in);//走到幸运轮盘时选择的操作
switch(m.map[position]){
case 1://幸运轮盘
System.out.println("\n◆◇◆◇◆欢迎进入幸运轮盘◆◇◆◇◆");
System.out.println(" 请选择一种运气:");
System.out.println(" 1. 交换位置 2. 轰炸");
System.out.println("=============================\n");
int choice=isNum(2);//走到幸运轮盘时选择的操作
while(!(judgeNumBound(choice,2))){
System.out.println("请输入1或2选择运气");
choice=isNum(2);
}
int temp; //交换时的临时变量
switch(choice){
case 1://交换位置
if(no==1){
/*post1=position;
temp=post1;
post1=post2;
post2=temp;*/
temp = position;
position = post2;
post2 = temp;
}else if(no==2){
/*post2=position;
temp=post2;
post2=post1;
post1=temp;*/
temp = position;
position = post1;
post1 = temp;
}
break;
case 2://轰炸
if(no==1){
post1=position;
//no为1并且玩家2位置小于6
/*Random random=new Random();
int r=(Math.abs(random.nextInt()))%5;
post2=r;*/
if(post2>6)
post2=post2-5;
else
post2=0;
}else if(no==2){
post2=position;
//no为2并且玩家1位置小于6
/*Random random=new Random();
int r=(Math.abs(random.nextInt()))%5;
post1=r;*/
if(post1>6)
post1=post1-6;
else
post1=0;
}
break;
}
break;
case 2://时空隧道
//进入时空隧道,加走10步
if(no==1){
position=position+10;
post1=position;
}
else if(no==2){
position=position+10;
post2=position;
}
System.out.println("|-P " + "进入时空隧道, 真爽!");
break;
case 3://地雷
//踩到地雷退6步
if(no==1){
position=position-7;
post1=position;
}
else if(no==2){
position=position-7;
post2=position;
}
System.out.println("~:-( " + "踩到地雷,气死了...");
break;
case 4://暂停
//设置下次暂停掷骰子
if(no==1){
post1=position;
stopAlarm=1;
goAndStop[0]="off";
goAndStop[1]="on";
}else if(no==2){
post2=position;
stopAlarm=2;
goAndStop[0]="on";
goAndStop[1]="off";
}
System.out.println("~~>_<~~"+"玩家"+stopAlarm+"要停战一局~~~~(>_<)~~~~ !");
break;
default://默认为普通路
if(no==1&&position==post2){
//踩到对方,对方回到起点
post2=0;
post1=position;
System.out.println(":-D 哈哈哈哈...踩到了!");
}
if(no==2&&position==post1){
//踩到对方,对方回到起点
post1=0;
post2=position;
System.out.println(":-D 哈哈哈哈...踩到了!");
}
break;
}
//返回此次掷骰子后玩家的位置坐标
if(position < 0){
return 0;
}else if(position > 99){
return 99;
}else{
return position;
}
//return position;
}
/**
* 显示对战结果
*/
public void showResult(){
//添加代码
if(post1==99){
System.out.println("O(∩_∩)O~玩家1"+playName[0]+"赢了!");
}else if(post2==99){
System.out.println("O(∩_∩)O~玩家2"+playName[1]+"赢了!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -