Thread: Cannot find Bug in my Othello program.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    Angry Cannot find Bug in my Othello program.

    I have written an Othello game program that you play in the console but it will not let you make a move in the bottom row of the board. The program crashes. I've looked and looked for hours and can't find the problem. Any help would be appreciated. I have a comment in the code where I think the problem is happening.

    Code:
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <utility>
    #include <stack>
    using namespace std;
    
    
    
    
    #define SIZE 4
    
    vector<pair<int,int> > get_all_flips(vector<vector<int> >& board, int r, int c, int color){
        vector<pair<int,int> > rv;
        int rr,cc;
        int opposite = color^3;
    
        //N,NE,E,SE,S,SW,W,NW
        int drs[]={-1,-1, 0, 1, 1, 1, 0,-1};
        int dcs[]={ 0, 1, 1, 1, 0,-1,-1,-1};
        for(int qq=0;qq<8;++qq){
            int dr = drs[qq];
            int dc = dcs[qq];
            rr=r+dr;
            cc=c+dc;
    
            if(board[rr][cc] == opposite ){
                stack<pair<int,int> >stk;
    
                while( rr >= 0 && cc >= 0 && rr < SIZE && cc < SIZE &&
                                            board[rr][cc] == opposite ){
                    stk.push(make_pair(rr,cc));
                    rr+=dr;
                    cc+=dc;
                }
    
                //fallen off board, hit blank, hit my own color
                if( rr >= 0 && cc >= 0 && rr < SIZE && cc < SIZE &&
                                            board[rr][cc] == color ){
                    while(!stk.empty()){
                        rv.push_back( stk.top() );
                        stk.pop();
                    }
                }
            }
        }
        return rv;
    }
    bool is_valid_move(vector<vector<int> >& board, int r, int c, int colour){
        if( get_all_flips(board,r,c,colour).empty())
            return true;
        else
            return false;
    }
    void print_board(vector<vector<int> >& board){
        cout << "  ";
        for(int i=0;i<SIZE;i++)
            cout  << " " << i ;
        cout << "\n";
    
        for(int i =0; i<SIZE; ++i){
            cout << i << " ";
            for(int j=0;j<SIZE;j++){
                if( board[i][j] == 0 )
                    cout << "| ";
                else if(board[i][j] == 1 )
                    cout << "|O" ;
                else
                    cout << "|#";
            }
            cout << "|\n";
        }
    }
    bool can_someone_move(vector<vector<int> >&board){
        for(int i=0;i<SIZE;++i){
            for(int j=0;j<SIZE;++j){
                if( board[i][j] == 0 )
                    return true;
            }
        }
        return false;
    }
    
    
    int blkCount(vector<vector<int> >&board){
        int blackcount = 0;
        for(int i = 0; i<SIZE; ++i)
            for(int j = 0; j<SIZE; ++j){
                if(board[i][j] == 2)
                    blackcount++;
            }
    
    
        return blackcount;
        }
    
    int whtCount(vector<vector<int> >&board){
        int whitecount = 0;
        for(int i = 0; i<SIZE; ++i)
            for(int j = 0; j<SIZE; ++j){
                if(board[i][j] == 1)
                    whitecount++;
            }
    
    
        return whitecount;
        }
    
    
    
    
    int main(int argc, char* argv[])
    {
    
        do{
        vector<vector<int> > board;
        vector<pair<int,int> > rv;
        int temp;
    
    
        board.resize(SIZE);
        for(int i=0;i<SIZE;++i)
            board[i].resize(SIZE);
        board[SIZE/2][SIZE/2] = board[SIZE/2-1][SIZE/2-1] = 1;
        board[SIZE/2-1][SIZE/2] = board[SIZE/2][SIZE/2-1] = 2;
    
        int turn=1;
        while( can_someone_move(board) ){
            print_board(board);
            //Shows score of the game.
            //int blkCount = 2;
            //int whtCount = 2;
            int blackcount = blkCount(board);
            int whitecount = whtCount(board);
            cout << "Black Count is: " << blackcount << endl;
            cout << "White Count is: " << whitecount << endl;
    
            if(turn == 1){
                cout << "Player 1 Move (row column)? ";
                //cout << "" << endl;
            }
            else{
                cout << "Player 2 Move (row column)? ";
                //cout << "" << endl;
            }
            int r,c;
            cin >> r >> c;
            if( cin.fail() ){
                cout << "BAD!\n";
                continue;
            }
    
    
            //off the board or space is used
            if( r < 0 || c < 0 || r >= SIZE || c >= SIZE ||                 //!!!!!!!!!!!!
                board[r][c] != 0 || !is_valid_move(board,r,c,turn) ){         //This code is causing problem
                cout << "You're stupid! Try Again!\n";
                continue;
            }
    
    
            //determine which pieces to flip
            temp = board[r][c];
            board[r][c] = turn ^ 3;
            rv = get_all_flips(board, r, c, turn ^ 3);
    
    
    
    
            //flip them
            if(!rv.empty()){                                        //if rv returns empty there is nothing to flip so move is invalid.
                for(int i = 0; i<rv.size(); i++ ){
                    board[rv[i].first][rv[i].second] = turn^3;
                }
            }
            else{
                cout << "Invalid Move, Choose Again." << endl;
                board[r][c] = temp;
                continue;
            }
    
    
            turn = turn ^ 3;
            cout << "" << endl;
    
    
        }
    
        }while(1);
    
    };

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Welcome to the wonderful world of debuggers.
    Code:
    $ g++ -g foo.cpp
    $ gdb ./a.out 
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/sc/Documents/coding/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/coding/a.out 
       0 1 2 3
    0 | | | | |
    1 | |O|#| |
    2 | |#|O| |
    3 | | | | |
    Black Count is: 2
    White Count is: 2
    Player 1 Move (row column)? 3 3
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400e7e in get_all_flips (board=..., r=3, c=3, color=1) at foo.cpp:27
    27	        if(board[rr][cc] == opposite ){
    (gdb) where
    #0  0x0000000000400e7e in get_all_flips (board=..., r=3, c=3, color=1) at foo.cpp:27
    #1  0x00000000004010c7 in is_valid_move (board=..., r=3, c=3, colour=1) at foo.cpp:50
    #2  0x00000000004016b3 in main (argc=1, argv=0x7fffffffe3a8) at foo.cpp:155
    (gdb) print rr
    $1 = 4
    (gdb) print cc
    $2 = 4
    Your initial position may be in bounds, but your attempts at locating neighbour cells also needs to check for being in-bounds as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Othello procedure
    By Morrow in forum Game Programming
    Replies: 7
    Last Post: 10-05-2004, 05:29 AM
  2. Help on writing a othello program
    By alice in forum C Programming
    Replies: 4
    Last Post: 03-16-2004, 09:18 AM
  3. othello AI
    By laasunde in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-02-2003, 02:37 PM
  4. Othello question
    By toaster in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-17-2002, 08:18 PM
  5. Reversi / Othello
    By dericosp in forum C Programming
    Replies: 6
    Last Post: 06-11-2002, 05:35 PM

Tags for this Thread