My most ambitous project till today - Tic Tac Toe (suggestions & insights)

Hi,

I am not really sure how long i have been learning the c++ programming language but it is not more than 3 weeks.
I have started to read a book named: Michael Dawson - Beginning C++ Through Game Programming .. i am at chapter 6/10 for now.
.. and here is a 'quote' from the book "This game is your most ambitious project yet. You certainly have all the skills
you need to create it...
" So iv stopped reading here and decided to check how much i have learned, did i consumed everything, and i decided to start visual c++ and i began coding the Tic Tac Toe game on my own (without looking at all at the book example or google)
The first time i failed due the BAD very bad pseudocode. I got pi**ed and i decided to start from the beginning. And luckily here i am done with the game. I got to say i am little proud of my self it feels good :$ :), though i am worried...

When i compared the source code of my game and the books example the difference between the lines is allot bigger! .. The author of the book managed to write the game in 269 lines.. and mine is amazingly 623 :S
This is the biggest reason i am writing this post.. i am afraid to caught bad c++ programming habits (i don't want that at all) every suggestion you got to offer please do!

After i compared my code with the code from the book i noticed that i could represent the board with a single vector where i used multidimansional vector instead... and i can notice how he saved so much lines by using that.. he could loop trough single vectors very easily and check for wins or ties etc.. so my point is.. even though i got the idea from his example i couldn't improve my code significantly ;( just because i didn't know how i can implement his ideas without reconstructing the whole program (mostly was because i was using multidimensional vrctor and he was using only vector to represent the board)...

Anyways... i as well will very much appreciate if you have concrete ideas on how this code can be improved.

This is my Tic Tac Toe code:
 
//The code is too long for one post so i will represent it in multiple comments bellow.. 

edit: here is link for the full souce code if any1 feels like beating my amazingly strong AI who threatens humanity :P
source code: http://www.mediafire.com/?w6icm9piqcciqap
compiled (setup file): http://www.mediafire.com/?ys8b3op5ujq3u49
Last edited on
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
// TIC TAC TOE - Second Attempt - success ;)
#include <iostream>	
#include <vector>
#include <string>
using namespace std;

void instructions();
bool doYouWantToPlayFirst(string question, char& humanPiece, char& computerPiece);
void generateEmptyBoard(vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
void getHumanMove(int& humanMove, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
void getComputerMove(const int& humanMove, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
bool legalMoves(string& tie, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
bool ifHumanWinner(char& winner, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
bool ifComputerWinner(char& winner, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
void printTable(vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y);
void switcher(int& var_switcher);

//global consts
const unsigned short MAX_HORI = 3;
const unsigned short MAX_VERT = 3;

//global vars
vector< vector<char> > board(MAX_VERT, MAX_HORI);
char humanPiece;
char computerPiece;
int humanMove;
int computerMove;
int var_switcher;
string occupied;

int main ()
	{
	//main vars
	string tie;
	char winner;
	char doYouWantToPlayAgain;
	instructions();

	do {	
		doYouWantToPlayFirst("Do you want to be first low form of life?? beep beeep....\n.... you hear something from your computers case ..WOW whats that!?? \n\nFirst? <y/n>: ", humanPiece, computerPiece);
		generateEmptyBoard(board, MAX_VERT, MAX_HORI);

		// **** MAIN LOOP ****
		do 
			{
			printTable(board, MAX_VERT, MAX_HORI); // prints the table
			switcher(var_switcher); // swaps the move
			} while ((legalMoves(tie, board, MAX_VERT, MAX_HORI) == false) && (ifHumanWinner(winner, board, MAX_VERT, MAX_HORI) == false) && (ifComputerWinner(winner, board, MAX_VERT, MAX_HORI) == false));
			// ^^^^ MAIN LOOP ^^^^

			printTable(board, MAX_VERT, MAX_HORI);
			if(tie == "yes") // declares TIE
				{
				printTable(board, MAX_VERT, MAX_HORI);
				cout << "\nTIE! Oooh you are worthy opponent! Though you are still low form of live.. \nno offense! ;) \nI challenge you again!!!\n";
				}
			else
				{
				if(ifComputerWinner(winner, board, MAX_VERT, MAX_HORI) == true) // declares a win if computer wins
					{
					cout << "That's right and now your kind awaits full assimilation! Huhaha ...\nrobotic laughs continues...\n...I may give you one more chance.. do you want to play again?!\n";
					}
				if (ifHumanWinner(winner, board, MAX_VERT, MAX_HORI) == true) // declares a win if human wins
					{
					cout << "How was that possible!?!? ... i mean.. i know 1+1=2, or 5*5 = 25, or that the grass is green.. and i know c++ better than you for sure! :confused: ...\nI'll get revenge! BLUE SCREEN OF DEATH IS COMMING SOON!!!\n\nOr just please give me one more chance!!!... \nthough you are still low form of life DON'T FORGET that :)\n";
					}
				}
			cout << endl;
			cout << "Again? <y/n>: "; // asks if you want to play again
			cin >> doYouWantToPlayAgain;
			cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
			doYouWantToPlayAgain=toupper(doYouWantToPlayAgain);
		}while((doYouWantToPlayAgain != 'N') || (doYouWantToPlayAgain == 'Y')); // asks if you want to play again while y/n is entered
	return 0;
	}
1
2
3
4
5
6
7
8
9
10
11
// generates empty board
inline void generateEmptyBoard(vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{
	for (int i=0; i<MAX_HORI; ++i)
		{
		for (int j=0; j<MAX_VERT; ++j)
			{
			vec[i][j]=' ';
			}
		}
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
//print table
inline void printTable(vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{
	cout << endl;
	cout << "\t" << vec[0][0] << " | " << vec[0][1] << " | " << vec[0][2] << endl;
	cout << "\t" << "---------\n";
	cout << "\t" << vec[1][0] << " | " << vec[1][1] << " | " << vec[1][2] << endl;
	cout << "\t" << "---------\n";
	cout << "\t" << vec[2][0] << " | " << vec[2][1] << " | " << vec[2][2] << endl;
	cout << endl;
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// displays the instructions & story
void instructions()
	{
	cout << "\n\t\t\t\tTIC TAC TOE\t\t\t by Masky007";

	cout << "\n--------------------------------------------------------------------------------";
	cout << "How to play the game:\n";

	board[0][0] = '7'; board[0][1] = '8';board[0][2] = '9';
	board[1][0] = '4'; board[1][1] = '5';board[1][2] = '6';
	board[2][0] = '1'; board[2][1] = '2';board[2][2] = '3';
	printTable(board, MAX_VERT, MAX_HORI);
	cout << "\-Above is the board. Every number represents a square accordingly.\n-If you want to take the center you type in 5.\n-If you want to occupie the bottom left corner you type in 1 etc..\n-HINT: Use the numeric keyboard 'its easier to coordinate'.\tGOOD LUCK!";
	cout << "\n--------------------------------------------------------------------------------";
	cout << "Beep err..beep I am your evil computer speaking! Preapre to be invaded,\ni as a supperior form of life and i'll doom your weak race forever!\nTry to defend if you can! You hold the desteny of humanity in your very hands.\nMuhahaha ... err.. beepp..beaap.    Creepy robotic vocies... :)";


	cout << endl << endl << endl;
	} // displays the instructions & story 
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
// asks if you want to play first
bool doYouWantToPlayFirst(string question, char& humanPiece, char& computerPiece)
	{
	char first;
	cout << question;

	do
		{
		cin >> first;
		first = toupper(first);
		if ((first != 'Y') && (first != 'N'))
			{	
			cout << "\nEnter 'y' for YES or 'n' for NO! <y/n>: ";

			}
		}while ((first != 'Y') && (first != 'N'));

		if (first == 'Y')
			{
			cout << "\n\n\n\n\n\nAs you wish week human, i give you the first move! But thats not \nimportant!! I will WIN!!! I MUST.... err beep beeep ..... \nRobotic voice talking...\n\n";
			humanPiece = 'X';
			computerPiece = 'O';
			var_switcher = 2;
			return true;
			}
		if (first == 'N')
			{
			cout << "\nAlright week human i'll have the first move cause you ain't got the brains for \nit... err beep beep...Robotic voice talking!!!\n\n";
			humanPiece = 'O';
			computerPiece = 'X';
			var_switcher = 7;
			return false;
			}
	}
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
// gets the human move
void getHumanMove(string& occupied, int& humanMove, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{

	do 
		{
		cout << "\n\nIzberi pole (1-9): ";
		cin >> humanMove;
		////////////////////////////////////////////////////////////////////////// 7
		if (humanMove == 7)
			if (vec[0][0] == 'X' || vec[0][0] == 'O')
				{
				occupied = "yes";
				cout << "This square is occupied brainless human, try again!";
				}

			else 
				{
				vec[0][0] = humanPiece;
				++var_switcher;
				occupied = "no";
				}
			////////////////////////////////////////////////////////////////////////// 8
			if (humanMove == 8)
				if (vec[0][1] == 'X' || vec[0][1] == 'O')
					{
					occupied = "yes";
					cout << "This square is occupied! Try again, and don't embares yourself this time!";
					}

				else 
					{
					vec[0][1] = humanPiece;
					++var_switcher;
					occupied = "no";
					}
				////////////////////////////////////////////////////////////////////////// 9
				if (humanMove == 9)
					if (vec[0][2] == 'X' || vec[0][2] == 'O')
						{
						occupied = "yes";
						cout << "This square is occupied! Try again, and don't embares yourself this time!";
						}

					else 
						{
						vec[0][2] = humanPiece;
						++var_switcher;
						occupied = "no";
						}
					////////////////////////////////////////////////////////////////////////// 4
					if (humanMove == 4)
						if (vec[1][0] == 'X' || vec[1][0] == 'O')
							{
							occupied = "yes";
							cout << "This square is occupied! Try again, and don't embares yourself this time!";
							}
						else 
							{
							vec[1][0] = humanPiece;
							++var_switcher;
							occupied = "no";
							}
						////////////////////////////////////////////////////////////////////////// 5
						if (humanMove == 5)
							if (vec[1][1] == 'X' || vec[1][1] == 'O')
								{
								occupied = "yes";
								cout << "This square is occupied! Try again, and don't embares yourself this time!";
								}

							else 
								{
								vec[1][1] = humanPiece;
								++var_switcher;
								occupied = "no";
								}
							////////////////////////////////////////////////////////////////////////// 6
							if (humanMove == 6)
								if (vec[1][2] == 'X' || vec[1][2] == 'O')
									{
									occupied = "yes";
									cout << "This square is occupied! Try again, and don't embares yourself this time!";
									}

								else 
									{
									vec[1][2] = humanPiece;
									++var_switcher;
									occupied = "no";
									}
								////////////////////////////////////////////////////////////////////////// 1
								if (humanMove == 1)
									if (vec[2][0] == 'X' || vec[2][0] == 'O')
										{
										occupied = "yes";
										cout << "This square is occupied! Try again, and don't embares yourself this time!";
										}

									else 
										{
										vec[2][0] = humanPiece;
										++var_switcher;
										occupied = "no";
										}
									////////////////////////////////////////////////////////////////////////// 1
									if (humanMove == 2)
										if (vec[2][1] == 'X' || vec[2][1] == 'O')
											{
											occupied = "yes";
											cout << "This square is occupied! Try again, and don't embares yourself this time!";
											}

										else 
											{
											vec[2][1] = humanPiece;
											++var_switcher;
											occupied = "no";
											}

										////////////////////////////////////////////////////////////////////////// 1
										if (humanMove == 3)
											if (vec[2][2] == 'X' || vec[2][2] == 'O')
												{
												occupied = "yes";
												cout << "This square is occupied! Try again, and don't embares yourself this time!";
												}

											else 
												{
												vec[2][2] = humanPiece;
												++var_switcher;
												occupied = "no";
												}
		} while (((humanMove <=0) || (humanMove >= 10)) || (occupied != "no"));
		return;
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// chceks legal moves & determines TIE
bool legalMoves(string& tie, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{
	if ((vec[0][0] != ' ') && (vec[0][1] != ' ' ) && (vec[0][2] != ' ' )
		&& (vec[1][0] != ' ' ) && (vec[1][1] != ' ' ) && (vec[1][2] != ' ' )
		&& (vec[2][0] != ' ' ) && (vec[2][1] != ' ' ) && (vec[2][2] != ' ' ))
		{
		tie = "yes";
		return true;
		}
	else
		{
		tie = "no";
		return false;
		}
	}
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
/* PART ONE */

// calculates the computers move
void getComputerMove(const int& computerMove, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{

	for (int i = 0; i < MAX_VERT; ++i) // winning combinations when computers turn
		{

		if ((vec[i][0] == ' ') && (vec[i][1] == computerPiece) && (vec[i][2] == computerPiece))
			{
			vec[i][0] = computerPiece;
			++var_switcher; return;
			}
		else	if ((vec[i][0] == computerPiece) && (vec[i][1] == ' ') && (vec[i][2] == computerPiece))
			{
			vec[i][1] = computerPiece;
			++var_switcher; return;
			}
		else	if ((vec[i][0] == computerPiece) && (vec[i][1] == computerPiece) && (vec[i][2] == ' '))
			{
			vec[i][2] = computerPiece;
			++var_switcher; return;
			}
		////^^^^ checks the  three column for a winning combination  (horizontal win)
		else	if ((vec[0][i] == ' ') && (vec[1][i] == computerPiece) && (vec[2][i] == computerPiece))
			{
			vec[0][i] = computerPiece;
			++var_switcher; return;
			}
		else	if ((vec[0][i] == computerPiece) && (vec[1][i] == ' ') && (vec[2][i] == computerPiece))
			{
			vec[1][i] = computerPiece;
			++var_switcher; return;
			}
		else	if ((vec[0][i] == computerPiece) && (vec[1][i] == computerPiece) && (vec[2][i] == ' '))
			{
			vec[2][i] = computerPiece;
			++var_switcher; return;
			}		////^^^^ checks the  three rows for a winning combination  (vertical win win)
		}



	if ((vec[0][0] == ' ') && (vec[1][1] == computerPiece) && (vec[2][2] == computerPiece)) //// diagonal wins
		{
		vec[0][0] =computerPiece;
		++var_switcher; return;
		}
	else	if ((vec[0][0] == computerPiece) && (vec[1][1] == ' ') && (vec[2][2] == computerPiece))
		{
		vec[1][1] =computerPiece;
		++var_switcher; return;
		}
	else if ((vec[0][0] == computerPiece) && (vec[1][1] == computerPiece) && (vec[2][2] == ' '))
		{
		vec[2][2] =computerPiece;
		++var_switcher; return;
		}	
	//^^^^ 7,5,3 diagonal wins
	else if ((vec[2][0] == ' ') && (vec[1][1] == computerPiece) && (vec[0][2] == computerPiece)) 
		{
		vec[2][0] =computerPiece;
		++var_switcher; return;
		}
	else	if ((vec[2][0] == computerPiece) && (vec[1][1] == ' ') && (vec[0][2] == computerPiece))
		{
		vec[1][1] =computerPiece;
		++var_switcher; return;
		}
	else	if ((vec[2][0] == computerPiece) && (vec[1][1] == computerPiece) && (vec[0][2] == ' '))
		{
		vec[0][2] =computerPiece;
		++var_switcher; return;
		}
	else
		//^^^^ 1,5,3 diagonal wins				
		for (int i = 0; i < MAX_VERT; ++i) 	 // counters the human next move if its a win ( blocks rows and columns)
			{

			if ((vec[i][0] == ' ') && (vec[i][1] == humanPiece) && (vec[i][2] == humanPiece))
				{
				vec[i][0] = computerPiece;
				cout << "DEBUGING: Computer choice:  Computer choice:  --horizontal block";
				++var_switcher; return;
				}
			else	if ((vec[i][0] == humanPiece) && (vec[i][1] == ' ') && (vec[i][2] == humanPiece))
				{
				vec[i][1] = computerPiece;
				cout << "DEBUGING: Computer choice:  --horizontal block";
				++var_switcher; return;
				}
			else	if ((vec[i][0] == humanPiece) && (vec[i][1] == humanPiece) && (vec[i][2] == ' '))
				{
				vec[i][2] = computerPiece;
				cout << "DEBUGING: Computer choice:  --horizontal block";
				++var_switcher; return;
				}
			////^^^^ checks the  three column for a possible win to BLOCK  (horizontal block)
			else	if ((vec[0][i] == ' ') && (vec[1][i] == humanPiece) && (vec[2][i] == humanPiece))
				{
				vec[0][i] = computerPiece;
				cout << "DEBUGING: Computer choice:  --vertical block";
				++var_switcher; return;
				}
			else	if ((vec[0][i] == humanPiece) && (vec[1][i] == ' ') && (vec[2][i] == humanPiece))
				{
				vec[1][i] = computerPiece;
				cout << "DEBUGING: Computer choice:  --vertical block";
				++var_switcher; return;
				}
			else	if ((vec[0][i] == humanPiece) && (vec[1][i] == humanPiece) && (vec[2][i] == ' '))
				{
				vec[2][i] = computerPiece;
				cout << "DEBUGING: Computer choice:  --vertical block";
				++var_switcher; return;
				}

			}
		////^^^^ checks the  three column for a possible win to BLOCK  (vertical block)

		if ((vec[0][0] == ' ') && (vec[1][1] == humanPiece) && (vec[2][2] == humanPiece)) // counters the human next move if its a win (diagonal blocks)
			{
			vec[0][0] =computerPiece;
			cout << "DEBUGING: Computer choice:  --diagonal block 7,5,3";
			++var_switcher; return;
			}
		else	if ((vec[0][0] == humanPiece) && (vec[1][1] == ' ') && (vec[2][2] == humanPiece))
			{
			vec[1][1] =computerPiece;
			cout << "DEBUGING: Computer choice:  --diagonal block 7,5,3";
			++var_switcher; return;
			}
		else if ((vec[0][0] == humanPiece) && (vec[1][1] == humanPiece) && (vec[2][2] == ' '))
			{
			vec[2][2] =computerPiece;
			cout << "DEBUGING: Computer choice:  --diagonal block 7,5,3";
			++var_switcher; return;
			}
		////^^^^ checks 7,5,3 blocking combinations (diagonal block)
		else	if ((vec[2][0] == ' ') && (vec[1][1] == humanPiece) && (vec[0][2] == humanPiece))
			{
			vec[2][0] =computerPiece;
			cout << "DEBUGING: Computer choice:  --diagonal block 1,5,9";
			++var_switcher; return;
			}
		else	if ((vec[2][0] == humanPiece) && (vec[1][1] == ' ') && (vec[0][2] == humanPiece))
			{
			vec[1][1] =computerPiece;
			cout << "DEBUGING: Computer choice:  --diagonal block 1,5,9";
			++var_switcher; return;
			}
		else	if ((vec[2][0] == humanPiece) && (vec[1][1] == humanPiece) && (vec[0][2] == ' '))
			{
			vec[0][2] =computerPiece;
			cout << "DEBUGING: Computer choice:  --diagonal block 1,5,9";
			++var_switcher; return;
			}
		////^^^^ checks 1,5,9 blocking combinations (diagonal block)

Last edited on
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
/* continues PART TWO */
// calculates the computers move
		if (((vec[1][1] == computerPiece) && (vec[0][0] == computerPiece) && (vec[1][2]) == ' ') && (vec [2][0] == ' ') && (vec[1][0] == ' ')) //creating traps
			{
			vec[1][0] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		else if (((vec[1][1] == computerPiece) && (vec[0][0] == computerPiece) && (vec[0][1]) == ' ') && (vec [0][2] == ' ') && (vec[2][1] == ' ')) 
			{
			vec[0][1] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		/////
		else	if (((vec[1][1] == computerPiece) && (vec[0][2] == computerPiece) && (vec[1][0]) == ' ') && (vec [2][2] == ' ') && (vec[1][2] == ' ')) 
			{
			vec[1][2] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		else	if (((vec[1][1] == computerPiece) && (vec[0][2] == computerPiece) && (vec[0][1]) == ' ') && (vec [2][1] == ' ') && (vec[2][0] == ' ')) 
			{
			vec[0][1] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		/////
		else	if (((vec[1][1] == computerPiece) && (vec[2][2] == computerPiece) && (vec[1][2]) == ' ') && (vec [1][0] == ' ') && (vec[0][0] == ' ')) 
			{
			vec[1][2] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		else	if (((vec[1][1] == computerPiece) && (vec[2][2] == computerPiece) && (vec[2][1]) == ' ') && (vec [0][1] == ' ') && (vec[0][0] == ' ')) 
			{
			vec[2][1] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		/////
		else	if (((vec[1][1] == computerPiece) && (vec[2][0] == computerPiece) && (vec[2][1]) == ' ') && (vec [0][1] == ' ') && (vec[2][2] == ' '))
			{
			vec[2][1] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		else	if (((vec[1][1] == computerPiece) && (vec[2][2] == computerPiece) && (vec[1][0]) == ' ') && (vec [1][2] == ' ') && (vec[0][0] == ' ')) 
			{
			vec[1][0] = computerPiece;
			cout << "DEBUGING: Computer choice:  --creating trap.";
			++var_switcher;return;
			}
		/////

		else if(vec[1][1] == ' ') // take the best possible movie if not occupied and no win or block possible and no trap possible
			{
			vec[1][1] = computerPiece;
			cout << "DEBUGING: Computer choice:  taking best possible move --center.";
			++var_switcher;return;
			}
		else if (vec[0][0] == ' ')
			{
			vec[0][0] = computerPiece;
			cout << "DEBUGING: Computer choice:  taking best possible move --corner.";
			++var_switcher;return;
			}
		else if (vec[0][2] == ' ')
			{
			vec[0][2] = computerPiece;
			cout << "DEBUGING: Computer choice:  taking best possible move --corner.";
			++var_switcher;return;
			}
		else if (vec[2][2] == ' ')
			{
			vec[2][2] = computerPiece;
			cout << "DEBUGING: Computer choice:  taking best possible move --corner.";
			++var_switcher;return;
			}
		else if (vec[2][0] == ' ')
			{
			vec[2][0] = computerPiece;
			cout << "DEBUGING: Computer choice:  taking best possible move --corner.";
			++var_switcher;return;
			}

		else


			for (int i = 0; i < MAX_VERT; ++i) // takes the next possible move if none of the above are possible
				{
				for (int j = 0; j < MAX_HORI; ++j)
					{
					if (vec[i][j] == ' ')
						{
						vec[i][j] = computerPiece;
						cout << "DEBUGING: Computer choice:  taking best possible move if none of the above are possible (last prio).";
						++var_switcher;return;
						}
					}
				}
	}	
1
2
3
4
5
6
7
8
9
10
11
12
// swaps the move (change turns)
inline void switcher(int& var_switcher) // swaps the move
	{
	if (var_switcher % 2 != 0)
		{
		getComputerMove(computerMove, board, MAX_VERT, MAX_HORI);
		} 
	else
		{
		getHumanMove(occupied, humanMove, board, MAX_VERT, MAX_HORI);
		}
	}
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
// if human winner determens win for human
bool ifHumanWinner(char& winner, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{

	//if the human have the 'X' :meaning he played first
	if (humanPiece == 'X')
		{if (//horizontal check if there is a winner
		(vec[0][0] == 'X') && (vec[0][1] == 'X') && (vec[0][2] == 'X') ||
		(vec[1][0] == 'X') && (vec[1][1] == 'X') && (vec[1][2] == 'X') ||
		(vec[2][0] == 'X') && (vec[2][1] == 'X') && (vec[2][2] == 'X') ||
		//vertical check if there is a winner
		(vec[0][0] == 'X') && (vec[1][0] == 'X') && (vec[2][0] == 'X') ||
		(vec[0][1] == 'X') && (vec[1][1] == 'X') && (vec[2][1] == 'X') ||
		(vec[0][2] == 'X') && (vec[1][2] == 'X') && (vec[2][2] == 'X') ||
		//diagonal check if there is a winner
		(vec[0][0] == 'X') && (vec[1][1] == 'X') && (vec[2][2] == 'X') ||
		(vec[2][0] == 'X') && (vec[1][1] == 'X') && (vec[0][2] == 'X'))
		{
		winner = 'h';
		return true;
		}
		else	return false;}

	//if the human have the 'O' :meaning he played second
	if (humanPiece == 'O')
		{if (//horizontal check if there is a winner
		(vec[0][0] == 'O') && (vec[0][1] == 'O') && (vec[0][2] == 'O') ||
		(vec[1][0] == 'O') && (vec[1][1] == 'O') && (vec[1][2] == 'O') ||
		(vec[2][0] == 'O') && (vec[2][1] == 'O') && (vec[2][2] == 'O') ||
		//vertical check if there is a winner
		(vec[0][0] == 'O') && (vec[1][0] == 'O') && (vec[2][0] == 'O') ||
		(vec[0][1] == 'O') && (vec[1][1] == 'O') && (vec[2][1] == 'O') ||
		(vec[0][2] == 'O') && (vec[1][2] == 'O') && (vec[2][2] == 'O') ||
		//diagonal check if there is a winner
		(vec[0][0] == 'O') && (vec[1][1] == 'O') && (vec[2][2] == 'O') ||
		(vec[2][0] == 'O') && (vec[1][1] == 'O') && (vec[0][2] == 'O'))
		{
		winner = 'h';
		return true;
		}
		else	return false;}


	else
		return false;
	}
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
// if computer winner determens win for computer
bool ifComputerWinner(char& winner, vector< vector<char> >& vec, const unsigned short& x, const unsigned short& y)
	{
	//if the comp have the 'X' :meaning he played first
	if (computerPiece == 'X')
		{if (//horizontal check if there is a winner
		(vec[0][0] == 'X') && (vec[0][1] == 'X') && (vec[0][2] == 'X') ||
		(vec[1][0] == 'X') && (vec[1][1] == 'X') && (vec[1][2] == 'X') ||
		(vec[2][0] == 'X') && (vec[2][1] == 'X') && (vec[2][2] == 'X') ||
		//vertical check if there is a winner
		(vec[0][0] == 'X') && (vec[1][0] == 'X') && (vec[2][0] == 'X') ||
		(vec[0][1] == 'X') && (vec[1][1] == 'X') && (vec[2][1] == 'X') ||
		(vec[0][2] == 'X') && (vec[1][2] == 'X') && (vec[2][2] == 'X') ||
		//diagonal check if there is a winner
		(vec[0][0] == 'X') && (vec[1][1] == 'X') && (vec[2][2] == 'X') ||
		(vec[2][0] == 'X') && (vec[1][1] == 'X') && (vec[0][2] == 'X'))
		{
		winner = 'c';
		return true;
		}
		else	return false;}

	//if the comp have the 'O' :meaning he played second
	if (computerPiece == 'O')
		{if (//horizontal check if there is a winner
		(vec[0][0] == 'O') && (vec[0][1] == 'O') && (vec[0][2] == 'O') ||
		(vec[1][0] == 'O') && (vec[1][1] == 'O') && (vec[1][2] == 'O') ||
		(vec[2][0] == 'O') && (vec[2][1] == 'O') && (vec[2][2] == 'O') ||
		//vertical check if there is a winner
		(vec[0][0] == 'O') && (vec[1][0] == 'O') && (vec[2][0] == 'O') ||
		(vec[0][1] == 'O') && (vec[1][1] == 'O') && (vec[2][1] == 'O') ||
		(vec[0][2] == 'O') && (vec[1][2] == 'O') && (vec[2][2] == 'O') ||
		//diagonal check if there is a winner
		(vec[0][0] == 'O') && (vec[1][1] == 'O') && (vec[2][2] == 'O') ||
		(vec[2][0] == 'O') && (vec[1][1] == 'O') && (vec[0][2] == 'O'))
		{
		winner = 'c';
		return true;
		}
		else	return false;}
	}
this is the end finally!
... i think it wasn't very polite of me by posting so many replys :S bah.. sorry for that.

Thanks for everything though!
Last edited on
Topic archived. No new replies allowed.