If statement doesn't work correctly

I'm trying to code a tic tac toe game. I'm adding some check functions and I've a problem with the if statement in the checkvalidmove(int move);. It doesn't return the exact value but just false when condition is true or vice-versa... is like it skip control ad go ahead... I tried to solve in some ways, I change the
function's parameters, I use variable or not to return the value, I tried to
use else statement or not and so on. I spent some hours on it but I don't know
why it give me that problem, so here there's 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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

char playground[3][3] {{'1', '2', '3'},  //Playground
					   {'4', '5', '6'}, 
					   {'7', '8', '9'}};

bool checkwin(); //This function check if you win and return the value true
bool checkvalidmove(int move); //Check for free place and decide if you can move there or not
void move(int x, char player); //Replace the number with the figure o or x in the positions you have chosen
void printground(); //It prints the playground so the player can se how the game develops
void reset(); //At the end of the game it will reset the playground and maybe the scores, don't know... I'll decide it when I'll writing this function

int main(){

	char player_o = 'o';
	char player_x = 'x';
	char turno; //Who's next move?
	bool validmove = true;
	bool win = false;
	int mossa;
	int i = 2; //Index it very useful to maintaning the switch between two players

	printground();
	while(true){
		if(i % 2 == 0) //It enables the switch between two players
			turno = player_o;
		else
			turno = player_x;

		cout << "It's " << turno << "'s' turn." << endl << "write a number to move: ";
		cin >> mossa;
		move(mossa, turno);
		validmove = checkvalidmove(mossa);

		if(validmove){
			printground();
			win = checkwin();

			if(win){
				cout << "Player " << turno << " won the game!" << endl;
				exit(10);
			}
			i++;

		}else{
			cout << "I'll request another number... ";
		}
	}

	return 0;
}
bool checkwin(){

	//RIGHE
	if(playground[0][0] == playground[0][1] && playground[0][1] == playground[0][2])
		return true;
	if(playground[1][0] == playground[1][1] && playground[1][1] == playground[1][2])
		return true;
	if(playground[2][0] == playground[2][1] && playground[2][1] == playground[2][2])
		return true;
	//COLONNE
	if(playground[0][0] == playground[1][0] && playground[1][0] == playground[2][0])
		return true;
	if(playground[0][1] == playground[1][1] && playground[1][1] == playground[2][1])
		return true;
	if(playground[0][2] == playground[1][2] && playground[1][2] == playground[2][2])
		return true;
	//OBLIQUE
	if(playground[0][0] == playground[1][1] && playground[1][1] == playground[2][2])
		return true;
	if(playground[2][0] == playground[1][1] && playground[1][1] == playground[0][2])
		return true;

	return false;
}
bool checkvalidmove(int move){
	char convertmove = move + 48;
	for(int i = 0; i < 3; i++)
		for(int j = 0; j <3; j++)
			if(playground[i][j] == convertmove)
				return true;
	return false;
}
void move(int x, char player){

	switch(x){

		case 1:
			playground[0][0] = player;
			break;
		case 2:
			playground[0][1] = player;
			break;
		case 3:
			playground[0][2] = player;
			break;
		case 4:
			playground[1][0] = player;
			break;
		case 5:
			playground[1][1] = player;
			break;
		case 6:
			playground[1][2] = player;
			break;
		case 7:
			playground[2][0] = player;
			break;
		case 8:
			playground[2][1] = player;
			break;
		case 9:
			playground[2][2] = player;
			break;
		default:
			cout << "CONTROLLO PRECEDENTE NON RIUSCITO!" << endl;

	}

}
void printground(){
	for(int i = 0; i < 3; i ++){
		for(int j = 0; j < 3; j++)
			cout << playground[i][j] << "  ";
		cout << endl;
	}
}


Thanks
Last edited on
I don't understand your idea.
Why 2 nested loops when you need to check only 1 location ?
A move is valid when the value is neither 'o' nor 'x'
A better way is to convert your move to a col and row index.
https://stackoverflow.com/questions/11821899/how-to-get-row-and-column-from-index
35
36
		move(mossa, turno);
		cout << checkvalidmove(mossa) << endl;
you move before checking if it's valid, then you wonder why that space is taken
@Thomas1965 I use two nested loops to search through matrix and compare each element with variable move that I convert into a char. Anyway thanks for your answer, I'll try that method :)
Hello RustyJoe,

Playing with the program a bit and working off of ne555's suggestion I came up with this in "main":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cin >> mossa;

validmove = checkvalidmove(mossa);

//cout << '\n' << boolalpha << validmove << '\n' << endl;

if (!validmove)
{  // <--- if "validmove" is false.
	std::cout << "\n That space is taken. Please try again.\n" << std::endl;
}
else
	move(mossa, turno);  // <--- If "validmove" is true.

if (validmove)
{

I left the "cout" statement in to show you the use of "boolalpha" which prints "false" or "true" instead of "0" or "1". Not that this line has any real use that I can see unless for testing.

Then in the function "checkvalidmove" I made this change:
char convertmove = static_cast<char>(move + '0');. Changing from a "char" to an "int" is easier, but it is not as easy going from an "int" to a "char hence the "static_cast".
Instead of trying to remember the decimal value of zero just use the character zero. Thes is even better when you are working with upper and lower case letters. This way you do not have to hunt for the decimal value.

Hope that helps,

Andy
Hello @Handy Andy @ne555 ,

Thanks a lot for your answers, they helped me to figure out the problem and introduced me to the static_cast. I don't really know why I made that mistake maybe too hours of programming :).

RustyJoe
Last edited on
Hello RustyJoe,

You are welcome.

Sometimes it is the simple things that are hard to find after of working on the program.

I find there are times that just walking away from the problem for a few minutes and not thinking about it can help greatly. Other times just giving up for the day and getting some sleep helps.

When you look back yo will realize that a good programmer just does not want to give up. in that case see previous paragraph. Or print it out and put it up in frount of you, so you can read it often.

Andy

Edit:
Last edited on
Topic archived. No new replies allowed.