### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109`` ``````#include #include 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"<>x; x--; return x; //Player 1 moves } else { cout<<"Player 2. Please enter number where you want 'X' to be"<>x; x--; return x; //Player 2 moves } } void main (void) { int a; cout<<"Tic-Tac-Toe Game"<>a; switch (a){ case 1: cout<<"You have chosen to play against players"<
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.

 ``12345678`` ``````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
 ``12345678910111213141516171819202122`` ``````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."<
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.