Tic Tac Toe

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

Make a two player tic tac toe game.

★ Modify the program so that it will announce when a player has won the game (and which player won, x or o)

★★ Modify the program so that it is a one player game against the computer (with the computer making its moves randomly)

★★★★ Modify the program so that anytime the player is about to win (aka, they have 2 of 3 x's in a row, the computer will block w/ an o)

I wrote most of the program all ready. I am a beginner, so I had to do a lot of research to make this. It is not near to completion just yet. I wrote a function int ImputMove (int move, bool p, char b[]), that imputs the move into the array, int CheckMove (int move, bool p, char a[]), that checks to see if a player has won yet, and void ShowBoard(), that shows the board before the user imputs their position. I am not sure how this all connects to make the tic tac toe game. These were the features that most people included in their c++ tic tac toe games. I am little lost on where to go with this code, even though I am close to being finished. I'd appreciate some divine guidance lol! thanks a lot :)


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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Tic Tac Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

//Declare Global Variables
string Board[9] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};

//Declare Functions
void ShowBoard();
int ImputMove (int move, bool p, char b[]);
int CheckMove (int move, bool p, char a[]);

int _tmain(int argc, _TCHAR* argv[])
{
	//Declare local Variables
	string Player_1_Name;
	string Player_2_Name;
	int Move;
	bool result = 1;

	//Imput player names
	cout << "Player 1: Please enter your name.\n";
	cin >> Player_1_Name;
	cout << "Player 2: Please enter your name.\n";
	cin >> Player_2_Name;

	//Show board
	ShowBoard();

	// Tell which player to move
	while(result == true)
	{
		cout << Player_1_Name << ": It's your move." << endl;
		cout << "Enter the number of the spot where you'd like to move." << endl;
		cin >> Move;

		cout << Player_2_Name << ": It's your move." << endl;
		cout << "Enter the number of the spot where you'd like to move." << endl;
		cin >> Move;
		
		ImputMove (int move, bool a, char b[]);
		CheckMove (int move, bool b, char a[]);

		result = CheckMove (int move, bool b, char a[]);
	}
		
	system("PAUSE");
	return 0;
}

void ShowBoard()
{
	cout << endl;
	cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
	cout << "--|---|--" << endl;
	cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
	cout << "--|---|--" << endl;
	cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
	cout << endl;
}

int ImputMove (int move, bool p, char b[])
{
	if(p == true)
	{
		switch(move)
		{
		case 1:
			b[0] = 'X';
			break;
		case 2:
			b[1] = 'X';
			break;
		case 3:
			b[2] = 'X';
			break;
		case 4:
			b[3] = 'X';
			break;
		case 5:
			b[4] = 'X';
			break;
		case 6:
			b[5] = 'X';
			break;
		case 7:
			b[6] = 'X';
			break;
		case 8:
			b[7] = 'X';
			break;
		case 9:
			b[8] = 'X';
			break;
		default:
			cout << "Invalid Move!\n";
			break;
		}
	}
	
	else
	{

		switch(move)
		{
		case 1:
			b[0] = 'O';
			break;
		case 2:
			b[1] = 'O';
			break;
		case 3: 
			b[2] = 'O';
			break;
		case 4:
			b[3] = 'O';
			break;
		case 5:
			b[4] = 'O';
			break;
		case 6:
			b[5] = 'O';
			break;
		case 7:
			b[6] = 'O';
			break;
		case 8:
			b[7] = 'O';
			break;
		case 9:
			b[8] = 'O';
			break;
		default:
			cout << "Invalid Move !\n";
			break;
		}
	}
}

int CheckMove (int move, bool p, char a[])
{
	if(p== false)
	{
		if((a[0] == 'X' && a[1] == 'X' && a[2] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[3] == 'X' && a[4] == 'X' && a[5] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[6] == 'X' && a[7] == 'X' && a[8] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[0] == 'X' && a[3] == 'X' && a[6] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[1] == 'X' && a[4] == 'X' && a[7] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[2] == 'X' && a[5] == 'X' && a[8] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[0] == 'X' && a[4] == 'X' && a[8] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[2] == 'X' && a[4] == 'X' && a[6] == 'X'))
		{
			cout << "PLAYER 1 WINS" << endl;
			return 0;
		}
		if((a[0] == 'O' && a[1] == 'O' && a[2] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[3] == 'O' && a[4] == 'O' && a[5] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[6] == 'O' && a[7] == 'O' && a[8] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[0] == 'O' && a[3] == 'O' && a[6] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[1] == 'O' && a[4] == 'O' && a[7] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[2] == 'O' && a[5] == 'O' && a[8] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[0] == 'O' && a[4] == 'O' && a[8] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
		if((a[2] == 'O' && a[4] == 'O' && a[6] == 'O'))
		{
			cout << "PLAYER 2 WINS" << endl;
			return 0;
		}
	}
}
You should really consider re-coding checkMove() and inputMove(). You could really turn this game into something around 70 lines of code.

checkMove() could be turned into something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool checkWin(const char board[3][3], const char p1, const char p2) {
    for(int l = 0; l < 2; ++l) {
        const char cP = (l == 0?p1:p2); // currentPlayer
	for(int i = 0; i < 3; ++i) {
	    for(int x = 0; x < 3; ++x) {
	        if(board[i][0] == cP && board[i][x+1] == cP && board[i][x+2] == cP)
		    return true;
		else if(board[i][x] == cP && board[i+1][x] == cP && board[i+2][x] == cP)
		    return true;
            }
        }
    }
    return false;
}
Loops can really make your program much more efficient.

The same goes for your InputMove() function, which could be turned into about 12 lines with two for loops. And I know, this doesn't answer your question, I just hate seeing people hard-code things like this.
Last edited on
layout of main.cpp(according to my program)

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
Declare variables
->data of type int to store input, winner
->data of type boolean to store playerOne's turn,
  playersTwo's turn and the entire Games turn
playerOne and Game should by defualt be set to true(Playertwo obviously false)
create the char map
DECLARATION ENDS
	set the map to blank spaces(i used '*' for mine)
	clear the screen(keeps the program organized, simply cosmetic)
	print the clear map
	WHILE game is TRUE
		WHILE its playerone's turn and the game is true
			prompt user for input
			read in input to variable declared above
			move player according to input
			clear screen
			display map
			check if game is still true(put in a function that returns type BOOL)
			playerTwo's turn becomes TRUE
			playerOne's turn becomes FALSE
			Current Winner is playerOne(if the loop exits, the variable will return
			the value of the last player to go, which is currently playerone)
		WHILE its playertwo's turn and the game is true
			prompt user for input
			read in input to variable decaled above
			move player according to input
			clear screen
			display map
			check if game is still true(put in a function that returns type BOOL)
			playerOne's turn becomes TRUE
			playerTwo's turn becomes FALSE
			Current Winner is playerTwo(if the loop exits, the variable will return
			the value of the last player to go, which is currently playerTwo)
	END main game loop(someone wins)
handle winner variable to display winner to user


EDIT: to clarify, here are the function headers i used for my program. Hopefully this can help with my explanation:
1
2
3
4
5
6
7
bool Checkwin(char board[][3]);
bool CatsEye(char board[][3]);
void printMap(char Map[][3]);
void Movep(int Input, char Map[][3], char Player); //ex for playerone- Movep(input, Map, 'x') //playerone and two use the same function, since the only difference is a char
void cls(); //windows function to clear screen
void Controls(int r, int c); //takes rows and columns as params
bool PlayAgain(int);


Tell me if this helps at all, i wrote my own TicTacToe program awhile back. PM me if you really need a good explanation, i struggled on this program for awhile also and understand your pain.
Last edited on
@Need4Sheep/xhtmlx

Thanks for your guys help! Sorry I have not replied, iv'e been very busy. I like your idea of mapping out the functions of the code in words before coding the program Need4Sheep!

@xhtmlx

Hey! Thanks for the abbreviated code. I was hard coding a lot. I am having a difficult time understanding the coding. Here are my questions:

What is board[3][3]? I understand that it refers to an array, but why is there two [][]? I made the board, board[9], since there are 8 different elements.

Why did you make the integer name '1' (the number), because the code has for(1=0....). 1 is never equal to 0 lol?

I dont understand this part:

1
2
3
4
5
6
for(int i = 0; i < 3; ++i) {
	    for(int x = 0; x < 3; ++x) {
	        if(board[i][0] == cP && board[i][x+1] == cP && board[i][x+2] == cP)
		    return true;
		else if(board[i][x] == cP && board[i+1][x] == cP && board[i+2][x] == cP)
		    return true;


I'd really appreciate your help!
Board[3][3] is a two-dimensional array, think of it like a matrix in math. The standard array:
Arr[4] = [a b c d];
two dimensional array:
1
2
3
4
5
Arr[4][4] = 
[a b c d]    //4 columns and 4 rows
[e f g h] 
[i j k l]
[m n o p]


A two dimensional array helps make the tic tac toe program easier to understand and write. I would try refreshing your memory/learning about these arrays
http://www.fredosaurus.com/notes-cpp/arrayptr/22twodim.html

EDIT: when xhtmlx typed that code he must have made a mistake, he might have meant to use the letter l instead of 1
Last edited on
@Need4Sleep wrote:
EDIT: when xhtmlx typed that code he must have made a mistake, he might have meant to use the letter l instead of 1


This reinforces the idea of not using i & j for array subsripts, when Row & Col are much better names, I have seen this happen lots of times, using good names helps avoid these problems - and will save you one day.

A lower case ell is a particulary bad choice of variable name, because of it's confusion with the number one, or the letter i.

I don't mean to criticise the code in any other way.
Topic archived. No new replies allowed.