Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Shorter Death Handler For Bomberman!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Gooie




PostPosted: Mon Dec 10, 2007 8:15 pm   Post subject: Shorter Death Handler For Bomberman!

Hey, I'm relatively new to Turing and was wondering if their was any simple way to make this piece of code any shorter it seems extremely long and unnecessary. I also don't want a super long unreadable line of an overly complicated if statement.
Turing:

     for i : 1 .. 7
            if bomb_t (i) > 10 then
                if (player_gx = bomb_gx (i) and player_gy = bomb_gy (i)) then
                    alive := false
                elsif (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) - 1) then
                    alive := false
                elsif (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) + 1) then
                    alive := false
                elsif (player_gx = bomb_gx (i) and player_gy = bomb_gy (i)) then
                    alive := false
                elsif (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) - 1) then
                    alive := false
                elsif (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) + 1) then
                    alive := false
                elsif (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) - 2) and right_condition = true then
                    alive := false
                elsif (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) + 2) and left_condition = true then
                    alive := false
                elsif (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) + 2) and up_condition = true then
                    alive := false
                elsif (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) - 2) and down_condition = true then
                    alive := false
                end if
            end if
     end for
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Mon Dec 10, 2007 11:27 pm   Post subject: Re: Shorter Death Handler For Bomberman!

Yes, there is a much better way visually of writing this code.

start by factoring out your: player_gy = bomb_gy (i), and player_gx = bomb_gx (i)
(note that two of your conditions are actually the same! im gonna assume you meant for one to be the corresponding missing one)

Turing:
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i))
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) - 1)
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) + 1)
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) + 2) and up_condition = true
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) - 2) and down_condition = true

    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i))
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) - 1)
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) + 1)
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) - 2) and right_condition = true
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) + 2) and left_condition = true

    % Which Creates

    if ((player_gx = bomb_gx (i)) then
        if (player_gy = bomb_gy (i)) or
                (player_gy = bomb_gy (i) - 1) or
                (player_gy = bomb_gy (i) + 1) or
                ((player_gy = bomb_gy (i) + 2) and up_condition) or
                ((player_gy = bomb_gy (i) - 2) and down_condition) then
            alive := false
        end if
    end if

    if ((player_gy = bomb_gy (i)) then
        if (player_gx = bomb_gx (i)) or
                (player_gx = bomb_gx (i) - 1) or
                (player_gx = bomb_gx (i) + 1) or
                ((player_gx = bomb_gx (i) + 2) and left_condition) or
                ((player_gx = bomb_gx (i) - 2) and right_condition) then
            alive := false
        end if
    end if


And there is also other ways to simplify this even more using absolute function and other methods that actually may look nicer, this is just an example of how to deal with big messy if statements. Smile

Also depending on your context, and what you want it to do, their may even be an easier way to simplify based on the problem you are trying to accomplish by these boolean statements.


Side Notes:
Turing:
if (a = true) then ...

% Is Same As

if (a) then ...

For Small Conditions:
Turing:
if (a) then
    b := false
end if

% Is Same As

b := (not a)
Clayton




PostPosted: Tue Dec 11, 2007 8:24 am   Post subject: RE:Shorter Death Handler For Bomberman!

Just a small nitpicky thing, but what if you want b was false even if a was false? (Something's happened in your program to make b false, and you want it to stay that way for example) There might be a point where that could be an issue. Just pointing that out
Gooie




PostPosted: Tue Dec 11, 2007 10:10 pm   Post subject: Re: Shorter Death Handler For Bomberman!

Ultrahex @ December 10th, 11:27 pm wrote:
Yes, there is a much better way visually of writing this code.

start by factoring out your: player_gy = bomb_gy (i), and player_gx = bomb_gx (i)
(note that two of your conditions are actually the same! im gonna assume you meant for one to be the corresponding missing one)

Turing:
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i))
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) - 1)
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) + 1)
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) + 2) and up_condition = true
    (player_gx = bomb_gx (i) and player_gy = bomb_gy (i) - 2) and down_condition = true

    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i))
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) - 1)
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) + 1)
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) - 2) and right_condition = true
    (player_gy = bomb_gy (i) and player_gx = bomb_gx (i) + 2) and left_condition = true

    % Which Creates

    if ((player_gx = bomb_gx (i)) then
        if (player_gy = bomb_gy (i)) or
                (player_gy = bomb_gy (i) - 1) or
                (player_gy = bomb_gy (i) + 1) or
                ((player_gy = bomb_gy (i) + 2) and up_condition) or
                ((player_gy = bomb_gy (i) - 2) and down_condition) then
            alive := false
        end if
    end if

    if ((player_gy = bomb_gy (i)) then
        if (player_gx = bomb_gx (i)) or
                (player_gx = bomb_gx (i) - 1) or
                (player_gx = bomb_gx (i) + 1) or
                ((player_gx = bomb_gx (i) + 2) and left_condition) or
                ((player_gx = bomb_gx (i) - 2) and right_condition) then
            alive := false
        end if
    end if


And there is also other ways to simplify this even more using absolute function and other methods that actually may look nicer, this is just an example of how to deal with big messy if statements. Smile

Also depending on your context, and what you want it to do, their may even be an easier way to simplify based on the problem you are trying to accomplish by these boolean statements.


Side Notes:
Turing:
if (a = true) then ...

% Is Same As

if (a) then ...

For Small Conditions:
Turing:
if (a) then
    b := false
end if

% Is Same As

b := (not a)


Nice! Thanks, it worked!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: