Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a program repeat within itself, but you can't make a method(?): Java

Tags:

I have a project for my computer science class and we're making battleship. Part of the program is that we have make sure that the piece the player puts down does not go off of the board.

I've made a method to check to see whether it goes off 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 once I've gotten it to print out an error message, I'm not sure how to make it so that the program can re-recieve the new position for the piece, without putting an if statement in and if statement in an if statement (and on and on), because you need to check the new position to make sure it doesn't go off of the board.

Here is the part where I get the position of the playing piece (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's one of the if statements that I use to check/place the pieces

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*****");  
}

This may be a duplicate, but I didn't know how to reword my search to find what I wanted. (So if anyone could direct me to the right article, that'd be nice as well)

like image 270
Gracelyn Rioux Avatar asked Nov 08 '16 22:11

Gracelyn Rioux


1 Answers

What you should do is split your code appropriately into methods and call that methods repeatedly until your program is satisfied with the outcome.

For example:

  • create a method startGame() which has the job call methods getting user input until satisfied
  • make a method to request the user to input all the different ships and other required data

That might look something like

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
}
like image 112
luk2302 Avatar answered Sep 24 '22 17:09

luk2302