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

This is the only missing portion of my code. `

`

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

bool Spot(vector<vector<char> > board, int column, char player);
void Board(vector<vector<char> > board);
bool Won(vector<vector<char> > board);
bool Matchfour(vector<vector<char> > values);
bool Matchfour(vector<char> values);

void dropDisc(char player, vector<vector<char> > board)
{
    bool done = false;

Since the only place done is changed to true is if Spot returns true it means that Spot is returning true too early.

Or other variables which spot uses are not initialised.

A couple of things that are giving you your immediate problem:

  • When you're displaying the board you're trying to concatenate a char and a string. You're better off just outputting each in turn. cout << board[i][j] << "|";

  • All your functions are taking board as a value not a reference, therefore the updated values aren't getting passed back. Using the reference operator & in the declaration will fix this. void dropDisc(char player, vector<vector<char> >& board)

When this is fixed you'll start to find other problems, but this will give you a start.

Edited 2 Years Ago by tinstaafl

Ok I added cout << board[i][j] << "|"; to each output and added the & to void dropDisc but it did not change the output.

As for done only being initialized when spot is true...I didnt see any other place to add done=true into the program.

Any other ideas...I would greatly appreciate it!

I got it all worked out. Thanks alot guys your help is much appreciated.

This question has already been answered. Start a new discussion instead.