Trying to make a tic tac toe game

Hello! I've struggled to make a solution to this but I can't seem to land on it.
I've managed to research a way to print my board and get user input, but I can't seem to figure out how to create an input validation that stops one player from choosing a used place on the board. The code as it is right now just switches turns once before sticking on player O.

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
 void TicTacToe(){
	int player = 1;
	sBoard();

	int row;
		int col;

				cout << "=== Player X ===" << endl;
		cout << "Pick a row and column (1-3)"<<endl;
		cout << "Row: ";
		cin >> row;
		cout << "Column: ";
		cin >> col;
		if ((row == 1 || row ==2 || row == 3) && (col == 1 || col == 2 || col == 3)){
				board[--row][--col]=1;

		}

		else {
			cout<< "Invalid Position"<<endl;
		}
		sBoard();
		player = 2;
		switchPlayer(player);
}


void playerI(int player){

	int row;
	int col;
	if(player==1){
			cout << "=== Player X ===" << endl;}

		else{
			cout << "=== Player O ===" << endl;}
	cout << "Pick a row and column (1-3)"<<endl;
	cout << "Row: ";
	cin >> row;
	cout << "Column: ";
	cin >> col;
	if ((row == 1 || row ==2 || row == 3 || col == 1 || col == 2 || col == 3) && board[row][col] ==0)

		{if (player==1)
			board[--row][--col]=1;
		if (player==2)
			board[--row][--col]=2;}


	else {
		cout<< "Invalid Position"<<endl;
		playerI(player);
	}
}

void sBoard(void){

	for(int r=0; r<=2; r++)
	{
		for(int c=0; c<=2; c++)
		{

			if( board [r][c]==0)
				cout << "- ";
			else if (board [r][c]==1)
				cout << "X ";
			else
				cout << "O ";
		}
		cout<<endl;
	}
}













void switchPlayer(int player)
{
	playerI(player);
	sBoard();

	if(player==2)
		player=1;
	else
		player=1;
	switchPlayer(player);

}
Create a while loop. In the loop get user input, a space on your board. The space should be blank. If the space is blank end the loop. If the space is taken already then output "Invalid move. This space is taken already dummy!" and continue the loop.
Topic archived. No new replies allowed.