Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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;
- break;
- }
- 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");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement