
-----------------------------------
Gooie
Mon Dec 10, 2007 8:15 pm

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.

     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

-----------------------------------
Ultrahex
Mon Dec 10, 2007 11:27 pm

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)

    (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.  :) 

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:
if (a = true) then ...

% Is Same As

if (a) then ...
For Small Conditions:
if (a) then
    b := false
end if

% Is Same As

b := (not a)

-----------------------------------
Clayton
Tue Dec 11, 2007 8:24 am

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
Tue Dec 11, 2007 10:10 pm

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)

    (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.  :) 

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:
if (a = true) then ...

% Is Same As

if (a) then ...
For Small Conditions:
if (a) then
    b := false
end if

% Is Same As

b := (not a)

Nice! Thanks, it worked!
