2016-10-26 3 views
-3

私はCの新人です。チェス盤のパターンを出力するプログラムを作ろうとしています。しかし、私は次のステップが何になるのか分かりません。誰も私にこれを手伝ってもらえますか?Cでチェス盤を作る方法は?

メイン:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdbool.h> 

int main() { 
    int length,width; 
    enum colors{Black,White}; 

    printf("Enter the length:\n"); 
    scanf("%d",length); 
    printf("Enter the width:\n"); 
    scanf("%d",width); 

    int board[length][width]; 
    createBoard(length,width); 
    for(int i =0;i< sizeof(board);i++) { 

     if(i%2==0) { 
      // To print black 
      printf("[X]"); 
     } 
     else { 
      // To print white 
      printf("[ ]"); 
     } 
    } 
} //main 

CreateBoard機能:

int createBoard(int len, int wid){ 
    bool b = false; 
    for (int i = 0; i < len; i++) { 
     for (int j = 0; j < wid; j++) { 
      // To alternate between black and white 
      if(b = false) { 
       board[i][j] = board[White][Black]; 
       b = false; 
      } 
      else { 
       board[i][j] = board[Black][White]; 
       b = true; 
      } 
     } 
    } 
    return board; 
} 
+0

C++よりもC++に似ています。注意してC++タグを削除しますか? –

答えて

1

は、まず(scanf関数を使用する方法を学びます)。

int length; 

不正:scanf関数( "%のD"、長さ)。 修正:scanf( "%d"、&の長さ);

希望はこのことができます:

#include <stdio.h> 

int main(void) 
{ 
    int length,width,i,j; 
    printf("Enter the length:\n"); 
    scanf("%d",&length); 
    printf("Enter the width:\n"); 
    scanf("%d",&width); 
    for(i=1;i<=length;i++) 
    { 
     for(j=1;j<=width;j++) 
     { 
      if((i+j)%2==0) 
       printf("[X]"); 
      else printf("[ ]"); 
     } 
     printf("\n"); 
    } 
    return 0; 
} 

注:必ず長さと幅も長さのものであることを確認します。

関連する問題