Hi..

I don't know how to do the draw condition & error checking on the following tic tac toe codes,
can anyone help me to complete this with elaborations and explanations please.. thanks

#include<iostream>
using namespace std;


char box[9]={'1','2','3','4','5','6','7','8','9'};

void drawboard()
{
    cout<< box[0]<<" | "<<box[1]<<" | "<<box[2]<<endl;    
    cout<< "---------"<<endl;
    cout<< box[3]<<" | "<<box[4]<<" | "<< box[5]<< endl;  
    cout<< box[6]<<" | "<<box[7]<<" | "<<box[8]<<endl;    
}

int winning(char f[]) 

{

    if(box[0]==box[1] && box[2]==box[0])        
        return 1;

    else if(box[3]==box[4] && box[5]==box[3])     
        return 1;

    else if(box[6]==box[7] && box[8]==box[6])   
        return 1;

    else if(box[0]==box[3] && box[6]==box[0])     
        return 1;

    else if(box[1]==box[4] && box[7]==box[1])     
        return 1;

    else if(box[2]==box[5] && box[8]==box[2])     
        return 1;

    else if(box[0]==box[4] && box[8]==box[0])     
        return 1;

    else if(box[2]==box[4] && box[6]==box[2])     
        return 1;

    else
    return 0;
}

int main()
{
    char c[2]={'x','o'}; 
    int move;           
    int i=0;           
    int player=2;     

    for (i=0;i<=9;i++) 
    {
      if (i<9&&winning(box)==0) 
      {
         drawboard();
        if(player==2)
            player=1; 
        else
            player=2; 

            cout<<"player"<<player<<" please select an available number"<<endl;
            cin>>move; 
            box[move-1]=c[player-1]; 
            if (winning(box)==1) 

            cout<<"congratulations dear player "<<player<<" "<<"you are the winner :)"<<endl;

return 0;
      }

    }

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please explain the draw condition and errors that you hope to check for in a paragraph form. Then convert this to pseudo code. Then you can write real functions. Also, I would probably store the board as a std::vector<std::vector< char > > so the squares can be accessed like a matrix (e.g. board[0][1] ) instead of a linear array (0-9). It will make the logic seems more natural. Then you can write loops like:

RowHasWinner(int row);
...
for(int i = 0; i < 3; i++)
{
RowHasWinner(i);
...
}

Good luck,

David

This article has been dead for over six months. Start a new discussion instead.