I have finally got the game running but it ends too early. I need to make the game continue until there are 4 in a row but it ends after the first turn and no disk is dropped.
do
{
cout << "Drop a " << (player == 'R' ? "red" : "yellow") <<
" disc at column (0-6): ";
int column;
cin >> column;
if (!Spot(board, column, player))
{
cout << "This column is full. Try a different column" << endl;
}
else
{
done = true;
}
} while (!done);
}
bool Spot(vector<vector<char> > board, int column, char player)
{
for (int i = 0; i < board.size(); i++)
{
if (board[i][column] == ' ')
{
board[i][column] = player;
return true; // successful
}
}
return false; // full, unsuccessful
}
void Board(vector<vector<char> > board)
{
for (int i = board.size() - 1; i >= 0; i--)
{
cout << "|";
for (int j = 0; j < board[i].size(); j++)
cout << (board[i][j] != ' ' ? board[i][j] + "|" : " |");
cout << endl;
}
cout << "----------------------" << endl;
}
bool Won(vector<vector<char> > board)
{
return Matchfour(board);
}
bool Tie(vector<vector<char> > board)
{
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[i].size(); j++)
if (board[i][j] == ' ') return false;
return true; // All cells are now occupied
}
bool Matchfour(vector<vector<char> > values)
{
int numberRows = values.size();
int numberColumns = values[0].size();
// Check rows
for (int i = 0; i < numberRows; i++) {
if (Matchfour(values[i]))
return true;
}
// Check columns
for (int j = 0; j < numberColumns; j++) {
vector<char> column(numberRows);
// Get a column into an array
for (int i = 0; i < numberRows; i++)
column[i] = values[i][j];
if (Matchfour(column))
return true;
}
// Check major diagonal (lower part)
for (int i = 0; i < numberRows - 3; i++) {
int numberDiagonal
= min(numberRows - i, numberColumns);
vector<char> diagonal(numberDiagonal);
for (int k = 0; k < numberDiagonal; k++)
diagonal[k] = values[k + i][k];
if (Matchfour(diagonal))
return true;
}
// Check major diagonal (upper part)
for (int j = 1; j < numberColumns - 3; j++) {
int numberDiagonal
= min(numberColumns - j, numberRows);
vector<char> diagonal(numberDiagonal);
for (int k = 0; k < numberDiagonal; k++)
diagonal[k] = values[k][k + j];
if (Matchfour(diagonal))
return true;
}
// Check sub-diagonal (left part)
for (int j = 3; j < numberColumns; j++) {
int numberDiagonal
= min(j + 1, numberRows);
vector<char> diagonal(numberDiagonal);
for (int k = 0; k < numberDiagonal; k++)
diagonal[k] = values[k][j - k];
if (Matchfour(diagonal))
return true;
}
// Check sub-diagonal (right part)
for (int i = 1; i < numberRows - 3; i++) {
int numberDiagonal
= min(numberRows - i, numberColumns);
vector<char> diagonal(numberDiagonal);
for (int k = 0; k < numberDiagonal; k++)
diagonal[k] = values[k + i][numberColumns - k - 1];
if (Matchfour(diagonal))
return true;
}
return false;
}
bool Matchfour(vector<char> values)
{
for (int i = 0; i < values.size() - 3; i++)
{
bool Equal = true;
for (int j = i; j < i + 3; j++) {
if (values[j] == L'\0' || values[j] != values[j + 1]) {
Equal = false;
break;
}
}
if (Equal) return true;
}
return false;
}
int main()
{
vector<vector<char> > board(6);
for (int i = 0; i < board.size(); i++)
{
board[i] = vector<char>(7);
for (int j = 0; j < 7; j++)
board[i][j] = ' ';
}
Board(board);
while (true)
{
// Prompt the first player
dropDisc('R', board);
Board(board);
if (Won(board))
{
cout << "The red player won" << endl;
system("pause");
return 0;
}
else if (Tie(board)) {
cout << "No winner" << endl;
system("pause");
return 0;
}
// Prompt the second player
dropDisc('Y', board);
Board(board);
if (Won(board)) {
cout << "The yellow player won" << endl;
system("pause");
return 0;
}
else if (Tie(board)) {
cout << "No winner" << endl;
system("pause");
return 0;
}
}
system("pause");
return 0;
}