我试图为玩家一制作打印的战舰地图。这是我制作的代码。
#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");
}
它真的很烦人,一直困扰着我好几个小时。谁能帮帮我吗?我真的很感激。
答案 0 :(得分:1)
查看代码时,您似乎应该在结构中将type
视为char
而不是int
typedef struct{
int size;
char type; // <== not int
} Ship;
然后有几个指针混淆。在 placeShips
while(c < 1) {
isVert = false;
scanf("%c %c", i, &nS->type); // <== not &i
并在开关
之后 scanf("%d %d", x, y); // <== not &x &y
后面的
player1Side[a + i - 1][b].visuals = nS->type; // <== not &
}
else {
player1Side[a][b + i - 1].visuals = nS->type; // <== not &
仅供参考,
int z;
int *p = &z;
使p
指向z
地址,因此在 scanf 中直接使用p
而不是&p
。
通过这些固定,电路板应该看起来更好。