Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Hi All, why this always goes to "thats a miss!" & Ill

$
0
0
If you enter 3 1 you should get a hit and the game board would
be updated to have an H at the coordinates entered
at least that is how I thought it would work
What did I miss??
Thank you for your time.


char shipPosition[ROW][COLUMN] =
{ 
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,1,1,1,0,1,0,0 },
{ 1,0,0,0,0,0,0,1,0,0 },
{ 1,0,0,0,0,0,0,1,0,0 },
{ 0,1,1,1,0,0,0,0,1,1 } 
};

//initialize game grid with '.'

char gameBoard[ROW][COLUMN] =
{
{ '.','.','.','.','.','.','.','.','.','.'},
{ '.','.','.','.','.','.','.','.','.','.'},
{ '.','.','.','.','.','.','.','.','.','.'},
{ '.','.','.','.','.','.','.','.','.','.'},
{ '.','.','.','.','.','.','.','.','.','.'}
};

int rowGuess = 0; //  which is ROW coordinate for player guess
int columnGuess= 0; //  which is COLUMN coordinate  for player guess
int hit = 219;  // prints solid square
int miss = 177; //prints light square



           //prints gameboard
            printf("\n\n\n\n\n");   //move board center screen

			for (int i = 0; i < ROW; i++)    
			{
  			printf("                      "); //moves board center screen
				for (int j = 0; j < COLUMN; j++) 
				{
			         printf(" ");
				 gameBoard[i][j] = '.';
				 printf("%c ", gameBoard[i][j]);
				}
			printf("\n");
			}

//get players guesses
//store coordinates enter by user 
//or enter -1 to quit

		printf("Please enter 2 coordinates 1 - 5 or -1 to quit \n");
		scanf("%d %d", &columnGuess, &rowGuess);

//		printf("%d\n", columnGuess);//stub stores and prints the users entry
//		printf("%d\n", rowGuess);//stub

		if (columnGuess == -1)   //THIS KEEPS GIVING ME AN ERROR THAT SAYS ILLEGAL USE OF BREAK
		break;
	
//compare coordinate to ships position board
//insert  177 for hit or 219 for miss into the gameboard array

	if  (shipPosition[rowGuess][columnGuess] == '1')
		{
           gameBoard[rowGuess][columnGuess] = 'H';//219;   //NEVER GETS A HIT????
           printf("That is a HIT!!\n");                    //ALWAYS SAYS MISS EVEN IF YOU ENTER IN A COORDINATE 
	    }                                              //WHERE THERE IS A 1 ON THE SHIPPOSITION  board???

	else
		{
           gameBoard[rowGuess][columnGuess] == 'M';//177;
           printf("That is a miss !!\n");
		}
       
     //back to the top of the loop to display gameBoard after each guess hit or miss
     //will put this into a while loop once I get the rest working

} // end main



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>