1

I have the following code:

  do{
     int health = 3;

     int xVal;
     int yVal;

        printf("Enter an x value from 0 to 9\n");
        scanf("%d", &xVal);
        printf("You entered '%d'\n",xVal);

        printf("Enter a y value from 0 to 9\n");
        scanf("%d", &yVal );
        printf("You entered '%d'\n",yVal);

        printf("Value at '%d', '%d' is '%d'\n", yVal , xVal, matrix[yVal][xVal]);




            if ( matrix[yVal][xVal] == 5 )
            {
                health = health - 1;
                printf("Ouch... You have found a 5 and lost a point\n If you lose %d more, you will lose.\n", health);
            }


    if (health == 0)
    {
        isGameOver = true;
        printf("You have lost.\n");
    }
   if ( matrix[yVal][xVal] == 9 )
   {
       isGameOver = true;
       printf("You have won.\n");
   }

}  while(isGameOver != true);

The problem comes each time I find a five in the array, it only subtracts from the health counter once. If I find 2 fives, it will stay as a 2 rather than counting down to one, the whole point of my game is that if you find 3 "5"s, you lose.

1 Answers1

1

The problem is you are declaring health = 3 inside the loop, so each time it loops it resets it to 3. Move it to before the loop (ie before the "do" statement)

waterjuice
  • 829
  • 5
  • 14