Thread: help needed for my connect four programme.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User KatherineNguyen's Avatar
    Join Date
    Jan 2012
    Posts
    3

    help needed for my connect four programme.

    hi there. katy here.
    I am doing a "Connect 4" program for my assignment and i've done the main part, which I should not change (my lecturer told me not to). .can anyone help me to convert this function below to suit my program??i need my program to have a verticalwin/horizontalwin and a diagonalwin function. and also when all the tiles is full,i want my program to give an output such as "the game is a tie".please help me.i'm new here.so forgive me if i'm wrong.
    the function that i need modified is:

    Code:
    bool check(int a, int b)
    {
        int vertical = 1;//(|)
        int horizontal = 1;//(-)
        int diagonal1 = 1;//(\)
        int diagonal2 = 1;//(/)
        char player = place[a][b];
        int i;//vertical
        int ii;//horizontal
    
        //check for vertical(|)
        for(i = a +1;place[i][b] == player && i <= 5;i++,vertical++);//Check down
        for(i = a -1;place[i][b] == player && i >= 0;i--,vertical++);//Check up
        if(vertical >= 4)return true;
    
        //check for horizontal(-)
        for(ii = b -1;place[a][ii] == player && ii >= 0;ii--,horizontal++);//Check left
        for(ii = b +1;place[a][ii] == player && ii <= 6;ii++,horizontal++);//Check right
        if(horizontal >= 4) return true;
    
        //check for diagonal 1 (\)
        for(i = a -1, ii= b -1;place[i][ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
        for(i = a +1, ii = b+1;place[i][ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right
        if(diagonal1 >= 4) return true;
    
        //check for diagonal 2(/)
        for(i = a -1, ii= b +1;place[i][ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
        for(i = a +1, ii= b -1;place[i][ii] == player && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left
        if(diagonal2 >= 4) return true;
    
        return false;
    }
    i need it to suit my current programme.please help.
    Code:
    #include <iostream>
    
      #include <fstream>
    
      using namespace std;
    
      void drawboard (ostream &, char [6][7], int, int);
    
      int humanmove (int player, char board [6][7], int rows, int columns);
    
      int verticalwin (char board[6][7], int rows, int columns, int moverow, int movecolumn);
    
      int main ()
    
      {
    
    cout<< "Connect Four is a two-player game in which the players first ";
    cout<< " choose a letter   and then take turns dropping their letters from the top into a seven-column,";
    
    cout<< "    six-row vertically-suspended grid.";
    
    cout<< " The pieces fall straight down, occupying the next available space within the column. ";
    
    cout<< " The objective of the game is to connectfour of one's own discs of the same color next to each other vertically,        horizontally, or diagonally before one's opponent can do so. ";
    
    ofstream outfile;
    outfile.open("gamelog01.txt");
    char board[6][7] = {0};
    
    int player;
    int row;
    int col;
    int times;
    bool win; 
    int moverow;
    int movecolumn;
    
    drawboard(cout, board,6,7);
    
    while (player = 1)
    {
        humanmove(player, board, 6, 7);
        verticalwin(board, 6, 7, moverow, movecolumn);
    
        player = 2;
        humanmove(player, board, 6, 7);
        verticalwin(board, 6, 7, moverow, movecolumn);
    }
    
    outfile.close();
    return 0;
    }
    
    //-------------------------------------------------------------
    
    void drawboard  (ostream & out, char b[6][7], int r, int c)
    
    {
    
    out << endl;
    for (int x = r-1; x >= 0; x--)
    {
        out << "  |";
    
        for (int y = 0; y < c-1; y++)
            out << "----";
    
        out << "---|" << endl << x << " |";
    
        for (int y = 0; y < c-1; y++)
            if (b[x][y] == 0)
                out << "   |";
            else
                out << " " << b[x][y] << " |";
    
        if (b[x][c-1] == 0)
            out << "   |" << endl;
        else
            out << " " << b[x][c-1] << " |" << endl;
    }
    
    out << "  |";
    
    for (int y = 0; y < c-1; y++)
        out << "----";
    
    out << "---|" << endl; 
    out << " ";
    
    for (int y = 0; y < c; y++)
        out << "   " << y;
    
    out << endl;
    }
    
    int humanmove  (int player, char board[][7], int rows, int columns)
    
    {
    
    char symbol;
    
    if(player == 1)
        symbol = 'X';
    else
        symbol = 'O';
    
    int col;
    cout << "What column would Player " << player << " like to play in?: " ;
    cin >> col;
    
    while ((col < 0) || (col >= columns))
    {
        cout << "Invalid selection. There aren't that many columns. Please enter a different    column number: ";
        cin >> col;
    }
    
    int row;
    for(row = 5; row >= 0; row--)
    { 
        if(board[row][col] != 0)
            break;
    }
    
    board[row+1][col] = symbol;
    
    if (row == 'X' || row == 'O')
    {
        cout << "Column " << col << " is full. Please choose another: ";
        cin >> col;
    }
    else 
    {
        drawboard(cout, board, 6, 7);
    }
    }
    
    int verticalwin  (char board[6][7], int rows, int columns, int moverow, int movecolumn)
    
    {
    
    }
    Last edited by KatherineNguyen; 01-08-2012 at 01:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tax Programme help
    By Ben Dover in forum C Programming
    Replies: 3
    Last Post: 07-19-2011, 09:11 PM
  2. First C++ Programme, Inspection Needed Please?
    By Danerrr in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2010, 04:32 PM
  3. help me with this programme
    By ChrisWang in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2008, 02:21 AM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  5. c programme
    By Dileep Kumar in forum C Programming
    Replies: 1
    Last Post: 11-14-2001, 04:47 AM

Tags for this Thread