2015-11-05 67 views
0

我在隨機化和添加2x2船到遊戲板時遇到了麻煩。我需要它看起來如下:如何創建戰艦軍艦遊戲算法

enter image description here

目前我只能似乎得到一個1x1的船,不太明白的邏輯添加2×2和隨機化它,這樣他們都連接。

也當用戶在主菜單中輸入'2'時,我需要顯示解決方案,這意味着船舶在哪裏。我也可以使用一些幫助。

幾乎沒有完成,但請評價我的代碼,一切都有幫助!

在此先感謝。

import java.util.Scanner; 

public class Battleship 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int [][] board = new int [5][5]; 
     int [][] ship = new int [4][2]; 
     int [] shot = new int[2]; 
     boolean done = false; 

     resetboard(board); 
     while(!done) 
     { 
      displayBoard(board); 
      displayMenu(); 
      for(int ships=0 ; ships < 4 ; ships++) 
      { 
       ship[ships][0]=(int) Math.random() * 5 + 1; 
       ship[ships][1]=(int) Math.random() * 5 + 1; 
      } 
      int choice = getMenuInput(input);  
      if(choice == 1) 
      { 
       getRow(shot); 
       getColumn(shot); 
       if(fireShot(shot,ship) == true) 
       { 
        board[shot[0]][shot[1]]= 1; 

       } 
       else 
       { 
        board[shot[0]][shot[1]]= 0; 

       } 
      } 
      else if(choice == 2) 
      { 
       for (int x = 0; x < 5; x++) 
       { 
        for(int y = 0; y < 5; y++) 
        { 
         for(int z = 0; z < 3; z++) 
         { 
          if(board[x][y] == ship[z][0] && board[x][y] == ship[z][1]) 
          { 
           board[ship[z][0]][ship[z][1]]= 1; 
          } 
         } 
        } 
       } 
       displayBoard(board); 
      } 
      else if (choice == 3) 
      { 
       done = true; 
       System.out.println("Thanks For Playing"); 

      } 
     } 

    } 
    public static void displayBoard(int [][] board) 
    { 
     System.out.println(" A B C D E"); 
     for(int r =0; r < 5; r++) 
     { 
      System.out.print((r + 1) + ""); 
      for(int c = 0; c < 5; c++) 
      { 
       if(board[r][c] == -1) 
       { 
        System.out.print(" -"); 
       } 
       else if(board[r][c] == 0) 
       { 
        System.out.print(" X"); 
       } 
       else if(board[r][c] == 1) 
       { 
        System.out.print(" *"); 
       } 
      } 
      System.out.println(""); 
     } 

    } 
    public static void resetboard(int[][] a) 
    { 
     for(int row=0 ; row < 5 ; row++) 
     { 
      for(int column=0 ; column < 5 ; column++) 
      { 
       a[row][column]=-1; 
      } 
     } 
    } 
    public static void displayMenu() 
    { 
     System.out.println("\nMenu:"); 
     System.out.println("1. Fire Shot"); 
     System.out.println("2. Show Solution"); 
     System.out.println("3. Quit"); 

    } 
    public static int getMenuInput(Scanner input) 
    { 
     int in = 0; 
     if(input.hasNextInt()) 
     { 
      in = input.nextInt(); 
      if(in>0 && in<4) 
      { 
       in = in;    
      } 
      else 
      { 
       System.out.println("Invalid Entry, Please Try Again.\n"); 
      } 
     } 
     else 
     { 
      System.out.println("Invalid Entry, Please Try Again.\n"); 
      input.nextInt(); 
     } 
     return in; 
    } 

    public static void getRow(int [] shot) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter a Row Number: "); 
     shot[0] = shotValid(input); 
     shot[0]--; 
    } 
    public static void getColumn(int [] shot) 
    { 
     Scanner input = new Scanner(System.in); 
     int numb = 0; 
     System.out.println("Enter a Column Letter: "); 
     String choice = input.next(); 
     if (choice.equals("A")) 
     { 
      numb = 0; 
     } 
     else if(choice.equals("B")) 
     { 
      numb = 1; 
     } 
     else if(choice.equals("C")) 
     { 
      numb = 2; 
     } 
     else if(choice.equals("D")) 
     { 
      numb = 3; 
     } 
     else if(choice.equals("E")) 
     { 
      numb = 4; 
     } 
     else 
     { 
      System.out.println("2Invalid Entry, Please Try Again.\n"); 
      input.nextLine(); 
     } 
     shot[1] = numb; 
    } 

    public static boolean fireShot(int [] shot, int [][]ship) 
    { 
     boolean result = false; 
     for(int shipHit=0 ; shipHit<ship.length ; shipHit++) 
     { 
      if(shot[0]==ship[shipHit][0] && shot[1]==ship[shipHit][1]) 
      { 
       result = true; 
      }else 
      { 
       result = false; 
      } 
     } 
     return result; 
    } 
    public static int shotValid(Scanner quantity) 
    { 
     int shot = 0; 
     boolean done = false; 
     while(!done) 
     { 
      if(quantity.hasNextInt()) 
      { 
       shot = quantity.nextInt(); 
       if(shot>0 && shot<6) 
       { 
        shot = shot; 
        done = true; 
       } 
       else 
       { 
        System.out.println("1Invalid Entry, Please Try Again.\n"); 
       } 
      } 
      else 
      { 
       System.out.println("2Invalid Entry, Please Try Again.\n"); 
       quantity.next(); 
      } 
     } 
     return shot; 
    } 
} 
+1

我從這裏看不到你的照片,但是如果它像標準戰列艦一樣,你只需要爲你的小船選擇一個位置,就必須爲「船的其餘部分」選擇一個方向。儘管如此,你必須檢查每個方向是否合適,如果你太靠近董事會的一邊,你不應該試圖在這個方向上擴大你的船 – Aaron

回答

2

要放置在電路板尺寸2 × 2的單船和做到這一點:

for(int ships=0 ; ships < 4 ; ships++) 
{ 
    ship[ships][0]=(int) Math.random() * 5 + 1; 
    ship[ships][1]=(int) Math.random() * 5 + 1; 
} 

這裏還有幾個誤區:

  • 隨機變量總會爲1,因爲(int)轉換僅影響Math.random()的結果,該結果是排他性的0到1之間的僞隨機浮點數。轉換爲int將其截斷爲0.使用(int) (Math.Random() * 5),這將產生一個從0到4的隨機數。
  • 您不應該添加1.在內部,您的遊戲使用Java使用的零基索引,這很好。 ()這些是外部已知的行1至5和列A至E,但您在getRowgetColumn函數中注意到這一點。)
  • 您最多可放置四個大小爲1的獨立船舶× 1.(這是多達四個,因爲你可能在一個已經佔領的地方結束了機智一艘船。)

要放置一個2 × 2艘,只是確定左上角randomply,使其他船舶座標取決於:

int x = (Math.random() * 4); 
int y = (Math.random() * 4); 

ship[0][0] = x; 
ship[0][1] = y; 
ship[1][0] = x + 1; 
ship[1][1] = y; 
ship[2][0] = x; 
ship[2][1] = y + 1; 
ship[3][0] = x + 1; 
ship[3][1] = y + 1; 

您現在有兩個獨立的數據結構:開始時所有減號的板和船舶列表。你的顯示程序表明你需要板上一個單元的三個不同的值:− 1是水; 1是一艘船的無武器部分,0是射擊的地方。

但您從未設置這些值。您在顯示之前設置了船的位置,但您應該立即設置它們。你也應該設置鏡頭的位置,以便你永遠不會在同一個單元格內射擊。

您需要兩種模式來顯示電路板:即插即用模式,其中未受傷害的船舶顯示爲水,解決模式顯示所有事情。你可以通過向例程傳遞一個標誌來做到這一點。

現在,如果你考慮一下,你並不需要ship陣列。只需使用信息板:

int x = (Math.random() * 4); 
int y = (Math.random() * 4); 

board[x][y] = 1; 
board[x + 1][y] = 1; 
board[x][y + 1] = 1; 
board[x + 1][y + 1] = 1; 

保持船舶的數量,初步4.當你火在水中,用標記0細胞當你在火船,標誌着細胞爲0和遞減船的數量。如果船隻數量爲零,則玩家贏了。否則,重新顯示boatrd並再次拍攝。