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 :)

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");
}
Last edited on
tictactoe.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#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
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
#ifndef tictactoe
# include <iostream>
    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. ]
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
// 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.
1
2
3
4
    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
Topic archived. No new replies allowed.