Page 1 of 1

Some code problems...

PostPosted: Wed Apr 22, 2009 12:55 am
by Caaz Games
okay i have this code, and it's not working... i'm not sure why.. it's a simple code...
Code: Select all
if(Turn==1)
{
    if(A1.animpos==0)
    {
        if(A2.animpos==0)
        {
            if(A3.animpos==0)
            {
                if(B1.animpos==0)
                {
                    if(B2.animpos==0)
                    {
                        if(B3.animpos==0)
                        {
                            if(C1.animpos==0)
                            {
                                if(C2.animpos==0)
                                {
                                    if(C3.animpos==0)
                                    {
                                        int n;
                                        n = rand(9)
                                        if(n==0)A1.animpos=2;     //line 27, one problem
                                        if(n==1)A2.animpos=2;
                                        if(n==2)A3.animpos=2;
                                        if(n==3)B1.animpos=2;
                                        if(n==4)B2.animpos=2;
                                        if(n==5)B3.animpos=2;
                                        if(n==6)C1.animpos=2;
                                        if(n==7)C2.animpos=2;
                                        if(n==8)C3.animpos=2;
                                    }
                                    if(C3.animpos==1)
                                    {
                                        int n;
                                        n = rand(8)
                                        if(n==0)A1.animpos=2;  //line37 the next.
                                        if(n==1)A2.animpos=2;
                                        if(n==2)A3.animpos=2;
                                        if(n==3)B1.animpos=2;
                                        if(n==4)B2.animpos=2;
                                        if(n==5)B3.animpos=2;
                                        if(n==6)C1.animpos=2;
                                        if(n==7)C2.animpos=2;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


it says expected ; on line 27 and 37...hmm?

Re: Code isn't working...

PostPosted: Wed Apr 22, 2009 1:12 am
by pyrometal
for line 27: (semicolon)
Code: Select all
n = rand(9);


and same deal for line 37.

And also your code is extremely inefficient...

Re: Code isn't working...

PostPosted: Wed Apr 22, 2009 1:14 am
by Caaz Games
the code isn't done yet :roll: :P
thanks,... i guess i was one line off lol, thanks again :D

+point thingy :D

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 1:27 am
by Caaz Games
Wait wait wait...
i can see why this is a bad code.. it will take a long time to type it all out...
i need something to check the actors, A1, A2, A3, B1, B2, B3, C1, C2, C3... i need a code to check all these actor's animpos for 1 if it's one it needs to exclude them from a rand code that changes one of them to the animpos 2... any ideas other than mine? hmm....my only idea was to have them with a bunch of if codes... but then what if elseif and then the others were not if... ugh... brain hurting..

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 2:38 am
by skydereign
Well you could incorporate some switch statements, get rid of if n==0, if n==1 and so on. For the most part, it seems hard to optimize without string splicing. I don't know what you are trying to do, but it looks like it would have been easier to make A1, and others all the same actor and deal with clones.

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 3:03 am
by Caaz Games
okay let me tell you about what this game is...
tic tac toe...
i have a simple (or so i thought) idea of how to get things working... i'd have 9 actors...with them in their rows... something like

________Row A Row B RowC
Column 1[____][____][____]
Column 2[____][____][____]
Column 3[____][____][____]
so actors would be named a1,a2,a3... and so on with one animation, with animpos being...
0 = white
1 = X
2 = O
3 = |
4 = \
5 = /
6 = _
yeah... so on mouse click on one..
Code: Select all
if(turn==0)
{
   if(animpos==0)
      {
            animpos=1;
            turn=1
      }
}

and then with the AI i need it to check all the actors to see which ones are animpos=1 so it can exclude it from changing to animpos=2.

does that help?

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 4:52 am
by skydereign
Yeah, I would just use clones. That way, you can search through the actor's animpos with a for loop. You would have to use getclone and actor*s but it would make this much faster, and much more efficient. Here is the beggining of one way, don't use this directely, as it is not that great. I can explain this concept if you want. But you could also just create an array, as you have 9 squares. Or you could probably skip the Actor* and just use the getclone as a substitute.
GlobalCode
Code: Select all
Actor * getCloneIdx (const char *cname, int cindex)    //g_GetCloneByIndex, its original name
{
    char buffer[50];

    sprintf(buffer,"%s.%i",cname,cindex);

    return(getclone(buffer));
}

AI script search
Code: Select all
Actor* square;
for(i=0;i<9;i++);
{
    square=getCloneIdx(SQUARE, i);
    if (square->animpos!=0)
    {
         goto end:
    }
}
end:

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 5:26 am
by Caaz Games
Yeah, i'm going to need some explaining, i'm pretty much still a noob lol.

...
so i should use clones... instead of seperate actors...

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 8:26 pm
by skydereign
Using clones would make the code more efficient, and a lot easier to read. I'll post an explanation later today. Right now I am busy. I'll probably just edit this post.

Actually, this is how I would do it. I would just use an array to store everything. It is a lot less complicated than what I was explaining. So whenever you wanted to search if all animpos are 0, you are instead searching the array.
GlobalCode
Code: Select all
Actor* squareP; // this allows for getClone
int SQUARE[9];
Actor * getCloneIdx (const char *cname, int cindex)    //g_GetCloneByIndex, its original name
{
    char buffer[50];

    sprintf(buffer,"%s.%i",cname,cindex);

    return(getclone(buffer));
}

Event that initiates computer
Code: Select all
int random;
do
    random=rand(9);
while (SQUARE[random]!=0);

squareP=getCloneIdx("square",random);
SQUARE[random]=2;
squareP->animpos=2;

This should work. I will explain later. The main idea I was getting at was the do while loop. It eliminates the need for all those lines of code, and it uses clones.

Re: Some code problems...

PostPosted: Wed Apr 22, 2009 10:18 pm
by Fuzzy
Even nine actors are overkill. This is a job that would work well with just one canvas to draw on, one canvas to show the board lines, and maybe 2 actors to store the X and O shapes. But you could draw them too.
I would make a small nine element array. I wouldnt make it a 2d array even. In theory you dont even need this.

Code: Select all
char squares[9];


Let a blank equal 0, an X equal 1 and a O equal 5 in the array. That is, when the player clicks a square, fill in a 5 at that array point if he is playing with O.

Image

As the blanks are filled in, store the values of rows, columns and diagonals in a array variable.

Code: Select all
int scores[9]; // I use nine, the last value tracks if the game is over.


The reason for this is that it doesn't make much sense to continuously add all of them up. Do it once and be done with it. Otherwise its making the computer work for no reason. Sure, its not a lot of work, but the philosophy counts very much on bigger programs.

The reason for the values I chose were simple. They are prime numbers and in the space that you have, three X's can never add up to a value that can be mistaken for one O. This is called meaningful data. It means something to the programmer, and its also easy to make it mean something to the AI. A bunch of if statements is vague.

Now all you have to do is check each row and compare its value against 3 and 15. if either is the result, that tells which player has won.

As an aside, the word information comes from this technique. Our possible formations exclude the possibility of data collisions. That is, there are no ambiguous results. We can also refer to this system as scoring, and it will allow the AI to decide where the next best move is.

if the row/column score is 3 or 15, somebody won.
If the row/column score is 7 or 11, then the computer may not use that row. It is full.

Since players take turns, and X always goes first, the O can only ever have 4 squares, and the score has to be 25 at the end of the game. so...

Code: Select all
 if (scores[9] == 25) { game over();}

Re: Some code problems...

PostPosted: Fri Apr 24, 2009 3:14 pm
by Fuzzy
I killed the topic, didnt I?

Re: Some code problems...

PostPosted: Fri Apr 24, 2009 9:56 pm
by pyrometal
lol, not at all Fuzzy, it should be very educative to a lot of users in the forum! The methods you have proposed is only one out of many ways to solve the problem and certainly is a good one. Careful consideration and planning in a project are critical; it can easily make or break everything you are trying to accomplish. I'm sure you know all of this already though, lol. ttyl now!

Re: Some code problems...

PostPosted: Fri May 08, 2009 12:13 am
by jimmynewguy
Code: Select all
Actor * getCloneIdx (const char *cname, int cindex)
{
    char buffer[50];

    sprintf(buffer,"%s.%i",cname,cindex);

    return(getclone(buffer));
}
Actor * target;
int RANDOM;

Code: Select all
if(turn == 1)
{
start:
RANDOM=rand(9);
target = getCloneIdx("blank", RANDOM);
if(target->animpos == 3)
{
target->animpos = 0;
turn = 0;
}
else
{
goto start;
}
}

I'm using these two codes, so what would be the best way to make the lines things?