2015-11-05 77 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并再次拍摄。