0

I am trying to make my program scan the board for move for an AI and the if statement works if I have the first statement by itself but if I add the else if statement and crashes.

void BoardSet::foxNextToSheep(int x, int y, int& moves)
{

    if(board[x+1][y] == 'S' && board[x+2][y] == '.') 
    {
        x = x + 2;
        moves++;
        foxNextToSheep(x, y, moves);
    }
    else if(board[x-1][y] == 'S' && board[x-2][y] == '.')
    {
        x = x - 2;
        moves++;
        foxNextToSheep(x, y, moves);
    }
}

I am assuming that it is due to the recursion, what would be a better way to do this?

I realize it is because board[x-1][y] causes x to become negative and won't work. Is there a way to stop this from happening?

class BoardSet {
public:

//Functions used for foxes
    bool findFox(int x, int y);
    void foxNextToSheep(int x, int y, int& moves);

private: 
    char board[7][8];
    int sheep;
    int foxes;
};

class Foxes {
public:

void foxesProcess(BoardSet& board);
void findFoxes(BoardSet& board);
void checkForPossibleMoves(BoardSet& board);
void print();



private:
    int foxes[2][2];


};


void Foxes::checkForPossibleMoves(BoardSet& board)
{
    int moves1 = 0;
    int foxOneX = foxes[0][0];
    int foxOneY = foxes[0][1];
    board.foxNextToSheep(foxOneX, foxOneY, moves1);
    cout << moves1 << endl;

}

call for check for moves

void Foxes::foxesProcess(BoardSet& board)
{
    cout << "The foxes move: ";
    findFoxes(board);
    checkForPossibleMoves(board);

}

From Main

void processGame(istream& in)
{
    int repeat = 1;

    BoardSet board;
    Sheep sheep;
    Foxes foxes;
    initalizeBoard(in, board);
    while(repeat)
    {
        board.checkForWin(repeat);
        if(repeat == 0)
            break;
        sheep.sheepProcess(board);
        board.print();
        board.checkForWin(repeat);
        if(repeat == 0)
            break;
        foxes.foxesProcess(board);
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

1

You are not doing any bounds checking in your function so assuming that board is just a standard array or a class that does not bound check then when x<=1 you will go out of bounds and right into undefined behavior.

Without seeing more code the most obvious check and fix would be this:

else if( x > 1 && (board[x-1][y] == 'S' && board[x-2][y] == '.') )

You also have a bounds check issue on the if statement, if x >= 4 then you will be going outside the bounds as well:

if( x <= 4 && (board[x+1][y] == 'S' && board[x+2][y] == '.') ) 

having some sort of variable that defines max would be helpful in case you decided to change it.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Better to do boundary check in an upper level since the sequence of if evaluation may not be from left to right. – Tianyun Ling Apr 12 '13 at 02:57
  • @TianyunLing Unless I am misunderstanding your comment evaluation order is required by the standard, please see this previous thread http://stackoverflow.com/questions/628526/is-short-circuiting-boolean-operators-mandated-in-c-c-and-evaluation-order – Shafik Yaghmour Apr 12 '13 at 03:02
  • It is still crashing. Should I do the bound check before I do the if statements? – UnhinderedLimpidity Apr 12 '13 at 03:15
  • @UnhinderedLimpidity It is really hard to say without seeing more code – Shafik Yaghmour Apr 12 '13 at 03:22
  • @UnhinderedLimpidity Really need to see how it is being called fro main – Shafik Yaghmour Apr 12 '13 at 03:38
  • @UnhinderedLimpidity updated answer, although without having an example I can run that reproduces the problem it make it hard to fully find all the issues. Which is why you will usually get a better and more accurate answer if you have a piece of code that fully reproduces the problem for us to test. – Shafik Yaghmour Apr 12 '13 at 07:43
  • @ShafikYaghmour I think I realized what was happening. After it scand through the board going downward, it would then scan going back upward and just keep repeating. Now I just need to figure out how to make it so it doesnt go back up after it scans downward, if you understand what I am saying – UnhinderedLimpidity Apr 12 '13 at 15:16