About Store Forum Documentation Contact



Post Reply 
Tic Tac Toe Victory (Solved)
Author Message
runewake2 Offline
Member

Post: #1
Tic Tac Toe Victory (Solved)
As a quick intro to Esenthel I wrote a Tic Tac Toe game that uses buttons to represent each of the squares and adds a quit button, a scoreboard and multiple games.

I have run into a few problems. First off the victory script does not work. Well it doesn't work all the time. The program runs by executing a MarkButton script when a button is clicked, Checking for victory and then incrementing the turn to the other player. Everything works except the victory script. Here is the Victory Checking script (Button numbers correlate to the numbers on the numpad):

Code:
void VictoryCheck(Char16 CheckChr,Int Player)
{
   bool won;
   if(button_1.text.first() == CheckChr && button_2.text.first() == CheckChr && button_3.text.first() == CheckChr) won = true;
   else if(button_4.text.first() == CheckChr && button_5.text.first() == CheckChr && button_6.text.first() == CheckChr) won = true;
   else if(button_7.text.first() == CheckChr && button_8.text.first() == CheckChr && button_9.text.first() == CheckChr) won = true;
   else if(button_1.text.first() == CheckChr && button_4.text.first() == CheckChr && button_7.text.first() == CheckChr) won = true;
   else if(button_2.text.first() == CheckChr && button_5.text.first() == CheckChr && button_8.text.first() == CheckChr) won = true;
   else if(button_3.text.first() == CheckChr && button_6.text.first() == CheckChr && button_9.text.first() == CheckChr) won = true;
   else if(button_1.text.first() == CheckChr && button_5.text.first() == CheckChr && button_9.text.first() == CheckChr) won = true;
   else if(button_3.text.first() == CheckChr && button_5.text.first() == CheckChr && button_7.text.first() == CheckChr) won = true;
   else won = false;

   if (won == true)
   {
      if (Player == 1)
      {
         p1_vic ++;
         winner = 1;
      }
      else
      {
         p2_vic ++;
         winner = 2;
      }
   }
}


My next problem involves my score counter. I am trying to draw two ints. One for each players victory count. The numbers however do not appear. My Draw function looks like this:

Code:
void Draw()
{
   D  .clear(WHITE);
   Gui.draw ();
   D.text(0,0.85,msg);
   //Draw Victories
   if (p1_vic > p2_vic)
      D.circL(Color(255,0,0,255),.075,-0.35,-0.9);
   else if (p2_vic > p1_vic)
      D.circL(Color(255,0,0,255),.075,0.35,-0.9);
   D.text(-0.35,-0.9,S+p1_vic);
   D.text(0.35,-0.9,S+p2_vic);
}

I have tried to move the text around but nothing works. Did I miss something?

If you need the full code I uploaded it to my site here: http://code.google.com/p/theprojects/wik...=TicTacToe. I would prefer a helpful hint rather then a "here's the code" type of answer as I feel I will learn better if I have to fix the problems myself.

Thanks for the help and time.
(This post was last modified: 12-09-2010 02:01 AM by runewake2.)
12-08-2010 02:50 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #2
RE: Tic Tac Toe Victory
Not sure why your victory check isn't working, without actually stepping through it in the debugger, which is what you should try doing. It probably has something to do with the equality check with CheckChr.

As for your text issue, replace the "" with S, i.e. S + p1_vic.
12-08-2010 06:13 AM
Find all posts by this user Quote this message in a reply
menajev Offline
Member

Post: #3
RE: Tic Tac Toe Victory
Not sure order of '==' and '&&' so try changing to:
Code:
if( (button_1.text.first() == CheckChr) && (button_2.text.first() == CheckChr) && (button_3.text.first() == CheckChr) ) won = true;
12-08-2010 11:47 AM
Find all posts by this user Quote this message in a reply
runewake2 Offline
Member

Post: #4
RE: Tic Tac Toe Victory
Thanks for the help with the text. I was wondering what the S in other peoples code meant. That just casts p1_vic to a String correct?

I will try to debug the program later today as I have no access to Visual Studio or Esenthel right now.
12-08-2010 06:24 PM
Find all posts by this user Quote this message in a reply
menajev Offline
Member

Post: #5
RE: Tic Tac Toe Victory
S is an empty Str so S+int runs Str::operator+(int).
(This post was last modified: 12-08-2010 06:39 PM by menajev.)
12-08-2010 06:38 PM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #6
RE: Tic Tac Toe Victory
You said you would prefer a hint, so here it is: Do not use an "Equal" sign to compare the values of two strings because doing so only compares their address in memory.
(This post was last modified: 12-08-2010 07:09 PM by Driklyn.)
12-08-2010 07:07 PM
Find all posts by this user Quote this message in a reply
runewake2 Offline
Member

Post: #7
RE: Tic Tac Toe Victory
I'm not comparing strings. I'm comparing Char's and unless this is different then java this should be valid.

I thought:
button.text.first() returns a Char16
since Char's are simply ASCII code comparing them compares their integer values
thus comparing Char16 == Char16 should be valid

Is it different in Esenthel/C++?
12-08-2010 07:16 PM
Find all posts by this user Quote this message in a reply
Tottel Offline
Member

Post: #8
RE: Tic Tac Toe Victory
As far as I know, you still handle a Char as a string, not as a number.
12-08-2010 07:50 PM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #9
RE: Tic Tac Toe Victory
Actually, runewake2, I believe you are correct. Since you are indeed using Char16 and not Str, using == should be valid. My mistake.

I think I see your problem. Take another look at your if statements. They're missing something.
12-08-2010 08:06 PM
Find all posts by this user Quote this message in a reply
Chris Offline
Member

Post: #10
RE: Tic Tac Toe Victory
There are several things wrong with this - but the Char checking is fine smile

1. Your victory check if loop is wrong, examine the "else".
2. Your victory check condition is called in the wrong place (before the buttons are marked) - I'd re-insert it after this process, e.g. in the update-loop.

You don't have to look at this code until you've tried the solution yourself.
http://pastebin.com/cCG0RJk0

Please also try to learn more about references. Also there are many design improvements you could make with this, its a bit "all over the place" - but you have the beginning of modular programming grasped.
12-08-2010 08:06 PM
Find all posts by this user Quote this message in a reply
runewake2 Offline
Member

Post: #11
RE: Tic Tac Toe Victory
Chris you make me feel like an idiot... but you were right.

I can't believe I forgot something so elementary as the else's. Lol'd a little since it took so many eyes to find it.

Everything works now. Thanks for the help.

While this topic is still active: is there any way to make the code seem less "all over the place"? Also comparing Chars is perfectly legal.

Finally I have updated the code on my site. When I have the time I'll do it in my OP as well.
12-09-2010 01:58 AM
Find all posts by this user Quote this message in a reply
Chris Offline
Member

Post: #12
RE: Tic Tac Toe Victory
(12-09-2010 01:58 AM)runewake2 Wrote:  Everything works now. Thanks for the help.

While this topic is still active: is there any way to make the code seem less "all over the place"? Also comparing Chars is perfectly legal.

I thought I'd spend my lunchbreak procrastinating and tried it myself, easier showing code than explaining. I used a 2d array of buttons, it seemed like a more natural mapping to the problem. Also, I set some early termination conditions on the victory state check. I split the game up into a Byte "Turn" which represents the state - see comments (I would have used 'State' as the variable name, but it was already taken in EE.

Please have a look/compile the code and tell me what you think (may need to change the engine.pak location depending on where you copy it):

Raw code: http://pastebin.com/raw.php?i=jpiPDgak
Or also available at PasteBin: http://pastebin.com/jpiPDgak

Trivia: 80 lines of code, 8 is my favourite number grin
12-09-2010 02:22 PM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #13
RE: Tic Tac Toe Victory (Solved)
You should add this to the Wiki, Chris. wink
12-09-2010 06:12 PM
Find all posts by this user Quote this message in a reply
runewake2 Offline
Member

Post: #14
RE: Tic Tac Toe Victory (Solved)
Chris that is a lot nicer then mine. Mine was 200+ yours is only 80. I'll see what I can do to work that into the game. I don't want to just copy the code.

Using an Array is what I had originally planned on doing. However I was going to create an Array of Char's and then assign the value every new turn. Your way is a lot simpler. I don't know why I didn't think of doing it this way.

Thanks again Chris. Your help is greatly appreciated.
12-09-2010 07:19 PM
Find all posts by this user Quote this message in a reply
Post Reply