Need help with if/then hierarchy

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mermuseBUSjpzh
Posts: 6
Joined: Sun Mar 03, 2013 8:00 am

Need help with if/then hierarchy

Post by mermuseBUSjpzh » Sat Apr 06, 2013 8:44 pm

I need some help with complex if/then hierarchy. I'm trying to turn one of my card game designs into an app but I haven't found a good breakdown of how to organize complex if/then/else statements.

Here's my problem...

I'm comparing the sum of the two opponents cards for the turn.
The basic logic is similar to Blackjack in that its bad for the total to go over 21.
The offense needs to be greater than the defense to score and they score more if they get exactly 21.



...core logic...

If the defense <= 21 and greater than the offense then they stopped the offense from scoring (causing a turnover)

If the defense is > 21 they bust and the offense scores a field goal.

If the offense <= 21 and greater than the defense then
- if offense = 21 they score a touchdown
- if offense < 21 they score a field goal

If the offense is > 21 they bust (causing a turnover)




...this is what my statement looks like right now...
...tEndDrive is the offense total and tDEndDrive is the defense total...

on mouseUp
put line 1 field "1st" into t1
put line 1 field "2nd" into t2
put line 1 field "3rd" into t3
put line 1 field "4th" into t4

put t1 + t2 + t3 + t4 into tEndDrive

put line 1 field "D1st" into tD1
put line 1 field "D2nd" into tD2
put line 1 field "D3rd" into tD3
put line 1 field "D4th" into tD4

put tD1 + tD2 + tD3 + tD4 into tDEndDrive

if tDEndDrive < tEndDrive then put tEndDrive & " / " & tDEndDrive into field "result" then
if tEndDrive > 21 then put tEndDrive & " / " & tDEndDrive & " = " & "fumble (turnover)" into field "result"
if tEndDrive < 21 then put tEndDrive & " / " & tDEndDrive & " = " & "fieldgoal" into field "result"
if tEndDrive = 21 then put tEndDrive & " / " & tDEndDrive & " = " & "Touchdown" into field "result"
else
if tDEndDrive > 21 then put tEndDrive & " / " & tDEndDrive & " = " & "fieldgoal" into field "result"
if tDEndDrive <= 21 then put tEndDrive & " / " & tDEndDrive & " = " & "Defnese! (turnover)" into field "result"
end if
end mouseUp



...bad results...

if the offense scores 19 and the defense 12 the result shows "Defense! (turnover)"
it should print "field goal" and end but it doesn't seem to be breaking out.

What am I doing wrong?


thanks,
Mermuse...

mermuseBUSjpzh
Posts: 6
Joined: Sun Mar 03, 2013 8:00 am

Re: Need help with if/then hierarchy

Post by mermuseBUSjpzh » Sat Apr 06, 2013 9:08 pm

I changed to two separate statements and it works but I don't understand why I need to use two end if's. How to make this simpler and more elegant?


...

if tEndDrive > tDEndDrive then
if tEndDrive > 21 then put tEndDrive & " / " & tDEndDrive & " = " & "fumble (turnover)" into field "result"
if tEndDrive < 21 then put tEndDrive & " / " & tDEndDrive & " = " & "fieldgoal" into field "result"
if tEndDrive = 21 then put tEndDrive & " / " & tDEndDrive & " = " & "Touchdown" into field "result"
else
end if
end if

if tDEndDrive > tEndDrive then
if tDEndDrive > 21 then put tEndDrive & " / " & tDEndDrive & " = " & "fieldgoal" into field "result"
if tDEndDrive <= 21 then put tEndDrive & " / " & tDEndDrive & " = " & "Defnese! (turnover)" into field "result"
else
end if
end if

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Need help with if/then hierarchy

Post by sturgis » Sat Apr 06, 2013 9:23 pm

Working through the logic right now. Couple questions.

If both offense and defense are over 21 what happens then?

EDIT: Ok that was 1 question.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Need help with if/then hierarchy

Post by sturgis » Sat Apr 06, 2013 9:38 pm

I sometimes have trouble getting nested ifs to work as I expect too. (and it looks like there is something going on due to the formatting of the if statements.... looks like your elses are not matching up with the proper ifs.

To get around this difficulty I usually do full if sets

Code: Select all

if blah blah blah then
   if blah blah blah then
     do something
   end if
   if blah blah blah other then
     do something else
   end if
else
   if something then
      do stuff
   end if
   if something else then
      do other stuff
   end if
end if
Makes it easier for the engine to figure out what you meant.

In this case though, I find it more readable to use an outside if, and an inside switch. (not sure I have your logic figured out yet but here goes)

Code: Select all

on mouseUp
   put field "tEndDrive" into tEndDrive -- numbers in a field for testing
   put field tdEndDrive into tDEndDrive
   
   if tEndDrive > tDEndDrive then -- initial check, which is higher
     -- switch with individual condition statements
      switch 
         case tEndDrive > 21 -- first case, tEndDrive over 21
            put tEndDrive & " / " & tDEndDrive & " = " & "fumble (turnover)" into field "result"
            break
         case tEndDrive < 21 
            put tEndDrive & " / " & tDEndDrive & " = " & "fieldgoal" into field "result"
            break
         case tEndDrive = 21 
            put tEndDrive & " / " & tDEndDrive & " = " & "Touchdown" into field "result"
            break
      end switch
   else   -- if tEndDrive < tdenddrive we do this section
      switch 
         case tDEndDrive > 21 
            put tEndDrive & " / " & tDEndDrive & " = " & "fieldgoal" into field "result"
            break
         case tDEndDrive <= 21 
            put tEndDrive & " / " & tDEndDrive & " = " & "Defnese! (turnover)" into field "result"
            break
      end switch
   end if
end mouseUp

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Need help with if/then hierarchy

Post by sturgis » Sat Apr 06, 2013 9:42 pm

How do you handle == situations and the other thing? The both over 21 issue?

mermuseBUSjpzh
Posts: 6
Joined: Sun Mar 03, 2013 8:00 am

Re: Need help with if/then hierarchy

Post by mermuseBUSjpzh » Sat Apr 06, 2013 11:08 pm

Thanks Sturgis,

-The results are checked in a specific order. The offense is always checked first and if the result is a bust or a touchdown (perfect 21) then the defenses total doesn't matter.

If the offense is over 21 then the defense succeeds.


-The defense must beat the offenses total to stop them so if they have the same result then the defense failed to stop them and the normal offense results are checked.

the offense scores immediately if their total equals 21... so there is no need to check the defenses value.

...

I'll try out the non-nested approach you showed. I'm not familiar with the switch command but I'll read up on it.
Are there any books or online tutorials that you recommend for getting a better grasp of how to use these statements. My game has a lot of special abilities that will require some complex logic when I reach the next phase.

thanks again,
Mermuse

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Need help with if/then hierarchy

Post by sturgis » Sun Apr 07, 2013 12:06 am

If you haven't looked there already, lessons.runrev.com has some good stuff. Also http://www.runrev.com/developers/lesson ... tutorials/
Might also click the resources button on the LC toolbar there are a lot of examples in there. Can also click on the user samples and see what others have done, try their stacks out etc.

My main go to information though is the dictionary (and the release notes too of course)

And here of course.

I think the main deal for you will be to break things down logically into self contained chunks. You're already doing that by knowing if this > that only these specific things can occur. Otherwise only those things can occur. Switch may not be the best way to go. If your modifiers (special cases) are applied before the value checks then what you're working on now shouldn't be affected. Apply the special cases, then do the check.

While you're thinking of things also remember you can do compound conditionals.
For example, if tOffense > 21 or tOffsense < tDefense then put "Its a turnover!" into field "result"

Here is a switch only example that I think does what you need:

Code: Select all

switch
-- in order of precedence, check for touchdown
      case field "off" is 21
         put "TOUCHDOWN!" into field "result"
         break
-- then offense over 21
      case field "off" > 21
         put "Turnover!" into field "result"
         break
-- then defense over 21
      case field "Def" > 21  
         put "Field goal!" into field "result"
         break
-- then defense defeats offense
      case field "def" > field "off" 
         put "Turnover!" into field "result"
         break
-- then offense defeats defense
      case field "off" > field "def" 
         put "Field goal!" into field "result"
   end switch
If matching scores are a push, that can be added easily, either by using a default entry in the switch block (because all other possibilities have been eliminated, if none of the others match, then they must be equal)

Also, I was thinking if this is going to be very complex, yet within a specific range of scores, the simplest solution might be a lookup table. Build a 2d matrix of messages. (either in a variable as straight text, or in an array, either works) then you can totally skip the conditionals.

So to make the example easy to type.. if you have a 3x3 grid of messages, and the score is 1 for offense and 2 for defense, grab the message contained in item 1, line 2 of the variable and use it. No comparisons necessary, just grab the text from the table.

Or if using an array keyed by offense and defense, you could just: put myArray[1][2] into field "result" Initial setup of the array or table might take a little bit depending on complexity, but as long as it is unchanging you only have to do it once. (should save the data to a property for perma storage of course) And it should be blindingly fast.

Just a thought.

mermuseBUSjpzh
Posts: 6
Joined: Sun Mar 03, 2013 8:00 am

Re: Need help with if/then hierarchy

Post by mermuseBUSjpzh » Sun Apr 07, 2013 1:43 am

Wow the switch is powerful... I like that it also has a default function if none of the cases are true.
It seems like a much better fit for what I'm trying to do.

I tried your code and it worked well except that it didn't deal with the values being equal so I added one more case at the end that says...

Code: Select all

-- then offense ties defense
      case field "off" = field "def" 
         put "Field goal!" into field "result"
and now it gets the perfect results every time.
Thanks again...

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Need help with if/then hierarchy

Post by sturgis » Sun Apr 07, 2013 1:55 am

YW, glad it worked. Was just thinking, another way you can simplify it is to separate by end result.

So, for all things that lead to a field goal..

Code: Select all

switch 
   case condition1 that leads to field goal OR condition 2 that leads to a field goal OR condition 3 that leads to a field goal
      put "FIELD GOAL!" into field result
      break
   case condition1 that leads to touchdown OR condition 2 that leads to touchdown
      put "TOUCHDOWN!" into field "result"
      break
   case condition 1 that leads to turnover OR condition2 that leads to turnover
      put "TURNOVER!" into field "result"
      break
 end switch
And still think that a lookup table might be very handy too. If you're interested in looking at that method i'll throw a quick sample stack together for you to look at.

mermuseBUSjpzh
Posts: 6
Joined: Sun Mar 03, 2013 8:00 am

Re: Need help with if/then hierarchy

Post by mermuseBUSjpzh » Sun Apr 07, 2013 2:30 am

Yes I'd love to see an example of a lookup table. I've been doing a lot of tutorials but I really want to get the big picture of what tools work best for different types of logic checks. I've seen "switch" used many times before but never understood what was going on until now.

The conditional approach seems like what I need for picking the best moves for my AI opponent. I need to compare their current value with the values still in their hand to allow them to plan their future turns. I've been looking for a way to compare multiple values all at once.


...


here's my other logic problem:
My cards have 4 values - 1 for each down position so the cards value will be different based on when it is played.

ex.
card values 3:7:10:0
if played on the first down its value is 3
but if its played on the fourth down its value is 0


at the start of a drive (players offensive turn) I would like to check all the legal value combinations to see if any result in a 21. By legal I mean the combination must be made up of 1 number from each position and the chosen values can't be on the same card.

ex.
10:3:9:5
0:11:6:6
could result in a turn 2 touchdown.
comparing 2 values is easy but trying to look farther ahead 3-4 downs is very difficult.


...
I can compare item 1 values with item 2 values
then item 1 values with item 2 values and item 3 values
etc

but I need to know that the values that work together are not all on the same card? This is the logic problem that has been troubling me. I can forgo this long term planning and just allow the AI to make the best decision for the current down and not try and plan a whole sequence ahead but if they have a great combo in their hand I'd love to be able to find it.

I always seem to be struggling with the fact that I don't know what the best tool is for my current problem.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Need help with if/then hierarchy

Post by sturgis » Sun Apr 07, 2013 2:49 am

Here is a link to a very simple lookup. Not sure I have all the data perfect, but it should give an idea of what i'm talking about.
https://dl.dropbox.com/u/11957935/lookuptable.livecode

There are 2 sliders, top is offense, bottom is defense.
There is a field with the lookup data just so you can see it, you'd want to store your table in a property or something of course.

Set the sliders to whatever value and click the lookup button, and it just grabs the corresponding value from the cell in question.

One nice thing about this is it makes it easy to have slightly different messages for variety. 20 to 1? "You destroyed that on, perfect field goal!" etc. Also makes it easy to adjust the messages.

As for your new problem, i'll have to think on it. Too fuzzy right at the moment (its getting late) to even be sure I understand the question.

EDIT: All the code is in the button. If you see stuff in the stack script, ignore it I was looking at something else. The version at that link now is the right one.

mermuseBUSjpzh
Posts: 6
Joined: Sun Mar 03, 2013 8:00 am

Re: Need help with if/then hierarchy

Post by mermuseBUSjpzh » Sun Apr 07, 2013 3:03 am

Interesting... it looks like the resolution charts used in hex and counter war games!

This method looks very useful for more complex and irregular systems.
Thank you for so generously sharing your time and knowledge.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Need help with if/then hierarchy

Post by Mark » Sun Apr 21, 2013 1:42 pm

Hi Mermuse,

I read your request for books or tutorials. I'm about the publish a book with the title "Progoramming LiveCode for the Real Beginner". I think this book contains quote a lot useful stuff for you. Chapter two discusses if-then-else and the switch control structure and many other basic subjects. I'm currently making the last corrections and preparing the book for print. I expect it to be printed in approximately two weeks. You can find more information on my company's homepage.

Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply