Program execution is repeated internally, but you cannot create a method (?): Java

I have a project for my computer science class, and we are doing the battleship. Part of the program is that we make sure that the part that the player puts does not go overboard.

I made a way to check if it comes out of the board:

private static boolean test(String s, int row, int column,int spaces) { if(s.equals("right")&&column+5<=10) { return true; } if(s.equals("up")&&row-spaces>=0) { return true; } if(s.equals("left")&&column-spaces>=0) { return true; } if(s.equals("Down")&&row+spaces<=10) { return true; } return false; } 

But as soon as I got a printout of the error message, I'm not sure how to do this so that the program can re-get the new position for the part without putting the if statement in the if statement in the if statement (and so on), because you need to check the new position to make sure it doesnโ€™t go out of the board.

Here is the part where I get the position of the play (although I donโ€™t think you need it)

 Scanner sonic= new Scanner(System.in); System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: "); int beginrow = sonic.nextInt(); System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: "); int begincolumn = sonic.nextInt(); System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board."); String direction = sonic.next(); 

And here is one of the if statements I use to check / place fragments

 if(direction.equals("left")&&test("left",beginrow,begincolumn,5)) { for(int i = beginrow; i>beginrow-5; i--) { battleship[begincolumn-1][i-1] = ('a'); } } else if(!test("left",beginrow,begincolumn,5)) { System.out.println(" "); System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****"); } 

It may be a duplicate, but I did not know how to change my search to find what I wanted. (So, if someone could direct me to the right article, that would be nice)

+5
source share
4 answers

As a first step, separate validation of the input from taking an action based on that input - you already have the validation logic in a separate function, so it's easy. Then find out what needs to be done in case of an invalid input - in your case you need to request a new input until you get the correct position:

 do { System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: "); beginrow = sonic.nextInt(); System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: "); begincolumn = sonic.nextInt(); System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board."); direction = sonic.next(); } while (!test(direction, beginrow, begincolumn, 5)) 

After that, you know that you have an active position.

My next step is probably to group the information needed to describe the ship on board (i.e. beginrow , begincolumn , direction , possibly also size ) in a separate object - possibly named Ship .

+1
source

What you need to do is split your code into methods and call those methods again until your program is satisfied with the result.

For instance:

  • create a startGame() method that has job invocation methods that receive user input until they are satisfied
  • create a method for asking the user to enter all the different ships and other necessary data.

It might look something like this:

 public void startGame() { // do some setup while(!requestShipInput()) { // request ship data until the data is valid System.out.println(" "); System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****"); } // do some more ship setup // get the actual playing started } public boolean requestShipInput() { Scanner sonic= new Scanner(System.in); System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: "); int beginrow = sonic.nextInt(); System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: "); int begincolumn = sonic.nextInt(); System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board."); String direction = sonic.next(); if(direction.equals("left")&&test("left",beginrow,begincolumn,5)) { for(int i = beginrow; i>beginrow-5; i--) { battleship[begincolumn-1][i-1] = ('a'); } return true; // valid ship data } return false; // invalid ship data } 
+2
source

I think you could naturally use recursion here:

 public void getInput() { // scanner code to get input if (!test("left",beginrow,begincolumn,5)) { // test failed getInput() return } // test succeeded, continue } 
+1
source

Do you already have something to the limits of your board? If you check first, you donโ€™t need to do an if-else cascade

 if(!test(direction,beginrow,begincolumn,size)) { System.out.println(" "); System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****"); } else { // check for collision with already placed ships } 

Keep in mind that it is possible to combine up / down and left / right. The calculation rules are almost the same, and you only need to decide whether to look in one direction or another.

0
source

Source: https://habr.com/ru/post/1259482/


All Articles