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;
}
}
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;
}
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.