-3

Hey guys I wrote a connect four program in C++ but Im having trouble figuring out the issue with my algorithm for finding out the winning move diagonally. Here is the code that I wrote: My horizontal and vertical functions work though

//checks for a horizontal win
    //returns 1 if there's a win
    for(int i=0;i<numrows;i++)
        for(int j=0;j<rowsize-3;j++)
            if(board[i][j]!=" " && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3])
                return 1;

    //checks for a vertical win
    //returns one if theres a win
    for(int i=0;i<numrows-3;i++)
        for(int j=0;j<rowsize;j++)
            if(board[i][j] !=" " && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j])
                return 1;  



    int numrows=6;
    int rowsize=7;

//checks the right side of a diagonal for a  win
    for(int i=0;i<numrows;i++)
        for(int j=0;j<rowsize;j++)
            if(board[i][j] !=" " && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3])
                return 1;


    //checks left diagonal win for a win
    for(int i=0;i<numrows;i++)
        for(int j=0;j<rowsize;j++)
            if(board[i][j] !=" " && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3])
            return 1;

//creates a typical 7 by 6 connect four board
    for (int i=0;i<numrows; i++){
        vector<string> row;

        for (int j=0;j<rowsize; j++)
            row.push_back(" ");
        board.push_back(row);
    }
newbie_at_linux
  • 35
  • 1
  • 2
  • 8

1 Answers1

-1

I think it could be an error since evaluating board[i+x][j+-y] for all the board will get out of bounds of the array.

For example when you have i=4, j=4 the first if statement will look for board board[4][4], board[5][5], board[6][6], board[7][7]

JMorales
  • 115
  • 1
  • 9
  • Since it's C++, its UB, and there won't be an error for array out of bounds. Check this out : http://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why – Chara Apr 05 '16 at 18:47
  • Sorry I forgot it was c++ however the UB will happen at the time i or j evaluates on 4 for the first time and as he said only if a piece is on the first row it is recognized. Couldn't it be the reason that the second row is not being evaluated? – JMorales Apr 05 '16 at 19:06
  • 1
    He said that if there is a piece on the last row it is recognized, otherwise it isn't – Chara Apr 05 '16 at 19:10
  • Yeah but the last row graphically might be the 0 index. I would have asked the index of the last row but I can't comment on the post – JMorales Apr 05 '16 at 19:14
  • 1
    Oh I see what you mean, it's possible if that's the way he's treating it. After all, UB can be anything :p – Chara Apr 05 '16 at 19:15
  • Wouldn't that only be the case if he overloaded the array operator so that the 0 index refers to the last row in the vector though? – Chara Apr 05 '16 at 19:18
  • Well normally when you print an array the first row is tinted so it is on the top – JMorales Apr 05 '16 at 19:25
  • Yeah, I'm not seeing how that explains why diagonals with a piece in the LAST row are showing (when `i = 5`) but diagonals when `i < 5` are not. Technically it should pick up the piece in the first row, since `i` isn't going out of bounds yet – Chara Apr 05 '16 at 19:28
  • I think the problem is still that we don't know what he's calling "last row" because it can be either i=0 or i=5 – JMorales Apr 05 '16 at 19:33
  • You're right, he was referring to bottom row as 1st row – Chara Apr 05 '16 at 19:43
  • The numrows-3 and rowsize-3 will do the trick, in his code he's still missing the -3 in some for loops – JMorales Apr 05 '16 at 20:40