Thread: Connect 4 with 'average-AI'...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Connect 4 with 'average-AI'...

    HI,

    Finally completed my game of connect 4. The source and the exe is included in the zip. I admit that there are sections in my source that can be reduced, but i think i already spent too much time on this, so i decided to use more lines in some places and instead make it simpler. Pls leave comments and suggestions...

    When the game is only two-player the source code is 302 lines, but now with its AI implemented, the code bumped up to 974 lines, thnx to all the AI functions. If anyone think it shouldn't be this long, pls drop me a few words on how to improve it, just a few words....pls.....

    Actually there's one problem i still can't figure out with this program. I can't seem to validate the user data inputted. I tried to use isdigit but can't get it to work since i think i have to pass the actual character like this '6' to the function. I used isalpha in my code, but it still doesn't work out. WHenever the user input a letter ( which is suppose to be a column number ), the code fall into a infinite loop for some reason. If anyone can correct this ( i really tried but still can't do it ) please tell me how you did it, i'll really appreciate.

    Anyway this is my first game in C ( my tic tac toe was done in vb ), i'm always wiling to learn.

    Thank you, thank you...

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Great work! Though when it asks for col to drop in the previous choice still remains.
    Do something like this:

    cout << "Which col: \b\b\b\b"; //4 extra spaces and then four backspaces

    And the beep and the bring it on.... in the beginning is annoying. The intro on the other hand is really cool! Keep that one...

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    And make it flip a coin or something and then say Player X goes first!, sleep a sec or two and then start.

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Do you know the solution to my problem btw.?

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Updated, now included some of the suggestions said above. But still can't fix the validate input problem. Pls give a hand if you know the answer.

    And leave comments for the game. And if you have, suggest what i should make next. ^_^
    Last edited by Nutshell; 04-28-2002 at 05:18 AM.

  6. #6
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Hmmm..... cant you do something like


    if(input < maxcolnr && input > mincolnr){
    //do stuff
    } else {
    cout << flush << "\n\nYou must have done something wrong";
    }

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I know that, and i already have that.

    BUt if the user enter a letter everything doesn't work anymore. The code will compare its ascii value to mincol and maxcol. If you have a look at my code u'll see what i mean.

    thnx

  8. #8
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i think it quite nice, though to make the blocks/lines you could use ascii chars which will make it look neater.

    and some small finishing touches can be given.
    Last edited by ihsir; 04-28-2002 at 10:55 AM.
    -

  9. #9
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Hey maybe you could make it so that you have different colors.
    Like red o's for computer and blue o's for player

  10. #10
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    take all of the input as a character as in the first scanf! this should fix it.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    HI,

    I already tried that. But it still doesn't work. It treats all my input as invalid.

  12. #12
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    To fix the input problem, do what ihsir suggested, use ASCII values.

    Comment on improving the AI, though, the game is very good.

    The AI should be programmed so it doesn't give the human a chance to win. In my first game I played the AI gave me the oppurtunity to win. Here was how I was set up: I had an O then a blank then two Os in a row. Under that blank was a blank again with an X under that. On the AI's turn it filled that missing blank giving me the chance to connect four.

    If you have any interest in iupdating the game, which is fine if you don't, I would improve the AI in certain situations like that. Never should the AI give the human a chance to connect four.

    Here is a screenshot of right after the AI made it's 'bad' move...and I colored the X red so it's easy for you to see.

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Hi,

    What version of my program did you download? I already fixed that i think. ANd i also tested it many times. Anyway here is the latest version of the game.

    For the AI i made it so the comp will NOT fill in a gap for the opponent to win. Can you pls download this version and try again?

    THnx

  14. #14
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    I tried the second version you had posted. To alleviate any further confusion on versions I would just edit my first post including the new version and make a new post stating that there is a new version in your first post.

    Well, I tried it again, and the AI won me the game again.

    Here is another screenshot, and again I marked the X red.

    BtW, the 'thinking...' thing is cool, but I would lessen the time of it from what seems like 1-2 seconds to about .5-1 second or a ratio of that (i.e. if it is a 1 second delay then make it a .25-.5 delay. i.e. if it is a .5 second delay make it a .25 second delay). And the intro takes too long too. I guess I'm a tad bit impatient.

  15. #15
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I see what you mean. I can reproduce what you did too. This is how my AI works. It generates a random column, and put a piece in there, if then the opponent can win it undo the move and generate a new random number and try that again. It does this for 5000 times and if it still can't find a safe column to put a piece into then it just pick a random column and put a piece in there. Do you think it can be like the loop already intereated 5000 times?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. AI contest: Connect four
    By Sang-drax in forum Contests Board
    Replies: 66
    Last Post: 11-11-2004, 01:54 PM
  4. Connect four AI demo
    By Sang-drax in forum Game Programming
    Replies: 6
    Last Post: 06-15-2004, 09:21 AM
  5. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM