### Tic Tac Toe game - Functions

hi huys,
so i'm working on this project - tic tac toe game for couple of days, and i have two problems:
first thing first, and most importantly, i cant figure out how to "move" the if's that check for a win to a function.
right now the code is working, but too messy.

second thing is that the user can enter an input to a cell that already been accupated.

i'd love to hear your advices! this is mt first program ever :)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200`` ``````//tic tac toe game #include 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"); }``````
Last edited on
tictactoe.cpp
 ``12345678910`` ``````#include #include "tictactoe.h" int main() { printMatrix(matrix); //printing the matrix playersTurns(); if (win == false) { //displaying game status for a draw std::cout << "it's a tie" << std::endl; } }``````

tictactoe.h
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183`` ``````#ifndef tictactoe # include int const SIZE = 4; 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; void printMatrix(char matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { std::cout << matrix[i][j] << "\t"; } std::cout << std::endl << std::endl; } } int checkWinMatrix(char matrix[SIZE][SIZE]) { bool win = false; for (int i = 1; i <= SIZE; i++) { for (int j = 1; j <= 1; j++) { //checks here if ((matrix[3][1] == 'X' && matrix[3][2] == 'X' && matrix[3][3] == 'X') || (matrix[2][1] == 'X' && matrix[2][2] == 'X' && matrix[2][3] == 'X') || (matrix[1][3] == 'X' && matrix[2][2] == 'X' && matrix[3][1] == 'X') || (matrix[1][1] == 'X' && matrix[1][2] == 'X' && matrix[1][3] == 'X') || (matrix[1][1] == 'X' && matrix[2][2] == 'X' && matrix[3][3] == 'X') || (matrix[1][1] == 'X' && matrix[2][1] == 'X' && matrix[3][1] == 'X') || (matrix[1][2] == 'X' && matrix[2][2] == 'X' && matrix[3][2] == 'X') || (matrix[1][3] == 'X' && matrix[2][3] == 'X' && matrix[3][3] == 'X')) { std::cout << "X wins" << std::endl; win = true; } if ((matrix[1][3] == 'O' && matrix[2][2] == 'O' && matrix[3][1] == 'O') || (matrix[1][1] == 'O' && matrix[2][2] == 'O' && matrix[3][3] == 'O') || (matrix[1][1] == 'O' && matrix[1][2] == 'O' && matrix[1][3] == 'O') || (matrix[2][1] == 'O' && matrix[2][2] == 'O' && matrix[2][3] == 'O') || (matrix[3][1] == 'O' && matrix[3][2] == 'O' && matrix[3][3] == 'O') || (matrix[1][1] == 'O' && matrix[2][1] == 'O' && matrix[3][1] == 'O') || (matrix[1][2] == 'O' && matrix[2][2] == 'O' && matrix[3][2] == 'O') || (matrix[1][3] == 'O' && matrix[2][3] == 'O' && matrix[3][3] == 'O')) { std::cout << "O wins" << std::endl; win = true; } } return (win); } } void playersTurns() { for (k = 1; k <= 9 && !win; k++) //before max amount of turns & not win { /* Player selection */ //player x if (isX == true) { std::cout << "Player number 1 (X): " << std::endl; std::cout << "Please enter cell input (for example A2): "; std::cin >> ch >> n; std::cout << std::endl; switch(ch){ case 'A': { switch(n) { case 1: matrix[1][1] = 'X'; break; case 2: matrix[2][1] = 'X'; break; case 3: matrix[3][1] = 'X'; break; } } break; case 'B': { switch(n) { case 1: matrix[1][2] = 'X'; break; case 2: matrix[2][2] = 'X'; break; case 3: matrix[3][2] = 'X'; break; } } break; case 'C': { switch(n) { case 1: matrix[1][3] = 'X'; break; case 2: matrix[2][3] = 'X'; break; case 3: matrix[3][3] = 'X'; break; } } break; default: std::cout <<"no input detected"<< std::endl; break; }; printMatrix(matrix); } //playyer y if (isX == false) { win = checkWinMatrix(matrix); std::cout << "Player number 2 (O): " << std::endl; std::cout << "Please enter cell input (for example A2): "; std::cin >> ch >> n; std::cout << std::endl; switch(ch){ case 'A': { switch(n) { case 1: matrix[1][1] = 'O'; break; case 2: matrix[2][1] = 'O'; break; case 3: matrix[3][1] = 'O'; break; } } break; case 'B': { switch(n) { case 1: matrix[1][2] = 'O'; break; case 2: matrix[2][2] = 'O'; break; case 3: matrix[3][2] = 'O'; break; } } break; case 'C': { switch(n) { case 1: matrix[1][3] = 'O'; break; case 2: matrix[2][3] = 'O'; break; case 3: matrix[3][3] = 'O'; break; } } break; default: std::cout <<"no input detected"<< std::endl; break; }; printMatrix(matrix); } isX = !isX; win = checkWinMatrix(matrix); } } #else #endif ``````

Best i could do but I've only been coding since May!
Last edited on
Thank you so much!
but why did you split the code?
how can i use it with one big cpp code?
Hello billboard100,

Although not the proper way to do this as the ".h" file should be a ".cpp" file. Compiled separately and linked together at the end.

When the compiler runs it will put the code of the ".h" file at the beginning of "main" and to the compiler it would appear as one big file of code.

Hope that helps,

Andy
thanks! I understand now :)
Yeah my b, to compile them separately and then to link together reminds me of a proper Makefile :p i shouldve known
Last edited on
Here's a function (untested) that returns the status of a 3x3 board:
[ edit: AbstractAnon points out below that there's a bug in this code. ]
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````// Return the state of the board: // 'X' means X won. // 'O' means O won. // 'F' means no winner and the board is full (aka a tie) // ' ' means the no winner and the board is not full (aka keep playing) char boardState(char board[3][3]) { unsigned row, col; // Winner in row? for (row=0; row<3; ++row) { if (board[row][0] == board[row][1] && board[row][0] == board[row][2]) { return board[row][0]; } } // Winner in column? for (col=0; col<3; ++col) { if (board[0][col] == board[1][col] && board[0][col] == board[2][col]) { return board[0][col]; } } // Winner on diagonals? if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) { return board[0][0]; } if (board[2][2] == board[1][1] && board[2][2] == board[0][2]) { return board[2][2]; } // No winner. Is it full? for (row=0; row<2; ++row) { for (col=0; col<2; ++col) { if (board[row][col] == ' ') return ' '; } } return 'F'; }``````

Last edited on
@dhayden:
Your check for the reverse diagonal is incorrect. It should be checking the lower left corner.
 ``1234`` `````` if (board[2][0] == board[1][1] && board[2][0] == board[0][2]) { return board[2][0]; }``````

Last edited on
thx guys this is good brain food :D