樓上說的很對(duì),switch首先不是循環(huán),只是一個(gè)判斷,i的值影響你地圖上輸出的到底是什么類型的圖案,我把我寫的源碼給你,你看看,注釋比較全,加油學(xué)吧,不懂多問問同學(xué)或老師
創(chuàng)新互聯(lián)公司專注于凌海企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購物商城網(wǎng)站建設(shè)。凌海網(wǎng)站建設(shè)公司,為凌海等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
地圖類:
package s1java.t70.qsfxq;
public class Map {
int[] map = new int[100]; //對(duì)戰(zhàn)地圖
int[] luckyTurn = ; //幸運(yùn)輪盤
int[] landMine = ; //地雷位置
int[] pause = ; //暫停
int[] timeTunnel = ; //時(shí)空隧道
/**
* 生成地圖:
* 關(guān)卡代號(hào)為:1:幸運(yùn)輪盤 2:地雷 3: 暫停 4:時(shí)空隧道 0:普通
*/
public void createMap(){
int i = 0;
//在對(duì)戰(zhàn)地圖上設(shè)置幸運(yùn)輪盤
for(i = 0; i luckyTurn.length; i++){
map[luckyTurn[i]] = 1;
}
//添加代碼實(shí)現(xiàn)在對(duì)戰(zhàn)地圖上設(shè)置地雷
for(i = 0; i landMine.length; i++) {
map[landMine[i]] = 2;
}
//添加代碼實(shí)現(xiàn)在對(duì)戰(zhàn)地圖上設(shè)置暫停
for(i = 0; i pause.length; i++) {
map[pause[i]] = 3;
}
//添加代碼實(shí)現(xiàn)在對(duì)戰(zhàn)地圖上設(shè)置時(shí)空隧道
for(i = 0; i timeTunnel.length; i++) {
map[timeTunnel[i]] = 4;
}
}
/**
* 顯示地圖關(guān)卡對(duì)應(yīng)的圖形
* @param i 地圖當(dāng)前位置的關(guān)卡代號(hào)
* @param index 當(dāng)前地圖位置編號(hào)
* @param playerPos1 玩家1的當(dāng)前位置
* @param playerPos2 玩家2的當(dāng)前位置
* @return 地圖當(dāng)前位置的對(duì)應(yīng)圖片
*/
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;
}
/**
* 輸出地圖的奇數(shù)行(第1、3行)
* @param start 輸出的起始點(diǎn)在地圖上的位置
* @param end 輸出的結(jié)束點(diǎn)在地圖上的位置
* @param playerPos1 玩家1的當(dāng)前位置
* @param playerPos2 玩家2的當(dāng)前位置
*/
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));
}
}
/**
* 輸出地圖的偶數(shù)行(第2行)
* @param start 輸出的起始點(diǎn)在地圖上的位置
* @param end 輸出的結(jié)束點(diǎn)在地圖上的位置
* @param playerPos1 玩家1的當(dāng)前位置
* @param playerPos2 玩家2的當(dāng)前位置
*/
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 輸出的起始點(diǎn)在地圖上的位置
* @param end 輸出的結(jié)束點(diǎn)在地圖上的位置
* @param playerPos1 玩家1的當(dāng)前位置
* @param playerPos2 玩家2的當(dāng)前位置
*/
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個(gè)空格
System.out.print(" ");
}
System.out.print(getGraph(map[i], i,playerPos1, playerPos2));
System.out.println();
}
}
/**
* 輸出地圖的左豎列
* @param start 輸出的起始點(diǎn)在地圖上的位置
* @param end 輸出的結(jié)束點(diǎn)在地圖上的位置
* @param playerPos1 玩家1的當(dāng)前位置
* @param playerPos2 玩家2的當(dāng)前位置
*/
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();
}
}
/**
* 顯示對(duì)戰(zhàn)地圖
* @param playerPos1 玩家1的當(dāng)前位置
* @param playerPos2 玩家2的當(dāng)前位置
*/
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;
//聲明對(duì)戰(zhàn)中玩家1的當(dāng)前位置
int playerPos1;
//聲明對(duì)戰(zhàn)中玩家2的當(dāng)前位置
int playerPos2;
//聲明走或停標(biāo)識(shí)設(shè)置
String[] goAndStop = new String[2];
//聲明對(duì)戰(zhàn)角色
String[] playerName = new String[2];
/**
* 初始化游戲的一局
*/
public void init(){
//創(chuàng)建Map對(duì)象
//生成地圖
map.createMap();
//設(shè)置玩家1起始位置
playerPos1 = 0;
//設(shè)置玩家2起始位置
playerPos2 = 0;
//記錄玩家1下一次走或停
goAndStop[0] = "on";
//設(shè)置玩家2下一次走或停
goAndStop[1] = "on";
}
/**
* 開始游戲
*/
public void start(){
//調(diào)用初始化方法
init();
//顯示游戲界面
map.showMap(playerPos1, playerPos2);
System.out.println("\n");
System.out.println("1、戴高樂 2、艾森豪威爾 3、麥克阿瑟 4、巴頓\n");
//角色設(shè)置
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("角色重復(fù),請(qǐng)重新選擇人物!");
}
else {
setRole(2,role2);
}
}while(judge == false);
//開始游戲
play();
}
/**
* 設(shè)置對(duì)戰(zhàn)角色
* @param no 玩家次序 1:玩家1 2:玩家2
* @param role 角色代號(hào)
*/
public void setRole(int no, int role){
switch(role){
case 1:
playerName[no-1] = "戴高樂";
break;
case 2:
//設(shè)置玩家名稱為"艾森豪威爾"
playerName[no-1] = "艾森豪威爾";
break;
case 3:
//設(shè)置玩家名稱為"麥克阿瑟"
playerName[no-1] = "麥克阿瑟";
break;
case 4:
//設(shè)置玩家名稱為"巴頓"
playerName[no-1] = "巴頓";
break;
default:
break;
}
}
/**
* 兩人對(duì)戰(zhàn)玩法
*/
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");
//顯示對(duì)戰(zhàn)雙方士兵樣式
System.out.println("^_^" + playerName[0] + "的士兵: A");
System.out.println("^_^" + playerName[1] + "的士兵: B\n");
//顯示對(duì)戰(zhàn)地圖
System.out.println("\n圖例: " + "■ 暫停 ¤ 幸運(yùn)輪盤 ★ 地雷 〓 時(shí)空隧道 ∷ 普通\n");
map.showMap(playerPos1, playerPos2);
//游戲開始
int step; //存儲(chǔ)骰子數(shù)目
while(playerPos1 99 playerPos2 99){ //有任何一方走到終點(diǎn),跳出循環(huán)
//輪流擲骰子
if(goAndStop[0].equals("on")){
//玩家1擲骰子
step = throwShifter(1); //擲骰子
System.out.println("\n-----------------"); //顯示結(jié)果信息
System.out.println("骰子數(shù): "+ step);
playerPos1 = getCurPos(1, playerPos1, step); //計(jì)算這一次移動(dòng)后的當(dāng)前位置
System.out.println("\n您當(dāng)前位置: "+ playerPos1);
System.out.println("對(duì)方當(dāng)前位置:"+ playerPos2);
System.out.println("-----------------\n");
map.showMap(playerPos1, playerPos2); //顯示當(dāng)前地圖
if(playerPos1 == 99){ //如果走到終點(diǎn)
break; //退出
}
}else{
System.out.println("\n" + playerName[0] +"停擲一次!\n"); //顯示此次暫停信息
goAndStop[0] = "on"; //設(shè)置下次可擲狀態(tài)
}
System.out.println("\n\n\n\n");
if(goAndStop[1].equals("on")){
//玩家2擲骰子
step = throwShifter(2); //擲骰子
System.out.println("\n-----------------"); //顯示結(jié)果信息
System.out.println("骰子數(shù): "+ step);
playerPos2 = getCurPos(2, playerPos2, step); //計(jì)算這一次移動(dòng)后的當(dāng)前位置
System.out.println("\n您當(dāng)前位置: "+ playerPos2);
System.out.println("對(duì)方當(dāng)前位置:"+ playerPos1);
System.out.println("-----------------\n");
map.showMap(playerPos1, playerPos2);
if(playerPos2 == 99){ //如果走到終點(diǎn)
break; //退出
}
}else{
System.out.println("\n" + playerName[1] + "停擲一次!\n"); //顯示此次暫停信息
goAndStop[1] = "on"; //設(shè)置下次可擲狀態(tài)
}
System.out.println("\n\n\n\n");
}
//游戲結(jié)束
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 擲出的骰子數(shù)目
*/
public int throwShifter(int no){
//定義變量存儲(chǔ)骰子數(shù)目
int step = 0;
//提示玩家啟動(dòng)擲骰子
System.out.println("\n");
System.out.print("請(qǐng)輸入任意鍵開擲骰子!");
Scanner input = new Scanner(System.in);
input.nextInt();
//模擬擲骰子:產(chǎn)生一個(gè)1~6的數(shù)字作為玩家擲的骰子數(shù)目
Random random = new Random();
step = random.nextInt(5) + 1;
return step;
}
/**
* 計(jì)算玩家此次移動(dòng)后的當(dāng)前位置
* @param no 玩家次序
* @param position 移動(dòng)前位置
* @param step 擲的骰子數(shù)目
* @return position 移動(dòng)后的位置
*/
public int getCurPos(int no, int position, int step){
position = position + step; //第一次移動(dòng)后的位置
if(position = 99){
return 99;
}
Scanner input = new Scanner(System.in);
switch(map.map[position]){ //根據(jù)地圖中的關(guān)卡代號(hào)進(jìn)行判斷
case 0: //走到普通格
if(position == playerPos2){ //添加條件:玩家1與對(duì)方騎兵相遇
//添加代碼實(shí)現(xiàn):踩到對(duì)方,對(duì)方回到起點(diǎn)
playerPos2 = 0;
System.out.println(":-D 哈哈哈哈...踩到了!");
}
if (position == playerPos1){ //添加條件:玩家2與對(duì)方騎兵相遇
//添加代碼實(shí)現(xiàn):踩到對(duì)方,對(duì)方回到起點(diǎn)
playerPos1 = 0;
System.out.println(":-D 哈哈哈哈...踩到了!");
}
break;
case 1: //幸運(yùn)輪盤
System.out.println("\n◆◇◆◇◆歡迎進(jìn)入幸運(yùn)輪盤◆◇◆◇◆");
System.out.println(" 請(qǐng)選擇一種運(yùn)氣:");
System.out.println(" 1. 交換位置 2. 轟炸");
System.out.println("=============================\n");
int choice = input.nextInt();
int temp; //交換時(shí)的臨時(shí)變量
switch(choice){
case 1: //交換位置
if(no == 1){
//添加代碼實(shí)現(xiàn)交換:position與playerPos2數(shù)值互換
temp = playerPos2;
playerPos2 = position;
position = temp;
}else if(no == 2){
//添加代碼實(shí)現(xiàn)交換:position與playPos1數(shù)值互換
temp = playerPos1;
playerPos1 = position;
position = temp;
}
break;
case 2: //轟炸
if(no == 1){ //no為1并且玩家2位置小于6
//添加代碼實(shí)現(xiàn):計(jì)算玩家2當(dāng)前位置
if(playerPos2 6) {
playerPos2 = 0;
}
else {
//添加代碼實(shí)現(xiàn):計(jì)算玩家2當(dāng)前位置
playerPos2 -= 6;
}
}
if(no == 2){ //no為2并且玩家1位置小于6
//添加代碼實(shí)現(xiàn): 計(jì)算玩家1當(dāng)前位置
if(playerPos1 6) {
playerPos1 = 0;
}
else{
//添加代碼實(shí)現(xiàn):計(jì)算玩家1當(dāng)前位置
playerPos1 -= 6;
}
}
break;
}
break;
case 2: //踩到地雷
//添加代碼實(shí)現(xiàn):踩到地雷退6步
position -= 6;
System.out.println("~:-( " + "踩到地雷,氣死了...");
break;
case 3: //下一次暫停一次
//添加代碼實(shí)現(xiàn):設(shè)置下次暫停擲骰子
goAndStop[no-1] = "off";
System.out.println("~~_~~ 要停戰(zhàn)一局了。");
break;
case 4: //時(shí)空隧道
//添加代碼實(shí)現(xiàn):進(jìn)入時(shí)空隧道,加走10步
position += 10;
System.out.println("|-P " + "進(jìn)入時(shí)空隧道, 真爽!");
break;
}
//返回此次擲骰子后玩家的位置坐標(biāo)
if(position 0){
return 0;
}else if(position 99){
return 99;
}else{
return position;
}
}
/**
* 顯示對(duì)戰(zhàn)結(jié)果
*/
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();
}
}
樓主好,這個(gè)是類似的哈
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=; //1:幸運(yùn)輪盤
int[] landMine=;//2:地雷
int[] pause=; //3:暫停
int[] timeTunnel=; //4:時(shí)空隧道
//給map[]寫入各種符號(hào)
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://幸運(yùn)輪盤
System.out.println("\n◆◇◆◇◆◇歡迎進(jìn)入幸運(yùn)輪盤◆◇◆◇◆◇");
System.out.println("請(qǐng)選擇一種運(yùn)氣:");
System.out.println("1.交換位置2.轟炸對(duì)手");
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: //轟炸對(duì)手
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("~~_~~ 要停戰(zhàn)一局了。");
break;
case 4://時(shí)空隧道
position+=10;
System.out.println("|-P 進(jìn)入時(shí)空隧道真爽!");
break;
}
if(position0){
return 0;
}else if(position99){
return 99;
}else{
return position;
}
}
public void judge(){
if(playerPos1playerPos2){
System.out.println("玩家1"+playerName[0]+"最先到達(dá)終點(diǎn),獲得勝利。恭喜!恭喜!");
}else{
System.out.println("玩家2"+playerName[1]+"最先到達(dá)終點(diǎn),獲得勝利。恭喜!恭喜!");
}
}
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圖例:"+"■暫停 ¤幸運(yùn)輪盤 ★地雷 〓時(shí)空隧道 ⊙ 普通\n");
map.showMap(playerPos1, playerPos2);
int step;
while(playerPos199playerPos299){
if(goAndStop[0].equals("on")){
step= throwShifter(1);
System.out.println("\n-----------------------");
System.out.println("骰子數(shù)"+step);
playerPos1=getCurPos(1,playerPos1,step);
System.out.println("你"+playerName[0]+"當(dāng)前的位置:"+playerPos1);
System.out.println("對(duì)手"+playerName[1]+"當(dāng)前的位置:"+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("骰子數(shù)"+step);
playerPos2=getCurPos(2,playerPos2,step);
System.out.println("你"+playerName[1]+"當(dāng)前的位置:"+playerPos2);
System.out.println("對(duì)手"+playerName[0]+"當(dāng)前的位置:"+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("http:// //");
System.out.println("http:// //");
System.out.println("http:// 騎士飛行棋 //");
System.out.println("http:// //");
System.out.println("http:// //");
System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~兩人對(duì)戰(zhàn)~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("請(qǐng)選擇角色:1.比爾2.奧巴馬3.普京4.布朗");
System.out.println("請(qǐng)玩家1選擇角色:");
int role1=input.nextInt();
int role2;
do{
System.out.println("請(qǐng)玩家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();
}
}
第一行:提醒用戶輸入角色值。
第二行:定義了一個(gè)int類型的角色值role1,并以用戶輸入的值為其初始化。
第三行:定義了第二個(gè)角色值role2。
第四到第七行是一個(gè)循環(huán),其中第五行與第一行類似,讓用戶輸入role2的角色值。并在第六行賦給role2.第七行是判斷兩個(gè)角色是否相同,若相同重新循環(huán),否則繼續(xù)向下執(zhí)行。
第八、九行設(shè)置角色。
第十行開始游戲。