2017-06-22 13 views
0
#include <iostream> 


using namespace std; 

int square[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

void board(); 
int check_win(); 
int main() 
{ 
    char mark;//player 1 x player 2 0 
    int player = 1, choice; 
    int i = check_win(); 
    while(i == -1){ 
    board(); 
    player = (player % 2) ? 1 : 2; 
    cout << "Player " << player << " enter a number"; 
    if(player == 1) 
    mark = 'X'; 
    else 
    mark = 'O'; 
    cin >> choice; 
    switch (choice) { 
     case 1: 
     square[1] = (char)(int)mark; 
     break; 
     case 2: 
     square[2] = (char)(int)mark; 
     break; 
     case 3: 
     square[3] = (char)(int)mark; 
     break; 
     case 4: 
     square[4] = (char)(int)mark; 
     break; 
     case 5: 
     square[5] = (char)(int)mark; 
     break; 
     case 6: 
     square[6] = (char)(int)mark; 
     break; 
     case 7: 
     square[7] = (char)(int)mark; 
     break; 
     case 8: 
     square[8] = (char)(int)mark; 
     break; 
     case 9: 
     square[9] = (char)(int)mark; 
     break; 
     default: 
     cout << "None of these"; 
     break; 
    } 
    i = check_win(); 
    player++; 
    } 
    board(); 
    if(i == 1) cout << "player " << --player << "win"; 
    else{ 
    cout << "Game DRAW"; 
    } 
    cin.get(); 
    return 0; 
} 

int check_win() 
{ 
    if (square[1] == square[2] && square[2] == square[3]) 

    return 1; 
    else if (square[4] == square[5] && square[5] == square[6]) 

    return 1; 
    else if (square[7] == square[8] && square[8] == square[9]) 

    return 1; 
    else if (square[1] == square[4] && square[4] == square[7]) 

    return 1; 
    else if (square[2] == square[5] && square[5] == square[8]) 

    return 1; 
    else if (square[3] == square[6] && square[6] == square[9]) 

    return 1; 
    else if (square[1] == square[5] && square[5] == square[9]) 

    return 1; 
    else if (square[3] == square[5] && square[5] == square[7]) 

    return 1; 

    else 
    return -1; 
} 


void board() 
{ 
    cout << "\n\n\tTic Tac Toe\n\n"; 
    cout << "Player 1 (X) - Player 2 (O)" << endl << endl; 
    cout << endl; 
    cout << "  |  |  " << endl; 
    cout << " " << square[1] << " | " << square[2] << " | " 
    << square[3] << endl; 
    cout << "_____|_____|_____" << endl; 
    cout << "  |  |  " << endl; 
    cout << " " << square[4] << " | " << square[5] << " | " << 
    square[6] << endl; 
    cout << "_____|_____|_____" << endl; 
    cout << "  |  |  " << endl; 
    cout << " " << square[7] << " | " << square[8] << " | " 
    << square[9] << endl; 
    cout << "  |  |  " << endl << endl; 
} 

ご覧のとおり、ゲーム自体のコードは完成しましたが、少し問題があります。 5のような数字を選択した場合、をXまたはOとすると、代わりに8879が使用されますが、解決方法はありますか? マークはXOのためのメモリを保持するchar変数ですが、ここでは関数で対話していると思います。 squareTicTacToeコードは数字をOとXに置き換えていません

square[5] = (char)(int)mark; 

+0

を書きます{square [choice] = mark;} else {cout << "これらのどれもありません";} '。 – Baldrickk

答えて

0

問題は、あなたが数字

int square[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 

ないchar

char square[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 

とあなたがX(またはO)を保存を暗記ということです、あなたは最初に変換charint88X)に、そしてcharX88)と、に最後、あなたは(再びX88に)intでそれを覚えます。

提案: `場合(選択> 0 &&選択<= 9):{...`あなただけ行うことができます代わりに `スイッチ(選択)のcharssquare配列を使用し、単に

square[5] = mark; 
関連する問題