Skip to content

Instantly share code, notes, and snippets.

@vianneynara
Last active December 10, 2022 16:32
Show Gist options
  • Save vianneynara/a67a7012e7719d61b2ecffef6f7388e4 to your computer and use it in GitHub Desktop.
Save vianneynara/a67a7012e7719d61b2ecffef6f7388e4 to your computer and use it in GitHub Desktop.
A simple very tic tac toe program built using c++
#include <iostream>
using namespace std;
char table[3][3] = {};
int checkTable(int sign) {
int result = 0;
if (
table[0][0] == sign && table[0][1] == sign && table[0][2] == sign ||
table[1][0] == sign && table[1][1] == sign && table[1][2] == sign ||
table[2][0] == sign && table[2][1] == sign && table[2][2] == sign ||
table[0][0] == sign && table[1][0] == sign && table[2][0] == sign ||
table[0][1] == sign && table[1][1] == sign && table[2][1] == sign ||
table[0][2] == sign && table[1][2] == sign && table[2][2] == sign ||
table[0][0] == sign && table[1][1] == sign && table[2][2] == sign ||
table[0][1] == sign && table[1][1] == sign && table[0][2] == sign
) result = 1;
return result;
}
void updateTable(int row, int col, char isi) {
table[row][col] = isi;
}
void drawTable() {
cout << " 1 2 3" << endl;
for (int i = 0; i < 3; i++) {
cout << " " << (i + 1) << " ";
for (int j = 0; j < 3; j++) printf("[%c]", table[i][j]);
cout << endl;
}
}
int main() {
int row, col;
char sign;
cout << "===================" << endl << "Simple Tic Tac Toe" << endl << "===================" << endl;
drawTable();
for (int i = 1; i <= 9; i++) {
if (i % 2 != 0) {
sign = 'X';
do {
cout << "Player 1: ";
do {
cin >> row >> col;
if (!(row >= 1 && row <= 3 || col >= 1 && col <= 3)) cout << ">>Please insert the correct coordinates (1-3, 1-3)!" << endl;
} while (!(row >= 1 && row <= 3 || col >= 1 && col <= 3));
row--; col--;
if (table[row][col] == 'X' || table[row][col] == 'O') printf("That box has been filled!\n");
} while (table[row][col] == 'X' || table[row][col] == 'O');
updateTable(row, col, sign);
if (checkTable(sign)) {
drawTable();
cout << ">>>Player 1 has won the round!" << endl; return 0;
}
}
else {
sign = 'O';
do {
cout << "Player 2: ";
do {
cin >> row >> col;
if (!(row >= 1 && row <= 3 || col >= 1 && col <= 3)) cout << "Please insert the correct coordinates (1-3,1-3)!" << endl;
} while (!(row >= 1 && row <= 3 || col >= 1 && col <= 3));
row--; col--;
if (table[row][col] == 'X' || table[row][col] == 'O') printf("That box has been filled!\n");
} while (table[row][col] == 'X' || table[row][col] == 'O');
updateTable(row, col, sign);
if (checkTable(sign)) {
drawTable();
cout << ">>>Player 2 has won the round!" << endl; return 0;
}
}
cout << endl;
drawTable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment