2d Array Problems

I am trying to write a connect four game but I cant get the check for winner fuctions to work just right. Everything else is good to go. When I compile and run, you get a segmentation error in the first loop. I know its in my check for winner code but Im stuck. Any help?



#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

// Gameboard properties
const int ROWS = 7; // Number of rows in the game board
const int COLUMNS = 6; // Number of columns in the game board

// Values to fill the array
const char USER = 'R'; //User's game piece
const char COMP = 'B'; //Computer's game piece
const char EMPTY = ' '; //Playable spot

//Array of characters to be filled
void Initialize(char Slots[ROWS][COLUMNS])
{
// Sets up Game Board
for (int r = 0; r < ROWS; r++)
for (int c = 0; c < COLUMNS; c++)
Slots[r][c] = EMPTY;
}


//Displays the game board
void PrintBoard (const char GameBoard[ROWS][COLUMNS])
{
cout << " 0 1 2 3 4 5 " << endl;
cout << "|-----------------------|" << endl;
for (int r = 0; r < ROWS ; r++)
{
cout << "| ";
for (int c = 0; c < COLUMNS; c++)
{
cout << GameBoard[r][c];
cout << " | ";
}
cout << endl << "|-----------------------|" << endl;
}
}



//Check Column for availability
bool isFull(const int Move, const char GameBoard[ROWS][COLUMNS])
{
bool isFull = true;

if(Move >= 0 && Move < COLUMNS) //protects against seg fault
{
if(GameBoard[0][Move] == EMPTY) //check if top position is full

isFull = false;
}

return isFull;
}


//Get User Move
void InputMove(char GameBoard[ROWS][COLUMNS])
{
int Move;
bool inBound;
bool isColumnFull;

do
{
inBound = true;
isColumnFull = false;

cout << "Enter column to play: " << endl;
cin >> Move;
if(Move < 0 || Move > COLUMNS-1) //check bounds
{
inBound = false;
cout << "Play must be between 0 and " << COLUMNS-1 << endl;
}
else if(isFull(Move, GameBoard)) //check if the column is full or not
{
isColumnFull = true;
cout << "Play must be in a column that is not full" << endl;
}
else //everything is correct
cout << "Thanks! Placing your piece in column #" << Move << "!" << endl;
}while(!inBound || isColumnFull);

for(int i = 1; i < ROWS; i++) //scan til you find another piece, then back up
{
if(GameBoard[i][Move] != EMPTY) //found a piece already placed
{
GameBoard[i-1][Move] = USER; //place a piece back one
break;
}
}




//Computers Turn
int CompMove(char GameBoard[ROWS][COLUMNS])
{
int number;

//Pick Random Number
number=(rand()%6)-1;

for(int i = 1; i < ROWS; i++) //scan til you find another piece, then back up
{
if(GameBoard[i][number] != EMPTY) //found a piece already placed
{
GameBoard[i-1][number] = COMP; //place a piece back one
break;
}
else if(i == ROWS-1) //reached the bottom, just place it
GameBoard[i][number] = COMP;
}
}




//Test for Vertical win
bool isWinRow(char Turn, const char GameBoard[ROWS][COLUMNS])
{
int r, c;
bool win;
for(r = 0; r < ROWS; r++)
{
win = true;
for(c = 0 ; c < COLUMNS; c++)
{
if(GameBoard[r][c] != Turn)
{
win = false;
break;
}
}
if(win)
return true;
}
return false;
}

//Test for Horizontal win
bool isWinCol(char Turn, const char GameBoard[ROWS][COLUMNS])
{
int r, c;
bool win;
for(c = 0; c < COLUMNS; c++)
{
win = true;
for(r = 0; r < ROWS; r++)
{
if(GameBoard[r][c] != Turn)
{
win = false;
break;
}
}
if(win)
return true;
}
return false;
}

//Test for Diagonal win
bool isWinDiag(char Turn, const char GameBoard[ROWS][COLUMNS])
{
int r, c;
bool win;
for(r = 0; r < ROWS; r++)
{
win = true;
for(c = 0; c < COLUMNS; c--)
{
if(GameBoard[r][c] != Turn)
{
win = false;
break;
}
}
if(win)
return true;
}
for(r = 0; r < ROWS; r--)
{
win = true;
for(c = 0; c < COLUMNS; c++)
{
if(GameBoard[r][c] != Turn)
{
win = false;
break;
}
}
if(win)
return true;
}
return false;
}


bool isWinner(char Turn, const char GameBoard[ROWS][COLUMNS])
{
if(isWinRow(Turn, GameBoard))
return true;
if(isWinCol(Turn, GameBoard))
return true;
if(isWinDiag(Turn, GameBoard))
return true;
else
return false;
}


//-------------------------------------------



int main ()
{
// Declare Variables
char GameBoard[ROWS][COLUMNS]; // Array for tracking the game
bool Victory;

// Initialize the board
Initialize(GameBoard);

// Display Game Board
PrintBoard(GameBoard);

//Game Loop
for (int i = 0 ; i < 22 ; i++)
{
Victory = false;

//User Loop
cout << "Your Turn. Round : " << i + 1 << endl;

InputMove(GameBoard); //Get user Move
PrintBoard(GameBoard); //Show user Move
Victory = isWinner(USER, GameBoard);
if (Victory == true) //Check for user win
{
cout << "Game Over. You WIN!\n"; //End Game if user win
break;
}

//Computer Loop
cout << "Computers Turn. Round: " << i + 1 << endl;

CompMove(GameBoard); //Get comp move
PrintBoard(GameBoard); //Show comp move
Victory = isWinner(COMP, GameBoard); //Check for comp win
if (Victory == true)
{
cout << "Game Over. The Computer beat you!\n"; //End game if comp win
break;
}
}
return 0;
}
Last edited on
Plss use code tags to post code !!

The 1st problem is that 'int CompMove(char GameBoard[ROWS][COLUMNS])' has a return type but is not returning anything. This will compile but can crash you program.

I ran it in my debugger and bool isWinDiag(char Turn, const char GameBoard[ROWS][COLUMNS]) ended up in a endless loop at for(r = 0; r < ROWS; r--), this loop will keep running till r overloads tht's where your program crashes.
Last edited on
Topic archived. No new replies allowed.