Untitled
unknown
c_cpp
a year ago
2.0 kB
16
Indexable
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using std::string;
using namespace std;
bool checkWin(string b[3], char player) {
for (int i = 0; i < 3; ++i) { // Check rows
if (b[i][0] == player && b[i][1] == player && b[i][2] == player)
return true;
}
for (int j = 0; j < 3; ++j) { // Check columns
if (b[0][j] == player && b[1][j] == player && b[2][j] == player)
return true;
}
// Check diagonals
if (b[0][0] == player && b[1][1] == player && b[2][2] == player)
return true;
if (b[0][2] == player && b[1][1] == player && b[2][0] == player)
return true;
return false;
}
int main() {
string b[3];
while (cin >> b[0] >> b[1] >> b[2]) {
int cX = 0, cO = 0;
bool hasEmpty = false;
// Count number of X's, O's and check for empty spaces
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (b[i][j] == 'X') {
cX++;
} else if (b[i][j] == 'O') {
cO++;
} else if (b[i][j] == '.') {
hasEmpty = true;
}
}
}
// Determine game state
bool xWins = checkWin(b, 'X');
bool oWins = checkWin(b, 'O');
//if ((xWins && oWins) || (cX+1 < cO) || (cO+1 < cX)) { ?if 1st is 'O'??
if ((xWins && oWins) || (xWins && cX != cO + 1) || (oWins && cX != cO) || (cX < cO) || (cX > cO + 1)){
cout << "Cheating!" << endl;
} else if (xWins) {
cout << "X win" << endl;
} else if (oWins) {
cout << "O win" << endl;
} else if (!hasEmpty) {
cout << "Tie" << endl;
} else {
cout << "Not yet" << endl;
}
}
return 0;
}Editor is loading...
Leave a Comment