2016-11-10 9 views
-3

私は比較的新しくコーディングしていて、C++でチックタックつま先ゲームを作成することを任されていましたが、すべてのコードが完了していると思っていましたが、 2人ではなく1人のプレーヤーの名前だけを入力することができます。この仕事をする方法について私が与える助けやアドバイスは本当に感謝しています。チックタックつま先で苦労しているC++

#include <iostream> 
using namespace std; 
int menumain; 
int oneplayer = 'X'; 

char square1('1'); 
char square2('2'); 
char square3('3'); 
char square4('4'); 
char square5('5'); 
char square6('6'); 
char square7('7'); 
char square8('8'); 
char square9('9'); 

void toggleplayers() { 
    if (oneplayer == 'X') 
    oneplayer = 'O'; 
    else 
    oneplayer = 'X'; 
} 

char win() 

{ 
    if (square1 == 'X' && square2 == 'X' && square3 == 'X') return 'X'; 
    if (square4 == 'X' && square5 == 'X' && square6 == 'X') return 'X'; 
    if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X'; 
    if (square1 == 'X' && square5 == 'X' && square9 == 'X') return 'X'; 
    if (square3 == 'X' && square5 == 'X' && square7 == 'X') return 'X'; 
    if (square3 == 'X' && square6 == 'X' && square9 == 'X') return 'X'; 
    if (square1 == 'X' && square4 == 'X' && square7 == 'X') return 'X'; 
    if (square2 == 'X' && square5 == 'X' && square8 == 'X') return 'X'; 
    if (square1 == 'O' && square2 == 'O' && square3 == 'O') return 'O'; 
    if (square4 == 'O' && square5 == 'O' && square6 == 'O') return 'O'; 
    if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X'; 
    if (square1 == 'O' && square5 == 'O' && square9 == 'O') return 'O'; 
    if (square3 == 'O' && square5 == 'O' && square7 == 'O') return 'O'; 
    if (square3 == 'O' && square6 == 'O' && square9 == 'O') return 'O'; 
    if (square1 == 'O' && square4 == 'O' && square7 == 'O') return 'O'; 
    if (square2 == 'O' && square5 == 'O' && square8 == 'O') return 'O'; 
    return '/'; 
} 

int main() { 
    int playerone, playertwo; 
    system("cls"); 

    cout << "tic tac toe" << endl; 
    cout << "-----------" << endl; 
    cout << "Start game (1)" << endl; 
    cout << "Quit game (2)" << endl; 

    cout << "Press 1 or 2 to proceed." << endl; 
    cin >> menumain; 
    if (menumain == 2) 

    { 
    return 0; 
    } else { 
    cout << "Player One, please enter your name: " << endl; 
    cin >> playerone; 
    system("cls"); 

    cout << "Player Two, please enter your name: " << endl; 
    cin >> playertwo; 
    system("cls"); 

    int playermove; 
    cout << "Choose a number between 1-9 to place!" << endl; 
    cin >> playermove; 

    if (playermove == 1) 
     square1 = oneplayer; 
    else if (playermove == 2) 
     square2 = oneplayer; 
    else if (playermove == 3) 
     square3 = oneplayer; 
    else if (playermove == 4) 
     square4 = oneplayer; 
    else if (playermove == 5) 
     square5 = oneplayer; 
    else if (playermove == 6) 
     square6 = oneplayer; 
    else if (playermove == 7) 
     square7 = oneplayer; 
    else if (playermove == 8) 
     square8 = oneplayer; 
    else if (playermove == 9) 
     square9 = oneplayer; 

    cout << "Tic tac toe!" << endl; 

    cout << "------------" << endl; 

    cout << " " << square1 << " | " << square2 << " | " << square3 << " " 
     << endl; 

    cout << " " << square4 << " | " << square5 << " | " << square6 << " " 
     << endl; 

    cout << " " << square7 << " | " << square8 << " | " << square9 << " " 
     << endl; 

    cout << " " << endl; 
    cout << playerone << endl; 
    cout << playertwo << endl; 

    while (1) { 
     if (win() == 'X') { 
     cout << playerone << " wins!" << endl; 
     break; 
     } 
     else if (win() == 'O') { 
     cout << playertwo << " wins!" << endl; 
     break; 
     } 
     toggleplayers(); 
    } 

    system("pause"); 
    return 0; 
    } 
} 
+0

**「ユーザー入力が許可されていません」**?許可するかどうかはあなた次第です –

+0

ボードに2次元配列を使用することで、コードを簡略化し短縮することができます。 –

+0

あなたのコードを書いたやり方は、tic-tac-toeが3 x 3のボードにすぎないことは幸いです。 10×10の場合、コードはどのように見えますか?コード内のパターンを特定し、一般化する方法があるかどうかを確認します.2次元行列の提案は、コードを短縮するための1つのステップにすぎません。 – PaulMcKenzie

答えて

0

私は簡単な修正を行いました。コピー、貼り付け、コンパイル、テストを行います。それはゲームの面で仕事をします。できます。それはチックタックです。 :D。さらなる調整をして幸運を祈る。

これらの}; }; };私はIDEではなくテキストエディタでこれを編集して以来、ちょうど自分のためのマーカーです。私の提案は、Drawの句を追加します。なぜなら勝者がいなければ何もしないからです。

#include <iostream> 
    using namespace std; 
    int menumain; 
    int oneplayer = 'X'; 


    char square1('1'); 
    char square2('2'); 
    char square3('3'); 
    char square4('4'); 
    char square5('5'); 
    char square6('6'); 
    char square7('7'); 
    char square8('8'); 
    char square9('9'); 

    void toggleplayers() 
    { 
    if (oneplayer == 'X') 
    oneplayer = 'O'; 
    else 
    oneplayer = 'X'; 
    }; 

    char win() 
    { 
    if (square1 == 'X' && square2 == 'X' &&square3 == 'X') 
    return 'X'; 
    if (square4 == 'X' && square5 == 'X' &&square6 == 'X') 
    return 'X'; 

    if (square7 == 'X' && square8 == 'X' &&square9 == 'X') 
    return 'X'; 

    if (square1 == 'X' && square5 == 'X' && square9 == 'X') 
    return 'X'; 
    if (square3 == 'X' && square5 == 'X' &&square7 == 'X') 
    return 'X'; 

    if (square3 == 'X' && square6 == 'X' &&square9 == 'X') 
    return 'X'; 
    if (square1 == 'X' && square4 == 'X' &&square7 == 'X') 
    return 'X'; 

    if (square2 == 'X' && square5 == 'X' &&square8 == 'X') 
    return 'X'; 

    if (square1 == 'O' && square2 == 'O' &&square3 == 'O') 
    return 'O'; 
    if (square4 == 'O' && square5 == 'O' &&square6 == 'O') 
    return 'O'; 

    if (square7 == 'X' && square8 == 'X' &&square9 == 'X') 
    return 'X'; 

    if (square1 == 'O' && square5 == 'O' && square9 == 'O') 
    return 'O'; 
    if (square3 == 'O' && square5 == 'O' &&square7 == 'O') 
    return 'O'; 

    if (square3 == 'O' && square6 == 'O' &&square9 == 'O') 
    return 'O'; 
    if (square1 == 'O' && square4 == 'O' &&square7 == 'O') 
    return 'O'; 

    if (square2 == 'O' && square5 == 'O' &&square8 == 'O') 
    return 'O'; 

    return '/'; 
    }; 

    int main() 
    { 
    int playerone, playertwo; 


    cout << "tic tac toe" << endl; 
    cout << "-----------" << endl; 
    cout << "Start game (1)" << endl; 
    cout << "Quit game (2)" << endl; 

    cout << "Press 1 or 2 to proceed." << endl; 
    cin >> menumain; 
    if (menumain == 2) 

    { 
    return 0; 
    } 
    else 
    { 

    cout << "Player One, please enter your name: " << endl; 
    // cin >> playerone; create char array or string and ask for an input; 


    cout << "Player Two, please enter your name: " << endl; 
    // cin >> playertwo; create char array or string and ask for an input; 


    cout << "Tic tac toe!" << endl; 

    cout << "------------" << endl; 

    cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl; 

    cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl; 

    cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<  endl; 

    cout << " " << endl; 
    cout << playerone << endl; //here replace playerone with char array variable or string; 
    cout << playertwo << endl; //here replace playerone with char array variable or string; 








    while(1) 
    { 


    int playermove; 
    cout << "Choose a number between 1-9 to place!" << endl; 
    cin >> playermove; 

    if (playermove == 1) 
    square1 = oneplayer; 

    else if (playermove == 2) 
    square2 = oneplayer; 
    else if (playermove == 3) 
    square3 = oneplayer; 

    else if (playermove == 4) 
    square4 = oneplayer; 
    else if (playermove == 5) 
    square5 = oneplayer; 
    else if (playermove == 6) 
    square6 = oneplayer; 

    else if (playermove == 7) 
    square7 = oneplayer; 
    else if (playermove == 8) 
    square8 = oneplayer; 
    else if (playermove == 9) 
    square9 = oneplayer; 

    cout << "Tic tac toe!" << endl; 

    cout << "------------" << endl; 

    cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl; 

    cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl; 

    cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<  endl; 

    cout << " " << endl; 
    cout << playerone << endl; 
    cout << playertwo << endl; 





    if (win() == 'X') 
    { 

    cout << playerone << " wins!" << endl; 
     break; 
    } 

    else if (win() == 'O') 
    { 

    cout << playertwo << " wins!" << endl; 
     break; 

    }; 
    toggleplayers(); 
    }; 



    return 0; 
    }; 


    }; 
0

コードには多くの問題があります。実際にあなたが始め、その後ではなくで、ユーザーの入力、を得るのですか

を許可されていない

ユーザー入力。プレーヤーをトグル

は、あなたが彼らの移動のためのプレーヤーを要求する必要があります。

toggleplayers(); 
cout << "Player " << oneplayer << ", enter your move (1 - 9): "; 
cout.flush(); 
cin >> playermove;