Page 2 of 2

Re: Another Tick Tack Toe Question!?

Posted: Fri Dec 31, 2010 8:09 pm
by Trebor29
You the man Neo! ;)

Happy New Year :P

Re: Another Tick Tack Toe Question!?

Posted: Sun Jan 02, 2011 12:12 am
by Neo
Wish you the same and thanks for the complements :D

Re: Another Tick Tack Toe Question!?

Posted: Sat Jan 08, 2011 5:59 pm
by Trebor29
Hi Neo,
Sorry to be a pain, but I need some more help with the TickTackToe game... I know you love it really! :lol:

The problem I am having is calculating the winners on a diagonal, as it only recognises a diagonal win when on the center diagonals i.e. anywhere on the corner to corner(s)... nowhere else!

I have been looking through the code this morning but am going round in circles! :x
Ive just pasted the diagonal check code here, think ive already pasted the the rest previously. lol

Code: Select all

 // diag check
                if (P == 0){
                   str1 = "";
                   str2 = "";
                   for (x = 0; x < N; x++){
                        str1 += tickTackButton[x, x].Text; // forward
                        str2 += tickTackButton[x, N - 1 - x].Text; // backward
                   }

                   if (str1.IndexOf(comb_X) != -1 || str2.IndexOf(comb_X) != -1)
                   {
                      P = 1;
                      PlayerWins(P);
                   }
                   else if (str1.IndexOf(comb_O) != -1 || str2.IndexOf(comb_O) != -1)
                   {
                      P = 2;
                      PlayerWins(P);
                   }
                }

Re: Another Tick Tack Toe Question!?

Posted: Sun Jan 09, 2011 2:34 am
by Neo
Had a quick look at that. I don't have much time today so you need to check this code.
  1. Define these new variables.

    Code: Select all

    int len_comb, N1;
    string str3, str4;
  2. Fine following code.

    Code: Select all

    comb_O = arr_comb_O[gridSizeComboBox.SelectedIndex];
    Add following code after that.

    Code: Select all

    len_comb = comb_X.Length;
  3. This code needs to come after the diagonal check code.

    Code: Select all

                // other cross dir check
                if (P == 0)
                {
                    N1 = 1;
    
                    for (y = 1; y < N; y++)
                    {
                        for (x = N1; x < N; x++)
                        {
                            str1 += tickTackButton[x, x - N1].Text; // forward upper
                            str2 += tickTackButton[x - N1, x].Text; // forward lower
    
                            str3 += tickTackButton[x - N1, (N - 1) - x].Text;   // backward upper
                            str4 += tickTackButton[x, (N - 1) - (x - N1)].Text; // backward lower
                        }
    
                        if (str1.IndexOf(comb_X) != -1 || str2.IndexOf(comb_X) != -1 || str3.IndexOf(comb_X) != -1 || str4.IndexOf(comb_X) != -1)
                        {
                            P = 1;
                            PlayerWins(P);
                            break;
                        }
                        else if (str1.IndexOf(comb_O) != -1 || str2.IndexOf(comb_O) != -1 || str3.IndexOf(comb_O) != -1 || str4.IndexOf(comb_O) != -1)
                        {
                            P = 2;
                            PlayerWins(P);
                            break;
                        }
                        else
                        {
                            N1++;
    
                            if ((N - N1) < len_comb)
                            { // Not enough length to match
                                break;
                            }
                        }
                    }
                }
    

Re: Another Tick Tack Toe Question!?

Posted: Sun Jan 09, 2011 10:37 pm
by Trebor29
Works sweat as a nut...! :P

I have been looking at Connect 4 source code trying to tackle it myself, but its very hard to try to unpick someone elses code, especially at my level!

When im working i'll have to put you on my pay roll.. :lol:

Thanks "again" Neo.

Re: Another Tick Tack Toe Question!?

Posted: Mon Jan 10, 2011 2:26 pm
by Neo
You are welcome....

Always try to group things and find a combination. This is the way to simply coding...