nicky
nicky

Reputation: 1

having trouble with while-loop in battleship program in c

I'm trying to make a battleship program. So far my program asks User 1 to input where he/she wants their ships. Then user 2 guesses where they think the ships are.

I've tried to make my program re-prompt user 2 if they didn't hit all of player 1's ships the first time.

I've tried putting a while loop while loop in several spots but every time my program crashes, and the while loop that is there now makes it crash too. Nothing seems to work.

#include <stdio.h>

int main(void)
{
    int board[2][2];
    int i, j;   //initialize loop variables
    int i2, j2; //initialize 2nd loop variables
    int i3, j3; // 3rd loop variables

    printf(" User 1: Enter a '1' where you want to place your ship and '0' where you do not.\n");

    /* these loops prompt the user to enter a 0 or 1 for each space in the 2d array depending on where they want their ship*/ 
    for(i = 0; i <= 1 ; i++)
    {
      for(j = 0 ; j <= 1 ; j++)
      {
          printf("space[%d][%d]: ", i, j);
          scanf("%d", &board[i][j]);
      }
    }

    while(board[i][j] == 1)
    { 
        /*used to prompt the user2 as long as user1 still has ships left*/
        int board2[2][2];
        printf("User 2: Enter a '1' where you think User 1 placed their ship and '0' where \nyou do not.\n");

        /* Asks user2 for their guesses */
        for(i2 = 0 ; i2 <= 1 ; i2++)
        {
          for(j2 = 0 ; j2 <= 1 ; j2++)
          {
              printf("space[%d][%d]:", i2, j2);
              scanf("%d", &board2[i2][j2]);
          }
        }

        for(i3 = 0 ; i3 <= 1 ; i3++)
        {
            //compares user1 input to user2 guess
            for(j3 = 0 ; j3 <= 1 ; j3++)
            {
                if(board[i3][j3] == 1 && board2[i3][j3] == 1)
                {
                    printf("Hit!\n"); // if the inputs match display "hit"
                    board[i][j] = 0;
                }
                else
                {
                    printf("Miss!\n"); // if no hit display miss
                }
            }
        }
    }

    return 0;
}

Upvotes: 0

Views: 108

Answers (1)

Sumit Gemini
Sumit Gemini

Reputation: 1836

I think, As per the rule of battleship program, we specified some limit for user to find a randomly placed ship. If the user does not enter a valid response, you will keep repeating this process until a valid response is entered or the limit cross.

In your case, you want to repeat the process until a user2 find all ships without any limit.

I'm observing some issues in your code :-

  1. Suppose user1 gives 1 0 1 0 and user2 gives 1 1 1 1, your program will give successful result because you are searching complete battle board with user2 input.
  2. User2 will continuously run until you board[][] contain zero value.

Some point to change the design of your program -:

  1. Keep limit for user2 to find the ship.
  2. Don't search complete matrix with user2 input, but check index of you battle board with user2 input.

Best of luck for challenge.

Upvotes: 1

Related Questions