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
它不會改變5
與X
或O
,它使用88
和79
而不是我如何解決它? Mark是char變量,它保存着X
和O
的內存,但我認爲它已被這裏的函數轉換。TicTacToe代碼不是用O和X代替數字
相反開關(選擇)的'{...'你可能只是做:'如果(選擇> 0 &&選擇<= 9) {square [choice] = mark;} else {cout <<「沒有這些」;}'。 – Baldrickk