ticTacToe

Hello all. I wrote a tic-tac-toe game using a class object as the board and was wondering if anyone has suggestions on improvements that I should make. The only thing that bugs me is that there is a lot of selection structures (I'm not sure how to avoid them). This is my biggest program thus far I'll admit and I want it to be very good. Any criticism will be welcomed, no pun intended. Anyway, here is the 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
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
#include <iostream>
#include <string>

using namespace std;

class ticTacToe
{
public:
	void printBoard() const;
	void getMove(string playerList[], int number, char& playMove, int& playPosition);
	void setMove(char move, int position);
	void checkMove(char& move, int& position, int num);
	int numOfMoves() const; //keeps track of the number of moves made
	bool isWinner() const;
	ticTacToe();
private:
	char board[3][3];
	int totalMoves;
};

void setIntro(string playerList[]);
void checkPlayer(int& number);
void congratPlayer(string playerList[], int num);

int main()
{
	ticTacToe myGame; //object to hold the board
	string player[2]; //variables to hold the names of the two players
	char move; //variable to hold wither X or O
	int position; //variable to hold the position on the board
	int index=0; //variable to hold the index of the player array

	setIntro(player);
	myGame.printBoard();

	while(!myGame.isWinner() && myGame.numOfMoves()<9) //checks that there is no winner and totalMoves<9
	{
		checkPlayer(index); //if index==0, its player[0] turn, otherwise player[1] turn.
		myGame.getMove(player,index,move,position);
		myGame.checkMove(move,position,index);
		myGame.setMove(move,position);

		if(myGame.isWinner()) //structure to end the program after someone wins
		{
			myGame.printBoard();
			congratPlayer(player, index);
			return 0;
		}

		cout << endl;
		myGame.printBoard(); //print board after each move

		index++;
	}//end while
	
	cout << "Cats game!" << endl; //executes if myGame.isWinner() is false and totalMoves==8

	return 0;
}//end main

void ticTacToe::printBoard() const
{
	for(int row=0;row<3;row++)
	{
		for(int col=0;col<3;col++)
			cout << board[row][col] << "   ";
		cout << endl << endl << endl;
	}
}

void ticTacToe::getMove(string playerList[], int number, char& playMove, int& playPosition)
{
	if(number==0)
	{
		cout << playerList[number] << ", please enter your move (X) and position: ";
		cin >> playMove >> playPosition;
	}
	else
	{
		cout << playerList[number] << ", please enter your move (O) and position: ";
		cin >> playMove >> playPosition;
	}
}

void ticTacToe::checkMove(char& move, int& position, int num)
{
	if(num==0)
	{
		while(move!='X' && move!='x')
		{
			cout << "You must enter X as your move. Please reenter your move: ";
			cin >> move;
		} 
	} //end if
	else
	{
		while(move!='O' && move!='o')
		{
			cout << "You  must enter O as your move. Please reenter your move: ";
			cin >> move;
		}
	} //end else


	while(position<=0 || position>=10)
	{
		cout << "Invalid position. Position must be between 1 and 9 inclusive. Reenter position: ";
		cin >> position;
		
	} //end while
}

void ticTacToe::setMove(char move, int position)
{
	if(position<=3)
		board[0][position-1]=static_cast<char>(toupper(static_cast<int>(move)));
	else if(position<=6)
	{
		switch(position)
		{
		case 4:
			board[1][0]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 5:
			board[1][1]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 6:
			board[1][2]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		}
	}
	else if(position<=9)
	{
		switch(position)
		{
		case 7:
			board[2][0]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 8:
			board[2][1]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 9:
			board[2][2]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		}
	}

	totalMoves++;
}

int ticTacToe::numOfMoves() const
{
	return totalMoves;
}

bool ticTacToe::isWinner() const
{
	if(board[0][0]==board[1][1] && board [0][0]==board[2][2])
			return true;
	else if(board[2][0]==board[1][1] && board [2][0]==board[0][2])
			return true;
	else if(board[0][0]==board[0][1] && board [0][0]==board[0][2])
			return true;
	else if(board[1][0]==board[1][1] && board [1][0]==board[1][2])
			return true;
	else if(board[2][0]==board[2][1] && board [2][0]==board[2][2])
			return true;
	else if(board[0][0]==board[1][0] && board [0][0]==board[2][0])
			return true;
	else if(board[0][1]==board[1][1] && board [0][1]==board[2][1])
			return true;
	else if(board[0][2]==board[1][2] && board [0][2]==board[2][2])
			return true;
	
	return false;
}

ticTacToe::ticTacToe()
{
	char checker='1';
	for(int row=0;row<3;row++)
		for(int col=0;col<3;col++)
		{
			board[row][col]=checker;
			checker++;
		}
		
	totalMoves=0;
}

void setIntro(string playerList[])
{
	cout << "       *****Hello and welcome to the game of TicTacToe!*****" << endl << endl;
	cout << "Please enter player one: ";
	getline(cin,playerList[0]);
	cout << "Please enter player two: ";
	getline(cin, playerList[1]);
	cout << playerList[0] << ", your moves are X's." << endl;
	cout << playerList[1] << ", your moves are O's." << endl;
	cout << endl;
}

void checkPlayer(int& number)
{
	if(number>1)
		number=0;
}

void congratPlayer(string playerList[], int num)
{
	cout << "Congratulations, " << playerList[num] << "! You have won!" << endl;
}
A few things:

1. The asking the user to input their symbol is meaningless. You should simply make the program put that.

2. You can make the board by using Vertical Bars and Hyphens Like this:

      1 2 3
    1 A| | 
      -|-|-
    2  | | 
      -|-|-
    3  | | 


Asking for Row and Column. (Like) 11 to Move in A(which in the program will be a space)

3.To choose which player plays:
Create two variables, each having a different value (preferably 1 & 0, since zero is false)

Now you can create a function that will Swap the values of the two numbers.
Then put the variables in two if - else statements that will check for the value of those variables.

Code Structure:
1
2
3
4
5
6
7
8
9
10
11
12
int a = 1, b = 0;
if (a)
{
     //First player
     Swap(a,b);
}

else if (!b)
{
     //Second player
     Swap(a,b);
}


If you are interested, here is the link to the code I made:
http://pastebin.com/FkLstPw6
Last edited on
Topic archived. No new replies allowed.