Re-writing this IF statement

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;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if (computerPick == placePatrol || computerPick == placeAirCarr || computerPick == placeBatt || 
computerPick == placeDest || computerPick == placeSub && computerPick < 25)
{
                cout <<"//Computer hits!" << endl;
                computerShipCount--;
}

else
                cout <<"//Computer misses!" << endl;




if (computerPick == placePatrol)
{
                 placePatrol = 100;
}

else if (computerPick == placeAirCarr)
{
                 placeAirCarr = 101;
}

else if (computerPick == placeBatt)
{
                 placeBatt = 102;
}

else if (computerPick == placeDest)
{
                 placeDest = 103;
}

else if (computerPick == placeSub)
{
                 placeSub = 104;
}



Thanks in advance,
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.
Last edited on
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.

My rand/srand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// for the computers battleships
srand ( time(0) ); 

compAirCarr = rand () % 25 + 26;
compBatt = rand () % 25 + 26;
compDest = rand () % 25 + 26;
compSub = rand () % 25 + 26;
compPatrol = rand () % 25 + 26;

..

//for the random number pick
srand ( time(0) ); 
computerPick = rand () % 25; 
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...)
a) Here's the while loop;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
while (shipCount > 0 && computerShipCount > 0)
{
cout << "//Select a number between 25 and 50 to bomb" << endl;
cin >> playerPick;

if (playerPick == compAirCarr || playerPick ==compBatt || playerPick ==compDest || 
playerPick == compSub || playerPick == compPatrol && playerPick <50 && playerPick >25)
{
               cout << "//HIT!" << endl;
               shipCount--;
}

else 
               cout << "//MISS!" << endl;

system ("PAUSE");
               cout <<"//Computer's turn!" << endl;

srand ( time(0) ); 
computerPick = rand () % 25;

             cout << "Computer pick = " << computerPick << endl;

system ("PAUSE");

if (computerPick == placePatrol || computerPick == placeAirCarr || computerPick == placeBatt || 
computerPick == placeDest || computerPick == placeSub && computerPick < 25)
{
                cout <<"//Computer hits!" << endl;
                computerShipCount--;
}

else
                cout <<"//Computer misses!" << endl;




system ("PAUSE");


}

if (computerShipCount == 0)
{ 
     cout << "Computer wins!" << endl;
}

else if (shipCount == 0)
{
     cout << "You win!" << endl;
}



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,
You need to store the numbers that are already chosen and check after the rand() whether it is a valid pick or not. You can use an array for this.
1
2
3
4
5
6
int PickedNumbersArrayComp[25];
while (computerPick==PickedNumbersArrayComp[computerPick])
		{
			computerPick = rand () % 25;
		}
PickedNumbersArrayComp[computerPick]=computerPick;


You code would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <ctime>
#include <stdio.h>
using namespace std;


int _tmain()
{
	int shipCount=5;
	int computerShipCount=5;
	int playerPick=0;
	int compAirCarr=5;
	int compBatt=6;
	int compDest=7;
	int compSub=8;
	int compPatrol=9;
	int placePatrol=0;
	int placeAirCarr=1;
	int placeBatt=2;
	int placeDest=3;
	int placeSub=4;
	int PickedNumbersArrayPlayer[50];
	int PickedNumbersArrayComp[25];
	while (shipCount > 0 && computerShipCount > 0)
	{
		cout << "//Select a number between 25 and 50 to bomb" << endl;
		cin >> playerPick;
		
		while (playerPick>50||playerPick<25||playerPick==PickedNumbersArrayPlayer[playerPick])
		{
			cout << "Already picked or invalid number. Choose again:" << endl;
			cin >> playerPick;
		}
		PickedNumbersArrayPlayer[playerPick]=playerPick;

		if (playerPick == compAirCarr || playerPick ==compBatt || playerPick ==compDest || 
		playerPick == compSub || playerPick == compPatrol && playerPick <50 && playerPick >25)
		{
					   cout << "//HIT!" << endl;
					   shipCount--;
		}

		else 
			cout << "//MISS!" << endl;

		system ("PAUSE");
		cout <<"//Computer's turn!" << endl;

		srand ( time(0) ); 
		int computerPick = rand () % 25;
		
		while (computerPick==PickedNumbersArrayComp[computerPick])
		{
			computerPick = rand () % 25;
		}
		PickedNumbersArrayComp[computerPick]=computerPick;

		cout << "Computer pick = " << computerPick << endl;

	system ("PAUSE");

	if (computerPick == placePatrol || computerPick == placeAirCarr || computerPick == placeBatt || 
	computerPick == placeDest || computerPick == placeSub && computerPick < 25)
	{
					cout <<"//Computer hits!" << endl;
					computerShipCount--;
	}

	else
					cout <<"//Computer misses!" << endl;




	system ("PAUSE");


	}

	if (computerShipCount == 0)
	{ 
		 cout << "Computer wins!" << endl;
	}

	else if (shipCount == 0)
	{
		 cout << "You win!" << endl;
	}

	return 0;
}

(I added some values to the variables to make it work. Of course you'll have your own values.)
See if it works! Good luck.
Last edited on
It certainly does work! Thank you very much :)!!
int PickedNumbersArrayPlayer[50]; should be int PickedNumbersArrayPlayer[51];. otherwise the player chooses 50 and gets an error message.

Topic archived. No new replies allowed.