Clean UP

Heres my tic tac to game.

How can i clean it up:
( Please ignore all

std::cout<<std::endl

i use them to keep it organized till finished product.

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

#include <iostream>
int main() 
{
	// Tic Tac Toe Game
	std::cout<<"Widget360 Tic Tac Toe Game" << std::endl;
	std::cout<<"Version 1.0 Beta" << std::endl;
	//Game Board - Define squares
	char Square1('1');
	char Square2('2');
	char Square3('3');
	char Square4('4');
	char Square5('5');
	char Square6('6');
	char Square7('7');
	char Square8('8');
	char Square9('9');
	int CheckPlayerTurn(1);
	bool CheckGameOver(true);
	////// Game Board - Set up
	do {
		std::cout<<std::endl;
		std::cout<<std::endl;
		std::cout<<std::endl;
		std::cout << "  " << Square1 << "   |   " << Square2 << "   |  " << Square3 << std::endl;
		std::cout <<"------|----" <<"---|----" <<std::endl;
		std::cout << "  " << Square4 << "   |   " << Square5 << "   |  " << Square6 << std::endl;
		std::cout <<"------|----" <<"---|----" <<std::endl;
		std::cout << "  " << Square7 << "   |   " << Square8 << "   |  " << Square9 << std::endl;
		// Players - Set up X & O
		char PlayerXO;
		if (CheckPlayerTurn == 1) {
				PlayerXO = 'X';
		} else {
				PlayerXO = 'O';
		}
		////// Players - Who Starts
		std::cout<<std::endl;
		std::cout<<"Player " << CheckPlayerTurn << "'s move:" << std::endl;
		// Play - Check if move valid
		bool ValidMove;
		////// Play - Loop until Valid Move made
		do {
			char NextMove;
			std::cin >> NextMove;
			ValidMove = true;
			////// Play - Check if move valid
			if ( NextMove == '1' && Square1 == '1') {
				Square1 = PlayerXO;
			} else if ( NextMove == '2' && Square2 == '2') {
				Square2 = PlayerXO;
			} else if ( NextMove == '3' && Square3 == '3') {
				Square3 = PlayerXO;
			} else if ( NextMove == '4' && Square4 == '4') {
				Square4 = PlayerXO;
			} else if ( NextMove == '5' && Square5 == '5') {
				Square5 = PlayerXO;
			} else if ( NextMove == '6' && Square6 == '6') {
				Square6 = PlayerXO;
			} else if ( NextMove == '7' && Square7 == '7') {
				Square7 = PlayerXO;
			} else if ( NextMove == '8' && Square8 == '8') {
				Square8 = PlayerXO;
			} else if ( NextMove == '9' && Square9 == '9') {
				Square9 = PlayerXO;
			} else {
				std::cout << "Invalid Move... Try Again..." << std::endl;
				ValidMove = false;
			}
		} while (!ValidMove);
		CheckGameOver = false;
		bool WinGame = true;
		if (Square1 != '1') {
			if (Square2 == Square1 && Square3 == Square1) {
				CheckGameOver = true;
			}
			if (Square4 == Square1 && Square7 == Square1) {
				CheckGameOver = true;
			}
		}
		if (Square5 != '5') {
			if (Square1 == Square5 && Square9 == Square5) {
				CheckGameOver = true;
			}
			if (Square2 == Square5 && Square8 == Square5) {
				CheckGameOver = true;
			}
			if (Square4 == Square5 && Square6 == Square5) {
				CheckGameOver = true;
			}
			if (Square3 == Square5 && Square7 == Square5) {
				CheckGameOver = true;
			}
		}
		if (Square9 != '9') {
			if (Square3 == Square9 && Square6 == Square9) {
				CheckGameOver = true;
			}
			if (Square7 == Square9 && Square8 == Square9) {
				CheckGameOver = true;
			}
		}
		// Need to check the board full (no-win condition)
		if (Square1 != '1' && Square2 != '2' && Square3 != '3' &&
			Square4 != '4' && Square5 != '5' && Square6 != '6' &&
			Square7 != '7' && Square8 != '8' && Square9 != '9' && !CheckGameOver)
		{
			CheckGameOver = true;
			WinGame = false;
		}
		if (CheckGameOver) {
			if (WinGame) {
				std::cout << "Player" << CheckPlayerTurn << " wins!" << std::endl;
			}
			// Print ending board
			std::cout<<std::endl;
			std::cout<<std::endl;
			std::cout<<std::endl;
			std::cout << "  " << Square1 << "   |   " << Square2 << "   |  " << Square3 << std::endl;
			std::cout <<"------|----" <<"---|----" <<std::endl;
			std::cout << "  " << Square4 << "   |   " << Square5 << "   |  " << Square6 << std::endl;
			std::cout <<"------|----" <<"---|----" <<std::endl;
			std::cout << "  " << Square7 << "   |   " << Square8 << "   |  " << Square9 << std::endl;

			std::cout << "Game Over!" << std::endl;
			std::cout << "Play again (y/n)?" << std::endl;
			char cPlayAgain;
			std::cin >> cPlayAgain;

			if (cPlayAgain == 'y') {
				CheckGameOver = false;
				// Clear the board
				Square1 = '1';
				Square2 = '2';
				Square3 = '3';
				Square4 = '4';
				Square5 = '5';
				Square6 = '6';
				Square7 = '7';
				Square8 = '8';
				Square9 = '9';
			}
			CheckPlayerTurn = 1;
		} else {
			// Alternate player turns
			if (CheckPlayerTurn == 1) {
				CheckPlayerTurn = 2;
			} else {
				CheckPlayerTurn = 1;
			}
		}
	} while (!CheckGameOver);
}


*** i give credit to xoax.net i believe thats were i got the original from.
*** i learn best from others work then i make my own.
lol I like your style Widget. Anyway, as I mentioned in your previous clean up post, I suggest functions or even a class for the game board at least. that way you can just do something like BOARD.show() and BOARD.checkWin() and stuff. this would clean up a lot of it. And again I suggest putting these functions in a separate header file. It makes reading and editing code in main() a lot easier. Sorry for not putting my example in brackets on the other post lol, I just wrote it, copied and pasted and hit submit. It might be more interesting, for practice at least, to make your board out of a 2D array. that way you can iterate through it a little easier to check for illegal moves and win / lose / draw conditions.
closed account (S6k9GNh0)
Then after that, try and create a tic tac toe game out of ncurses. And then after that, make one using SDL/SFML/a multimedia library.
Then after that, try and create a tic tac toe game into a mobile app. Nowadays is all about mobile apps :P

PS Btw, there are tons of them (tic-tac-toe) already in the mobile apps stores be it Apple, Android, BlackBerry etc so it will be more of a learning exercise then.
Oh! But if you make an app, you have to do something original, like a realtime tic-tac-toe, action style, with upgrades and battlesuits!!
closed account (S6k9GNh0)
"Hahahaha, I just nuked your O up!"
"That's fine! I'm level 20 and just got my anti-nuking chainmail for my lower left X!"

On a real note, try and stick to one game at a time btw. I used to jump from project to project without finishing any of them and it just causes confidence and reliability issues.
Yeah, games are sort of fun. Somehow I always end up getting distracted when trying to get into game programming "Yay, let's program a vertical shooter!" "Hm, looks like this could be done with using OOD"... "I sort of want this design to be more flexible.." *Heads over to book store and buys book about design patterns* "Wow, OOP is fun! Let's do some Java!" "Hm, Java is boring. Let's do some Prolog!" "Prolog is boring. Let's do some math!" "Hey, I have seen that in OpenGL once... hey, I could write a game. How about a vertical shooter?"
Topic archived. No new replies allowed.