Thread: Connect 4 game

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Aeoskype View Post
    Could you just write that checkwin function for me? Only for horizontal or vertical win.
    Ah ha, when the "can you do it for me" finally comes out, we know it's a case of trying to get us to do your homework. If this were just for learning, you would get your mind stuck into solving the problem and manage to write at least something for it.
    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"

  2. #17
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Quote Originally Posted by iMalc View Post
    Ah ha, when the "can you do it for me" finally comes out, we know it's a case of trying to get us to do your homework. If this were just for learning, you would get your mind stuck into solving the problem and manage to write at least something for it.
    No, it's not my homework. I'm doing it for myself and trying to learn c++. I'm trying to solve it, but nothing works. Elysia mentioned that i should try with flowchart or pseudo code, but i don't know what it is. Tried to google it but still can't understand. I think that i should change something here:
    Code:
    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++;
           }
    should i change column into a variable? And is it possible to change row pos0,pos1,pos2... into one variable?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Flowchart - Wikipedia, the free encyclopedia
    Regardless of homework or not, we do not usually help people solve their problems by fixing it for them. We tend to give them a nudge in the right direction, so to speak.
    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.

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Ok, but could you write some little code? Just a start of it, because i don't know how to start.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's better if you just begin with that flowchart instead. You will see how easy it is.
    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.

  6. #21
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Yes!!! Finally finished my checkwin function:
    Code:
    int checkwin(){
        for(int i=0; i<4; i++) // check for horizontal
        {
            for(int j=0; j<6; j++)
            {
                if(place[j][i] == cPlayerMark && place[j][i+1] == cPlayerMark && place[j][i+2] == cPlayerMark && place[j][i+3] == cPlayerMark)
                {
                        return 1;
                }
            }
    
    
        }
    
    
        for(int i=0; i<4; i++) // check for vertical
        {
            for(int j=0; j<3; j++)
            {
                if(place[j][i] == cPlayerMark && place[j+1][i] == cPlayerMark && place[j+2][i] == cPlayerMark && place[j+3][i] == cPlayerMark)
                {
                    return 1;
                }
            }
        }
    
    
        for(int i=0; i<4; i++) // check for diagonal right to left
        {
            for(int j=0; j<6; j++)
            {
                if(place[j][i] == cPlayerMark && place[j+1][i+1] == cPlayerMark && place[j+2][i+2] == cPlayerMark && place[j+3][i+3] == cPlayerMark)
                {
                    return 1;
                }
            }
        }
    
    
        for(int i=0; i<4; i++) // check for diagonal left to right
        {
            for(int j=0; j<6; j++)
            {
                if(place[j][i] == cPlayerMark && place[j+1][i-1] == cPlayerMark && place[j+2][i-2] == cPlayerMark && place[j+3][i-3] == cPlayerMark)
                {
                    return 1;
                }
            }
        }
    }
    Works perfectly. Thanks to all who helped me!

    EDITED: Not perfectly.
    Last edited by Aeoskype; 04-24-2013 at 07:42 AM.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Aeoskype View Post
    I think that i should change something here:
    should i change column into a variable? And is it possible to change row pos0,pos1,pos2... into one variable?
    You should change to using an array for posN, and then this code will be 1/7th of the length.

    Quote Originally Posted by Aeoskype View Post
    Yes!!! Finally finished my checkwin function:
    Code:
    int checkwin(){
        for(int i=0; i<4; i++) // check for horizontal
        {
            for(int j=0; j<6; j++)
            {
                if(place[j][i] == cPlayerMark && place[j][i+1] == cPlayerMark && place[j][i+2] == cPlayerMark && place[j][i+3] == cPlayerMark)
                {
                        return 1;
                }
            }
    
    
        }
    
    
        for(int i=0; i<4; i++) // check for vertical
        {
            for(int j=0; j<3; j++)
            {
                if(place[j][i] == cPlayerMark && place[j+1][i] == cPlayerMark && place[j+2][i] == cPlayerMark && place[j+3][i] == cPlayerMark)
                {
                    return 1;
                }
            }
        }
    
    
        for(int i=0; i<4; i++) // check for diagonal right to left
        {
            for(int j=0; j<6; j++)
            {
                if(place[j][i] == cPlayerMark && place[j+1][i+1] == cPlayerMark && place[j+2][i+2] == cPlayerMark && place[j+3][i+3] == cPlayerMark)
                {
                    return 1;
                }
            }
        }
    
    
        for(int i=0; i<4; i++) // check for diagonal left to right
        {
            for(int j=0; j<6; j++)
            {
                if(place[j][i] == cPlayerMark && place[j+1][i-1] == cPlayerMark && place[j+2][i-2] == cPlayerMark && place[j+3][i-3] == cPlayerMark)
                {
                    return 1;
                }
            }
        }
    }
    Works perfectly. Thanks to all who helped me!

    EDITED: Not perfectly.
    Yes not perfectly alright.
    If you came up with that on your own then well done, I'm glad you stuck in there and thought it through. Getting there on your own is always more satisfying.
    The problem remaining is that you have buffer overruns and buffer underruns. These can be corrected for by adjusting the bounds of all of the for-loops.
    E.g. if you loop contains [i-1]. Then start i at one, not zero.
    You've already corrected line 18 for this, but you have more to go.
    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"

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