My program stops and idk why(tictactoe)

I cannot understand why this program stops working, probably in the first loop of do while when it tries to do makeMove or ChangePlayer.
Can someone help?

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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;
void displayBoard(char board[]);
void makeMove(char board[], char player);
char changePlayer(char player);
int checkWin(char board[]);
void results(char board[], char player);

int main()
{
	char player = 'x';
	int move;
	char board[9] = { '1','2','3', '4','5','6', '7','8','9' };
	do {
		displayBoard(board);
		cout << "It's player's " << player << " turn.\n" << endl;
		makeMove(board, player);
		player = changePlayer(player);
	} while (checkWin(board) == 0);
	results(board, player);


	system("pause");
}

void displayBoard (char board[])
{
	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;
}

void makeMove(char board[], char player)
{
	int move;
	char secondPlayer = changePlayer(player);
	cout << "Choose where you want to place your mark(1 - 9):" << endl;
	cin >> move;
	if (board[move - 1] != player && board[move - 1] != secondPlayer)
	{
		board[move - 1] = player;
	}
	else
	{
		cout << "You have made invalid move! Try again!\n";
		makeMove(board, player);
	}
}

char changePlayer(char player)
{
	char x;
	char o;
	if (player == 'x')
	{
		char o = player;
		return o;
	}
	else if (player == 'o')
	{
		char x = player;
		return x;
	}
	else
	{
		cout << "Sth wrong with players\n\n\n";
		return 0;
	}
}

int checkWin(char board[])
{
	int result = 0;
	do
	{
			if (board[0] == board[1] && board[1] == board[2])
			{
				result = 1;
			}
			else if (board[3] == board[4] && board[4] == board[5])
			{
				result = 1;
			}
			else if (board[6] == board[7] && board[7] == board[8])
			{
				result = 1;
			}
			else if (board[0] == board[3] && board[6] == board[2])
			{
				result = 1;
			}
			else if (board[1] == board[4] && board[7] == board[2])
			{
				result = 1;
			}
			else if (board[2] == board[5] && board[8] == board[2])
			{
				result = 1;
			}
			else if (board[0] == board[4] && board[8] == board[2])
			{
				result = 1;
			}
			else if (board[2] == board[4] && board[6] == board[2])
			{
				result = 1;
			}
			else
			{
				if (board[0] != '1' && board[1] != '2' && board[2] != '3' && board[3] != '4' && board[4] != '5' && board[5] != '6' && board[6] != '7' && board[7] != '8' && board[8] != '9')
				{
					result = 2;//draw
				}
			}

	} while (result == 0);
	return result;
}

void results(char board[], char player)
{
	int x = checkWin(board);
	if (x == 1)
	{
		cout << "Player " << player << " has Won!!!\n";
	}
	else if(x==2)
	{ 
		cout << "It's a draw :d\n";
	}
}


Hi,

Maybe this is a good time to learn using a debugger :+)

Hopefully there is a GUI one in your IDE. Setup a watch-list of variables, step through the program 1 line at a time, look at the values and deduce where it goes wrong.

Hint: when is the first time it's worth checking for a win?
Thx :>
Found the mistake, checkWin was just an infinite loop...
Gotta learn how to use debugger now
Topic archived. No new replies allowed.