okay, I've got a connect four, 2 player, console game due today, and I've got everything pegged down except for checking for a win, I've tried multiple things, ifs, whiles, fors, and I only have around 5 weeks of experience in C++, with around 15 hrs of class every week.

I'm going to put my code in and see if anyone can help me with this

#include <iostream>
#include <vector>

using namespace std;

const int ROWS = 6;
const int COLS = 7;
const int CONNECT = 4;
int turnsTaken = 0;
const char EMPTY='_';


void plyrSwitch(char &curPlyr);
void annceTurn(char &curPlyr);
void checkBoard(int &turnsTaken, bool &Game_Over);
void intlzBoard(vector<vector<char> >&Board);
void printBoard(vector<vector<char> >&Board);
bool checkColumn(vector<vector<char> >&Board, int &cur_col , char &curPlyr,int &curX, int &curY);
bool checkWin(vector<vector<char> >&Board,int &curX, int &curY, char &curPlyr);


int main()
{
    vector<vector<char> >Board;
    intlzBoard(Board);
    printBoard(Board);
    int cur_col = 0;
    char curPlyr = 'O';
    bool Game_Over=false;
    bool Game_Won=false;
    int curX=0;
    int curY=0;

    while (!Game_Over)
    {
        cout << "" << curPlyr << ", please choose which column you'd like to place your piece." << endl;
        cin >> cur_col;
        while (checkColumn(Board, cur_col, curPlyr, curX, curY) == false)
        {
            cout << "current column is " << cur_col;
            printBoard(Board);
            cout << "That column is full, please try another spot." << endl;
            cin >> cur_col;
        }
        turnsTaken++;
        printBoard(Board);

        //if (checkWin(Board, curX, curY, curPlyr) == true)
        //{
        //    cout << "" << curPlyr << " you have won!!" << endl;
        //    Game_Won = true;
        //}
        if (!Game_Won)
        {
            plyrSwitch(curPlyr);
            checkBoard(turnsTaken, Game_Over);
        }

    }
}

void plyrSwitch(char &curPlyr)
{
    if (curPlyr == 'O')
    {
        curPlyr = 'X';
    }

    else if (curPlyr == 'X')
    {
        curPlyr = 'O';
    }
    cout << "Player is now " << curPlyr << "." << endl;
}

void annceTurn(char &curPlyr)
{
    cout << "It's your turn, " << curPlyr << "! \n" << endl;
}

void checkBoard(int &turnsTaken, bool &Game_Over)
{
    //here is where the check for a win is done everyturn taken
    if (turnsTaken == 42)
    {
        //here is where I will place the final check for a win
        cout << "It seems that no-one has won; that means that it's a tie!" << endl;
        Game_Over = true;
    }
}


void printBoard(vector<vector<char> >&Board)
{
 for (int i=0; i<ROWS; i++)
 { cout << endl;
  for(int j=0; j<COLS; j++)
  {
   cout << Board[i][j] << " ";

  }
 cout << endl;
 }
}
bool checkColumn(vector<vector<char> >&Board, int &cur_col, char &curPlyr, int &curX, int &curY)
{
    cur_col = (cur_col--);
    int returner = false;
    
        if (Board[5][cur_col] == EMPTY)
        {
            Board[5][cur_col] = curPlyr;
            curX = 5;
            curY = cur_col;
            returner = true;
        }
        else if (Board[4][cur_col] == EMPTY)
        {            
            Board[4][cur_col] = curPlyr;
            curX = 4;
            curY = cur_col;
            returner = true;
        }
        else if  (Board[3][cur_col] == EMPTY)
        {            
            Board[3][cur_col] = curPlyr;
            curX = 3;
            curY = cur_col;
            returner = true;
        }
        else if  (Board[2][cur_col] == EMPTY)
        {            
            Board[2][cur_col] = curPlyr;
            curX = 2;
            curY = cur_col;
            returner = true;
        }
        else if  (Board[1][cur_col] == EMPTY) 
        {            
            Board[1][cur_col] = curPlyr;
            curX = 1;
            curY = cur_col;
            returner = true;
        }
        else if  (Board[0][cur_col] == EMPTY) 
        {            
            Board[0][cur_col] = curPlyr;
            curX = 0;
            curY = cur_col;
            returner = true;
        }
        else
        {
            return returner;
        }
}
void intlzBoard(vector<vector<char> >&Board)
{
    Board.resize(ROWS);
    for (int i=0; i != ROWS; i++)
    { 
        Board[i].resize(COLS);
        for (int j=0; j != COLS; j++)
        {
            Board[i][j] = EMPTY;
        }
    }
}


/* here's the bit i Can't get
bool checkWin(vector<vector<char> >&Board,int &curX, int &curY, char &curPlyr)
{
    int checkX = 0;
    int checkY = 0;
    int checkCon = 0;
    char conn = 'N';
    while (conn == 'N')
    {
        while (Board[curX][checkY] != curPlyr)
        {
            checkY++;
        }
        while (Board[curX][checkY] == curPlyr)
        {
            checkY++;
            checkCon++;
        }
        if (checkCon != 4 || checkCon != 5)
        {
            while (Board[curX][checkY] != curPlyr)
            {
                checkY--;
            }
            while (Board[curX][checkY] == curPlyr)
            {
                checkY--;
                checkCon++;
            }
        }
        else if (checkCon == 4 || checkCon == 5)
        {
            conn = 'Y';
        }
        else
        {
            checkX = 0;
            checkY = 0;
            checkCon = 0;
            while (Board[checkX][curY] != curPlyr)
            {
                checkX++;
            }
            while (Board[checkX][curY] == curPlyr)
            {
                checkX++;
                checkCon++;
            }
            if (checkCon >= 4)
            {
                conn = 'Y';
            }
            else
            {
                checkX = 0;
                checkY = 0;
                checkCon = 0;
                while (Board[checkX][checkY] != curPlyr && checkX != 5 && checkY != 6)
                {
                    checkX++;
                    checkY++;
                }
                while (Board[checkX][checkY] == curPlyr && checkX != 5 && checkY != 6)
                {
                    checkX++;
                    checkY++;
                    checkCon++;
                }
                if (checkCon >= 4)
                {
                    conn = 'Y';
                }
                else
*/

p.s. it's the very bottom part I can't get

Member Avatar for iamthwee

>okay, I've got a connect four, 2 player, console game due today,

If only you didn't try to do it all at the last minute...maybe this will be too late.

//  This (private) method checks for a win in the
    //  horizontal plane
    = boolean checkForWinHorizontal(PlayerColour c) {

        for (int y = 0; y < numRows; y++)
            for (int x = 0; x < (numCols - 3); x++)
                if (board[x][y].equals(c)   &&
                    board[x+1][y].equals(c) &&
                    board[x+2][y].equals(c) &&
                    board[x+3][y].equals(c))
                    return true;

        //  Otherwise return false
        return false;
    }
    

    
    //  This (private) method checks for a win in the
    //  vertical plane
    private boolean checkForWinVertical(PlayerColour c) {

        for (int x = 0; x < numCols; x++)
            for (int y = 0; y < (numRows - 3); y++)
                if (board[x][y].equals(c)   &&
                    board[x][y+1].equals(c) &&
                    board[x][y+2].equals(c) &&
                    board[x][y+3].equals(c))
                    return true;

        //  Otherwise return false
        return false;
    }


    
    //  This (private) method checks for diagonal wins
    private boolean checkForWinDiagonal(PlayerColour c) {

        for (int x = 0; x < (numCols - 3); x++)
            for (int y = 0; y < (numRows - 3); y++)
                
                if (
                    //  "Forward slash" diag
                    (board[x][y].equals(c)     &&
                     board[x+1][y+1].equals(c) &&
                     board[x+2][y+2].equals(c) &&
                     board[x+3][y+3].equals(c)
                    )
                ||  
                    //  "Backslash" diag
                    (board[x+3][y].equals(c)   &&
                     board[x+2][y+1].equals(c) &&
                     board[x+1][y+2].equals(c) &&
                     board[x][y+3].equals(c)
                    )
                ) return true; 
        
        //  Otherwise return false
        return false;
    }

Taken from here. It's java but the idea should be similar?
http://www.ecs.soton.ac.uk/~icm02r/ecs/cm143/cw4.php

thanks bro..

Be a part of the DaniWeb community

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