Need adive with a portion of my tic tac toe project.

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
void getInput()
{
do 
		{
			bool isMoveValid;
			string playerInput;
			cin >> playerInput;
			

			// Check for a valid move
			
			if (playerInput == "1,1" && grid[0][0] = "") 
				grid[0][0] = playerMark;
			else if (playerInput == "1,2" && grid[0][1] = "")
				grid[0][1] = playerMark;
			else if (playerInput == "1,3" && grid[0][2] = "")
				grid[0][2] = playerMark;
			else if (playerInput == "2,1" && grid[1][0] = "")
				grid[1][0] = playerMark;
			else if (playerInput == "2,2" && grid[1][1] = "")
				grid[1][1] = playerMark;
			else if (playerInput == "2,3" && grid[1][2] = "")
				grid[1][2] = playerMark;
			else if (playerInput == "3,1" && grid[2][0] = "")
				grid[2][0] = playerMark;
			else if (playerInput == "3,2" && grid[2][1] = "")
				grid[2][1] = playerMark;
			else if (playerInput == "3,3" && grid[2][2] = "")
				grid[2][2] = playerMark;
			else 
			{
				cout << "Invalid input, please use x,y format." << endl;
				isMoveValid = false;
			}
}while (!isMoveValid);

}


I get errors saying I can't use && operator for strings. How can I fix this? (sorry for formatting)

Thanks for all the help
Last edited on
The second part of your if statements...you want a == instead of a =
Also, there has to be a better way to add the the player mark then brute force. Did you consider reading the input then inserting based on that instead of just checking all possible combinations?
Well im messing around with this:
but its putting me in an infinite loop. Heres my whole source, can anyone see whats going on?

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
void getPlayerInput()
{
	std::cout << "Player" << playerTurn << "'s turn (enter in x,y format):" << std::endl;
		
		// Loop until we get a valid move
		do 
		{
			int x,y;
			bool isMoveValid;
			string playerInput;
			getline(cin,playerInput)
			x = (int(playerInput.at(0)) - 1);
			y = (int(playerInput.at(2)) - 1);

			// Check for a valid move
			
			if (grid[x][y] = ' ')
			{
				grid[x][y] = playerMark;
				isMoveValid = true;
			}

			else 
			{
				cout << "Invalid input, please use x,y format." << endl;
				isMoveValid = false;
			}

        }while (isMoveValid == false);
}

but its putting me in an infinite loop. Heres my whole source, can anyone see whats going on?

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

using namespace std;

void printBoard();
void checkMark();
void moveInput();
void CheckWin();
void getPlayerInput();

char grid[3][3] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
int playerTurn = 1;
char playerMark;
bool isMoveValid;


int main()
{	
	printBoard();
	checkMark();
	getPlayerInput();
	system("CLS");
	printBoard();
	getPlayerInput();
	return 0;
}

void printBoard()
{
	cout << endl;
	cout << "   " << setw(2) << grid[0][0] << "|" << setw(2) << grid[0][1] << "|" << setw(2) << grid[0][2] << endl;
	cout << "   " << "--+--+--"<< endl;
	cout << "   " << setw(2) << grid[1][0] << "|" << setw(2) << grid[1][1] << "|" << setw(2) << grid[1][2] << endl;
	cout << "   " << "--+--+--"<< endl;
	cout << "   " << setw(2) << grid[2][0] << "|" << setw(2) << grid[2][1] << "|" << setw(2) << grid[2][2] << endl;
	cout << endl;
}

void checkMark()
{
	if (playerTurn == 1) 
			playerMark = 'X';
	else
			playerMark = 'O';
}

void getPlayerInput()
{
	cout << "Player" << playerTurn << "'s turn (enter x coordinate then press enter, follow b):" << endl;
		
		// Loop until we get a valid move
		do 
		{
			int x,y;
			bool isMoveValid;
			string playerInput;
			cin >> playerInput;
			x = (int(playerInput.at(0)) - 1);
			y = (int(playerInput.at(2)) - 1);

			// Check for a valid move
			
			if (grid[x][y] = ' ')
			{
				grid[x][y] = playerMark;
				isMoveValid = true;
			}

			else 
			{
				cout << "Invalid input, please use x,y format." << endl;
				isMoveValid = false;
			}

        }while (isMoveValid == false);
}


Don't use cin>> to get data into a string; using getline() instead. That's probably where the issue is coming from.
Last edited on
still getting stuck in an infinite, not sure at all why.

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

using namespace std;

void printBoard();
void checkMark();
void moveInput();
void checkWin();
void getPlayerInput();

char grid[3][3] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
int playerTurn = 1;
char playerMark;
bool isMoveValid;
bool isGameWon = false;

int main()
{	
	do
	{
		printBoard();
		checkWin();
		checkMark();
		getPlayerInput();
		system("CLS");
	}while (isGameWon != true);
	
	return 0;
}

void printBoard()
{
	cout << endl;
	cout << "   " << setw(2) << grid[0][0] << "|" << setw(2) << grid[0][1] << "|" << setw(2) << grid[0][2] << endl;
	cout << "   " << "--+--+--"<< endl;
	cout << "   " << setw(2) << grid[1][0] << "|" << setw(2) << grid[1][1] << "|" << setw(2) << grid[1][2] << endl;
	cout << "   " << "--+--+--"<< endl;
	cout << "   " << setw(2) << grid[2][0] << "|" << setw(2) << grid[2][1] << "|" << setw(2) << grid[2][2] << endl;
	cout << endl;
}

void checkMark()
{
	if (playerTurn == 1) 
			playerMark = 'X';
	else
			playerMark = 'O';
}

void getPlayerInput()
{
	cout << "Player" << playerTurn << "'s turn (enter x coordinate then press enter, follow b):" << endl;
		
		// Loop until we get a valid move
		do 
		{
			int x,y;
			bool isMoveValid;
			string playerInput;
			getline(cin,playerInput);
			x = (playerInput.at(0)) - 1);
			y = (playerInput.at(2)) - 1);

			// Check for a valid move
			
			if (grid[x][y] = ' ')
			{
				grid[x][y] = playerMark;
				isMoveValid = true;
			}

			else 
			{
				cout << "Invalid input, please use x,y format." << endl;
				isMoveValid = false;
			}

        }while (isMoveValid == false);
}

void checkWin()
{

		if (grid[0][0] == grid[0][1] == grid[0][2])
			isGameWon = true;
		if (grid[0][0] == grid[1][0] == grid[2][0])
			isGameWon = true;
		if (grid[0][0] == grid[1][1] == grid [2][2])
			isGameWon = true;
		if (grid[0][1] == grid[1][1] == grid [2][1])
			isGameWon = true;
		if (grid[0][2] == grid[1][1] == grid [2][0])
			isGameWon = true;
		if (grid[0][2] == grid[1][2] == grid [2][2])
			isGameWon = true;
		if (grid[1][0] == grid[1][1] == grid [1][2])
			isGameWon = true;
		if (grid[2][0] == grid[2][1] == grid [2][2])
			isGameWon = true;
		else
			isGameWon = false;
}


Im so confused =(

Heres my output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

     |  |
   --+--+--
     |  |
   --+--+--
     |  |

Player1's turn (enter x coordinate then press enter, follow b):
1,3
1,3
1,3
1,2
1,3
1,1
2,2 


It loops in the input im guessing
Last edited on
After some debugging I found out its these lines:

1
2
x = (playerInput.at(0)) - 1);
y = (playerInput.at(2)) - 1);


when i would enter 1,3 it would say x was 48 and y was 50. Whats up with that? It there any better method for input i could use?
When you use .at() on the string, it returns a character. You need to use atoi() or some math to get an integer out of it; when you just cast it to an int it gives you the ASCII value.
Topic archived. No new replies allowed.