1
\$\begingroup\$

My main problems are making draw case and any player can choose a place which is chosen by another player here is the code :

#include <iostream>
using namespace std;

char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
static int turnnumber = 1 ;
bool winner = false, flag = false ;
bool win(){
     if(board[0][0]==board[1][1]&&board[1][1]==board[2][2])
    winner = true ;
    if(board[0][2]==board[1][1]&&board[1][1]==board[2][0])
    winner = true ;
if(board[1][0]==board[1][1]&&board[1][1]==board[1][2])
    winner = true ;
if(board[0][0]==board[0][1]&&board[0][1]==board[0][2])
    winner = true ;
if(board[2][0]==board[2][1]&&board[2][1]==board[2][2])
    winner = true ;    
if(board[0][0]==board[1][0]&&board[1][0]==board[2][0])
    winner = true ;
if(board[0][1]==board[1][1]&&board[1][1]==board[2][1])
    winner = true ;
if(board[0][2]==board[1][2]&&board[1][2]==board[2][2])
    winner = true ;
            
    if(winner==true&&turnnumber==1)
       cout << "player2 won \n\n" ;
    if(winner==true&&turnnumber==2)
       cout << "player1 won \n\n" ;
 
    return winner;
}

void view()
{
    for (int i = 0; i < 3; i++)
    {
        for (int x = 0; x < 3; x++)
        {
            cout << "[" << board[i][x] << "]  ";
        }
        cout << endl
             << "-------------" << endl;
    }
}
    
void players()
{
    
    char player1 = 'X', player2 = 'O';
    int number;
    cout << "\nplayer " << turnnumber << " it's your turn ";
    if(turnnumber==1)
       turnnumber++;
    else if(turnnumber==2)
       turnnumber--;
    char player;
    if(turnnumber==1)
       player=player2;
    if(turnnumber==2)
       player=player1;
    cin >> number ;
    
    switch(number){
        
    case 1:
        board[0][0] = player;
        break;
    case 2:
        board[0][1] = player;
        break;
    case 3:
        board[0][2] = player;
        break;
    case 4:
        board[1][0] = player;
        break;
    case 5:
        board[1][1] = player;
        break;
    case 6:
        board[1][2] = player;
        break;
    case 7:
        board[2][0] = player;
        break;
    case 8:
        board[2][1] = player;
        break;
    case 9:
        board[2][2] = player;
        break;
    default:
        cout << "\nwrong number\n";
        players();
         
    }
    system("cls");
    view();
 
    if(!win())
        players();
}

int main()
{
    view();
    players();
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You can address both of the your problems using "struct".

By making something like this:

struct myBorad 
{
    char board[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };
    bool bMyBoardStatus[9] = { false };
};
myBorad g_myBoard;

You then need to change each of the fields as "taken" by setting them to "true". after each player play.

g_myBoard.bMyBoardStatus[0] = true;

Now that you have this in place you can add two ifs:

  1. Check if the status of selected by player place is "taken" aka not false.
  2. Check if all the array aka all nine of them are true and you didn't meet the win condition yet that means its draw.

there are more elegant solutions but this will help you get what you need, and this does not have to be in struct. i prefer this way.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! Please note, questions involving code that's not working as intended, are off-topic on this site. If OP edits their post to address the problem and make their post on-topic, they might make your answer moot. It is advised not to answer such questions. Protip: There are tons of on-topic questions to answer; you'll make more reputation faster if you review code in questions that will get higher view counts for being on-topic. \$\endgroup\$ Jul 8, 2020 at 22:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.