Tic Tac Toe Player switch problem

Self teaching myself C++ right now. Trying to put together a Tic Tac Toe program right now. The problem I currently have is the turn is always player ones turn. The functions that have not been commented out are working, so I have left them out, I will add them in an edit if required.

I am primarily concerned with getting the turn to alternate like it should but if any of you have suggestions on things to improve that would be welcomed as well.

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
// Two player tic tac toe

#include <iostream>

using namespace std;

void displayBoard(int board[]);
char moveMade(int space, int board[]);
void getMove(int playerTurn, int board[]);
void checkLegal(int playerTurn, int move, int board[]);

//void checkWin();  Not written yet
//void winnable();
//void gameEnd();



int main()
{
	int playerTurn = 1; // Starts game with player ones turn.
	cout << "Player One will be 'X'.  Player Two will be 'O'.  First player to get three in a row wins.";
	int board[10];
	
	
	int spaces = 0;
	for (int i = 0; i < 10; i++)
	{
		board[i] = spaces;
		spaces++;
	}


	for (int i = 0; i < 9; i++)
	{
		
		displayBoard(board);
		getMove(playerTurn, board);
		//checkWin();
		//checkWinnable();


		// This is where the player swap should happen, but it is not
		if (playerTurn == 1)
		{
			playerTurn = 2;
		}
		else 
		{
			playerTurn = 1;
		}
	}

	cin.ignore();
	cin.get();
}

void displayBoard(int board[])
{
	cout << "\n   |   |" << endl;
	cout << " " << moveMade(1, board) << " | " << moveMade(2, board) << " | " << moveMade(3, board) << endl;
	cout << "   |   |" << endl;
	cout << "---+---+---" << endl;
	cout << "   |   |" << endl;
	cout << " " << moveMade(4, board) << " | " << moveMade(5, board) << " | " << moveMade(6, board) << endl;
	cout << "   |   |" << endl;
	cout << "---+---+---" << endl;
	cout << "   |   |" << endl;
	cout << " " << moveMade(7, board) << " | " << moveMade(8, board) << " | " << moveMade(9, board) << endl;
	cout << "   |   |" << endl;
}

char moveMade(int space, int board[])
{
	char moveMade;
	if (board[space] == 15)
	{
		moveMade = 'X';
	}
	else if (board[space] == 25)
	{
		moveMade = 'O';
	}
	else if (board[space] == 1)
	{
		 moveMade = 49;
	}
	else if (board[space] == 2)
	{
		moveMade = 50;
	}
	else if (board[space] == 3)
	{
		moveMade = 51;
	}
	else if (board[space] == 4)
	{
		moveMade = 52;
	}
	else if (board[space] == 5)
	{
		moveMade = 53;
	}
	else if (board[space] == 6)
	{
		moveMade = 54;
	}
	else if (board[space] == 7)
	{
		moveMade = 55;
	}
	else if (board[space] == 8)
	{
		moveMade = 56;
	}
	else if (board[space] == 9)
	{
		moveMade = 57;
	}


	return moveMade;
}

void getMove(int playerTurn, int board[])
{
	int move;
	if (playerTurn = 1)
	{
		cout << "Player One enter the number of the square for your move: ";
	}
	else {
		cout << "Player Two enter the number of the square for your move: ";
	}
	cin >> move;

	checkLegal(playerTurn, move, board);
}
	
void checkLegal(int playerTurn, int move, int board[])
{
	if (board[move] == 15 || board[move] == 25)
	{
		cout << "\nThat is not a legal selection.\n";
		getMove(playerTurn, board);
	}
	else if (playerTurn == 1)
	{
		board[move] = 15;
	}
	else if (playerTurn == 2)
	{
		board[move] = 25;
	}

}
Last edited on
it's perplexing that the player switching logic isn't working. looks right to me.

A couple of suggestions:

1) use a multi-dimensional array to represent the board. int board[3][3]; not absolutely nescessary, but it more accurately expresses the problem.

2) rather than a fixed lenght for() loop to run the game (your for( int i = 0; i < 9; i++ ) use a while() loop. the reason for this is that a game might end before all 9 squares are filled...you know... if a 3-year-old plays it or something. Regardless of the probability that someone might actually lose a game, it is theoretically possible, and you'll need to check for that in the code and exit.
I originally has the board be a two dimensional array but switched to a one dimensional array after I ran into a problem that I fixed and don't remember right now.

The next thing I was going to work on was a function to see if the game had been won and then end it. You can see the commented out function call above.
I'm sceptical that playerTurn is actually always staying at 1. I suspect that there is a flaw in the getMove() function which is improperly interpreting it.
I just changed the int playerTurn = 1 to int playerTurn = 2 and it went to player ones turn. I didn't want to post all of the code because even with the little experience I have it looks a mess. I will be my original post with the whole thing.
look at line 127. should be ==
Thank you Esslercuffi!
No problem. I've never* made that mistake myself.

*never in this context is not guaranteed to include any span of time which extends beyond 30 days prior to initial posting
Biggest lesson learned tonight is just because it looks like it is running as expected doesn't mean there isn't anything wrong with it.
Very true. Learning how to use a debugger to step through your code and monitor variables is a skill that will help immensely.
Topic archived. No new replies allowed.