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
とすると、代わりに88
と79
が使用されますが、解決方法はありますか? マークはX
とO
のためのメモリを保持するchar変数ですが、ここでは関数で対話していると思います。 square
TicTacToeコードは数字をOとXに置き換えていません
square[5] = (char)(int)mark;
で
を書きます{square [choice] = mark;} else {cout << "これらのどれもありません";} '。 – Baldrickk