Trouble with exception handling

ONCE I WIN OR LOSE THE OUTPUT OF THE BOARD GETS MESSED UP.

#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

const int NUMROWS = 5;
const int NUMCOLS = 5;
const int TAILLENGTH = 3;

class Board{
public:
char marker;
int moveNumber;
void initialize();
void showBoard(int move);
void placePiece(int &r, int &c, char i, int move);
void moveOpponent(Board board[NUMROWS][NUMCOLS], int &row, int &col, int &move);
};


void Board::initialize(){
Board board[NUMROWS][NUMROWS];
for (int row = 0; row< NUMROWS; row++) {
for (int col= 0; col < NUMCOLS; col++) {
board[row][col].marker = '_';

}

}

}

void Board::showBoard(int move){
Board board[NUMROWS][NUMROWS];
for (int row = 0; row< NUMROWS; row++) {
for (int col= 0; col < NUMCOLS; col++) {
cout << board[row][col].marker <<" ";
}
cout << endl;

}

}


void Board::placePiece(int &row, int &col, char marker, int move){
Board board[NUMROWS][NUMROWS];
board[row][col].marker = marker;
board[row][col].moveNumber = move;

}


void movePlayer(Board bo, int &row, int &col, int move){
Board board[NUMROWS][NUMCOLS];
char direction;
board[row][col].moveNumber = move;


if ( board[row][col].moveNumber >= TAILLENGTH + 1){
int counter = (move - (TAILLENGTH + 1)) +1;
for (int y = 0; y< NUMROWS; y++) {
for (int x= 0; x < NUMCOLS; x++) {

if ((board[y][x].moveNumber == counter) && (board[y][x].marker =='u')){
board[y][x].marker ='_';

}


}
}
}

cout <<"Enter direction (N/S/E/W): ";
cin >> direction;


board[row][col].marker = 'u';

if (direction == 'N'){
row--;
}
if (direction == 'S'){
row++;
}
if (direction == 'E'){
col++;
}
if (direction == 'W'){
col--;
}
if((row < 0 ) || ( row == NUMROWS ) || (col < 0 ) || ( col == NUMCOLS )){
throw runtime_error("YOU FELL OFF THE BOARD!");
}
if((board[row][col].marker != '_')){
throw runtime_error("YOU COLLIDED!");

}

board[row][col].marker = 'U';
}

void moveOpponent(Board bo, int &row, int &col, int &move){
Board board[NUMROWS][NUMCOLS];
board[row][col].moveNumber = move;
board[row][col].marker = 'x';
string excpt;




if ( board[row][col].moveNumber >= TAILLENGTH + 1){
int counter = (move - (TAILLENGTH + 1)) +1;
for (int y = 0; y< NUMROWS; y++) {
for (int x= 0; x < NUMCOLS; x++) {

if ((board[y][x].moveNumber == counter) && (board[y][x].marker =='x')){
board[y][x].marker ='_';

}


}
}
}


if((row > 0)&&(board[row-1][col].marker == '_')){
row--;
board[row][col].marker = 'X';

}

else if ((col > 0) &&(board[row][col-1].marker == '_')){
col--;
board[row][col].marker = 'X';
}


else if ((row < NUMROWS -1) && (board[row+1][col].marker == '_')){
row++;
board[row][col].marker = 'X';
}

else if ((col < NUMCOLS -1)&&(board[row][col+1].marker=='_')){

col++;
board[row][col].marker = 'X';
}
else{

throw runtime_error(" ");
}




}


void processException(runtime_error &excpt, bool &win ){
string lose1 = "YOU COLLIDED!";
string lose2 = "YOU FELL OFF THE BOARD!";
string opp =" ";
if(excpt.what() == opp){
}
else{
cout << excpt.what() << endl;
}

if ( (excpt.what() == lose1) || (excpt.what() == lose2) ){
win = false;
}
else{
win = true;
}

}



int main() {
int uRow = 0, uCol = 0;
int oRow = NUMROWS - 1, oCol = NUMCOLS - 1;
bool win = true;
int move = 0;
Board board;
board.initialize();
board.placePiece(uRow, uCol, 'U', move);
board.placePiece(oRow, oCol, 'X', move);
board.showBoard(move);
while (true) {
move++;
try {
movePlayer(board, uRow, uCol, move);
moveOpponent(board, oRow, oCol, move);
board.showBoard(move);
}
catch (runtime_error &excpt) {
processException(excpt, win);
break;
}

}
board.showBoard(move);
if (win)
cout << "*** YOU WIN! ***" << endl;
else
cout << "*** YOU LOSE! ***" << endl;
}
Without digging through your unformatted code, I will say that this is exactly the wrong way to use exceptions.
There are some issues.

The board looks like this at the beginning:
1
2
3
4
5
8 D   A
L % ■ ♀ è
  (   ♦ @
° > j W e
  $ ı ■ ]


You create many local variables of type Board that go out of scope like here.
1
2
3
4
5
6
7
8
9
10
11
void Board::initialize ()
{
  Board board[NUMROWS][NUMROWS];
  for (int row = 0; row < NUMROWS; row++)
  {
    for (int col = 0; col < NUMCOLS; col++)
    {
      board[row][col].marker = '_';
    }
  }
}


1
2
if((board[row][col].marker != '_')){
   throw runtime_error("YOU COLLIDED!");

In this case an exception is not the right way. It's not an error, collisions are part of a game.
Last edited on
Thank You guys for your input I appreciate it, I was able to figure out what the problem.
Topic archived. No new replies allowed.