1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
#define FIELDSIZE 3
#define EMPTY ' '
#define CROSS 'X'
#define NAUGHT 'O'
using namespace std;
class PlayingField {
private:
char field[FIELDSIZE][FIELDSIZE];
public:
PlayingField() {
for(int i = 0; i < FIELDSIZE; i++)
for(int j = 0; j < FIELDSIZE; j++)
field[i][j] = EMPTY;
};
void startGame() {
this->printField();
this->gameCycle();
};
void gameCycle() {
int emptyFieldCounter = FIELDSIZE*FIELDSIZE;
int x, y;
char state = CROSS;
while(emptyFieldCounter > 0) {
cout << state << "' turn! Enter the coordinates, plz!" << endl;
while (x < 1 || x >= FIELDSIZE + 1 || y < 1 || y >= FIELDSIZE + 1) {
cout << "x: ";
cin >> x;
cout << "y: ";
cin >> y;
}
x--;
y--;
if (isEmpty(x, y)) {
if (state == CROSS) {
setCell(x, y, CROSS);
state = NAUGHT;
emptyFieldCounter--;
}
else {
setCell(x, y, NAUGHT);
state = CROSS;
emptyFieldCounter--;
}
if (winTest()) {
this->printField();
break;
}
}
else {
cout << " " << endl;
cout << " This cell is full!!! " << endl;
}
this->printField();
x = 0;
y = 0;
}
};
bool winTest() {
bool result;
if (field[0][0] == field[0][1] && field[0][0] == field[0][2] && field[0][0] != EMPTY)
result = true;
else if (field[1][0] == field[1][1] && field[1][0] == field[1][2] && field[1][0] != EMPTY)
result = true;
else if (field[2][0] == field[2][1] && field[2][0] == field[2][2] && field[2][0] != EMPTY)
result = true;
else if (field[0][0] == field[1][0] && field[0][0] == field[2][0] && field[0][0] != EMPTY)
result = true;
else if (field[0][1] == field[1][1] && field[0][1] == field[2][1] && field[0][1] != EMPTY)
result = true;
else if (field[0][2] == field[1][2] && field[0][2] == field[2][2] && field[0][2] != EMPTY)
result = true;
else if (field[0][0] == field[1][1] && field[0][0] == field[2][2] && field[0][0] != EMPTY)
result = true;
else if (field[0][2] == field[1][1] && field[0][2] == field[2][0] && field[0][2] != EMPTY)
result = true;
else
result = false;
cout << " WIN!!! " << endl;
return result;
};
void setCell(int x, int y, int value) {
field[x][y] = value;
};
bool isEmpty(int x, int y) {
if (field[x][y] == EMPTY)
return true;
return false;
};
void printField() {
cout << " " << endl;
cout << " *** TIC TAC TOE, v. 0.1 ***" << endl;
cout << " === ===" << endl;
cout << " === " << field[0][0] << " " << field[0][1] << " " << field[0][2] << " ===" << endl;
cout << " === ===" << endl;
cout << " === " << field[1][0] << " " << field[1][1] << " " << field[1][2] << " ===" << endl;
cout << " === ===" << endl;
cout << " === " << field[2][0] << " " << field[2][1] << " " << field[2][2] << " ===" << endl;
cout << " === ===" << endl;
cout << " ===========================" << endl;
cout << " " << endl;
};
};
int main(int argc, char** argv) {
PlayingField pf;
pf.startGame();
return 0;
};
|