Thread: Connect 4 game

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    34

    Connect 4 game

    Hi all, i'm new at c++ so please be patient with me .I'm writing connect 4 game and stuck at checkwin function.

    Here's my code:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    char place[6][7];
    int iPlayerTurn=1, turn, win;
    int pos0=5, pos1=5, pos2=5, pos3=5, pos4=5, pos5=5, pos6=5;
    int tie=0; // if the board is full, game ends as draw.
    char cPlayerMark; // player mark, X and O.
    
    
    void drawBoard() // function to draw a board
    {
        cout << " 1   2   3   4   5   6   7" << endl;
        for(int a=0; a<=5; a++)
        {
            for(int b=0; b<=6; b++)
    
    
                cout << char(218) << char(196) << char(191) << " ";
                cout<<'\n';
    
    
            for(int b=0; b<=6; b++)
    
    
                cout<<char(179)<<place[a][b]<<char(179)<<" ";
                cout<<'\n';
    
    
            for(int b=0; b<=6; b++)
    
    
                cout<<char(192)<<char(196)<<char(217)<<" ";
                cout<<'\n';
    
    
        }
    }
    
    
    int checkwin() // totaly stuck here
    {
    
    
    }
    
    
    int main()
    {
       drawBoard();
    
    
       do
       {
           cout << "Player " << iPlayerTurn << " turn: " << endl;
           cin >> turn;
    
    
           if(iPlayerTurn == 1) // sets player mark between X and O
           {
               cPlayerMark = 'X';
           }
           else
           {
               cPlayerMark = 'O';
           }
    
    
           if(iPlayerTurn == 1) // after 1 move changes the player
           {
               iPlayerTurn = 2;
           }
           else
           {
               iPlayerTurn = 1;
           }
    
    
           if(turn == 1)
           {
                place[pos0][0] = cPlayerMark;
                pos0--; // every time you place mark, it goes up by 1 column
                tie++; // not sure if this necessary , but if tie == 42, game is draw, because then its full board.
           }
           else if(turn == 2)
           {
               place[pos1][1] = cPlayerMark;
               pos1--;
               tie++;
           }
           else if(turn == 3)
           {
               place[pos2][2] = cPlayerMark;
               pos2--;
               tie++;
           }
           else if(turn == 4)
           {
               place[pos3][3] = cPlayerMark;
               pos3--;
               tie++;
           }
           else if(turn == 5)
           {
               place[pos4][4] = cPlayerMark;
               pos4--;
               tie++;
           }
           else if(turn == 6)
           {
               place[pos5][5] = cPlayerMark;
               pos5--;
               tie++;
           }
           else if(turn == 7)
           {
               place[pos6][6] = cPlayerMark;
               pos6--;
               tie++;
           }
           else
           {
               cout << "Wrong number." << endl; // if wrong move, player turn and mark stays the same
               if(cPlayerMark == 'X')
               {
                   iPlayerTurn = 1;
               }
               if(cPlayerMark == 'O')
               {
                   iPlayerTurn = 2;
               }
           }
    
    
    
    
           drawBoard();
       }while(tie!=42);
    
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    You can try adding a condition to your do-while cycle, such as: do <code>, while(tie!=42 || checkwin != true), where checkwin is a boolean function and returns true if there is a winner and false if not.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    I think i know what you are saying, but i don't know how to write that boolean function. Also, my cPlayerMark and place[][] are chars, so doesn't it make it harder to do?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    Quote Originally Posted by Aeoskype View Post
    I think i know what you are saying, but i don't know how to write that boolean function. Also, my cPlayerMark and place[][] are chars, so doesn't it make it harder to do?
    you have to pass the place into the function
    Code:
    bool checkwin(char place[6][7],int iPlayer)
    and then make 3 separate cycles to check for horizontal, vertical and diagonal returning true in the cycle or false in the end if none of the cycles found any. (you'll also need to pass the player that just played or at least the 'X'/'O' char so the functions knows what to check for).
    Last edited by killme; 04-22-2013 at 09:20 AM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    But how i check for horizontal, vertical and diagonal win?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How would you do it on paper?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    But how i check for horizontal, vertical and diagonal win?
    You will need quite a bit of work/study to get there. As Elysia says, it is probably best to start with pen and paper - That way you can start seeing the relationships between 'nodes' in your board building up and think of ways you might determine a checking method - one thing that will become apparent is that each token is (potentially) surrounded by 8 other positions - so one approach can be to check sequentially if any of the tokens in an adjacent slot matches the token at the current node - If so, you can know the direction of a potential winline and continue checking in that direction - ie move to that node and check in the same direction if there is a matching token - if it fails you return to your original token and check the next adjacent one in the opposite direction for a match and repeat the above etc. - because the current token may be at the centre of a winline for example - If it fails that way then you have to return to your original token and check the next adjacent one in a the next direction and repeat the above. If your matched count reaches 4 then you have a win. If not move to next node and check the same way.
    Don't forget to accomodate the edges of the board.
    Last edited by rogster001; 04-22-2013 at 01:19 PM.
    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'"

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Aeoskype View Post
    But how i check for horizontal, vertical and diagonal win?
    Using loops.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Ok, but i don't know how to make loops for this. Can someone give one example for horizontal or vertical check? I would appreciate it.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you do know how to make loops - you already used nested loops in your other code
    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'"

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    I know how to make loops, but i don't know how to make loop to check for win. This is what i am asking - to write a loop for horizontal or vertical win check.

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Well what sort of things do you think you need to do to check? I gave you a description of a method you could try and implement. I think it might work well with recursion actually but equally fine as loops of course
    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'"

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Could you just write that checkwin function for me? Only for horizontal or vertical win.

  14. #14
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You really have to make an attempt - spoon feeding is not going to help you - the CheckWin function should be a fun challenge for you if are interested in programming. You will be so pleased when you crack the problem and have a working function, and you will have learnt a lot getting there! Plus this 'check a connect four win' must have been asked a gazillion times all over the place - you can easily research some sample code - just for ideas to get you going.
    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'"

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Aeoskype View Post
    Could you just write that checkwin function for me? Only for horizontal or vertical win.
    Use a flowchart or pseudo code. Give an attempt with that, at least.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hey im trying to create a connect four game please help.
    By EarthDevistator in forum C Programming
    Replies: 3
    Last Post: 11-16-2012, 12:13 PM
  2. Connect four game help
    By kenneth_888 in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2007, 08:42 AM
  3. New Connect Four Game
    By PJYelton in forum Game Programming
    Replies: 4
    Last Post: 01-17-2003, 10:13 AM
  4. Connect 4 game
    By sundeeptuteja in forum Game Programming
    Replies: 6
    Last Post: 08-12-2002, 11:09 PM
  5. Connect Four game...need help
    By Ion Blade in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2002, 06:18 PM