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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
//tic tac toe game
#include <iostream>
using namespace std;
int const SIZE = 4;
void printMatrix(char matrix[SIZE][SIZE]);
void printMatrix(char matrix[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl << endl;
}
}
int main() {
char matrix[SIZE][SIZE]{ { ' ' ,'A','B','C' },{ '1',' ' },{ '2',' ' },{ '3',' ' } };
int i, j, k;
char ch; //A,B or C
int n; // 1,2 or 3
bool win = false, isX = true;
printMatrix(matrix); //printing the matrix
for (k = 1; k <= 9 && !win; k++) {
//displaying game status
if (matrix[1][3] == 'X' && matrix[2][2] == 'X' && matrix[3][1] == 'X') {
cout << " X wins!" << endl;
win = true;
}
else if (matrix[1][3] == 'O' && matrix[2][2] == 'O' && matrix[3][1] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[1][1] == 'X' && matrix[2][2] == 'X' && matrix[3][3] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[1][1] == 'O' && matrix[2][2] == 'O' && matrix[3][3] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[1][1] == 'X' && matrix[1][2] == 'X' && matrix[1][3] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[1][1] == 'O' && matrix[1][2] == 'O' && matrix[1][3] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[2][1] == 'X' && matrix[2][2] == 'X' && matrix[2][3] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[2][1] == 'O' && matrix[2][2] == 'O' && matrix[2][3] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[3][1] == 'X' && matrix[3][2] == 'X' && matrix[3][3] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[3][1] == 'O' && matrix[3][2] == 'O' && matrix[3][3] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[1][1] == 'X' && matrix[2][1] == 'X' && matrix[3][1] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[1][1] == 'O' && matrix[2][1] == 'O' && matrix[3][1] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[1][2] == 'X' && matrix[2][2] == 'X' && matrix[3][2] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[1][2] == 'O' && matrix[2][2] == 'O' && matrix[3][2] == 'O') {
cout << " O wins!" << endl;
win = true;
break;
}
if (matrix[1][3] == 'X' && matrix[2][3] == 'X' && matrix[3][3] == 'X') {
cout << " X wins!" << endl;
win = true;
break;
}
else if (matrix[1][3] == 'O' && matrix[2][3] == 'O' && matrix[3][3] == 'O') {
cout << " X wins!" << endl;
win = true;
break;
}
if (isX == true) {
cout << "Player number 1 (X): " << endl;
cout << "Please enter cell input (for example A2): ";
cin >> ch >> n;
cout << endl;
if (ch == 'A' && n == 1) {
matrix[1][1] = 'X';
}
if (ch == 'A' && n == 2) {
matrix[2][1] = 'X';
}
if (ch == 'A' && n == 3) {
matrix[3][1] = 'X';
}
if (ch == 'B' && n == 1) {
matrix[1][2] = 'X';
}
if (ch == 'B' && n == 2) {
matrix[2][2] = 'X';
}
if (ch == 'B' && n == 3) {
matrix[3][2] = 'X';
}
if (ch == 'C' && n == 1) {
matrix[1][3] = 'X';
}
if (ch == 'C' && n == 2) {
matrix[2][3] = 'X';
}
if (ch == 'C' && n == 3) {
matrix[3][3] = 'X';
}
printMatrix(matrix); //printing the matrix
}
if (isX == false) {
cout << "Player number 2 (O): " << endl;
cout << "Please enter cell input (for example A2): ";
cin >> ch >> n;
cout << endl;
if (ch == 'A' && n == 1) {
matrix[1][1] = 'O';
}
if (ch == 'A' && n == 2) {
matrix[2][1] = 'O';
}
if (ch == 'A' && n == 3) {
matrix[3][1] = 'O';
}
if (ch == 'B' && n == 1) {
matrix[1][2] = 'O';
}
if (ch == 'B' && n == 2) {
matrix[2][2] = 'O';
}
if (ch == 'B' && n == 3) {
matrix[3][2] = 'O';
}
if (ch == 'C' && n == 1) {
matrix[1][3] = 'O';
}
if (ch == 'C' && n == 2) {
matrix[2][3] = 'O';
}
if (ch == 'C' && n == 3) {
matrix[3][3] = 'O';
}
printMatrix(matrix); //printing the matrix
}
isX = !isX;
}
if (win == false) { //desplaying game status for a draw
cout << "it's a tie" << endl;
}
system("pause");
}
| |