C++ game

My teacher has assigned up to do this assignment and I have been trying new things out for days but I cant get anything to work. So I decided to post on the forums a very simple version of what I have so far. 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
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

	int x,y;

		char userDisp[11][11]=
	{
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},

	};



	char myDisp[11][11]=
	{
		{'F','F','F','-','-','-','-','-','-','F'},
		{'-','-','-','-','-','-','-','-','-','F'},
		{'-','-','-','-','-','-','-','-','-','F'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','E','E','E','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'E','E','E','-','-','-','-','-','-','-'}

	};


	do
	{
		cout<<"myDisp"<<endl;
		for ( int i = 0; i <= 9; i++)
		{
			for ( int j = 0; j <= 9; j++)
			{
				cout << setw(4) <<  myDisp[i][j];
			}
			cout<<endl;
		}

		cout<<endl<<"\nuserDisp"<<endl;
		for ( int i = 0; i <= 9; i++)
		{
			for ( int j = 0; j <= 9; j++)
			{
				cout << setw(4) << userDisp[j][i];
			}
			cout<<endl;
		}


		cout << "Please enter the X and Y coordinates."<<endl;
		cout << " X: ";
		cin >> x;
		cout << " \n Y: ";
		cin >> y;
	
		//Friendly ------------------------------------------------
		if(myDisp[x][y] == myDisp[0][0])
		{
			cout<<"Friendly Shot"<<endl;
			userDisp[0][0] = 'F';
		}

		else if(myDisp[x][y] == myDisp[0][1])
		{
			cout<<"Friendly Shot"<<endl;
			userDisp[0][1] = 'F';
		}

		else if(myDisp[x][y] == myDisp[0][2])
		{
			cout<<"Friendly Shot"<<endl;
			userDisp[0][2] = 'F';
		}



                //Enemy ------------------------------------------------
                //Deleted for now..

	}while( !( (myDisp[x][y] == myDisp[0][9]) );
}


Everytime I put in anything other than 0 and 0, the program is only showing F in user display, as if the else statement is not moving on. I apologize if I am not clear, I am learning c++.

I dont want code, I just want to know what I am doing wrong. These are the instructions given my instructor. I wrote the whole program but when I went to go test it, it would not work and slowly i started deleting code trying to narrow it down and finally narrowed it down that the FIRST if statement wont execute properly.

Teacher's instructions: Your program will start out printing the user ocean. It will then ask the user for x and y location coordinates for firing a cannon.

If the entered location is water, print something to the user (like “Splash!”) to indicate that there was no ship at that location.

If the entered location is an Enemy ship, print something to indicate that an enemy ship has been hit. Also, change the user ocean, at that x,y position, to an ‘E’.

If the entered location is a Friendly ship, print something to indicate that a friendly ship has been hit. Also change the user ocean, at that x,y position, to an ’F’.

Then print the user ocean again.

This cycle of firing the cannon, resolving the shot, and redrawing the user ocean continues until:

1. Any ONE friendly ship is sunk (all three ship locations are hit). At this point the user has lost the game.
2. BOTH enemy ships are sunk (all three ship locations are hit for both ships). At this point the user has won the game. Print out how many shots the game was won with.


Thank you in advance.
Last edited on
The value of myDisp at [0][0], [0][1], [0][2] and [0][9] is 'F', so if you choose any coordinate with 'F' all of these will be true. Due to else statements you the 'F' will be put on [0][0] and then the loop will end.
The problem is that you compare to each 'F' tile separately. myDisp[x][y] == myDisp[0][1] does not mean that x == 0 and y == 1. You should instead compare myDisp[x][y] to 'F', '-' or 'E' to see what was hit. You know where that thing is because you have the x and y variables.
Another problem is the while condition. It's the most complicated part of this program. I suggest that you put true or x != 9 || y != 9 or something like that, until you are done with the first part.

By the way, your arrays should only be of size [10][10].
Thank you very much for great advice and quick response!!!

Now, time to write the whole program, once I am done, ill post it. Thank you once again. I will surely return if I need more 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
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

	int x,y,counter = 1;

		char userDisp[10][10]=
	{
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},

	};



	char myDisp[10][10]=
	{
		{'F','F','F','-','-','-','-','-','-','F'},
		{'-','-','-','-','-','-','-','-','-','F'},
		{'-','-','-','-','-','-','-','-','-','F'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','E','E','E','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'-','-','-','-','-','-','-','-','-','-'},
		{'E','E','E','-','-','-','-','-','-','-'}

	};


	do
	{
		/*
		cout<<"myDisp"<<endl;
		for ( int i = 0; i <= 9; i++)
		{
			for ( int j = 0; j <= 9; j++)
			{
				cout << setw(4) <<  myDisp[i][j];
			}
			cout<<endl;
		}
		*/


		cout<<endl<<"\n >> BATTLESHIP << "<<endl<<endl;
		for ( int i = 0; i <= 9; i++)
		{
			for ( int j = 0; j <= 9; j++)
			{
				cout << setw(4) << userDisp[i][j];
			}
			cout<<endl;
		}

		cout<< "----------------------------------------------------"<<endl<<endl;
		cout << "Please enter the X and Y coordinates."<<endl<<endl;
		cout << " X: ";
		cin >> x;
		cout << " \n Y: ";
		cin >> y;
		cout << "\n--------------------> Shots fired: "<<counter;
		counter++;
			
		//Friend Enemy or Splash check ------------------------------------------------
		if(myDisp[x][y] == 'F')
		{
			cout<<"\nFriendly Shot"<<endl;
			cout<< endl << endl << "----------------------------------------------------";
			userDisp[x][y] = 'F';
		}		
		else if(myDisp[x][y] == 'E')
		{
			cout<<"\nEnemy shot"<<endl;
			cout<< endl << endl << "----------------------------------------------------";
			userDisp[x][y] = 'E';
		}
		else
			{
				cout<<"\nSplash!!";
				cout<< endl << endl << "----------------------------------------------------";
			}


		//Friendly Ship conditions, this checks to see if user has sunk their friendly ship, if so then the game is over and user loses
		if( (userDisp[0][0] == 'F' && userDisp[0][1] == 'F' && userDisp[0][2] == 'F') || (userDisp[0][9] == 'F' && userDisp[1][9] == 'F' && userDisp [2][9] == 'F') )
			{
				cout<<endl<<endl<<"\n You have lost due to because one of your friendly ships has sunk."<<endl<<endl<<endl;
						cout<<endl<<"\n >> BATTLESHIP << "<<endl<<endl;
						for ( int i = 0; i <= 9; i++)
						{
							for ( int j = 0; j <= 9; j++)
							{
								cout << setw(4) << userDisp[i][j];
							}
							cout<<endl<<endl<<endl;
						}
						return 0;
			}
		else 
			continue;

		//Enemy Ship conditions, this checks to see if the user sunk both of the eneny ship, if so the user wins
		if( (userDisp[6][6] == 'E' && userDisp[6][7] == 'E' && userDisp[6][8] == 'E') && (userDisp[9][0] == 'E' && userDisp[9][1] == 'E' && userDisp [9][2] == 'E') )
			{
				cout<<"\n You have WON! YOU SUNK MY BATTLESHIP! ."<<endl;
				return 0;
			}
		else 
			continue;


	}while( x!='a' );


	

}




I am almost complete but I ran into another problem..
1
2
3
4
5
6
7
8
		//Enemy Ship conditions, this checks to see if the user sunk both of the eneny ship, if so the user wins
		if( (userDisp[6][6] == 'E' && userDisp[6][7] == 'E' && userDisp[6][8] == 'E') && (userDisp[9][0] == 'E' && userDisp[9][1] == 'E' && userDisp [9][2] == 'E') )
			{
				cout<<"\n You have WON! YOU SUNK MY BATTLESHIP! ."<<endl;
				return 0;
			}
		else 
			continue;

This should check to see if both enemy ships are shot then the game should prompt that I won but even after I shoot down both ships, it still continues rather than going into the if condition and outputting that I have won.

I checked the logic with
operator short-circuit
&& --- if the left-hand side expression is false, the combined result is false (right-hand side expression not evaluated).
|| --- if the left-hand side expression is true, the combined result is true (right-hand side expression not evaluated).


and it matches


If you haven't lost, the continue on line 112 prevents lines 114-121 from executing.

It's not very good that you hard coded positions of your ships. Now to change the positions and sizes of the ships you would have to change not only the map, but also the code. I know that it's hard to write a search algorithm for this, but maybe you could try some other solution that would make your code a bit more flexible. For example you could have several arrays that describe where the ships are (one with lengths of the ships (if they can vary), and one with coordinates of each tile).

By the way, x!='a' means x != 97. Entering "a" wouldn't work here.
Topic archived. No new replies allowed.