Another Tick Tack Toe Question!?

Topics on common programming languages
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Another Tick Tack Toe Question!?

Post by Trebor29 » Fri Dec 31, 2010 8:09 pm

You the man Neo! ;)

Happy New Year :P
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Another Tick Tack Toe Question!?

Post by Neo » Sun Jan 02, 2011 12:12 am

Wish you the same and thanks for the complements :D
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Another Tick Tack Toe Question!?

Post by Trebor29 » Sat Jan 08, 2011 5:59 pm

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);
                   }
                }
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Another Tick Tack Toe Question!?

Post by Neo » Sun Jan 09, 2011 2:34 am

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;
                            }
                        }
                    }
                }
    
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: Another Tick Tack Toe Question!?

Post by Trebor29 » Sun Jan 09, 2011 10:37 pm

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.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Another Tick Tack Toe Question!?

Post by Neo » Mon Jan 10, 2011 2:26 pm

You are welcome....

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

Return to “.Net & Other Programming”