我的Java階段項目1 – 騎士飛行棋[兩人對戰][隨機地圖版]

來源:互聯網
上載者:User
package s1java.xmal1.stochasticmap;/** * S1T037階段項目1 - 騎士飛行棋[隨機地圖版] * 啟動遊戲類(StartGame) * @author 鄧超 * @version 0.1 2008/5/4 * 開發/測試環境:JDK 1.6 + Eclipse 3.3.2 */public class StartGame {/** * 啟動遊戲 * @param args */public static void main(String[] args) {Game game = new Game();   //建立遊戲類game.start();             //開始遊戲}}
 
 
package s1java.xmal1.stochasticmap;/** * S1T037階段項目1 - 騎士飛行棋[隨機地圖版] * 地圖類(Map) * @author 鄧超 * @version 0.1 2008/5/4 * 開發/測試環境:JDK 1.6 + Eclipse 3.3.2 */public class Map {int[] map = new int[100];                               //對戰地圖int[] luckyTurn = new int[6];                           //幸運輪盤 int[] landMine = new int[9];                            //地雷位置int[] pause = new int[4];                               //暫停int[] timeTunnel = new int[7];                          //時空隧道int[] especialLattice = new int[luckyTurn.length +      //儲存特殊地圖格座標                                landMine.length +                                pause.length +                                timeTunnel.length];/** * 產生隨機特殊地圖格 */public void randomLattice() {int randomNum;     //儲存隨機整數int i, j;          //迭代變數//幸運輪盤for (i = 0; i < luckyTurn.length; i++) {//產生1~98的隨機數randomNum = (int)Math.round((Math.random() * (98 - 1)) + 1);luckyTurn[i] = randomNum;//將隨機產生的幸運輪盤座標儲存到特殊地圖格座標數組especialLattice[i] = luckyTurn[i];}//地雷位置for (i = 0; i < landMine.length; i++) {//產生1~98的隨機數randomNum = (int)Math.round((Math.random() * (98 - 1)) + 1);landMine[i] = randomNum;for (j = 0; j < especialLattice.length; j++) {if (landMine[i] == especialLattice[j]) {    //若特殊地圖格重複i--;break;} else if (especialLattice[j] == 0) {       //如果是空位especialLattice[j] = landMine[i];break;}}}//暫停for (i = 0; i < pause.length; i++) {//產生1~98的隨機數randomNum = (int)Math.round((Math.random() * (98 - 1)) + 1);pause[i] = randomNum;for (j = 0; j < especialLattice.length; j++) {if (pause[i] == especialLattice[j]) {       //若特殊地圖格重複i--;break;} else if (especialLattice[j] == 0) {       //如果是空位especialLattice[j] = pause[i];break;}}}//時空隧道for (i = 0; i < timeTunnel.length; i++) {//產生1~98的隨機數randomNum = (int)Math.round((Math.random() * (98 - 1)) + 1);timeTunnel[i] = randomNum;for (j = 0; j < especialLattice.length; j++) {if (timeTunnel[i] == especialLattice[j]) {  //若特殊地圖格重複i--;break;} else if (especialLattice[j] == 0) {       //如果是空位especialLattice[j] = timeTunnel[i];break;}}}}/**     * 產生地圖:      * 關卡代號為:1:幸運輪盤  2:地雷  3: 暫停  4:時空隧道  0:普通     */public void createMap(){//產生隨機特殊地圖格randomLattice();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(index == playerPos1 && index== playerPos2){    graph = "@@";    }else if(index == playerPos1){      //graph = "♀";    graph = "A";    }else if(index == playerPos2){    //graph = "♂";    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;    }        /**     * 輸出地圖的奇數行(第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--){  //輸出28個(對)空格    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.println(getGraph(map[i], i, playerPos1, playerPos2));    }    }    /**     * 顯示對戰地圖     * @param playerPos1 玩家1的當前位置     * @param playerPos2 玩家2的當前位置     */    public void showMap(int playerPos1, int playerPos2){    showLine1(0, 31, playerPos1, playerPos2);      //顯示地圖第一行    System.out.println();                          //換行    showRLine(31, 34, playerPos1, playerPos2);     //顯示地圖右豎行    showLine2(35, 66, playerPos1, playerPos2);     //顯示地圖第二行    System.out.println();                          //換行    showLLine(66, 68, playerPos1, playerPos2);     //顯示地圖左豎行    showLine1(69, 100, playerPos1, playerPos2);    //顯示地圖第三行    }  }
 
 
 
 
package s1java.xmal1.stochasticmap;/** * S1T037階段項目1 - 騎士飛行棋[隨機地圖版] * 遊戲類(Game) * @author 鄧超 * @version 0.1 2008/5/4 * 開發/測試環境:JDK 1.6 + Eclipse 3.3.2 */import java.util.Scanner;                 //匯入Scanner類public class Game {    Map map;                              //地圖(map)對象    int playerPos1;                       //記錄對戰中玩家1的當前位置    int playerPos2;                       //記錄對戰中玩家2的當前位置    String[] goAndStop = new String[2];   //記錄玩家下一次走或停的標記    String[] playerName = new String[2];  //記錄玩家選擇的對戰角色名稱        /**     * 初始化遊戲的一局     */    public void init(){       map = new Map();    map.createMap();                  //產生地圖        playerPos1 = 0;                   //設定玩家1起始位置        playerPos2 = 0;                   //設定玩家2起始位置        goAndStop[0] = "on";              //記錄玩家1下一次走或停        goAndStop[1] = "on";              //設定玩家2下一次走或停    }          /**     * 設定對戰角色     * @param no 玩家次序 1:玩家1 2:玩家2     * @param role 角色代號為:1:戴高樂  2:艾森豪  3:麥克阿瑟  4:巴頓  其它選擇:匿名玩家     */    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 void start(){          //初始化          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. 巴頓");          Scanner input = new Scanner(System.in);          String role1;          do {          System.out.print("請玩家1選擇角色:  ");          role1 = input.next();  //玩家1選擇角色代號          /*只能選擇1~4的角色編號*/          } while (!role1.equals("1") && !role1.equals("2") && !role1.equals("3") && !role1.equals("4"));          String role2;          do{          System.out.print("請玩家2選擇角色: ");          role2 = input.next();  //玩家2選擇角色代號          /*不允許角色重複,且只能選擇1~4的角色編號*/          } while (role2.equals(role1) || !role2.equals("1") && !role2.equals("2") && !role2.equals("3") && !role2.equals("4"));          setRole(1, Integer.parseInt(role1));    //設定玩家1代表的角色          setRole(2, Integer.parseInt(role2));    //設定玩家2代表的角色               play();                                 //開始兩人對戰      }            /**       * 兩人對戰玩法       */      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 + 1));                System.out.println("對方當前位置:"+ (playerPos2 + 1));                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 + 1));              System.out.println("對方當前位置:"+ (playerPos1 + 1));              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.print("/n/n" + playerName[no-1] + ", 請您按任意字母鍵後斷行符號啟動擲骰子: ");      //從控制台接收輸入的方式啟動擲骰子(可以讓玩家按任意字母鍵後斷行符號啟動)      Scanner input = new Scanner(System.in);      input.next();      //產生一個隨機數(1~6)作為此玩家擲的骰子數目      step = (int)(Math.random()*10) % 6 + 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 (no == 1 && playerPos2 == position){ //玩家1與對方騎兵相遇      playerPos2 = 0;                       //踩到對方,對方回到起點      System.out.println(":-D  哈哈哈哈...踩到了!");      }       if (no == 2 && playerPos1 == position){ //玩家2與對方騎兵相遇      playerPos1 = 0;                       //踩到對方,對方回到起點      System.out.println(":-D  哈哈哈哈...踩到了!");      }      break;      case 1:                                   //幸運輪盤      char choice;                          //接收玩家選擇      do {      System.out.println("/n◆◇◆◇◆歡迎進入幸運輪盤◆◇◆◇◆");          System.out.println("   請選擇一種運氣:");          System.out.println("   1. 交換位置  2. 轟炸");          System.out.println("=============================/n");          choice  = input.next().charAt(0);      } while (choice != '1' && choice != '2');          int temp;                             //交換時的臨時變數          switch(choice){          case '1':                             //交換位置          if(no == 1){                      //position與playerPos2數值互換          temp = position;          position = playerPos2;          playerPos2 = temp;           }else if(no == 2){                //position與playerPos1數值互換          temp = position;          position = playerPos1;          playerPos1 = temp;          }          break;          case '2':                             //轟炸          //計算玩家2當前位置          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;      case 2:                                   //踩到地雷          position = position - 6;               //踩到地雷退6步          System.out.println("~:-(  " + "踩到地雷,氣死了...");          break;          case 3:                                   //下一次暫停一次          goAndStop[no-1] = "off";              //設定下次暫停擲骰子          System.out.println("~~>_<~~  要停戰一局了。");          break;          case 4:                                   //時空隧道              position = position + 10;             //進入時空隧道,加走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("/n恭喜" + playerName[0] + "將軍! 您獲勝了!");      }else {      System.out.println("/n恭喜" + playerName[1] + "將軍! 您獲勝了!");      }      }}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.