Hi, I need to fix the 4 * 4 tic tac toe but the program will fail, and I have only 3 to 4 hours, thanks for helping me.

+1 vote
asked Jan 30, 2019 by maryam
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;

char board[4][4]={{'-','-','-','-'},{'-','-','-','-'}};

    const int row=4;
    const int col=4;
    char b;
   
    void printboard(char[][col]){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++)
            cout<<b[i][j]<<" ";
            cout<<endl;
        }
    }

    bool boardIsFull(char b[][col]){
        for (int i=0;i<row;i++)
            for(int j=0;j<col;j++)
                if(b[i][j]=='-')
                    return false;
                return true;
    }
   
    bool occupied(char b[][col], int indx1,int indx2){
        if(b[indx1][indx2]=='-')
            return false;
        return true;
    }
   
    void printremianindex(char b[][col]){
        for ( int i=0;i<row;i++)
            for(int j=0;j<col;j++)
                if(b[i][j]=='-')
                    cout<<"("<<i<<","<<j<<")";
                    cout<<endl;
    }
   
    bool win(char b[][col]){
        char c;
        int winlen=4;
        for( int i=0;i<row;i++){
            c=b[i][0];
            if(c=='-')
                continue;
                int seq=1;
                for( int j=1; j<col ;j++){
                    if(b[i][j]!=c)
                    break;
                    seq++;
                   
                }
                if(seq==winlen){
                    cout<<"win!!"<<endl;
                    return true;
                }
       
        int i=0;
        int j=0;
        b[i][j];
        c=b[0][0];
        if(c=='-')
        continue;
            i++;
            j++;
            return false;
        }
    }
   
   
   
int main() {
        char board[row][col];
       
        int r1,r2,c1,c2;
       
        char x;
       
            cout<<"main board:"<<endl;
            //char board[4][4]={{'-','-','-','-'},{'-','-','-','-'}};
            cout<<board;
           
                printboard(board);
                for(int i=0;i<4;i++){
                   
                    cout<<" "<<endl;
                for(int j=0;j<4;j++){
                    cout<<" ";
                    cout<<board[i][j];
                }
               
                }
               
            //char board[4][4]={{'-','-','-','-'},{'-','-','-','-'}};
            //cout<<board;
           
       
           
            cout<<"-------------------------------------------"<<endl;
           
            cout<<"enter your sing:"<<endl;
           
            cout<<"-------------------------------------------"<<endl;
           
        while(!boardIsFull(board)){
            cout<<"empty  place:"<<endl;
            printremianindex(board);
            cout<<"choose number (row and col):(player1)";
            cin>>r1;
            cin>>c1;
            while(occupied(board ,r1 ,c1)){
                cout<<"choose number (row and col):(player1)";
                cin>>r1;
                cin>>c1;
           
            }
            board[r1][c1]='x';
           
            cout<<"------------------------------------------"<<endl;
           
            printremianindex(board);
           
            cout<<"------------------------------------------"<<endl;
           
            if(win(board)||boardIsFull(board)){
               
                return 0;
               
            }
           
            while(!boardIsFull(board)){
            cout<<"empty  place:"<<endl;
            printremianindex(board);
            cout<<"choose number (row and col):(player2)";
            cin>>r2>>c2;
            while(occupied(board ,r2 ,c2)){
                cout<<"choose number (row and col):";
                cin>>r2>>c2;
           
            }
            board[r2][c2]='o';
           
            cout<<"------------------------------------------"<<endl;
           
            printremianindex(board);
           
            cout<<"------------------------------------------"<<endl;
           
            if(win(board)){
               
                return 0;
            }   
            }
           
           
        }
        }
        //cout<<"no win";   
       
        //return 0;
   
    //}

2 Answers

0 votes
answered Mar 15, 2019 by Ryan (1,210 points)
Well, it appears as if you don't have a 4 x 4 board. Instead, you have a 4 x 2. You need to add two more rows to your board array. If you didn't for some reason, get a compile error from your array (for not declaring a 4 x 4), you probably got a runtime segmentation fault for accessing array index bounds that don't exist (ex. If you tried to access 4, 4, which doesn't exist).
commented Mar 18, 2019 by jamiuy (170 points)
I think this is the correct answer
0 votes
answered Mar 18, 2019 by jamiuy (170 points)
Not boardls that is wrong this is the right answer board
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...