Hi all,
In this code below I have IF statements (bear in mind this is for a battleships game). The bit in bold and the rest of the code I want to be separate, but I don't want the bit that's not in bold to be a part of the bit in bold as it's separate.
Basically what I'm trying to do with the bit that's not in bold is to change the values. I'm having a hard time trying to get the computer to stop choosing the same numbers over and over again, and so I thought that this re-assignment of values would help. But I don't think it's working because the compiler thinks that the if statements are a part of the bit that compares the computer number pick with where the user has placed their ship. Here's the code;
The code is fine. The parts are seperated, so you shouldn't try seperating them.
The problem is probably that you're using rand() and srand() to create random numbers.
Bear in mind that feeding (the time) to srand should be done before any loops, at the beginning of the function using rand().
See http://www.cplusplus.com/reference/clibrary/cstdlib/srand/
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand ...
That means you'll have to initialize srand (fed with the time) once. Then call rand() as many times as you want and it'll create pseudo-random numbers.
The rand function seems fine - I'm able to get the random number - it's just that there aren't any restrictions on how many times the same number can pop up.
a. Your code is within a loop I presume... What does the loop look like? (more code please)
b. What do you mean by "same numbers over and over again?" (5 different numbers over and over again in the same order or...)
b) I mean that since there's no restrictions to what number the rand function can choose (apart from between 0 and 24) it can pick any number however many times. In my code you can see that there are two "counts" - one for the computer and the player. If either the player or the computer selects a number where a ship is located, that count is reduced by one. The problem is that if the rand function chooses a number it's already chosen, the count is still reduced by one.
Sorry if it's not clear, I shall try again if you so need me to :P
Many thanks for your time,