java骑士飞行棋.docx

上传人:b****6 文档编号:3600460 上传时间:2022-11-24 格式:DOCX 页数:19 大小:44.17KB
下载 相关 举报
java骑士飞行棋.docx_第1页
第1页 / 共19页
java骑士飞行棋.docx_第2页
第2页 / 共19页
java骑士飞行棋.docx_第3页
第3页 / 共19页
java骑士飞行棋.docx_第4页
第4页 / 共19页
java骑士飞行棋.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

java骑士飞行棋.docx

《java骑士飞行棋.docx》由会员分享,可在线阅读,更多相关《java骑士飞行棋.docx(19页珍藏版)》请在冰豆网上搜索。

java骑士飞行棋.docx

java骑士飞行棋

骑士飞行棋

骑士飞行棋简介:

《骑士飞行棋》又名《别生气》,是一款跟传统的飞行棋一模一样的小游戏。

游戏中每家有4个棋兵,谁4个兵最先到达目的地谁就赢(每一家的目的地各不相同,用颜色区别,大本营是什么颜色目的就是什么颜色)。

当没有活动的兵时,每家可以连续扔3次,直到扔出6点释放出一个兵。

之后每轮只能扔一次,棋兵前进的格数由扔出的点数决定,扔几点就走几步。

无论是前进或者后退,当棋兵最后落脚的地方踩到对方棋兵时,就把对方棋兵轰回老家,当踩到地雷就后退6格,当踩到“¤”时,就进入幸运轮盘,里面有8种运气:

前进、后退、交换位置、转移、暂停一轮、奖励一次、设置地雷、轰炸(将对方棋兵炸退6步)。

一、需求分析:

1、定义一个类MAP,生成游戏中的地图,地图中包括幸运轮盘“¤”的位置,地雷位置,暂停位置,时候隧道位置。

同时构造成员方法creatMap生成对战图,设置相关的成员变量;构造成员方法getGraph返回地图当前位置的对应图片。

定义构造方法showLine1输出地图的奇数行(第1、3行),定义showLine2输出地图的偶数行(第2行);定义showRLine输出地图的右竖列;定义showLLine输出地图的左数列;最后定义showMap方法显示对战图。

2、定义游戏类Game,定义相关的成员变量,包括对战图map,对战中玩家1的当前位置playerPos1,对战中玩家2的当前位置playerPos2,走或停标识设置goAndStop,对战角色playerName。

构造成员方法init()调用生成地图方法creatMap;开始游戏方法start(),同时构造成员方法setRole设置对战角色。

构造对战玩法的方法play。

其中方法play中调用掷骰子成员方法throwShifter,并返回掷出的骰子数目;构造成员方法getCurPos计算玩家此次移动后的当前位置,返回移动后的位置,构造方法judge显示对战结果。

3、定义类StartGame启动游戏。

4、本程序是两人对战玩法。

5、程序执行的命令:

1)启动游戏2)创建游戏类Game3)开始游戏4)定义测试类。

二、概要设计:

1、定义启动游戏类StartGame:

//启动游戏

classStartGame{

publicstaticvoidmain(String[]args){

Gamegame=newGame();//创建游戏类

game.start();//开始游戏

}

}

2、创建游戏类Game:

publicclassGame{

Mapmap;//地图

intplayerPos1;//对战中玩家1的当前位置

intplayerPos2;//对战中玩家2的当前位置

String[]goAndStop=newString[2];//走或停标识设置

String[]playerName=newString[2];//对战角色

publicvoidinit(){

//调用createMap()生成地图

}

publicvoidstart(){

//两人对战开始游戏

}

publicvoidsetRole(intno,introle){

//设置对战角色

}

publicvoidplay(){

//两人对战玩法

}

publicintthrowShifter(intno){

//掷骰子并返回掷出的骰子数目

}

publicintgetCurPos(intno,intposition,intstep){

//计算玩家此次移动后的当前位置并返回移动后的位置

}

publicvoidjudge(){

//显示对战结果

}

}

3、定义Map类:

classMap{

int[]map=newint[100];//对战地图

int[]luckyTurn={6,23,40,55,69,83};//幸运轮盘

int[]landMine={5,13,17,33,38,50,64,80,94};//地雷位置

int[]pause={9,27,60,93};//暂停

int[]timeTunnel={20,25,45,63,72,88,90};//时空隧道

publicvoidcreateMap(){

//生成地图

}

publicStringgetGraph(inti,intindex,intplayerPos1,intplayerPos2){

//显示地图关卡对应的图形并返回地图当前位置的对应图片

}

publicvoidshowLine1(intstart,intend,intplayerPos1,intplayerPos2){

//输出地图的奇数行(第1、3行)

}

publicvoidshowLine2(intstart,intend,intplayerPos1,intplayerPos2){

//输出地图的偶数行(第2行)

}

publicvoidshowRLine(intstart,intend,intplayerPos1,intplayerPos2){

/输出地图的右竖列

}

publicvoidshowLLine(intstart,intend,intplayerPos1,intplayerPos2){

//输出地图的左竖列

}

publicvoidshowMap(intplayerPos1,intplayerPos2){

//显示对战地图

}

}

4、定义测试类Test:

classTest{

publicstaticvoidmain(String[]args){

Mapmap=newMap();

map.createMap();

map.showMap(0,0);

}

}

三、程序代码:

importjava.util.*;

//Map类

publicclassGame{

Mapmap;//地图

intplayerPos1;//对战中玩家1的当前位置

intplayerPos2;//对战中玩家2的当前位置

String[]goAndStop=newString[2];//走或停标识设置

String[]playerName=newString[2];//对战角色

//初始化游戏的一局

publicvoidinit(){

map=newMap();

map.createMap();//生成地图

playerPos1=0;//设置玩家1起始位置

playerPos2=0;//设置玩家2起始位置

goAndStop[0]="on";//记录玩家1下一次走或停

goAndStop[1]="on";//设置玩家2下一次走或停

}

//开始游戏

publicvoidstart(){

//初始化

init();

System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※");

System.out.println("////");

System.out.println("////");

System.out.println("//骑士飞行棋//");

System.out.println("////");

System.out.println("////");

System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n\n\n");

System.out.println("\n~~~~~~~~~~~~~~~~~~~两人对战~~~~~~~~~~~~~~~~~~~");

System.out.println("\n请选择角色:

1.戴高乐2.艾森豪威尔3.麦克阿瑟4.巴顿");

Scannerinput=newScanner(System.in);

System.out.print("请玩家1选择角色:

");

introle1=input.nextInt();

introle2;

do{

System.out.print("请玩家2选择角色:

");

role2=input.nextInt();//双方选择角色代号

}while(role2==role1);//不允许角色重复

setRole(1,role1);//设置玩家1代表的角色

setRole(2,role2);//设置玩家2代表的角色

play();//开始两人对战

}

/*设置对战角色

*no玩家次序1:

玩家12:

玩家2

*role角色代号

*/

publicvoidsetRole(intno,introle){

switch(role){

case1:

playerName[no-1]="戴高乐";

break;

case2:

playerName[no-1]="艾森豪威尔";

break;

case3:

playerName[no-1]="麦克阿瑟";

break;

case4:

playerName[no-1]="巴顿";

break;

default:

break;

}

}

//两人对战玩法

publicvoidplay(){

System.out.println("\n\n\n\n");

System.out.print("\n\n****************************************************\n");

System.out.print("GameStart\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(playerPos1,playerPos2);

//游戏开始

intstep;//存储骰子数目

while(playerPos1<99&&playerPos2<99){//有任何一方走到终点,跳出循环

//轮流掷骰子

if(goAndStop[0].equals("on")){

//玩家1掷骰子

step=throwShifter

(1);//掷骰子

System.out.println("\n-----------------");//显示结果信息

System.out.println("骰子数:

"+step);

playerPos1=getCurPos(1,playerPos1,step);//计算这一次移动后的当前位置

System.out.println("\n您当前位置:

"+playerPos1);

System.out.println("对方当前位置:

"+playerPos2);

System.out.println("-----------------\n");

map.showMap(playerPos1,playerPos2);//显示当前地图

if(playerPos1==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,playerPos2,step);//计算这一次移动后的当前位置

System.out.println("\n您当前位置:

"+playerPos2);

System.out.println("对方当前位置:

"+playerPos1);

System.out.println("-----------------\n");

map.showMap(playerPos1,playerPos2);

if(playerPos2==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("GameOver\n");

System.out.print("****************************************************\n\n");

judge();

}

/*掷骰子

*no玩家次序

*returnstep掷出的骰子数目

*/

publicintthrowShifter(intno){

intstep=0;

System.out.print("\n\n"+playerName[no-1]+",请您按任意字母键后回车启动掷骰子:

");

Scannerinput=newScanner(System.in);

Stringanswer=input.next();

step=(int)(Math.random()*10)%6+1;//产生一个1~6的数字,即掷的骰子数目

returnstep;

}

/*计算玩家此次移动后的当前位置

*no玩家次序

*position移动前位置

*step掷的骰子数目

*returnposition移动后的位置

*/

publicintgetCurPos(intno,intposition,intstep){

position=position+step;//第一次移动后的位置

if(position>=99){

return99;

}

Scannerinput=newScanner(System.in);

switch(map.map[position]){//根据地图中的关卡代号进行判断

case0:

//走到普通格

if(no==1&&playerPos2==position){//玩家1与对方骑兵相遇

playerPos2=0;//踩到对方,对方回到起点

System.out.println(":

-D哈哈哈哈...踩到了!

");

}

if(no==2&&playerPos1==position){//玩家2与对方骑兵相遇

playerPos1=0;//踩到对方,对方回到起点

System.out.println(":

-D哈哈哈哈...踩到了!

");

}

break;

case1:

//幸运轮盘

System.out.println("\n◆◇◆◇◆欢迎进入幸运轮盘◆◇◆◇◆");

System.out.println("请选择一种运气:

");

System.out.println("1.交换位置2.轰炸");

System.out.println("=============================\n");

intchoice=input.nextInt();

inttemp;

switch(choice){

case1:

if(no==1){

temp=position;

position=playerPos2;

playerPos2=temp;

}elseif(no==2){

temp=position;

position=playerPos1;

playerPos1=temp;

}

break;

case2:

if(no==1&&playerPos2<6){

playerPos2=0;

}else{

playerPos2=playerPos2-6;

}

if(no==2&&playerPos2<6){

playerPos1=0;

}else{

playerPos1=playerPos1-6;

}

break;

}

//System.out.println(":

~)"+"幸福的我都要哭了...");

break;

case2:

//踩到地雷

position=position-6;//踩到地雷退6步

System.out.println("~:

-("+"踩到地雷,气死了...");

break;

case3:

//下一次暂停一次

goAndStop[no-1]="off";//设置下次暂停掷骰子

System.out.println("~~>_<~~要停战一局了。

");

break;

case4:

//时空隧道

position=position+10;//进入时空隧道,加走10步

System.out.println("|-P"+"进入时空隧道,真爽!

");

break;

}

//返回此次掷骰子后玩家的位置坐标

if(position<0){

return0;

}elseif(position>99){

return99;

}else{

returnposition;

}

}

//显示对战结果

publicvoidjudge(){

if(playerPos1>playerPos2){

System.out.println("\n恭喜"+playerName[0]+"将军!

您获胜了!

");

}else{

System.out.println("\n恭喜"+playerName[1]+"将军!

您获胜了!

");

}

}

}

//packages1java.xmal1;

classMap{

int[]map=newint[100];//对战地图

int[]luckyTurn={6,23,40,55,69,83};//幸运轮盘

int[]landMine={5,13,17,33,38,50,64,80,94};//地雷位置

int[]pause={9,27,60,93};//暂停

int[]timeTunnel={20,25,45,63,72,88,90};//时空隧道

/*生成地图:

*关卡代号为:

1:

幸运轮盘2:

地雷3:

暂停4:

时空隧道0:

普通

*/

publicvoidcreateMap(){

inti=0;

//在对战地图上设置幸运轮盘

for(i=0;i

map[luckyTurn[i]]=1;

}

//在对战地图上设置地雷

for(i=0;i

map[landMine[i]]=2;

}

//在对战地图上设置暂停

for(i=0;i

map[pause[i]]=3;

}

//在对战地图上设置时空隧道

for(i=0;i

map[timeTunnel[i]]=4;

}

}

/*显示地图关卡对应的图形

*i地图当前位置的关卡代号

*index当前地图位置编号

*playerPos1玩家1的当前位置

*playerPos2玩家2的当前位置

*return地图当前位置的对应图片

*/

publicStringgetGraph(inti,intindex,intplayerPos1,intplayerPos2){

Stringgraph="";

if(index==playerPos1&&index==playerPos2){

graph="@@";

}elseif(index==playerPos1){

//graph="♀";

graph="A";

}elseif(index==playerPos2){

//graph="♂";

graph="B";

}else{

switch(i){

case1:

//幸运轮盘

graph="¤";

break;

case2:

//地雷

graph="★";

break;

case3:

//暂停

grap

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1