Overwhelmed - Battleship Project

I need to create a battleship game where each ship takes up only 1 space and there cant be any repeating locations. Here is my code but there are still times when there are 2 ships on one space. Please help!

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

int main(){

	srand(time(NULL));
	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[], 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.x == l.x && s.loc.y == l.y)
        return true;  
}

Last edited on
on line 23 you assign a value to s[i]. loc. on line 24 you check the array to see if the value you assigned is there. it will always be there because s[i].loc is part of that array. you are effectivly doing this s[i].loc == s[i].loc. on line 25 you have the statement if (e=-1) here you are assigning -1 to e not checking if e == -1. You need to rethink your match function as well what happens if s.loc.x isn't -1 and dosn't match the other location? The function will return true by default.

Give this a try
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
#include <iostream>
#include "BATTLESHIP.h"
#include <ctime>
#include <cstdlib>
using namespace std;

int main(){

	srand(time(NULL));
	ship s[FLEET_SIZE];
	initialize(s);
	deploy(s);
	
	cin.ignore(1000, '\n');
	return 0;
}

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
	location temp;
	while (i<FLEET_SIZE)
	{
		temp = pick();
		int e = check(s, temp);
		if (e==-1)
		{
			s[i].loc = temp;
			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[], 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 == l.x) && (s.loc.y == l.y))
          {
                return true; 
          }
	else
		return false;
}

Last edited on
What's "BATTLESHIP.h"?

Can you post
Yeah I saw the BATTLESHIP.h file in a differant post. here it is

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   



I assume they are the same lol.
Placed them all in and nothing works :P
- Personally, i would make a 2 dimensional array as the field, with all of the values set to "-1" or something.

- Then, if the value of the space is not negative one, you know that there is a ship at said location.
Example:

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <string>
#include "concor.h"
using namespace std;

#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

class AI
{
    public:
        int x;
        int y;
        int playerarray[8];
};

void displayXY(int player[])
{
    for(int i = 0;i < 8;i++)
    {
        cout << player.x << "\t\t" << player.y << endl;
    }
}

void display(int playerarray[8])
{
    for(int index = 0;index < 8;index++)
    {
        displayXY(playerarray[index]);
    }
}
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
char battleground[40][40] = {
                                {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','0',},
                                {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0',}};
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
void print_world(char battleground[40][40],const int number,int& the_x,int& the_y);
void clear_screen();
bool check_collision(char battleground[][40], int the_size, int dir1, int dir2, int the_x, int the_y);
// dir1 -1 for up, 1 for down, otherwise 0
// dir2 -1 for left, 1 for right, otherwise 0
// If adding or subtracting the numbers, does nor cause a collision, then x or y is added or subtracted

void setcursor(bool, DWORD);
int main(int nNumberofArgs,char* pszArgs[])
{
    AI player;
    setcursor(0, 0);
	char dummy = ' ';
	int x=1, y=1;
	bool collision;
	print_world(battleground,40,x,y);
	string kills;
	string direction;

    for(;;)
    {
        clear_screen();
		int ch = getch();
		kills = "              ";
		direction = "      ";
		switch(ch)
		{
		case RIGHT:
			{
				collision=check_collision(battleground,40,0,1,x,y);
				if(!collision)
				{
					x++;
					direction="East";
				}
				break;
			}
		case LEFT:
			{
				collision=check_collision(battleground,40,0,-1,x,y);
				if(!collision)
				{
					x--;
					direction="West";
				}
				break;
			}
		case UP:
			{
				collision=check_collision(battleground,40,-1,0,x,y);
				if(!collision)
				{
					y--;
					direction="North";
				}
				break;
			}
		case DOWN:
			{
				collision=check_collision(battleground,40,1,0,x,y);
				if(!collision)
				{
					y++;
					direction="South";
				}
				break;
			}
		}

		print_world(battleground,40,x,y);
		if(collision)
			direction = "  NO ";

		cout << direction << "\t" << kills << "\t" << endl;
		display(player.playerarray);

	}

	cin >> dummy;
	return 0;
}

void print_world(char battleground[][40],const int number, int& the_x, int& the_y)
{
	for(int i=0;i<number;i++)
	{
		for(int j=0;j<number;j++)
		{
			if(the_x==j && the_y==i)
            {
                setcolor(green,black);
                cout << 'x';
                setcolor(7,black);
            }
            else if(battleground[i][j]=='0')
				cout << '\xB2';
			else
				cout << battleground[i][j];

		}
		cout<<endl;
	}
}

bool check_collision(char battleground[][40], int the_size, int dir1, int dir2,int the_x, int the_y)
{
	if(battleground[the_y+dir1][the_x+dir2]=='0')
		return true;
	else
		return false;
}

void clear_screen()
{
	HANDLE handle_out;
	COORD position;

	handle_out=GetStdHandle(STD_OUTPUT_HANDLE);//std output handle is the monitor

	position.X=0;
	position.Y=0;

	SetConsoleCursorPosition(handle_out,position);//sets cursor in the specified position
}

void setcursor(bool visible, DWORD size)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	if(size == 0)
	{
		size = 20;	// default cursor size Changing to numbers from 1 to 20, decreases cursor width
	}
	CONSOLE_CURSOR_INFO lpCursor;
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console,&lpCursor);
}


Paste these all together
Topic archived. No new replies allowed.