Tic-Tac-Toe questions

I'm making a C++ game for an assignment. This is what I have so far (its far from complete since its a group project). I'm missing some functions in here because its a group project, and my friend is making that function.

The problem I've hit is that whenever I input the coordinate, the program would enter the X and O coordinate increment by 1. For example, if I enter 4, it would place the O at 5. I tried using an "x--" to get past that, but it doesn't seem to work.

Another problem is my X are not appearing. I have no idea why really...

Sorry if I can't describe properly. I'm not a native English speaker.

Edit: I fixed the above problems in this updated code. However, I'm having problems in that when player 2 inputs in a space already occupied by player 1, it will overide it.

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
#include <iostream>
#include <time.h>

using namespace std;

void tictactoeboard (int PorC);


int PVP (int turns){			//player moves function
int x;
if (turns %2 !=0){
	cout<<"Player 1. Please enter number where you want 'O' to be"<<endl;
	cin>>x;
	x--;
	return x; //Player 1 moves
	}
else {
	cout<<"Player 2. Please enter number where you want 'X' to be"<<endl;
	cin>>x;
	x--;
	return x; //Player 2 moves
	}
}


void main (void) {
	int a;
	cout<<"Tic-Tac-Toe Game"<<endl;
	cout<<"Press 1 to play against players. Press 2 to play against the computer"<<endl;
	cout<<"Press 3 to exit game"<<endl;
	cin>>a;
	switch (a){
		case 1: cout<<"You have chosen to play against players"<<endl;
			tictactoeboard (a); //function to go to board here
			break;
		case 2: cout<<"You have chosen to play against the computer"<<endl;
			tictactoeboard (a);//Function to go to board here
			break;
		case 3: return;
			break;
		}
	cout<<"Thank you for playing this game."<<endl;
	system("pause");
}


void tictactoeboard (int PorC){
	char board [10] = { '1','2','3','4','5','6','7','8','9','/0'};
	int counter=1;
	int playerturns=1;
	char x;
	int check=0;

	cout << "| " << board[0] << "  | " << board[1] << "  | " << board[2] << "  |"<<endl; //Initial Start board
	cout << "|____|____|____|"<<endl;
	cout << "| " << board[3] << "  | " << board[4] << "  | " << board[5] << "  |"<<endl;
	cout << "|____|____|____|"<<endl;
	cout << "| " << board[6] << "  | " << board[7] << "  |  " << board[8] << " |"<<endl;
	cout << "|    |    |    |"<<endl;
	
	while (counter <10){   //loop so that players can switch turns


	if (PorC=1){      
		if (counter %2 != 0){	// choose function
			while (check!=1) {
				x= PVP (playerturns);//function for PVP
				if (board[x] != 'O'||'X' ==true){ //check
		board[x]='O';
		check++;
				}		//Player 1 Moves
				else {
					cout<<"Invalid. Please type again."<<endl;
				}
			}
		}
		else {
			while (check!=1) {
				x = PVP (playerturns); //function for PVP
				if (board[x] != 'O'||'X' ==true){ //check
					board[x]='X';
					check++;
				}
				else {
				cout<<"Invalid. Please type again."<<endl;
				}
			}
		}//Player 2 moves
		check=0;
		playerturns++;
	}
	else {
		//function for CVP
	}

	
	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;
	cout << "|    |    |    |"<<endl;

	//function check for winning move

	counter ++;
	}
		return;
}
Last edited on
You will need to decrease the input by 1.

An array's first element is 0(zeo).
But I did that in the tictactoeboard function. I got the returned number and decreased it by an increment of 1.

1
2
3
4
5
6
7
8
if (counter %2 !=0){	// choose function
		x= PVP (playerturns); //function for PVP
		board[x--]='O';		//Player 1 Moves
		}
		else {
			x= PVP (playerturns); //function for PVP
		board[x--]='X';
		}		//Player 2 moves 


Or is there something wrong this code?
You're decreasing it after using the value. In other words, you're taking the initial value and using it as an array index, and then decrementing it afterwards.

I fixed the above problems in this updated code. However, I'm having problems in that when player 2 inputs in a space already occupied by player 1, it will overide it.

I believe the problem is here, but isn't sure how to fix it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while (check!=1) {
				x= PVP (playerturns);//function for PVP
				if (board[x] != 'O'||'X' ==true){ //check
		board[x]='O';
		check++;
				}		//Player 1 Moves
				else {
					cout<<"Invalid. Please type again."<<endl;
				}
			}
		}
		else {
			while (check!=1) {
				x = PVP (playerturns); //function for PVP
				if (board[x] != 'O'||'X' ==true){ //check
					board[x]='X';
					check++;
				}
				else {
				cout<<"Invalid. Please type again."<<endl;
				}
			}
The problem may be in the following if statement, which looks weird to me:

if (board[x] != 'O'||'X' ==true)

What this means is that the following two conditions are checked, and that if either of them is true, then the entire condition is true:

board[x] != 'O'

and

'X' ==true

Since 'X' has a non-zero value, that second condition will always be true, which means that the entire OR condition will always be true.

It looks to me that what you're trying to do is check that the board position doesn't have either an 'X' or an 'O'. If this is the case, the logic should be that both

board[x] != 'O'

and

board[x] != 'X'

must be true.

A simpler approach would be to ensure that the elements of board are initialised to some value that indicates it hasn't been filled yet, and then just check whether the element still has that initial value.
Last edited on
Topic archived. No new replies allowed.