Hi. I have completed a program that plays a Zero Gravity Floating Connect Four. I.E. it can take checkers from the bottom, top, left, or right sides. It works, however, the win conditions do not seem to be working. I am not sure where the problem lies.

``````#include <iostream>
using namespace std;

//Constants
const int BOARD_COL_SIZE = 7;
const int BOARD_ROW_SIZE = 6;

class ConnectFourBoard {
public:

//initializes the board and constructs it
ConnectFourBoard()
{
for(int i = 0; i < 6; i++)
{
for(int j=0; j<7; j++)
{
grid[i][j] = ' ';
}
}
}

//prints the board
void print() {
cout << "Floating Connect Four Board" << endl;
cout << "  1 2 3 4 5 6 7" << endl;
for(int i = 0; i < 6; i++) {
cout << i+1;
for(int j = 0; j < 7; j++) {
cout << "|" << grid[i][j];
}
cout << "|" << endl;
cout << "  - - - - - - -" << endl;
}
}

//adds pieces 'R' or 'B' to the board
void addPiece(char side, int location, char player)
{
switch(side)
{
case 'l':
for(int i = 0; i < 7; i++)
{
if(grid[location-1][i] == ' ')
{
if(i+1 >= 7)
{
grid[location-1] = player;
}
else if(grid[location-1][i+1] != ' ')
{
grid[location-1][i] = player;
}
}
}
break;

case 'r':
for(int i = 6; i >= 0; i--)
{
if(grid[location-1][i] == ' ')
{

if((grid[location-1][i-1] != ' ') && (i > -1))
{
grid[location-1][i] = player;
}
else
{
grid[location-1][-1] = player;
}
}
}
break;

case 't':
for(int i = 0; i <= 6; i++)
{
if(grid[i][location-1] == ' ')
{

if((grid[i+1][location-1] != ' ') && (i < 6))
{
grid[i][location-1] = player;
}
else
{
grid[location-1] = player;
}
}
}
break;

case 'b':
for(int i = 5; i >= -1; i--)
{
if(grid[i][location-1] == ' ')
{

if((grid[i-1][location-1] != ' ') && (i > -1))
{
grid[i][location-1] = player;
}
else
{
grid[-1][location-1] = player;
}
}
}
break;

}
}

//enters R or B and whether it is valid
bool checkInput(char side, int location)
{
if(side != 't' && side != 'b' && side != 'r' && side != 'l')
{
return false;
}
else
{
if(side == 't' || side == 'b')
{
if(location < 1 || location > 7)
{
return false;
}
else
{
if(location < 1 || location > 6)
{
return false;
}
}
}
}
if(side == 't')
{
if(grid[location-1] != ' ')
{
return false;
}
}
else if(side == 'b')
{
if(grid[location-1] != ' ')
{
return false;
}
}
else if(side == 'l')
{
if(grid[location-1] != ' ')
{
return false;
}
}
else if(side == 'r')
{
if(grid[location-1] != ' ')
{
return false;
}
}
return true;
}

//checks to see if there is a winner after each input
bool checkWinner(char player)
{
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
if(grid[i][j] == player)
{
if(i-3 >= 0)
{
if((grid[i-1][j] == player) && (grid[i-2][j] == player) && (grid[i-3][j] == player))
{
return true;
}
}
if(i+3 < 6)
{
if((grid[i+1][j] == player) && (grid[i+2][j] == player) && (grid[i+3][j] == player))
{
return true;
}
}
if(j-3 >= 0)
{
if((grid[i][j-1] == player) && (grid[i][j-2] == player) && (grid[i][j-3] == player))
{
return true;
}

}
if(j+3 < 7)
{
if((grid[i][j+1] == player) && (grid[i][j+2] == player) && (grid[i][j+3] == player))
{
return true;
}
}
if((i-3 >= 0) && (j -3 >= 0))
{
if((grid[i-1][j-1] == player) && (grid[i-2][j-2] == player) && (grid[i-3][j-3] == player))
{
return true;
}
}
if((i-3 >= 0) && (j + 3 < 7))
{
if((grid[i-1][j+1] == player) && (grid[i-2][j+2] == player) && (grid[i-3][j+3] == player))
{
return true;
}
}
if((i+3 < 6) && (j+3 < 7))
{
if((grid[i+1][j+1] == player) && (grid[i+2][j+2] == player) && (grid[i+3][j+3] == player))
{
return true;
}
}
if((i+3 < 6) && (j-3 > 0))
{
if((grid[i+1][j-1] == player) && (grid[i+2][j-2] == player) && (grid[i+3][j-3] == player))
{
return true;
}
}
}
}
}

return false;
}

bool checkTie(char player)
{
for(int i = 0; i < 6; i++)
{
if(grid[i] == ' ')
{
return false;
}
}
for(int i = 0; i < 6; i++)
{
if(grid[i] == ' ')
{
return false;
}
}
for(int j = 0; j < 7; j++)
{
if(grid[j] == ' ')
{
return false;
}
}
for(int j = 0; j < 7; j++)
{
if(grid[j] == ' ')
{
return false;
}
}

return true;
}

private:

char grid[BOARD_ROW_SIZE][BOARD_COL_SIZE];
};

class Game {
public:
void play();

private:
cout << "Let's play Floating Connect Four" << endl;
}

void printPrompt() {
cout << "Enter side (t, b, l, r) and location (1 to 7): ";
}

void printError() {
cout << "Invalid location or side." << endl;
}

void printWinner(int player) {
cout << "Player " << player << " wins!" << endl;
}

void printTieGame() {
cout << "Tie game. No one wins." << endl;
}

ConnectFourBoard board;

};

void Game::play()
{
bool isGameOver = false;
char side;
int location;
while(isGameOver == false)
{
printPrompt();
cin >> side;
cin >> location;
while(board.checkInput(side, location) == false)
{
printError();
printPrompt();
cin >> side;
cin >> location;
}
if(board.checkWinner('R') == true)
{
printWinner(1);
isGameOver = true;
}
if(board.checkTie('R') == true)
{
printTieGame();
isGameOver = true;
}
board.print();
if(isGameOver == false)
{
printPrompt();
cin >> side;
cin >> location;
if(board.checkTie('B') == true)
{
printTieGame();
isGameOver = true;
}
if(board.checkWinner('B') == true)
{
printWinner(2);
isGameOver = true;
}
board.print();
}
}
}

//main function that plays the game
int main() {

Game myGame;
myGame.play();

return 0;
}
``````

Can you be less vague?

Not one win condition works?
Some work but not others?
All but one works?

And where should we start looking?
This is like looking for a needle (vague idea what we're looking for) in a haystack (371 uncommented lines of code)

## All 3 Replies

Can you be less vague?

Not one win condition works?
Some work but not others?
All but one works?

And where should we start looking?
This is like looking for a needle (vague idea what we're looking for) in a haystack (371 uncommented lines of code)

Sorry, none of my win conditions are working. When placing 4 of the same checkers in a row vertically, the program crashes. When placing 5 in a row horizontally, then it works and the program displays the winner. But it should be 4, and not 5.

Here is where my code begins the win conditions:

``````//checks to see if there is a winner after each input
bool checkWinner(char player)
{
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
if(grid[i][j] == player)
{
if(i-3 >= 0)
{
if((grid[i-1][j] == player) && (grid[i-2][j] == player) && (grid[i-3][j] == player))
{
return true;
}
}
if(i+3 < 6)
{
if((grid[i+1][j] == player) && (grid[i+2][j] == player) && (grid[i+3][j] == player))
{
return true;
}
}
if(j-3 >= 0)
{
if((grid[i][j-1] == player) && (grid[i][j-2] == player) && (grid[i][j-3] == player))
{
return true;
}
}
if(j+3 < 7)
{
if((grid[i][j+1] == player) && (grid[i][j+2] == player) && (grid[i][j+3] == player))
{
return true;
}
}
if((i-3 >= 0) && (j -3 >= 0))
{
if((grid[i-1][j-1] == player) && (grid[i-2][j-2] == player) && (grid[i-3][j-3] == player))
{
return true;
}
}
if((i-3 >= 0) && (j + 3 < 7))
{
if((grid[i-1][j+1] == player) && (grid[i-2][j+2] == player) && (grid[i-3][j+3] == player))
{
return true;
}
}
if((i+3 < 6) && (j+3 < 7))
{
if((grid[i+1][j+1] == player) && (grid[i+2][j+2] == player) && (grid[i+3][j+3] == player))
{
return true;
}
}
if((i+3 < 6) && (j-3 > 0))
{
if((grid[i+1][j-1] == player) && (grid[i+2][j-2] == player) && (grid[i+3][j-3] == player))
{
return true;
}
}
}
}
}
return false;
}
``````
#1:

Given

``````const int BOARD_COL_SIZE = 7;
const int BOARD_ROW_SIZE = 6;
``````

why are you using the values 6 & 7 in the code:

``````for(int i = 0; i < 6; i++)
{
for(int j=0; j<7; j++)
{
``````
#2:

Make your `checkWinner()` check each direction individually, not in one convoluted loop.

Set up separate nested loops for each of the directions to check, even the diagonals.
During each inner loop count player positions. But each time you find an opponent's value, reset the counter to 0. Any time the counter reaches 4, return TRUE.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.