A simple sinking ship game with C++

i am trying to create a simple game with C++, but i meet some bug when running the game. First thing will be the random position generated. It is not always correct. Sometime i want 5 ships for total, but it give me 4 ships. Can anyone tell me what is the problem?

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
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cstdlib>

using namespace std;

const int row = 5;
const int element = 5;
int matrix[row][element];

void set();
//Set the matrix[row][element] = 0

void setShip(int maxShip);
// Generate a random position for the ship, and set the position = 1

void show();
// show the matrix on the screen

void attack(int& numberShip);
//Obtain a position (x,y) from the player for attacking
//if success, return "U got me"
//else, return "Fail"

void surrender(int& numberShip);
//Ask the player to surrender
//If player enter 'y' or 'Y', exit the game 

void clearScreen();
//Clear the screen.


int main()
{
	int maxShip;

	cout << "Enter the number of ship: ";

	cin >> maxShip;

	int numberShip = maxShip;

	set();
	setShip(maxShip);	
	show();
	attack(numberShip);

	return 0;
}

void set()
{
	for(int y = 0; y < element; y++)
		for(int x = 0; x < row; x++)
			matrix[y][x] = 0;

}

void setShip(int maxShip)
{
	int ry, rx;
	
	srand(time(0));
	
	do
	{
		ry = rand()%row;
		rx = rand()%element;

		matrix[ry][rx] = 1;

		maxShip--;
	}while(maxShip != 0);

}

void show()
{
	for(int y = 0; y < element; y++)
	{
		for(int x = 0; x < row; x++)
			cout << matrix[y][x] << " ";
		cout << endl;
	}
	cout << "---------------------------------\n";
}

void attack(int& numberShip)
{
	int x, y;

	
	while(numberShip != 0)
	{
		cout << "Please enter a position to attack :";
		cin >> x >> y;

		if(matrix[y-1][x-1] == 1)
		{
			clearScreen();
			cout << "You got me >,<\n";
			numberShip--;
			cout << "Number of ship remain :" << numberShip << endl;
			matrix[y-1][x-1] = 2;
			show();
		}
		else
		{
			cout << "You missed XD";
			surrender(numberShip);
		}

		
	}

	cout << "You win :(\n";
}

void surrender(int& numberShip)
{
	cout << " surrender?(Y/N)";

	char a;

	cin >> a;

	if(a == 'Y' || a == 'y')
	{
		exit(1);
	}
	else
	{
		clearScreen();
		show();
		attack(numberShip);
	}
}

void clearScreen()
{
	int index = 0;

	for(; index <100;index ++)
	{
		cout << "\n";
	}
}
When you generate the ship positions you don't check if there is already a ship there.
You're randomly generating a ship's position. This means that you can technically randomly generate the same position, but you don't check to make sure that maxtrix[rx][ry] is already occupied.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void setShip(int maxShip)
{
	int ry, rx;
	
	srand(time(0));
	
	do
	{
		ry = rand()%row;
		rx = rand()%element;

		if(matrix[ry][rx] == 0)
		{
				matrix[ry][rx] = 1;
				maxShip--;
		}		
			
	}while(maxShip != 0);

}


i had edited my function into this, but it still give me the same result?
Topic archived. No new replies allowed.