Structures

Here is the header file for a battleship game I am required to use. This part is just for initialization.

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  



And here is my code so far:





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
#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==i)
                       s.loc =   <===== WHAT GOES HERE?
                else if(e=-1)
                             <========= WHAT GOES HERE?
}

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[], 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;
}


I need help with what goes in lines 27 and 29. Thank you.
maybe something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void deploy(ship s[])                   
{
	int e = 0;
	int i = 0;
	ship temp;

	temp.loc = pick();        
	s[i].loc = temp.loc; 
	i++;
		
	while (i < FLEET_SIZE)
	{
		temp.loc = pick();            
		e = check(s, temp.loc);      
		if (e == -1)                  
		{
		s[i].loc = temp.loc;            
		i++;
		}
	}
}
Line 26: will always be true.
code you probably want:
1
2
3
4
5
6
7
8
9
void deploy(ship s[])                   
{
        location l = pick()
	for(int i = 0; i < FLEET_SIZE; ++i){
                while(check(s, l) != -1)//Make sure that chosen
                        l = pick();     //location will be free
                s[i].loc = l;
        }
}
Last edited on
Topic archived. No new replies allowed.