當前位置:編程學習大全網 - 源碼下載 - 關於java騎士飛行棋的問題

關於java騎士飛行棋的問題

樓上說的很對,switch首先不是循環,只是壹個判斷,i的值影響妳地圖上輸出的到底是什麽類型的圖案,我把我寫的源碼給妳,妳看看,註釋比較全,加油學吧,不懂多問問同學或老師

地圖類:

package s1java.t70.qsfxq;

public class Map {

int[] map = new int[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:普通

*/

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();

}

}

  • 上一篇:eureka原理簡介
  • 下一篇:momo怎麽將c#轉android
  • copyright 2024編程學習大全網