how to print a 2d battleship game

I'm trying to make a printed battleship map for player one. this is the code that I made.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define W '~'
#define H '*'
#define M 'm'
#define C 'c'
#define B 'b'
#define R 'r'
#define S 's'
#define D 'd'
typedef int boolean;
enum {false,true};
typedef struct {
int aim;
char visuals;
}Space;
typedef struct{
int size;
int type;
}Ship;
Space player1Side[10][10];
Space player2Side[10][10];
Ship s, *nS;
int a, b;
char l;
void printBoard() { for (int x = 0; x < 10; x++) { printf(" | "); for (int y = 0; y < 10; y++) { if (player1Side[x][y].aim != 0) { printf("%c", player1Side[x][y].visuals); } else { switch (player1Side[x][y].visuals) { case H: printf("%c", H); break; case M: printf("%c", M); break; case C: printf("%c", C); break; case B: printf("%c", B); break; case R: printf("%c", R); break; case S: printf("%c", S); break; case D: printf("%c", D); break; case W: default: printf("%c", W); break; break; } } printf(" | "); } printf(" | | "); for (int y = 10; y > 0; y--) { if (player2Side[x][y].aim != 0) { printf("%c", player2Side[x][y].visuals); } else { switch (player2Side[x][y].visuals) { case H: printf("%c", H); break; case M: printf("%c", M); break; default: printf("%c", W); break; break; } } printf(" | "); } printf("%d \n", x + 1); }
printf(" | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |\n");
printf(" Player's board
Computer's board\n");
}
void placeShips() {
printf("Start placing the ships by specifying what orientation ('h' for horizontal and 'v' for vertical) ship you wish to place, then putting the coordinates of where you wish to place it. 'c' for aircraft Carrier, 'd' for Destroyer, 'r' for Cruser, 's' for Submarine, and 'b' for BATTLESHIP.\n");
int *x = &a;
int *y = &b;
int c = 0;
char *i = &l;
Ship *nS = &s;
boolean isVert; while(c < 1) { isVert = false; scanf("%c %c", &i, &nS->type); if (l == 'v') { isVert = true; } switch (s.type) { case C: s.size = 5; break; case B: s.size = 4; break; case R: s.size = 3; break; case S: s.size = 3; break; case D: s.size = 2; break; default: break; } scanf("%d %d", &x, &y); if (a - 1 >= 10 || b - 1 >= 10 || (a + s.size >= 10 && isVert!=true) || (a + s.size >= 10 && isVert == true)){ printf("That is out of bounds. please try again\n"); placeShips(); break; } else { for (int i = s.size; i > 0; i--) { if (isVert!= true) { player1Side[a + i - 1][b].visuals = &nS->type; } else { player1Side[a][b + i - 1].visuals = &nS->type; } } } c++; } } void cpuPlaceShips() { } boolean checkWinner() {
return true;
}
main() { for (int x = 0; x < 10;x++) { for (int y = 0; y < 10;y++) { player1Side[x][y].aim = 0; player2Side[x][y].aim = 0; player1Side[x][y].visuals = '~'; player2Side[x][y].visuals = '~'; } } printBoard(); placeShips(); printBoard(); system("pause"); }

when I run it, I expect to get the symbols of a ship in the coordinates inputted. but instead, I get this.result of

it's really annoying and has been bugging me for hours on end. Can anyone please help me? I'd really appreciate it.

3

1 Answer

Looking at the code it seems you should consider type to be char and not int in the struct

typedef struct{ int size; char type; // <== not int
} Ship;

Then there are a few pointer confusions. In placeShips

while(c < 1) { isVert = false; scanf("%c %c", i, &nS->type); // <== not &i 

and after the switch

 scanf("%d %d", x, y); // <== not &x &y

later in the for

 player1Side[a + i - 1][b].visuals = nS->type; // <== not &
}
else { player1Side[a][b + i - 1].visuals = nS->type; // <== not &

FYI, doing

int z;
int *p = &z;

makes p points to z address, so in scanf use directly p and not &p.

With these fixed the board should look better.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like