2016-12-28 86 views
1

我被要求創建一個我選擇的星球大戰主題遊戲,它必須使用JOptionPane而不是控制檯來玩。我設置主菜單和基本的東西很好,但其餘的我不知道該怎麼做。將控制檯代碼轉換爲JOptionPane

我的遊戲是戰列艦遊戲,到目前爲止它在控制檯中工作,但它是網格工作,我不知道該怎麼做。遊戲必須具備電影中的BB-8角色。我的代碼是在這裏:

import java.util.*; 
import javax.swing.*; 
public class BattleShips { 

    public static void main(String[] args) { 

     Scanner a = new Scanner (System.in); 
     System.out.print("Welcome To Battleships!\n\nSelect One Of The Following Options:\n\n1. Play\n2. How To Play\n3. Exit\n\n"); 
     String choice = a.nextLine(); 
     if(choice.equals("2")){ 
      System.out.print("\nThe rules of battleships are simple." 
        + "\n\n- Your aim is to destroy all enemy ships, the first to destoy all opposing ships first wins." 
        + "\n- You will have 4 ships all of which are 3 spaces long." 
        + "\n- You will have the choice of placing your ships anywhere you wish, " 
        + "simply by deciding if you want it vertical or horizontal and entering the centre coordinate." 
        + "\n- You will then have to guess where the enemy ships are and enter the coordinates, " 
        + "if you hit the enemy ships you will recieve points." 
        + "\n- If your ships are hit, you will lose a point."); 
     } 
     String[][] table = new String[7][7]; 
     String[] letter = {"A","B","C","D","E","F"}; 
     String[] numbers = {"0","1","2","3","4","5"}; 
     table[0][0] = " "; 
     for(int i = 1 ;i < 7 ;i++){ 
      table[i][0] = (letter[i-1]); 
      for(int j = 1 ;j < 7 ;j++){ 
       table[0][j] = (numbers[j-1]); 
      } 
     } 
     for(int i = 1 ;i < 7 ;i++){ 
      for(int j = 1 ;j < 7 ;j++){ 
       table[i][j] = "~"; 
      } 
     } 
     String[][] hmtable = new String[7][7]; 
     String[] hmletter = {"A","B","C","D","E","F"}; 
     String[] hmnumbers = {"0","1","2","3","4","5"}; 
     hmtable[0][0] = " "; 
     for(int i = 1 ;i < 7 ;i++){ 
      hmtable[i][0] = (hmletter[i-1]); 
      for(int j = 1 ;j < 7 ;j++){ 
       hmtable[0][j] = (hmnumbers[j-1]); 
      } 
     } 
     for(int i = 1 ;i < 7 ;i++){ 
      for(int j = 1 ;j < 7 ;j++){ 
       hmtable[i][j] = "~"; 
      } 
     } 
     String[][] table2 = new String[7][7]; 
     String[] letter2 = {"A","B","C","D","E","F"}; 
     String[] numbers2 = {"0","1","2","3","4","5"}; 
     table2[0][0] = " "; 
     for(int i = 1 ;i < 7 ;i++){ 
      table2[i][0] = (letter2[i-1]); 
      for(int j = 1 ;j < 7 ;j++){ 
       table2[0][j] = (numbers2[j-1]); 
      } 
     } 
     for(int i = 1 ;i < 7 ;i++){ 
      for(int j = 1 ;j < 7 ;j++){ 
       table2[i][j] = (letter2[i-1])+(numbers2[j-1]); 
      } 
     } 
     String[][] AItable = new String[7][7]; 
     String[] AIletter = {"A","B","C","D","E","F"}; 
     String[] AInumbers = {"0","1","2","3","4","5"}; 
     table[0][0] = " "; 
     for(int i = 1 ;i < 7 ;i++){ 
      AItable[i][0] = (AIletter[i-1]); 
      for(int j = 1 ;j < 7 ;j++){ 
       AItable[0][j] = (AInumbers[j-1]); 
      } 
     } 
     for(int i = 1 ;i < 7 ;i++){ 
      for(int j = 1 ;j < 7 ;j++){ 
       AItable[i][j] = "~"; 
      } 
     } 
     boolean done = true; 
     int counter = 0; 
     while(done){ 
       int posnum = 1 + (int)(Math.random() * 6); 
       int posletter = 1 + (int)(Math.random() * 6); 
       int vorh = 1 + (int)(Math.random() * 2); 
       if(vorh==1&&posnum==1){ 
        posnum = posnum+1; 
       } 
       if(vorh==1&&posnum==6){ 
        posnum = posnum-1; 
       } 
       if(vorh==2&&posletter==1){ 
        posletter = posletter+1; 
       } 
       if(vorh==2&&posletter==6){ 
        posletter = posletter-1; 
       } 
       if(vorh==(1)&&AItable[posletter][posnum].equals("~")&&AItable[posletter][posnum+1].equals("~")&&AItable[posletter][posnum-1].equals("~")){ 
        AItable[posletter][posnum] = ("X"); 
        AItable[posletter][posnum+1] = ("X"); 
        AItable[posletter][posnum-1] = ("X"); 
        counter = counter + 1; 
       } 
       if(vorh==(2)&&AItable[posletter][posnum].equals("~")&&AItable[posletter+1][posnum].equals("~")&&AItable[posletter-1][posnum].equals("~")){ 
        AItable[posletter][posnum] = ("X"); 
        AItable[posletter+1][posnum] = ("X"); 
        AItable[posletter-1][posnum] = ("X"); 
        counter = counter + 1; 
        } 
       if(counter == 4){ 
        done=false; 
       } 
       } 
     /*AItable[0][0] = " "; 
     System.out.println("The AI's Ships"); 
     for(int i = 0 ;i < 7 ;i++){ 
      for(int j = 0 ;j < 7 ;j++){ 
       System.out.printf("%-4s",AItable[i][j]); 
      } 
      System.out.println(); 
     }*/ 
     System.out.println("\n\nHere are all the coordinates where you may place your ships:\n"); 
     for(int i = 0 ;i < 7 ;i++){ 
      for(int j = 0 ;j < 7 ;j++){ 
       System.out.printf("%-4s",table[i][j]); 
      } 
      System.out.println(); 
     } 
     boolean choose = true; 
     int counter2 = 0; 
     int i = 1; 
     while(choose){ 
       System.out.print("\nDo you want ship #"+(i)+" to be vertical or horizontal? (V/H) > "); 
       String vorh = a.nextLine().toUpperCase(); 
       System.out.print("Please enter the centre coordinate of ship #"+(i)+" > "); 
       String input = a.nextLine().toUpperCase(); 
       for(int l = 1 ;l < 7 ;l++){ 
        for(int j = 1 ;j < 7 ;j++){ 
         if(input.equals(table2[l][j])){ 
          if(vorh.equals("V")&&input.charAt(0)==('A')){ 
           System.out.print("Sorry, the position you have chosen is out of bounds, please choose another."); 
          } 
          else if(vorh.equals("V")&&input.charAt(0)==('F')){ 
           System.out.print("Sorry, the position you have chosen is out of bounds, please choose another."); 
          } 
          else if(vorh.equals("V")){ 
           table[l][j] = ("X"); 
           table[l+1][j] = ("X"); 
           table[l-1][j] = ("X"); 
           counter2 = counter2 + 1; 
           i = i+1; 
          } 
          if(vorh.equals("H")&&input.charAt(1)==('0')){ 
           System.out.print("Sorry, the position you have chosen is out of bounds, please choose another."); 
          } 
          else if(vorh.equals("H")&&input.charAt(1)==('5')){ 
           System.out.print("Sorry, the position you have chosen is out of bounds, please choose another."); 
          } 
          else if(vorh.equals("H")){ 
           table[l][j] = ("X"); 
           table[l][j+1] = ("X"); 
           table[l][j-1] = ("X"); 
           counter2 = counter2 + 1; 
           i = i+1; 
          } 
         } 
        } 
       } 
      //} 
      if (counter2 == 4){ 
       choose = false; 
      } 
      System.out.println(); 
      for(int p = 0 ;p < 7 ;p++){ 
       for(int j = 0 ;j < 7 ;j++){ 
        System.out.printf("%-4s",table[p][j]); 
       } 
       System.out.println(); 
      } 
     } 
     System.out.print("\nNow You Must Destroy All Enemy Ships!"); 
     boolean destroy = true; 
     int destroyed = 0; 
     int aidestroyed = 0; 
     while(destroy){ 
      System.out.print("\nPlease enter a coordinate > "); 
      String input = a.nextLine().toUpperCase(); 
      for(int l = 1 ;l < 7 ;l++){ 
       for(int j = 1 ;j < 7 ;j++){ 
        if(input.equals(table2[l][j])){ 
         if(AItable[l][j].equals("X")){ 
          System.out.println("HIT!\n"); 
          destroyed = destroyed + 1; 
          hmtable[l][j] = "!"; 
         } 
         else{ 
          System.out.println("MISS!\n"); 
          hmtable[l][j] = "O"; 
         } 
         System.out.println(); 
         for(int p = 0 ;p < 7 ;p++){ 
          for(int q = 0 ;q < 7 ;q++){ 
           System.out.printf("%-4s",hmtable[p][q]); 
          } 
          System.out.println(); 
         } 
        } 
       } 
      } 
      System.out.println("The Enemy Has Chosen A Coordinate."); 
      int posnum = 1 + (int)(Math.random() * 6); 
      int posletter = 1 + (int)(Math.random() * 6); 
      if(table[posletter][posnum].equals("X")){ 
       System.out.println("You Have Been Hit By The Enemy!\n"); 
       table[posletter][posnum] = ("!"); 
       aidestroyed = aidestroyed + 1; 
      } 
      else if(table[posletter][posnum].equals("~")){ 
       System.out.println("The Enemy Missed!\n"); 
       table[posletter][posnum] = ("O"); 
      } 
      else if(!table[posletter][posnum].equals("!")||!table[posletter][posnum].equals("X")){ 
       System.out.println("The Enemy Missed!\n"); 
      } 
      System.out.println(); 
      for(int p = 0 ;p < 7 ;p++){ 
       for(int q = 0 ;q < 7 ;q++){ 
        System.out.printf("%-4s",table[p][q]); 
       } 
       System.out.println(); 
      } 
      if(destroyed == 12){ 
       System.out.print("Great Job! You Have Destroyed All Enemy Ships!"); 
       destroy = false; 
       break; 
      } 
      if(aidestroyed == 12){ 
       System.out.print("Unlucky! The Enemy Have Destroyed All Of Your Ships!"); 
       destroy = false; 
       break; 
      } 
     } 
    } 
} 
+1

'我很好與設置主菜單和基本的東西, ' - 這裏發佈的代碼都與GUI沒有任何關係,所以我不知道這個評論意味着什麼。無論如何,從[Swing教程](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html)開始。有關於如何製作對話框的部分介紹瞭如何使用'JOptionPane'。 – camickr

回答

1

要接收來自用戶的輸入與JOptionPane使用:

String inputString = JOptionPane.showInputDialog(null, "Enter input"); 

只需與該行替換您scanner.nextXXX()

注:結果可能是null如果用戶按下了JOptionPaneX按鈕,你應該檢查,像這樣:

String inputString = JOptionPane.showInputDialog(null, "Enter input"); 
if(inputString!=null){ 
    //and then use the inputString  
... 
} 
+0

我明白如何接受用戶輸入,但是我不知道如何在JOptionPane中顯示圖形。如果你在java中運行我的代碼,看看我的意思和遊戲的玩法,你應該更好地理解。 –

+0

@TreyAesthetics您無法在其中顯示圖表。它更復雜 – ItamarG3

+0

你可以在JOptionPane中顯示一個圖形,我的朋友做了它,但我不太喜歡/理解他的方法,並想知道是否有其他選擇。 –