怎样用Java开发一个飞行棋游戏?
楼主好,这个是类似的哈
创新互联公司是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站设计、网站建设、电商网站制作开发、重庆小程序开发公司、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!
import java.util.Scanner;
{
SnakeLadder3333333(String play, int sum) {
this.play = play;
this.sum = sum;
}
String play;
int sum = 0;
int i = 0;
public void add(SnakeLadder3333333 other) {
int i = (int) (Math.random() * (6 - 1) + 1);
sum = sum + i;
if ((sum 16 || sum 16) (sum 38 || sum 38) (sum 72 || sum 72) (sum 83 || sum 83)
(sum 78 || sum 78) (sum 99 || sum 99) (sum = 100)) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (15 sum sum 17) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + "is on a ladder");
sum = sum + 8;
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (37 sum sum 39) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + "is on a ladder");
sum = sum + 26;
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (71 sum sum 73) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + "is on a ladder");
sum = sum + 19;
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (82 sum sum 84) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + "is on a snake");
sum = sum - 19;
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (77 sum sum 79) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + "is on a snake");
sum = sum - 18;
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (98 sum sum 100) {
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + "is on a snake");
sum = sum - 7;
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
} else if (sum = 100|| other.sum = 100) {
System.out.println(play + " got " + sum);
System.out.println(other.play + " got " + other.sum);
System.out.println(play + "got " + i + " points on the die");
System.out.println(play + " lost " + other.play + " Won");
}
}
public boolean live(SnakeLadder3333333 other) {
if (sum = 100 || other.sum = 100) {
System.out.println(play + " lost " + other.play + " Won");
return false;
}
return true;
}
public static void main(String[] args) {
String q, m;
int e, n;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
SnakeLadder3333333 p1 = new SnakeLadder3333333("Player a ", 0);
SnakeLadder3333333 p2 = new SnakeLadder3333333("Player b ", 0);
while (true) {
p1.add(p2);
System.out.print("Enter c to continue, s to stop: ");
q = scanner.next();
e = q.indexOf('s');
p2.live(p1);
if (e == 0) {
return;
} else {}
p2.add(p1);
System.out.print("Enter c to continue, s to stop: ");
m = scanner.next();
n = m.indexOf('s');
p1.live(p2);
if (n == 0) {
return;
} else {
}
}
}
}
急求骑士飞行棋的代码
画MAP类:
package s1java.xmal1;
public class Map {
int[] map=new int[100]; //0:普通
int[] luckyTurn={6,23,40,55,69,83}; //1:幸运轮盘
int[] landMine={5,13,17,33,38,50,64,80,94};//2:地雷
int[] pause={9,27,60,93}; //3:暂停
int[] timeTunnel={20,25,45,63,72,88,90}; //4:时空隧道
//给map[]写入各种符号
public void createMap(){
for(int i=0;iluckyTurn.length;i++){
map[luckyTurn[i]]=1;
}
for(int i=0;ilandMine.length;i++){
map[landMine[i]]=2;
}
for(int i=0;ipause.length;i++){
map[pause[i]]=3;
}
for(int i=0;itimeTunnel.length;i++){
map[timeTunnel[i]]=4;
}
}
public void showMap(int playerPos1,int playerPos2){
showLine1(0,31,playerPos1,playerPos2);
showRLine(31,35,playerPos1,playerPos2);
showLine2(35,66,playerPos1,playerPos2);
showLLine(66,69,playerPos1,playerPos2);
showLine1(69,100,playerPos1,playerPos2);
}
public void showLine1(int start,int end,int playerPos1,int playerPos2){
for(int i=start;iend;i++ ){
System.out.print(getGraph(map[i],i,playerPos1,playerPos2));
}
System.out.println();
}
public void showRLine(int start,int end,int playerPos1,int playerPos2){
for(int i=start;iend;i++){
for(int j=0;j73;j++){
System.out.print(" ");
}
System.out.println(getGraph(map[i],i,playerPos1,playerPos2));
}
}
public void showLine2(int start,int end,int playerPos1,int playerPos2){
for(int i=end-1;i=start;i--){
System.out.print(getGraph(map[i],i,playerPos1,playerPos2));
}
System.out.println();
}
public void showLLine(int start,int end,int playerPos1,int playerPos2){
for(int i=start;iend;i++){
System.out.println(getGraph(map[i],i,playerPos1,playerPos2));
}
}
public String getGraph(int i,int index,int playerPos1,int playerPos2){
String graph=" ";
if(index==playerPos1index==playerPos2){
graph="@@";
}else if(index==playerPos1){
graph="A";
}else if(index==playerPos2){
graph="B";
}else{
switch(i){
case 1:
graph="¤";
break;
case 2:
graph="★";
break;
case 3:
graph="■";
break;
case 4:
graph="〓";
break;
default:graph="⊙";
break;
}
}
return graph;
}
}
Game类:
package s1java.xmal1;
import java.util.Scanner;
public class Game {
Scanner input = new Scanner(System.in);
Map map;
int playerPos1;
int playerPos2;
String[] goAndStop=new String[2];
String[] playerName=new String[2];
public void init(){
map=new Map();
map.createMap();
map.showMap(0, 0);
goAndStop[0]="on";
goAndStop[1]="on";
}
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;
}
}
public int throwShifter(int no){
int step=0;
System.out.println(playerName[no-1]+"按任意字母键回车:");
String answer=input.next();
step=(int)(Math.random()*10%6)+1;
return step;
}
public int getCurPos(int no,int position,int step){
position=position+step;
if(position99){
return 99;
}
switch(map.map[position]){
case 0://普通格
if(no==1playerPos2==position){//玩家1与玩家2相遇
playerPos2=0;
System.out.println(":-D 哈哈哈...踩到了!");
}else if(no==2playerPos1==position){
playerPos1=0;
System.out.println(":-D 哈哈哈...踩到了!");
}
break;
case 1://幸运轮盘
System.out.println("\n◆◇◆◇◆◇欢迎进入幸运轮盘◆◇◆◇◆◇");
System.out.println("请选择一种运气:");
System.out.println("1.交换位置2.轰炸对手");
int choice=input.nextInt();
int temp;
switch(choice){
case 1: //交换位置
if(no==1){
temp=position;
position=playerPos2;
playerPos2=temp;
}else if(no==2){
temp=position;
position=playerPos1;
playerPos1=temp;
}
break;
case 2: //轰炸对手
if(no==1){
if(playerPos26){
playerPos2=0;
}else{
playerPos2-=6;
}
}else if(no==2){
if(playerPos16){
playerPos1=0;
}else{
playerPos1-=6;
} }
break;
}
break;
case 2://地雷
position-=6;
System.out.println("踩到地雷,气死了。");
break;
case 3://暂停
goAndStop[no-1]="off";
System.out.println("~~_~~ 要停战一局了。");
break;
case 4://时空隧道
position+=10;
System.out.println("|-P 进入时空隧道真爽!");
break;
}
if(position0){
return 0;
}else if(position99){
return 99;
}else{
return position;
}
}
public void judge(){
if(playerPos1playerPos2){
System.out.println("玩家1"+playerName[0]+"最先到达终点,获得胜利。恭喜!恭喜!");
}else{
System.out.println("玩家2"+playerName[1]+"最先到达终点,获得胜利。恭喜!恭喜!");
}
}
public void play(){
System.out.println("\n\n\n\n");
System.out.println("\n※※※※※※※※※※※※※※※※※※※※※※※※※\n");
System.out.println(" Game Start \n");
System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※\n\n");
System.out.println("^_^"+playerName[0]+"的士兵: A");
System.out.println("^_^"+playerName[1]+"的士兵: B\n");
System.out.println("\n图例:"+"■暂停 ¤幸运轮盘 ★地雷 〓时空隧道 ⊙ 普通\n");
map.showMap(playerPos1, playerPos2);
int step;
while(playerPos199playerPos299){
if(goAndStop[0].equals("on")){
step= throwShifter(1);
System.out.println("\n-----------------------");
System.out.println("骰子数"+step);
playerPos1=getCurPos(1,playerPos1,step);
System.out.println("你"+playerName[0]+"当前的位置:"+playerPos1);
System.out.println("对手"+playerName[1]+"当前的位置:"+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")){
step= throwShifter(2);
System.out.println("\n-------------------------------");
System.out.println("骰子数"+step);
playerPos2=getCurPos(2,playerPos2,step);
System.out.println("你"+playerName[1]+"当前的位置:"+playerPos2);
System.out.println("对手"+playerName[0]+"当前的位置:"+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.println("\n************************************************\n");
System.out.println(" Game Over \n");
System.out.println("************************************************\n\n");
judge();
}
public void start(){
init();
System.out.println("\n※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※");
System.out.println("// //");
System.out.println("// //");
System.out.println("// 骑士飞行棋 //");
System.out.println("// //");
System.out.println("// //");
System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~两人对战~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("请选择角色:1.比尔2.奥巴马3.普京4.布朗");
System.out.println("请玩家1选择角色:");
int role1=input.nextInt();
int role2;
do{
System.out.println("请玩家2选择角色:");
role2=input.nextInt();
}while(role2==role1);
setRole(1,role1);
setRole(2,role2);
play();
}
}
主方法类:
package s1java.xmal1;
public class StartGame {
public static void main(String[] args) {
Game game=new Game();
game.start();
}
}
Java编写骑士飞行棋的程序段
楼上说的很对,switch首先不是循环,只是一个判断,i的值影响你地图上输出的到底是什么类型的图案,我把我写的源码给你,你看看,注释比较全,加油学吧,不懂多问问同学或老师
地图类:
package s1java.t70.qsfxq;
public class Map {
int[] map = new int[100]; //对战地图
int[] luckyTurn = ; //幸运轮盘
int[] landMine = ; //地雷位置
int[] pause = ; //暂停
int[] timeTunnel = ; //时空隧道
/**
* 生成地图:
* 关卡代号为:1:幸运轮盘 2:地雷 3: 暂停 4:时空隧道 0:普通
*/
public void createMap(){
int i = 0;
//在对战地图上设置幸运轮盘
for(i = 0; i luckyTurn.length; i++){
map[luckyTurn[i]] = 1;
}
//添加代码实现在对战地图上设置地雷
for(i = 0; i landMine.length; i++) {
map[landMine[i]] = 2;
}
//添加代码实现在对战地图上设置暂停
for(i = 0; i pause.length; i++) {
map[pause[i]] = 3;
}
//添加代码实现在对战地图上设置时空隧道
for(i = 0; i timeTunnel.length; i++) {
map[timeTunnel[i]] = 4;
}
}
/**
* 显示地图关卡对应的图形
* @param i 地图当前位置的关卡代号
* @param index 当前地图位置编号
* @param playerPos1 玩家1的当前位置
* @param playerPos2 玩家2的当前位置
* @return 地图当前位置的对应图片
*/
public String getGraph(int i, int index, int playerPos1, int playerPos2){
String graph = "";
// 添加代码
if(playerPos1 == index playerPos2 == index) {
graph = "@@";
}
else if(playerPos1 == index) {
graph = "A";
}
else if(playerPos2 == index) {
graph = "B";
}
else {
switch(i) {
case 0:
graph = "∷";
break;
case 1:
graph = "¤";
break;
case 2:
graph = "★";
break;
case 3:
graph = "■";
case 4:
graph = "〓";
}
}
return graph;
}
/**
* 输出地图的奇数行(第1、3行)
* @param start 输出的起始点在地图上的位置
* @param end 输出的结束点在地图上的位置
* @param playerPos1 玩家1的当前位置
* @param playerPos2 玩家2的当前位置
*/
public void showLine1(int start, int end, int playerPos1, int playerPos2){
//添加代码
for(int i = start; i = end; i++ ){
System.out.print(getGraph(map[i], i,playerPos1, playerPos2));
}
}
/**
* 输出地图的偶数行(第2行)
* @param start 输出的起始点在地图上的位置
* @param end 输出的结束点在地图上的位置
* @param playerPos1 玩家1的当前位置
* @param playerPos2 玩家2的当前位置
*/
public void showLine2(int start, int end, int playerPos1, int playerPos2){
for(int i = end-1; i = start; i-- ){
System.out.print(getGraph(map[i], i,playerPos1, playerPos2));
}
}
/**
* 输出地图的右竖列
* @param start 输出的起始点在地图上的位置
* @param end 输出的结束点在地图上的位置
* @param playerPos1 玩家1的当前位置
* @param playerPos2 玩家2的当前位置
*/
public void showRLine(int start, int end, int playerPos1, int playerPos2){
for(int i = start; i end; i++){
for(int j = 28; j 0; j--){ //输出29个空格
System.out.print(" ");
}
System.out.print(getGraph(map[i], i,playerPos1, playerPos2));
System.out.println();
}
}
/**
* 输出地图的左竖列
* @param start 输出的起始点在地图上的位置
* @param end 输出的结束点在地图上的位置
* @param playerPos1 玩家1的当前位置
* @param playerPos2 玩家2的当前位置
*/
public void showLLine(int start, int end, int playerPos1, int playerPos2){
//添加代码
for(int i = start; i end; i++){
System.out.print(getGraph(map[i], i,playerPos1, playerPos2));
System.out.println();
}
}
/**
* 显示对战地图
* @param playerPos1 玩家1的当前位置
* @param playerPos2 玩家2的当前位置
*/
public void showMap(int playerPos1, int playerPos2){
//显示地图第一行
showLine1(0, 30,playerPos1,playerPos2);
//换行
System.out.println();
//显示地图右竖行
showRLine(31,35,playerPos1,playerPos2);
//显示地图第二行
showLine2(35,66,playerPos1,playerPos2);
//换行
System.out.println();
//显示地图左竖行
showLLine(66,69,playerPos1,playerPos2);
//显示地图第3行
showLine1(69, 99,playerPos1,playerPos2);
}
}
GAME类:
package s1java.t70.qsfxq;
import java.util.*;
public class Game {
//声明地图
Map map =new Map();
//Map map;
//声明对战中玩家1的当前位置
int playerPos1;
//声明对战中玩家2的当前位置
int playerPos2;
//声明走或停标识设置
String[] goAndStop = new String[2];
//声明对战角色
String[] playerName = new String[2];
/**
* 初始化游戏的一局
*/
public void init(){
//创建Map对象
//生成地图
map.createMap();
//设置玩家1起始位置
playerPos1 = 0;
//设置玩家2起始位置
playerPos2 = 0;
//记录玩家1下一次走或停
goAndStop[0] = "on";
//设置玩家2下一次走或停
goAndStop[1] = "on";
}
/**
* 开始游戏
*/
public void start(){
//调用初始化方法
init();
//显示游戏界面
map.showMap(playerPos1, playerPos2);
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();
}
/**
* 设置对战角色
* @param no 玩家次序 1:玩家1 2:玩家2
* @param 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(" Game Start \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);
//游戏开始
int step; //存储骰子数目
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(" Game Over \n");
System.out.print("****************************************************\n\n");
judge();
}
/**
* 掷骰子
* @param no 玩家次序
* @return step 掷出的骰子数目
*/
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;
}
/**
* 计算玩家此次移动后的当前位置
* @param no 玩家次序
* @param position 移动前位置
* @param 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 == playerPos2){ //添加条件:玩家1与对方骑兵相遇
//添加代码实现:踩到对方,对方回到起点
playerPos2 = 0;
System.out.println(":-D 哈哈哈哈...踩到了!");
}
if (position == playerPos1){ //添加条件:玩家2与对方骑兵相遇
//添加代码实现:踩到对方,对方回到起点
playerPos1 = 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 = playerPos2;
playerPos2 = position;
position = temp;
}else if(no == 2){
//添加代码实现交换:position与playPos1数值互换
temp = playerPos1;
playerPos1 = position;
position = temp;
}
break;
case 2: //轰炸
if(no == 1){ //no为1并且玩家2位置小于6
//添加代码实现:计算玩家2当前位置
if(playerPos2 6) {
playerPos2 = 0;
}
else {
//添加代码实现:计算玩家2当前位置
playerPos2 -= 6;
}
}
if(no == 2){ //no为2并且玩家1位置小于6
//添加代码实现: 计算玩家1当前位置
if(playerPos1 6) {
playerPos1 = 0;
}
else{
//添加代码实现:计算玩家1当前位置
playerPos1 -= 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] + "获得了胜利!");
}
}
}
程序入口:
package s1java.t70.qsfxq;
public class StartGane {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
本文标题:java飞行棋代码主类 java围棋代码
URL标题:http://lswzjz.com/article/dodpgij.html