2016-06-13 3 views
2

playerWin関数に何か問題があります。それは私が勝ったことを伝え続けます。私は勝つためにボード上にxを置いていないような気がする。私の論理に何が問題なの?これは宿題ではありません。私は永遠にプログラミングしていないし、私の最初の言語はJavaだった。私はC++を学ぼうとしていて、何かするためにティックタックのつま先を選んだ。C++の選択文trueを返す

#include <iostream> 
using namespace std; 

int boardSize = 3; 
char board[3][3] = {'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n' }; 


void printBoard() { 
    for (int i=0; i < boardSize; i++) { 
    for (int j=0; j < boardSize; j++) { 
     cout << board[i][j] << " "; 
    } 
    cout << endl; 
    } 
} 

// void playerMove() { }; 

bool playerWin() { 
    if ((board[0][0] == 'x') && (board[1][0] == 'x') && 
     (board[2][0] == 'x') || 

     (board[0][1] == 'x') && (board[1][1] == 'x') && 
     (board[2][1] == 'x') || 

     (board[0][2] == 'x') && (board[1][2] == 'x') && 
     (board[2][2] == 'x') || 

     (board[0][0] == 'x') && (board[0][1] == 'x') && 
     (board[0][2] == 'x') || 

     (board[1][0] == 'x') && (board[1][1] == 'x') && 
     (board[1][2] == 'x') || 

     (board[2][0] == 'x') && (board[2][1] == 'x') && 
     (board[2][2] == 'x') || 

     (board[0][0] == 'x') && (board[1][1] == 'x') && 
     (board[2][2] == 'x') || 

     (board[2][0] == 'x') && (board[1][1] == 'x') && 
     (board[0][2] == 'x')) { 

     return true; 
    } 
} 

int main() { 

    // game loop 

    char play = 'y'; 

    cout << "Welcome to Tic Tac Toe\n"; 

    do { 

    if (playerWin()) { 
     cout << "Looks like you won!" << endl; 
    } else { 
     cout << "You didn't win!"; 
    } 

    break; 

    // after game is over ask if want to play again here 
    // cout << "Would you like to play Tic Tac Toe? \n"; 
    // cin >> play; 

    } while (play = 'y'); 
} 
+0

[C++のブール型戻り関数のデフォルト戻り値](http://stackoverflow.com/questions/7529432/default-return-value-of-a-boolean-type-return-function- in-c) –

答えて

4

あなたはif文が失敗した明示的なreturn false;を、逃しています。

ストーリーの残りの部分では、「未定義の動作」のstackoverflow検索を実行します。

あなたがコンパイルしようとしたときにあなたのコンパイラが叫んだ場合は、これをレッスンにしてください。コンパイラがあなたに叫んでいるときにコンパイラを無視しないでください。

これをコンパイルしようとしたときにあなたのコンパイラが叫んでいなかった場合、将来、同様の状況でコンパイラがあなたに怒鳴らせる警告オプションまたは設定を見つけます。これは頭​​を掻くことをたくさん節約します。

+0

これはまさに問題でした。コンパイラは私に警告しなかった。私はCygwinでg ++を使っています。これらの警告をどのようにするかを理解する必要があります。ありがとうございました! –

+2

@NickBlack: 'g ++ -Wall' –

1

例をコンパイルすると、10個の警告があります。これらは:

test.cpp:20:52: warning: '&&' within '||' [-Wlogical-op-parentheses] 
    if ((board[0][0] == 'x') && (board[1][0] == 'x') && 
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ 
test.cpp:20:52: note: place parentheses around the '&&' expression to silence this warning 
    if ((board[0][0] == 'x') && (board[1][0] == 'x') && 
               ^
test.cpp:23:52: warning: '&&' within '||' [-Wlogical-op-parentheses] 
     (board[0][1] == 'x') && (board[1][1] == 'x') && 
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ 
test.cpp:23:52: note: place parentheses around the '&&' expression to silence this warning 
     (board[0][1] == 'x') && (board[1][1] == 'x') && 
               ^

はおそらく修正する必要がありますが、問題はありません。しかし、これは:

test.cpp:46:1: warning: control may reach end of non-void function [-Wreturn-type] 
} 
^ 

である可能性があり、対処する必要があります。

加えて、私はこれが見つかりました:

test.cpp:71:17: warning: using the result of an assignment as a condition without parentheses [-Wparentheses] 
    } while (play = 'y'); 
      ~~~~~^~~~~ 
test.cpp:71:17: note: place parentheses around the assignment to silence this warning 
    } while (play = 'y'); 
       ^
      (  ) 
test.cpp:71:17: note: use '==' to turn this assignment into an equality comparison 
    } while (play = 'y'); 
       ^
       == 

を、あなたのコードを見ることは、あなたが何をしたいのかないの割り当てを、やったことを伝えるあなたのコンパイラです。

-Wallを使用してビルドし、警告を修正することをおすすめします。コンパイラが助けようとしています。

+0

ありがとう!その代入文のエラーが表示され、-Wallフラグを使用して調査する必要があります。私は何でも経験は豊富ですが、特にコマンドラインを使っています。私はちょうどIDEでうんざりしたくない。私はコマンドラインを使用する方法を学びたい!ありがとうございました。 –