gpt4 book ai didi

c - 如何打印 2D 战舰游戏

转载 作者:行者123 更新时间:2023-11-30 14:52:08 24 4
gpt4 key购买 nike

我正在尝试为玩家一制作一张打印的战舰 map 。这是我编写的代码。

#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");


}

当我运行它时,我希望在输入的坐标中获得船舶的符号。但相反,我明白了。 result of

这真的很烦人,已经连续困扰我几个小时了。谁能帮帮我吗?我真的很感激。

最佳答案

查看代码,您似乎应该将结构中的 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 .

修复这些后,电路板应该看起来更好。

关于c - 如何打印 2D 战舰游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com