Battleship Program

I am having trouble with the beginning of a battleship program. Here is the header file I must use for the program. All of the ships are just 1 space big. I am getting a bunch of error messages saying "local function definitions are illegal" and "syntax error: identifier 'loc'". Can someone please tell me what I am doing wrong? I am very new to c++.


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
//
// data structures definitions
//

const int FLEET_SIZE=5; // number of battleships
const int FIELD_SIZE=5;  // the field (ocean) is FIELD_SIZExFIELD_SIZE

// coordinates (location) of the ship and shots
struct location{
  int x;  // 1 through FIELD_SIZE
  char y; // 'a' through FIELD_SIZE
};

// contains ship's coordinates (location) and whether is was sunk
struct ship{
  location loc;
  bool sunk;
};

//
// initialization functions
//
void initialize(ship[]); // places all ships in -1 X location to signify
                        // that the ship is not deployed
location pick(void); // generates a random location
bool match(ship, location); // returns true if this location matches
                            // the location of the ship
                            // returns false otherwise
int check(const ship[], location); // returns the index of the element
                                   // of the ship[] array that matches
                                   // location. Returns -1 if none match
                                   // uses match()
void deploy(ship[]); // places an array of battleships in
                     // random locations in the ocean 


Here is my 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
#include <iostream>
#include "BATTLESHIP.h"
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){

	srand(0);
	ship s[FLEET_SIZE];
	initialize(s);
	deploy(s);
	
}

void initialize(ship s[]){
	for(int i=0; i<FLEET_SIZE; i++)
		s[i].loc.x = -1;		
}

void deploy(ship s[]){
	int i=0; // Number of ships so far
	while (i<FLEET_SIZE){
		s[i].loc = pick();
		int e = check(s, s[i].loc);
		if(e!=-1){
			s[i].sunk = false;
			i++;
		}
}

location pick(void){
	location loc;
	loc.x=rand()%FIELD_SIZE+1;
	loc.y=rand()%FIELD_SIZE+1;
	switch(loc.y){
		case 1: loc.y='a'; break;
		case 2: loc.y='b'; break;
		case 3: loc.y='c'; break;
		case 4: loc.y='d'; break;
		case 5: loc.y='e'; break;
	}
	return loc;
}

int check(const ship s[], loc){
	for(int i=0; i<FLEET_SIZE; i++)
	{
		if (s[i] == s[i].loc)
			return i;
	}

	return -1;
}
Line 46: loc does not have a type. I think you meant location loc
Line 49: a) Incompatible types. Maybe you meant if(s[i].loc == loc)
b) Above line have only 1/256 chance to work, because it compares both location.x and location.y, which is undefined in initialize() function.
c) You didn't use match() function as required
Im honestly not sure what to do in the check() function. I just tried this as a starting point and that clearly wasn't right. I also didn't use match() because I didn't know what to do or exactly what its purpose was. Also, should the initialize function also set location.y to -1? I wasn't sure on how to approach that.
An example of check and match functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int check(const ship s[], location loc)
{
	for(int i=0; i<FLEET_SIZE; i++)
		if (match(s[i], loc))
			return i;
	return -1;
}

bool match(ship s, location l)
{
    if (s.loc.x == -1)
        return false;
    else if(s.loc == l)
        retun true;
    return false;
}
Can someone explain to me exactly how line 4 works? I am a little confused.
statement after if will execute only if logical statement evaluates to true. match() is a functions which returns bool (true or false). So line 5 in this example will be executed only if match(s[i], loc) returns true.
Lool at the equivalent snippet:
1
2
bool temp = match(s[i], loc);
if(temp == true)
Topic archived. No new replies allowed.