2016-05-16 216 views
0

我想知道如果你能告訴我我做錯了什麼。這裏是我正在研究的戰列艦遊戲的一小部分,它應該顯示兩個清晰的數組,填充了「=」符號,但我所得到的只是一團糟。傳遞數組作爲參數C

#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; 
} 
+6

刪除標籤'c#' – SeM

+0

你爲什麼使用標籤C++?這是c代碼 –

回答

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; 
} 

有了這個作爲主要功能,PROGRAMM打印出正確的所有「=」

 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 = = = = = = = = = = 

如果沒有正確初始化數組,你會得到隨機的數據垃圾,它會打印像

 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 = = = = =