我想知道你是否可以告诉我我做错了什么。这是我正在研究的战舰游戏的一个小片段,它应该显示两个充满“=”符号的清晰阵列,但我得到的只是一团糟。
#include <stdio.h>
int p_ship_count;
int e_ship_count;
#define EMPTY 0
#define BORDER 2
#define SHIP 1
#define HIT 4
#define MISS 3
#define SHOW 5
void print_boards(char *message,int e_board[][12],int p_board[][12])
{
int i;
int v,q;
printf("\t A B C D E F G H I J\n");
for(i=0,v=10,q=0;i<100;++i)
{
if(v==10)
{
if(q<9) printf("\n\t %i ", ++q);
else printf("\n\t%i ", ++q);
v=0;
}
++v;
if(e_board[v][q] == EMPTY) printf("= ");
if(e_board[v][q] == SHIP) printf("O ");
if(e_board[v][q] == BORDER) printf("+ ");
if(e_board[v][q] == MISS) printf("+ ");
if(e_board[v][q] == HIT) printf("@ ");
}
printf("\n\n\tenemy %i\n\t------------------------\n\tplayer %i\n\n\t A B C D E F G H I J\n",e_ship_count,p_ship_count);
for(i=0,v=10,q=0;i<100;++i)
{
if(v==10)
{
if(q<9) printf("\n\t %i ", ++q);
else printf("\n\t%i ", ++q);
v=0;
}
++v;
if(p_board[v][q] == EMPTY) printf("= ");
if(p_board[v][q] == SHIP) printf("O ");
if(p_board[v][q] == BORDER) printf("+ ");
if(p_board[v][q] == MISS) printf("+ ");
if(p_board[v][q] == HIT) printf("@ ");
if(p_board[v][q] == SHOW) printf("X ");
}
printf("\n\n\t");
for(i=0;message[i]!='\0';++i)
{
printf("%c",message[i]);
}
printf("\n\n");
}
int main()
{
int e_board[12][12];
int p_board[12][12];
print_boards("",e_board,p_board);
return 0;
}
答案 0 :(得分:0)
在开头将所有内容初始化为零。我想你也可以用一些C函数分配数组,如calloc
或者为0
初始化int main()
{
int i=0; int j=0;
int e_board[12][12];
int p_board[12][12];
for(i=0; i < 12; i++){
for(j=0; j < 12; j++){
e_board[i][j] = 0;
p_board[i][j] = 0;
}
}
print_boards("",e_board,p_board);
return 0;
}
,但我现在忘记了。
A B C D E F G H I J
1 = = = = = = = = = =
2 = = = = = = = = = =
3 = = = = = = = = = =
4 = = = = = = = = = =
5 = = = = = = = = = =
6 = = = = = = = = = =
7 = = = = = = = = = =
8 = = = = = = = = = =
9 = = = = = = = = = =
10 = = = = = = = = = =
enemy 0
------------------------
player 0
A B C D E F G H I J
1 = = = = = = = = = =
2 = = = = = = = = = =
3 = = = = = = = = = =
4 = = = = = = = = = =
5 = = = = = = = = = =
6 = = = = = = = = = =
7 = = = = = = = = = =
8 = = = = = = = = = =
9 = = = = = = = = = =
10 = = = = = = = = = =
将此作为主要功能,程序正确打印出所有&#34; =&#34;
A B C D E F G H I J
1
2
3 +
4 = = =
5 =
6 =
7
8 @
9 @ =
10 @ = =
enemy 0
------------------------
player 0
A B C D E F G H I J
1 = =
2 = = O =
3 = = =
4 = =
5 = = = =
6 = = = =
7 = = = =
8 = =
9 = = + = =
10 = = = = =
如果您没有正确初始化数组,那么您会获得随机数据垃圾,它会打印出类似
的内容ViewController