Battle Ship Game Random Generator

Hi everyone,

i'm a kind of newbee in both this forum & c++ programming, anyway i wrote a code which is randomly generates ships in a 10x10 grid. here is the details;

battleship: size: 4 quantity: 1 shown as 'a'
cruiser: size: 3 quantity: 2 shown as 'k'
submarine: size: 1 quantity: 4 shown as 'd'
patrol boat: size: 2 quantity: 3 shown as 'm'

total number of placed ships is 10

and here is 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
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
147
148
149
150
151
152
153
154
155
156

#include <iostream>
#include <cstring>
#include <ctime>

using namespace std;

unsigned char* board;

int obj = 0;
int kru = 0;
int den = 0;
int muh = 0;
int ami= 0;


bool kontrol(int* yer, int ent=0, int yon=0) {
	switch(yon) {
	case 0:
		if(board[*yer] != 0) return false;
		break;
	case 1:
		for(int i=0; i < ent; i++) 
			if(board[*yer+i] != 0) return false;
		break;
	case 2:
		for(int i=0; i< ent; i++)
			if(board[*yer+i*10] != 0) return false;
		break;
	}

	return true;
}
			

void ekle(int* yer, int ent) {
	int yon=0;
	switch (ent) {
	case 1:
		if (kru < 2) {
			yon=rand() %2 + 1;
		switch (yon) {
		case 1:
			if( *yer % 10 < 8)
				if(kontrol(yer, 3, yon)) {
					memset(board+*yer,'k',3);
					kru++;
					obj++;
					}
			break;
		case 2:
			if((int)*yer/10 < 8)
				if(kontrol(yer, 3, yon)) {
					board[*yer] = 'k'; board[*yer+10] = 'k'; board[*yer+20] = 'k';
					kru++;
					obj++;
					}
			break;
		default: break;
		}
		}
		break;
	case 2:
		if (den < 4)
			if(kontrol(yer)) {
				memset(board+*yer,'d',1);
				den++;
				obj++;
		}
		break;
	case 3:
		if (muh < 3) {
			yon=rand() %2 +1;
			switch(yon) {
			case 1:
				if( *yer % 10 < 9)
					if(kontrol(yer,2, yon)) {
					memset(board+*yer,'m',2);
					muh++;
					obj++;
					}
			break;
			case 2:
				if((int)*yer/10 < 9) 
					if(kontrol(yer, 2, yon)) {
					board[*yer] = 'm'; board[*yer+10] = 'm';
					muh++;
					obj++;
					}
				break;
			default: break;
			}
		}
		break;
	case 4:
		if (ami < 1) {
			yon=rand() %2 +1;
			switch(yon) {
			case 1:
				if( *yer % 10 < 7)
					if(kontrol(yer, 4, yon)) {
					memset(board+*yer,'a',4);
					ami++;
					obj++;
					}
			break;
			case 2:
				if((int)*yer/10 < 7) 
					if(kontrol(yer, 4, yon)) {
					board[*yer] = 'a'; board[*yer+10] = 'a'; board[*yer+20] = 'a'; board[*yer+30] = 'a';
					ami++;
					obj++;
					}
				break;
			default: break;
			}
		}
		break;

	default: break;
	}
}

int main() {
	board = new unsigned char[100];
	memset(board, 0, 100);
	srand((unsigned) time(NULL));
	int sec=0;
	int eva=0;
	while(eva < 100) {
		if(rand() %100 > 90) {
			sec =rand() %4 + 2;
			ekle(&eva,sec);
		}
		eva++;
		}
	while(obj < 10) {
		eva=rand() %100;
		if(kru < 2) ekle(&eva, 1);
		if(den < 4) ekle(&eva, 2);
		if(muh < 3) ekle(&eva, 3);
		if(ami < 1) ekle(&eva, 4);
		}
	for(int i=0; i<10; i++) {
		for(int g=0; g<10; g++)
			cout << board[i*10+g];
		cout << endl;
	}
	cout << "Ship Count = " << obj << endl;
	cout << "Deleting Game Board... " << endl;
	delete [] board;
	system("pause");
	return 0;
}



the program looks to me running fast and produce exact results. And i wonder how the algorithm looks to you? and can you please show me different approaches to solving this problem which are lesser code length and lesser running time.

thank you all, and forgive me for my poor english!!
Topic archived. No new replies allowed.