2012-01-15 6 views
1

私は理解できないエラーを除いて、Cでtic-tac-toeゲームをコーディングしようとしていました。私はまだこれにいくつかの作業が必要だと知っていますが、今は私が追加する前にプログラムを実行したいだけです。誰か助けてくれますか?ここでTic-Tac-Toeゲームのエラー

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

int board[3][3] = { 
         {0, 0, 0}, 
         {0, 0, 0}, 
         {0, 0, 0} 
        }; 

int main (void) 
{ 
    int const user1 = 1; 
    int const user2 = 2; 
    char move[10]; 

    while (! all_locations_filled()) { 
     printf("User-1, please enter your move:"); 
     scanf("%s", move[10]); 

     if(valid_location(move[10])) 
      mark_location(user1, move[10]); 
      display_board(board[3][3]); 
     else if(won_the_game(user1) 
      printf("Congratulations User-1, You Won the Game!"); 
      break; 
     else 
      printf("Invalid Move"); 

     printf("User-2, please enter your move:"); 
     scanf("%s", move[10]); 

     if(valid_location(move[10])) 
      mark_location(user2, move[10]); 
      display_board(); 
     else if(won_the_game(user2) 
      printf("Congratulations User-2, You Won the Game!"); 
      break; 
     else 
      printf("Invalid Move"); 

    return 0; 
} 

bool valid_location(char str[10]) { 
    int strcmp(x, y); 

    if (strcmp(str[10], "upperLeft") == 0 || strcmp(str[10], "up") == 0 || strcmp(str[10], "upperRight") == 0 || strcmp(str[10], "left") == 0 || strcmp(str[10], "center") == 0 || strcmp(str[10], "right") == 0 || strcmp(str[10], "lowerLeft") == 0 || strcmp(str[10], "down") == 0 || strcmp(str[10], "lowerRight") == 0) 
     return true; 
} 

void mark_location(int userU, char str[10]) { 
    int strcmp(x, y); 

    if (strcmp(str[10], "upperLeft") == 0) 
     board[0][0] = userU; 
    else if (strcmp(str[10], "up") == 0) 
     board[0][1] = userU; 
    else if (strcmp(str[10], "upperRight") == 0) 
     board[0][2] = userU; 
    else if (strcmp(str[10], "left") == 0) 
     board[1][0] = userU; 
    else if (strcmp(str[10], "center") == 0) 
     board[1][1] = userU; 
    else if (strcmp(str[10], "right") == 0) 
     board[1][2] = userU; 
    else if (strcmp(str[10], "lowerLeft") == 0) 
     board[2][0] = userU; 
    else if (strcmp(str[10], "down") == 0) 
     board[2][1] = userU; 
    else if (strcmp(str[10], "lowerRight") == 0) 
     board [2][2] = userU; 
} 

char display_board(int array[][]) { 
    int i, j; 

    for (i=0; i<3; ++i) 
     for (j=0; j<3; ++j) 
      if (array[i][j] == 0) 
       print("-"); 
      else if (array[i][j] == 1) 
       print("x"); 
      else if (array[i][j] == 2) 
       print("o"); 
} 

void all_locations_filled() { 
    int i, j; 

    for (i=0; i<3; ++i) 
     for (j=0; j<3; ++j) 
      if board[i][j] == 0 
       return false; 
    return true; 
} 

bool won_the_game(userU) { 
    int i, j; 

    if (board[0][j] == userU) 
     return true; 
    else if (board[1][j] == userU) 
     return true; 
    else if (board[2][j] == userU) 
     return true; 
    else if (board[i][0] == userU) 
     return true; 
    else if (board[i][1] == userU) 
     return true; 
    else if (board[i][2] == userU) 
     return true; 
    else 
     return false; 
} 

コンパイラが私に与えますエラーがされています:ここに私のコードはあなたが整数をスキャンしようとしているが、scanfの引数が文字列(char配列)を見込んで

tictactoe.c: In function ‘main’: 
tictactoe.c:19: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ 
tictactoe.c:24: error: expected expression before ‘else’ 
tictactoe.c:115: error: expected declaration or statement at end of input 
tictactoe.c:115: error: expected declaration or statement at end of input 
+1

ヒント:Cコンパイラはインデントを無視します。 (しかし、最初の警告メッセージは、ランクの初心者にも明らかです。) –

+0

あなたのカッコはバランスが取れていない(そして、それらのトンがない)。 – Mat

+0

ちょうどヒント - あなたは明示的に初期化されていなければ、グローバル変数と静的変数は自動的にゼロにされます(怪我をすることはありませんが、 'board'のようにゼロに明示的に初期化する必要はありません)。これは、 'move'や' user1'、 'user2'のようなローカルではない静的変数には適用されないことに注意してください。初期値を初期化しない限り、これらの初期値は未定義です。 – bdonlan

答えて

0

%sの代わりに%dを試してください。 10進数のFormatstringです。

2
if(valid_location(move[10])) 
     mark_location(user1, move[10]); 
     display_board(board[3][3]); 

2行あるので "{"と "}"を使用する必要があります。

1
  1. 利用moveの代わりに、あなたのscanf文でmove[10]、あなたは機能にそれを渡しています。 moveは配列を指し、move[10]は配列内の10番目の位置を意味します。
  2. 彼らは単一のコード行以上なら入れがあなたのif/elseブロック内のコードの周り{}ブレース(または好ましくは常に、それはスタイルの問題です。)
0

あなたは1、より多くのを入れたい場合命令の後に指示がある場合は、{}で終了してください。 それ以外の場合、コンパイラは最初のものだけがインコンディションであると考え、残りはとにかく実行する必要があります。

0

scanf("%s", move);ないscanf("%s", move[10]);

if(valid_location(move))ないif(valid_location(move[10]))

mark_location(user1, move);ないmark_location(user1, move[10]);

if (strcmp(str, "upperLeft") == 0)ないif (strcmp(str[10], "upperLeft") == 0)

などなど

あなたはしないでください配列を使うたびに大括弧を入れてCで配列を作成します。基本的に2つの状況で角括弧を使用します。配列を宣言している場合、角括弧に配列のサイズが含まれています。その場合、角括弧にはインデックスが含まれています。

あなたはこれを聞きたくないかもしれませんが、コードには他にも多くの間違いがあります。あなたはたぶん本を読んで、もっと簡単に始める必要があります。

0

あなたがコンパイルエラーを処理した後は、初期化されていない変数iとjを読み込んでいる関数won_the_gameを見て、iとjがおそらく "アクセス違反"境界。

あなたのロジックは間違っているので、明らかにあなたは1つのポジションを占有するだけで勝つことはできません。

1

いくつかエラーが見つかりました。

scanf("%s", move[10]); 

ここで何をしますか?あなたは、文字列を読みたい場合は、配列の10位に一つだけの文字を読みたい場合は、お使いの配列が移動[10として宣言されたことを

scanf("%c", &move[9]); 

注意を使用

scanf("%s", move); 

を使用]ので、位置は移動[0]から移動[9]に移動します。位置移動[10]は無効です。ここで

if(valid_location(move[10])) 
     mark_location(user1, move[10]); 
     display_board(board[3][3]); 
    else if(won_the_game(user1) 
     printf("Congratulations User-1, You Won the Game!"); 
     break; 
    else 
     printf("Invalid Move"); 

おそらく意味:

if(valid_location(move[10])) 
    { 
     mark_location(user1, move[10]); 
     display_board(board[3][3]); 
    } 
    else if(won_the_game(user1) 
    { 
     printf("Congratulations User-1, You Won the Game!"); 
     break; 
    } 
    else 
     printf("Invalid Move"); 

そして、ここで:

void all_locations_filled() { 
int i, j; 

    for (i=0; i<3; ++i) 
     for (j=0; j<3; ++j) 
      if board[i][j] == 0 
       return false; 
    return true; 
} 

あなたは "もし" に()を忘れてしまいました。

if (board[i][j] == 0) 

また、関数を呼び出す前に宣言する必要があります。したがって、mainの前に関数を宣言してください。

これを実装する必要はありません。宣言するだけです。最後の関数で

void all_locations_filled(); 

int main (void) 
{ 
... 
} 

:たとえば

bool won_the_game(userU) 

あなたは "userU" のタイプを定義する必要があります。

また、メインの末尾にブレース "}"を閉じるのを忘れました。

関連する問題