Thread: Making Board Games...

  1. #16
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    ok thanks but that function is working fine for some reason. I know c++ that was just easier for me to keep track of. Ill go back and change it and see if it makes a difference. No need to be rude

  2. #17
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok, now i have found the general area that the problem is coming from. What i think is happening is that the variables are not being defined (compc and compr) If anyone has the time to sift through the deeply nested if statements please do so Id appreciate it. You can also see the AI behind my game. Whether it will work or not well as of now its not but i feel as if it will once i find what statements still need to be fixed/ added in to make it work. So if someone could take a look at it and see if they can find the problem without reprimanding me for using too many if statements or other things like that I dont know why im so psyched about this, maybe its just cause i have spent literally all day doing this, lol. So here she is the heart of my program that must be in cardiac arrest as of now :P

    Oh and alot of this isnt finished yet. There will be even more if statements (though not many) and some of the if statements are empty cause i havent gotten around to filling them yet

    Code:
    void evaluateboard(char testboard[3][3], int compc, int compr, char player, char computer)
    {
        //evaluates a board in order to find the best move
        //  init vars
        int rowi=0;
        int rowii=0;
        int rowiii=0;
        int coli=0;
        int colii=0;
        int coliii=0;
        int diagi=0;
        int diagii=0;
        int randrow=0;
        int randcol=0;
        //begin evaluation
        // if empty board
        if(testboard[0][0] == testboard[0][1] == testboard[0][2] == testboard[1][0] == testboard[1][1]
        == testboard[1][2] == testboard[2][0] == testboard[2][1] == testboard[2][2] == '\0')
        {
            //randomly pick
            srand((unsigned)time(NULL));
            randrow=(rand()%3);
            randcol=(rand()%3);
            compr=randrow;
            compc=randcol;
        }    
        // columns and rows
        for(int i=0; i<3; i++)
        {
            //columns
            //  2 in a row
            if((testboard[i][0] || testboard[i][1] || testboard[i][2])=='\0')
            {
                if(((testboard[i][0]==testboard[i][1]) ||
                    (testboard[i][1]==testboard[i][2]) ||
                    (testboard[i][0]==testboard[i][2])) &&
                    ((testboard[i][0] && testboard[i][2]) != '\0'))
                {
                    if(testboard[i][0]==computer || testboard[i][2]==computer)
                    {
                        if(i==0)
                        {
                            coli+=50;
                        }
                        else if(i==1)
                        {
                            colii+=50;
                        }
                        else if(i==2)
                        {
                            coliii+=50;
                        }
                    }
                    else if(testboard[i][0]==player || testboard[i][2]==player)
                    {
                        if(i==0)
                        {
                            coli+=20;
                        }
                        else if(i==1)
                        {
                            colii+=20;
                        }
                        else if(i==2)
                        {
                            coliii+=20;
                        }
                    }
                }
                //rows        
                //  2 in a row 
                if(((testboard[0][i]==testboard[0][i]) ||
                    (testboard[1][i]==testboard[2][i]) ||
                    (testboard[0][i]==testboard[2][i])) &&
                    ((testboard[0][i] && testboard[2][i]) != '\0'))
                {
                    if(testboard[0][i]==computer || testboard[2][i]==computer)
                    {
                        if(i==0)
                        {
                            rowi+=50;
                        }
                        else if(i==1)
                        {
                            rowii+=50;
                        }
                        else if(i==2)
                        {
                            rowiii+=50;
                        }
                    }
                    else if(testboard[0][i]==player || testboard[2][i]==player)
                    {
                        if(i==0)
                        {
                            rowi+=20;
                        }
                        else if(i==1)
                        {
                            rowii+=20;
                        }
                        else if(i==2)
                        {
                            rowiii+=20;
                        }
                    }
                }
            }
        }    
        //test for single places
        //  rows and columns
        
        //diagonals LR=1 RL=2
        //  testing for 2 in a row
        if((testboard[0][0] || testboard[1][1] || testboard[2][2])=='\0')
        {
            if((testboard[0][0]==testboard[1][1] ||
                testboard[1][1]==testboard[2][2] ||
                testboard[0][0]==testboard[2][2]) &&
                (testboard[0][0] && testboard[2][2]) != '\0')
            {
                if(testboard[0][0]==computer || testboard[2][2]==computer)
                {
                    diagi+=50;
                }
                else if(testboard[0][0]==player || testboard[2][2]==player)
                {
                    diagi+=20;
                }
            }
            if((testboard[2][0]==testboard[1][1] ||
                testboard[1][1]==testboard[0][2] ||
                testboard[2][0]==testboard[0][2]) &&
                (testboard[2][0] && testboard[0][2]) != '\0')
            {
                if(testboard[2][0]==computer || testboard[0][2]==computer)
                {
                    diagi+=50;
                }
                else if(testboard[2][0]==player || testboard[0][2]==player)
                {
                    diagi+=20;
                }
            }
        }                    
        //find best location
        int drow=0;
        int pospositions;
        int poscolone;
        int poscoltwo;
        int posrowone;
        int posrowtwo;
        int dcol=0;
        if((rowi || rowii || rowiii) > ((coli && colii && coliii) && (diagi && diagii)))
        {
            //rows are the priority
            if(rowi > (rowii && rowiii))
            {
                //row one is the priority
                compr=0;
                drow=0;
            }
            else if(rowii > (rowi && rowiii))
            {
                //row two is the priority
                compr=1;
                drow=1;
            }
            else if(rowiii > (rowi && rowii))
            {
                //row three is the priority
                compr=2;
                drow=2;
            }
            pospositions=0;
            poscolone=0;
            poscoltwo=0;
            for(int c=0; c<3; c++)
            {
                if(testboard[c][drow]=='\0')
                {
                    pospositions++;
                    if(pospositions==1)
                    {
                        poscolone=c;
                    }
                    else if(pospositions==2)
                    {
                        poscoltwo=c;
                    }        
                }
            }         
            if(pospositions==1)
            {
                compc=poscolone;
            }
            else if(pospositions==2)
            {
                if(poscolone==1 && poscoltwo==2)
                {
                    if(coli > colii)
                    {
                        compc=0;
                    }
                    else if(coli < colii)
                    {
                        compc=1;
                    }    
                }
                else if(poscolone==2 && poscoltwo==3)
                {
                    if(colii > coliii)
                    {
                        compc=1;
                    }
                    else if(colii < coliii)
                    {
                        compc=2;
                    }
                }               
                else if(poscolone==1 && poscoltwo==3)
                {
                    if(coli > coliii)
                    {
                        compc=0;
                    }
                    else if(coli < coliii)
                    {
                        compc==2;
                    }
                }
            }                     
        }
        else if((coli || colii || coliii) > ((rowi && rowii && rowiii) && (diagi && diagii)))
        {
            //columns are the priority
            if(coli > (colii && coliii))
            {
                //col 1 is priority
                compc=0;
                dcol=0;
            }
            else if(colii > (coli && coliii))
            {
                //col 2 is priority
                compc=1;
                dcol=1;
            }
            else if(coliii > (coli && colii))
            {
                //col 3 is priority
                compc=2;
                dcol=2;
            }
            posrowone=0;
            posrowtwo=0;
            pospositions=0;
            for(int r=0; r<3; r++)
            {
                if(testboard[dcol][r]=='\0')
                {
                    pospositions++;
                }
                if(pospositions==1)
                {
                    posrowone=r;
                }
                else if(pospositions==2)
                {
                    posrowtwo=r;
                }        
            }
            if(pospositions==1)
            {
                compr=posrowone;
            }
            else if(pospositions==2)
            {
                if(posrowone==1 && posrowtwo==2)
                {
                    if(rowi > rowii)
                    {
                        compr=0;
                    }
                    else if(rowi < rowii)
                    {
                        compr=1;
                    }
                }                      
                else if(posrowone==2 && posrowtwo==3)
                {
                    if(rowii > rowiii)
                    {
                        compr=1;
                    }
                    else if(rowii < rowiii)
                    {
                        compr=2;
                    }
                }
                else if(posrowone=1 && posrowtwo==3)
                {
                    if(rowi > rowiii)
                    {
                        compr=0;
                    }
                    else if(rowi < rowiii)
                    {
                        compr=2;
                    }
                }
            }                            
        }
        else if((diagi || diagii) > ((rowi && rowii && rowiii) && (coli && colii && coliii)))
        {
            //diagonals are the priority
            if(diagi > diagii)
            {
                //diag one is priority
                
            }
            else if(diagii > diagi)
            {
                //diag two is priority
                
            }        
        }               
    }
    Thanks for anyone who has gotten this far (and read the code) or anyone else who can help me, its greatly appreciated. I started off thinking that i would be able to work my way to chess, now i dont think so lol

    Thanks again everyone!
    Last edited by Junior89; 12-27-2005 at 02:49 AM.

  3. #18
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I programmed Tic Tac Toe a long time ago. The method I used was to hardcode moves required for a human or AI move. You can programatically generate these. Then based on the selected difficulty you can decide whether or not to use the correct data for the move or choose a random one. It worked quite well and it was very easy to code.

    To do this, number each cell.

    0 1 2
    3 4 5
    6 7 8

    Now let's code the moves for the top row.

    0 - 1,3,4
    1 - 0,2,4
    2 - 1,4,5

    Now you might not want to use the actual data unless the player has moved more than once. Provide some randomness to the moves and then use the known data to respond once the board gets too complex to use random moves. This way the computer won't always move next to you or near the number selected all the time. Of course you would want to check to make sure the slot on the board was empty before placing the piece.

    EDIT: You might want to group these into single moves and double moves. For instance a double move would look like this:

    if (0 and 4) move is 8 or we lose

    or
    0,4,8

    These all can be encoded into one byte move codes.
    Last edited by VirtualAce; 12-27-2005 at 02:58 AM.

  4. #19
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok thanks, ill take a look at implementing that design tomorrow, i gotta go to bed now though

    But thanks ill definitely take a look, if its simpler than my algorithm im willing to try it

  5. #20
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    These all can be encoded into one byte move codes.
    Another reason to have a thorough look through the articles on AI Horizon.com - it teaches you good strategies for storing this kind of information - like how to store the location of every piece on a chess board.

  6. #21
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    on AIHorizon in that minimax tree bit? It explained everything about the minimax tree but not really how to implement it. Ill check out the site again. Im still working on my mathematical approach, i saw some code for another TTT program that used a mathematical approach so well see how i do on this. Maybe after i get this working ill try my hand at linked lists and such. Although i still have no idea how to go about implementing it. Ill keep looking, thanks!

  7. #22
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    If you are good at reading code, there is a complete tic-tac-toe game, including minmax algorithm, in the AI threads. Feel free to ask questions about it, I'll answer whatever I can. For tic-tac-toe, you can search the tree all the way to the bottom. A game that you can do after tic-tac-toe, that is closer to chess (but still fairly easy) is connect four. Next would be checkers. Then chess. Also, you might find the following link helpful, I know I did:

    http://www.seanet.com/~brucemo/topics/topics.htm
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #23
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks! Ill take a look, i was thinking if i ever got this Tic Tac Toe to work that i would maybe move on to connect four. However i have to get this TTT to work first Thanks again, ill take a look!

    EDIT: I dont see it on these ai threads... Perhaps on another site? Thanks
    Last edited by Junior89; 12-29-2005 at 02:30 AM.

  9. #24
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok, i just did some more searching for actual programs and came up with an interesting one. However this individual did use a Minimax tree they coded the entire thing into switch statements. The source file code is over 8000 lines long! This surely cannot be the best way to implement a minimax tree, however it is effective. She claims the computer cannot be beat. However i think if it were to go first it could be. Heres the link:

    http://moonflare.com/code/tttwithoutloops.php

    Good night all!

  10. #25
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  11. #26
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks ill look it over, luckily there isnt a big difference between C and C++

    Why do people still use C instead of C++?

    Thanks again, ill take a look!

  12. #27
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Why do people still use C instead of C++?
    Some people prefer it. It's simpler. Maybe people learnt it and it's just what they're comfortable with. And then of course there's always the need to maintain old programs, etc...

  13. #28
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Oh ok, thanks. Looks more complicated to me i guess cause im used to C++ and i havent gotten too complicated with C++ lol

    Anyway thanks, im still looking at a bunch of code and articles and stuff. Hopefully ill figure it out. Thanks everyone for all the help!

  14. #29
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    If you like, I can post my tic tac toe engine. Its just under 100 lines of code and uses the alphabeta optimisation of minimax. I actually tried to code it in as few lines as possible, without obfuscating anything. It's very simple to understand, would you like to see?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Biggest problem when making games?
    By Yarin in forum Game Programming
    Replies: 15
    Last Post: 09-29-2007, 01:53 PM
  2. starting making games like warcraft 3
    By Mythic Fr0st in forum Game Programming
    Replies: 13
    Last Post: 01-22-2007, 09:31 PM
  3. making games
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 02-23-2002, 02:23 PM
  4. best language for making games?
    By Unregistered in forum Game Programming
    Replies: 9
    Last Post: 11-09-2001, 05:37 AM
  5. making money and playing games...
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-10-2001, 07:13 PM