My anti-copy part of the script isn't working :(

My problem is this... I'm trying to make a sink ships game and that is only in command prompt. Because of me basically having like a month of c++ programming experience my knowledge is very limited.

First the script asks how many ships you want. once you add, let's say 3, it uses a for-loop to randomly generate 3 random positions and checks every time the for-loop reruns if the position is already taken, if it is, it uses a "i--" command so that the integer runs again and tries a new random number, until it finds a number that is not taken.
But this part does not seem to work because it sometimes still uses the same coordinates twice :(

I'll first add the part where it bugs and then I'll add the entire code if for some reason you need it which I actually doubt :/

Any advice will be appreciated.
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	cout << "Add the ammount of ships you wish to have \nin the game. (Minimum 3 and max 10).";
	while (!(cin >> antalSkepp) || antalSkepp < 3 || antalSkepp >10)
	{
		cout << "\nCheck your imput, something seems to be wrong there:" << endl;
		cout << "Add the ammount of ships you wish to have \nin the game. (Min 3, max 10)";
		cin.clear();
		cin.ignore(1000, '\n');
	}
	randomPick.push_back(randPick(1 + rand() % 7, 1 + rand() % 7));
	for (int i = 0; i < antalSkepp; i++)
	{
		addRand1 = 1 + rand() % 7;
		addRand2 = 1 + rand() % 7;
		if (addRand1 != randomPick[i].randnr1 && addRand2 != randomPick[i].randnr2)
		{
			randomPick.push_back(randPick(addRand1, addRand2));
			cout << "index: " << i << " (x Kordinat: " << randomPick[i].randnr1 << ")" << " (y kordinat: " << randomPick[i].randnr2 << ")" << endl;
		}
		else
		{
			cout << "CopyCheck! Retry random\n";
			i--;
		}
	}


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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <Windows.h>
#include <vector>
#include <time.h>

using namespace std;

class randPick
{
public:
	int randnr1;
	int randnr2;

	randPick(int _randnr1, int _randnr2)
	{
		randnr1 = _randnr1;
		randnr2 = _randnr2;
	}
};

int main()
{
	int addRand1;
	int addRand2;
	srand(time(0));
	int indx = -1;
	int antalSkepp;
	vector<randPick>randomPick;
	const int x = 8;
	const int y = 9;
	int usernr1;
	int usernr2;

	cout << "Add the ammount of ships you wish to have \nin the game. (Minimum 3 and max 10).";
	while (!(cin >> antalSkepp) || antalSkepp < 3 || antalSkepp >10)
	{
		cout << "\nCheck your imput, something seems to be wrong there:" << endl;
		cout << "Add the ammount of ships you wish to have \nin the game. (Min 3, max 10)";
		cin.clear();
		cin.ignore(1000, '\n');
	}
	randomPick.push_back(randPick(1 + rand() % 7, 1 + rand() % 7));
	for (int i = 0; i < antalSkepp; i++)
	{
		addRand1 = 1 + rand() % 7;
		addRand2 = 1 + rand() % 7;
		if (addRand1 != randomPick[i].randnr1 && addRand2 != randomPick[i].randnr2)
		{
			randomPick.push_back(randPick(addRand1, addRand2));
			cout << "index: " << i << " (x Kordinat: " << randomPick[i].randnr1 << ")" << " (y kordinat: " << randomPick[i].randnr2 << ")" << endl;
		}
		else
		{
			cout << "CopyCheck! Retry random\n";
			i--;
		}
	}

	char gameBoard[x][y] = { { "OOOOOOOO" },
							 { "OOOOOOOO" },
							 { "OOOOOOOO" },
							 { "OOOOOOOO" },
							 { "OOOOOOOO" },
							 { "OOOOOOOO" },
							 { "OOOOOOOO" },
							 { "OOOOOOOO" } };
	while (true)
	{
		cout << "Add the X cordinate: (1-7)";
		while (!(cin >> usernr1) || usernr1 < 1 || usernr1 > 7)
		{
			cout << "\nCheck your imput, something seems to be wrong there:" << endl;
			cout << "Add the correct cordinate (1-7)";
			cin.clear();
			cin.ignore(1000, '\n');
		}
		cout << "Add the y cordinate: (1-7)";
		while (!(cin >> usernr2) || usernr2 < 1 || usernr2 > 7)
		{
			cout << "\nCheck your imput, something seems to be wrong there:" << endl;
			cout << "Add the correct cordinate (1-7)";
			cin.clear();
			cin.ignore(1000, '\n');
		}
		if (gameBoard[usernr1][usernr2] == 'X' || gameBoard[usernr1][usernr2] == 'Y')
		{
			if (gameBoard[usernr1][usernr2] == 'X')
			{
				cout << "You already tried that spot" << endl;
			}
			else
			{
				cout << "you have already found that" << endl;
			}
		}
		while (usernr2 > y - 2 || usernr1 > x - 1 || usernr1 == 0 || usernr2 == 0)
		{
			cout << "Check your imput, something seems to be wrong there" << endl;
			cout << "Add the X cordinate: ";
			cin >> usernr1;
			cout << "Add the y cordinate: ";
			cin >> usernr2;
			gameBoard[usernr1][usernr2] = 'X';
			indx = 1;
		}
		gameBoard[usernr1][usernr2] = 'X';
		for (int i = 0; i < randomPick.size(); i++)
		{
			if (randomPick[i].randnr1 == usernr1 && randomPick[i].randnr2 == usernr2)
			{
				cout << "found!";
				indx = 1;
				gameBoard[usernr1][usernr2] = 'Y';
				break;
			}
		}
		if (indx == -1)
		{
			cout << "try again!";
		}
		for (int i = 1; i < x; i++)
		{
			cout << "-----------------------------" << endl;
			cout << "| ";
			for (int j = 1; j < y - 1; j++)
			{
				//Sleep(50);
				cout << gameBoard[j][i];
				cout << " | ";
			}
			cout << endl;
		}
		cout << "-----------------------------" << endl;
		indx = -1;
	}
}
Last edited on
Each iteration of the for loop you generate a new random coordinate and forget the old one, so every time you check for a duplicate you're basically hoping for a small chance that the random coordinate you just generated is the same as the index you currently happen to be at.

Generate the coordinate before the loop.
Hum, how do you mean? :P When I generate the coordinates before the loop, the program goes in an eternal loop and bugs out :(
Topic archived. No new replies allowed.