BattleShip Program Errors!

I'm trying to write a battleship program that reads from a file. Here is what the file looks like that I am trying to get the program to read from.

##~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~###~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~#####~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~~~~~~~~~~~~~~~~
~~~#~~~~~~######~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~#~~~~~~~~~~~~~~~~~~~~
~~~~#~~~~~~~~~~~~~~~~~~~~
~~~~#~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~#~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~######~~~~~~~~~~~~~~~~


My main problem is that I cannot update the file when I hit a ship from "~" to "H". I have a feeling that my program is not reading the file but I could be wrong... because when I un-comment the for loops that I use to initialize the gameBoard it does not run properly.. everything else seems to be coded logically. Here is the rest of 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
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
//Include statements
#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

ifstream inputFile;

//Global declarations: Constants and type definitions only -- no variables
const int Width = 25;
const int Length = 25;

//Function prototypes
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25]);
void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER);


int main()
{
	//In cout statement below substitute your name and lab number
	cout << "Tyler Warren -- Lab 9: Battleship" << endl << endl;
	inputFile.open("BoardText.txt");

	//Variable declarations
	 
	char gameBoard[25][25];
	int x, y;
	bool GAMEOVER = false;

	//Program logic

	if (!inputFile)
	{
		cout << "Cannot open file." << endl;
	}

	cout << "BattleShip!" << endl;
	
	//reads gameboard from file
	/*for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j = 25; j++)
			inputFile >> gameBoard[i][j];
	}

	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			inputFile >> gameBoard[i][j];
	}*/

	while (!GAMEOVER)
	{
		cout << "Enter co-ordinates for attack! (X,Y): ";
		cin >> x >> y;
		cout << endl;	
		
		FIRE(Width, Length, x, y, gameBoard);
		FleetSunk(Width, Length, x, y,gameBoard, GAMEOVER);
	}

	inputFile.close();

	//Closing program statements
	system("pause");
	return 0;
}

//Function definitions
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25])
{
	if (gameBoard[x][y] == '#')
	{
		cout << "HIT" << endl;
		gameBoard[x][y] = 'H';
		return true;
	}
	else if (gameBoard[x][y] == 'H')
	{
		cout << "HIT AGIAN!" << endl;
		return true;
	}
	else
	{
		cout << "MISS!" << endl;
		return false;
	}
}

void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER)
{
	bool NoShip;
	NoShip = false;

	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			if (gameBoard[x][y] == '~' || gameBoard[x][y] == 'H')
				NoShip = true;
			else
			{
				NoShip = false;
				break;
			}
		if (NoShip == false)
			break;
	}

	if (NoShip == true)
	{
		GAMEOVER = true;
		cout << "Fleet has been destroyed!!" << endl;
		cout << "GAMEOVER" << endl << endl;
	}
}


Any help would be appreciated!
Last edited on
closed account (E0p9LyTq)
Why are you reading your input file twice? Twice into the same array? The second read will trash your previously correct values.

PLEASE learn to use code tags, it will make reading and commenting on your source code much easier.

HINT, you can go back and edit your post to add the tags.
http://www.cplusplus.com/articles/jEywvCM9/
Thank you for the reply. And sorry about the code tags.

I tried removing one of the for loops to see if that changed anything and I ran into the same problem. The program would compile then I would be greeted with the first two cout statement.
Last edited on
@tjwarren1

The problem you're having is because of the function, FleetSunk();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			if (gameBoard[x][y] == '~' || gameBoard[x][y] == 'H')
// You're checking gameBoard[x][y] but should be checking gameBoard[i][j]
				NoShip = true;
			else
			{
				NoShip = false;
				break;
			}
		if (NoShip == false)
			break;
	}


I revamped the function, taking out some code that wasn't really needed.

You should also print out the battleship board with where your shots were, and the hits. Makes it easier to play, IMHO.

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
#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

ifstream inputFile;

//Global declarations: Constants and type definitions only -- no variables
const int Width = 25;
const int Length = 25;

//Function prototypes
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25]);
void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER);


int main()
{
	//In cout statement below substitute your name and lab number
	cout << "Tyler Warren -- Lab 9: Battleship" << endl << endl;
	inputFile.open("BoardText.txt");

	//Variable declarations
	 
	char gameBoard[25][25];
	int x, y;
	bool GAMEOVER = false;

	//Program logic

	if (!inputFile)
	{
		cout << "Cannot open file." << endl;
		return 0;
	}

	cout << "BattleShip!" << endl;
	
	//reads gameboard from file
	
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			inputFile >> gameBoard[i][j];
	}
	inputFile.close();

	while (!GAMEOVER)
	{
		cout << "Enter co-ordinates for attack! (X,Y): ";
		cin >> x >> y;
		cout << endl;	
		
		FIRE(Width, Length, x, y, gameBoard);
		FleetSunk(Width, Length, x, y,gameBoard, GAMEOVER);
	}

	

	//Closing program statements
	system("pause");
	return 0;
}

//Function definitions
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25])
{
	if (gameBoard[x][y] == '#')
	{
		cout << "HIT" << endl;
		gameBoard[x][y] = 'H';
		return true;
	}
	else if (gameBoard[x][y] == 'H')
	{
		cout << "HIT AGAIN!" << endl;
		return true;
	}
	else
	{
		cout << "MISS!" << endl;
		return false;
	}
}

void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER)
{
	bool NoShip;
	NoShip = true;

	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			if (gameBoard[i][j] == '#')
		NoShip = false;
	}

	if (NoShip)
	{
		GAMEOVER = true;
		cout << "Fleet has been destroyed!!" << endl;
		cout << "GAMEOVER" << endl << endl;
	}
}
@whitenite1

Thanks for your help! So I changed one of the conditions in the FIRE function to rewrite the file and to print out "H" when it hits a ship. Here is the code

1
2
3
4
5
6
if (gameBoard[x][y] == '#')
	{
		cout << "HIT" << endl;
		gameBoard[25][25] = 'H';
		return true;
	}


I feel like I should write it to gameBoard[i][j] instead of [25][25] but when I rewrite the function to support [i][j] it gives me an error.
Last edited on
@tjwarren1

If your loops are using x, y, then use gameBoard[x][y], but if the loops are using i,j, then use gameBoard[i][j]. The code you just showed, would use the x, y

i.e.
1
2
3
4
5
6
if (gameBoard[x][y] == '#')
{
	cout << "HIT" << endl;
	gameBoard[x][y] = 'H';
	return true;
}


( EDIT: )
Added in the printing of the gameBoard.
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
#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

ifstream inputFile;

//Global declarations: Constants and type definitions only -- no variables
const int Width = 25;
const int Length = 25;

//Function prototypes
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25]);
void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER);
void Show_Board(char gameBoard[25][25]);

int main()
{
	//In cout statement below substitute your name and lab number
	cout << "Tyler Warren -- Lab 9: Battleship" << endl << endl;
	inputFile.open("BoardText.txt");

	//Variable declarations
	 
	char gameBoard[25][25];
	int x, y;
	bool GAMEOVER = false;

	//Program logic

	if (!inputFile)
	{
		cout << "Cannot open file." << endl;
		return 0;
	}

	cout << "BattleShip!" << endl;
	
	//reads gameboard from file
	
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			inputFile >> gameBoard[i][j];
	}
	inputFile.close();
	Show_Board(gameBoard);
	while (!GAMEOVER)
	{
		
		cout << "Enter co-ordinates for attack! (X,Y): ";
		cin >> x >> y;
		cout << endl;	
		
		FIRE(Width, Length, x, y, gameBoard);
		FleetSunk(Width, Length, x, y,gameBoard, GAMEOVER);
		Show_Board(gameBoard);
	}

	//Closing program statements
	system("pause");
	return 0;
}

//Function definitions
bool FIRE(const int Width, const int Length, int x, int y, char gameBoard[25][25])
{
	if (gameBoard[x][y] == '#')
	{
		cout << "HIT" << endl;
		gameBoard[x][y] = 'H';
		return true;
	}
	else if (gameBoard[x][y] == 'H')
	{
		cout << "HIT SAME SHIP AGAIN!" << endl;
		return true;
	}
	else
	{
		cout << "MISS!" << endl;
		gameBoard[x][y] = 'X';
		return false;
	}
}

void FleetSunk(const int Width, const int Length, int x, int y, char gameBoard[25][25], bool &GAMEOVER)
{
	bool NoShip;
	NoShip = true;

	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
			if (gameBoard[i][j] == '#')
		NoShip = false;
	}

	if (NoShip)
	{
		GAMEOVER = true;
		cout << "Fleet has been destroyed!!" << endl;
		cout << "GAMEOVER" << endl << endl;
	}
}

void Show_Board(char gameBoard[25][25])
{
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 25; j++)
		{
			if (gameBoard[i][j] != '#')
				cout << gameBoard[i][j];
			else
				cout << "~";
		}
		cout << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.