Thread: help needed for my connect four programme.

  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.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    where did that function you showed come from? Why if you could write the rest have you not posted an attempt of your own at writing the required function? What exactly do you have trouble with, because nobody is going to just offer a complete piece of work for you to appropriate. sorry- that costs money ;->
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User KatherineNguyen's Avatar
    Join Date
    Jan 2012
    Posts
    3
    my fren gav it to me.it was her previous assignment.she only gav the verticalwin/horizontalwin/diagonalwin function.thing is,i'm nt sure how to make do with it n modify..any tips how do i modify it? or is there any other way to create my verticalwin function?please help.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by KatherineNguyen View Post
    or is there any other way to create my verticalwin function?
    You could think about it and do it yourself, and learn something.

  5. #5
    Registered User KatherineNguyen's Avatar
    Join Date
    Jan 2012
    Posts
    3
    i've been trying since friday.if i could do it by then,i wouldnt hav to ask around for help.i've done 80% of my program.now the only this dats buigging me is the verticalwin/horizontalwin/diagonalwin function.i would realy appreciate if someone can at least giv hints on how to modify.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You should post what you have attempted as far as refactoring this function goes, but really i would abandon and code something that is more in line with your own work, it may be you are trying to fit a square peg into a round hole (sure thats a pun here...) and actually making more work for yourself trying to crowbar in the code your friend gave you. especially if you are having difficulty understanding what she has given you anyhow.

    Also consider if its the best way to do it that she has shown you? Just because she managed to get it working in her project does not mean it will translate neatly to your version.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    my fren gav it to me.it was her previous assignment.she only gav the verticalwin/horizontalwin/diagonalwin function.
    Your "fren" at StackOverflow? Which may have originated here?

    Lies, lies, lies. Stop trying to get everyone to do your homework for you. If you don't want to program, then drop out of your class and go study something else.

    5 1/2 steps to get others to doing your homework
    Last edited by rags_to_riches; 01-09-2012 at 08:19 AM.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Sorry you best ask that buddy at StackOverflow!
    Last edited by rogster001; 01-09-2012 at 09:39 AM. Reason: REMOVED code example
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I like the technique of using a female name and a random human female's picture as an avatar. Nice touch.

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