2016-09-23 41 views
0

我正在嘗試製作一個顯示棋盤/棋盤的代碼。我在棋盤上的每個方格上都有一個數字,所以我可以手動選擇棋子的位置作爲未來編程的目的。這是我的了:preview 2d array 8x8在c中不起作用

#include <stdio.h> 
//{",",",",",",","}, 
void main() 
{ 
int board[8][8] = { 
{'1','2','3','4','5','6','7','8'}, 
{'9','10','11','12','13','14','15','16'}, 
{'17','18','19','20','21','22','23','24'}, 
{'25','26','27','28','29','30','31','32'}, 
{'33','34','35','36','37','38','39','40'}, 
{'41','42','43','44','45','46','47','48'}, 
{'49','50','51','52','53','54','55','56'}, 
{'57','58','59','60','61','62','63','64'} 
}; 

     /* Display the checkerboard */ 
printf("\n\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6], board[0][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[6][0], board[6][1], board[6][2], board[6][3], board[6][4], board[6][5], board[6][6], board[6][7]); 
printf("---+---+---+---+---+---+---+---\n"); 
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[7][0], board[7][1], board[7][2], board[7][3], board[7][4], board[7][5], board[7][6], board[7][7]); 

} 

這是輸出:

1 | 2 | 3| 4 | 5 | 6 | 7 | 8                                       

---+---+---+---+---+---+---+---                                      

9 | 0 | 1| 2 | 3 | 4 | 5 | 6                                       

---+---+---+---+---+---+---+---                                      

7 | 8 | 9| 0 | 1 | 2 | 3 | 4                                       

---+---+---+---+---+---+---+---                                      

5 | 6 | 7| 8 | 9 | 0 | 1 | 2                                       

---+---+---+---+---+---+---+---                                      

3 | 4 | 5| 6 | 7 | 8 | 9 | 0                                       

---+---+---+---+---+---+---+---                                      

1 | 2 | 3| 4 | 5 | 6 | 7 | 8                                       

---+---+---+---+---+---+---+---                                      

9 | 0 | 1| 2 | 3 | 4 | 5 | 6                                       

---+---+---+---+---+---+---+---                                      

7 | 8 | 9| 0 | 1 | 2 | 3 | 4 

有了這樣的警告:

main.c:13:37: warning: multi-character character constant[-Wmultichar]                   
{'57','58','59','60','61','62','63','64'} 

它看起來像一個字符不能打印高於9?我想要的是棋盤的簡單預覽(使用2d數組)。我試圖改變這些代碼中的某些部分,但沒有任何運氣......任何想法?

+0

你需要雙引號的字符串不是字符,並且%2S格式在一個循環。真的,你不能像這樣複製/粘貼printf的行和行。 –

+1

刪除所有單引號,並在'printf'中使用''%d'' –

+0

'int'不是'char'。多符號字符文字沒有任何意義。因此編譯器警告。只有相鄰數字的查找表沒有任何意義,您可以在運行時計算這些數據。 – Lundin

回答

3

你不能把在單引號字符的字符串。必須是唯一的角色。 但它沒有什麼意義硬編碼連續整數的字符串列表,以便:

包含公式的簡單雙迴路更加緊湊,節省了大量的複製/粘貼:

#include <stdio.h> 
void main() 
{ 
int i,j; 

     /* Display the checkerboard */ 
    printf("\n\n"); 
    for (i=0;i<8;i++) 
    { 
     for (j=0;j<8;j++) 
     { 
     printf("%3d ",i*8+(j+1)); 
     } 
     printf("\n---+---+---+---+---+---+---+---\n"); 

    } 
    } 

結果:

1 2 3 4 5 6 7 8 
---+---+---+---+---+---+---+--- 
    9 10 11 12 13 14 15 16 
---+---+---+---+---+---+---+--- 
17 18 19 20 21 22 23 24 
---+---+---+---+---+---+---+--- 
25 26 27 28 29 30 31 32 
---+---+---+---+---+---+---+--- 
33 34 35 36 37 38 39 40 
---+---+---+---+---+---+---+--- 
41 42 43 44 45 46 47 48 
---+---+---+---+---+---+---+--- 
49 50 51 52 53 54 55 56 
---+---+---+---+---+---+---+--- 
57 58 59 60 61 62 63 64 
---+---+---+---+---+---+---+--- 

更好的取景(只是爲了好玩)

#include <stdio.h> 
void main() 
{ 
int i,j; 

    /* Display the checkerboard */ 
printf("\n\n\n|---+---+---+---+---+---+---+---+\n"); 

for (i=0;i<8;i++) 
{ 
    printf("|"); 
    for (j=0;j<8;j++) 
    { 
     printf("%2d |",i*8+(j+1)); 
    } 
    printf("\n|---+---+---+---+---+---+---+---+\n"); 

} 
增強版3210

}

結果

|---+---+---+---+---+---+---+---+ 
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
|---+---+---+---+---+---+---+---+ 
| 9 |10 |11 |12 |13 |14 |15 |16 | 
|---+---+---+---+---+---+---+---+ 
|17 |18 |19 |20 |21 |22 |23 |24 | 
|---+---+---+---+---+---+---+---+ 
|25 |26 |27 |28 |29 |30 |31 |32 | 
|---+---+---+---+---+---+---+---+ 
|33 |34 |35 |36 |37 |38 |39 |40 | 
|---+---+---+---+---+---+---+---+ 
|41 |42 |43 |44 |45 |46 |47 |48 | 
|---+---+---+---+---+---+---+---+ 
|49 |50 |51 |52 |53 |54 |55 |56 | 
|---+---+---+---+---+---+---+---+ 
|57 |58 |59 |60 |61 |62 |63 |64 | 
|---+---+---+---+---+---+---+---+ 

仍然不是一個ASCII藝術的掌握,但更好:)

+0

是的,它使代碼更加緊湊!而你的ASCII藝術技能似乎很好,P –

1

更換您的主板定義'xx' =>xx

int board[8][8] = { 
{1,2,3,4,5,6,7,8}, 
    .... 
{57,58,59,60,61,62,63,64} 
}; 

更改printf格式(%c =>%2d

printf(" %2d | %2d | %2d | %2d | %2d | %2d | %2d | %2d \n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]); 
.... 

注:

  • 當你寫int foo = '0';,您要求編譯器將字符'0'的值存儲在變量foo中。

    字符'0'的值不同於0

  • 當你寫int foo = '10';,你的編譯器會告訴你,它不明白你的意思,因爲如果'0''1'是指文字,'10'意味着什麼。

  • %c%dprintf是不同的:

    • printf("%c", foo);將詢問要打印的代碼是foo

    • printf("%d", foo);將要求打印的foo

    • printf("%c", 'a');的值的字符將要求打印字符a
    • printf("%d", 'a');會要求打印字符a的價值
+1

爲什麼你會想要只有一個查詢表,只有不變的,相鄰的數字?似乎這些數字不會改變,因爲它們的目的是列舉正方形。 – Lundin

+0

謝謝,那些最後的筆記真的很有用 –