Hi everyone! I'm pretty new to coding so there's still much for me to learn.
Recently I've been working on creating this battleship program that utilizes a two-dimensional array, and basically you (P1) would be playing with the computer (P2), with the battleship's position completely random.
I succeeded in having the code work until I couldn't figure out how I'd put the characters "P1" into my multi dimensional array.
An excerpt of the code looks something like this;
1 2 3 4 5 6 7
for (int i=0; i<=len; i++)
{
if (a[r][c+i]==0)
{
strcpy(a[r][c+i],arrayp1.c_str());
}
In the beginning of the code, I have declared
string arrayp1="P1";
char a[10][10];
variables len, r and c are of int types, and they are numbers I have input previously.
Hope the noble code masters here can help shed some light on my issue :)
looks like you are trying to copy a string into a char.
if you are trying to store a players "tag" into the "board" then you need a character to represent the player instead of a string because that is what your board is declared as.
1 2
constchar P1 = '1';
constchar P2 = '2';
then you can do something like....
1 2 3 4 5 6 7 8 9 10 11 12 13 14
char playerTag = P1;
while(playing)
{
// accept player move
// validate player move
a[r][c+i] = playerTag;
// next player
if (playerTag == P1)
playerTag = P2;
else
playerTag = P1;
}
What I meant was for "P1" to be shown on my two-dimensional (a[10][10]) game board, not just '1'.
How will I be able to do such a conversion? I searched high and low for answers and so far the best one I got was in my code above, that doesn't work..
What I meant was for "P1" to be shown on my two-dimensional (a[10][10]) game board, not just '1'.
Dont treat that array as the display, its your programs version of the current state of play, that info (as chervil says) needs converting to text for humans when you display it.
cout << "square " << x << "," << y << " contains player << a[x][y] << "'s ship.";