
-----------------------------------
SwAnK
Thu Apr 21, 2005 4:05 pm

Function Failing to Give a Result
-----------------------------------
Here is my function, I was trying to create that  if a straight was rolled with 5 dice then so many points were awarded, when executed, it states that my function is failing to give a result. Can someone show me why? Here is my code

var count : int := 0
var roll : array 1 .. 5 of int

function straight_on (die : array 1 .. * of int) : boolean
    var one, two, three, four, five, six : boolean := false

    for i : 1 .. 5
        case die (1) of
            label 1 :
                one := true
            label 2 :
                two := true
            label 3 :
                three := true
            label 4 :
                four := true
            label 5 :
                five := true
            label 6 :
                six := true
        end case
    end for
    if (one and two and three and four and five) or (two and three and four and five and six)
            then
        result true
    elsif false then count+=1000
    end if
end straight_on


proc rolling
    for i : 1 .. 5
        roll (i) := Rand.Int (1, 6)
        put "Rolled ", roll (i)
    end for
end rolling
rolling
if straight_on (roll) then
    count += 1000
end if
 put "count", count , ""

thanx

-----------------------------------
Token
Thu Apr 21, 2005 4:34 pm


-----------------------------------
okay, i dont see how that could help you doing it that way, you should probibly order them by size, (not sure how to do that) and then check to see if the seccond one is 1 less than the first, and if that is true 4 times then its a small straight and 5 times its a large straight ... providing that your going for some sort of yatzee thing

-----------------------------------
wtd
Thu Apr 21, 2005 4:41 pm


-----------------------------------
if (one and two and three and four and five) or (two and three and four and five and six) then
   result true
elsif false then 
   count += 1000
end if

A few things here:


You have no "else".  There's a possibility your if and elsif aren't exhaustive, so nothing would happen.
elsif false then

false, by it's very nature, isn't true.  This elsif will never happen.
The code in the elsif doesn't return anything.  Even if it could be evaluated, the problem would still exist.

-----------------------------------
Cervantes
Thu Apr 21, 2005 4:41 pm


-----------------------------------
You're function fails to give a result if 

(one and two and three and four and five) or (two and three and four and five and six) 

is false.
You need something to result in an 'else' part of that if statement.

No matter what, your function needs to give a result.  This could also be achieved by having a 'result something' at the end of the procedure.  Note that this doesn't mean that that result will always be returned, because if the function reaches a result line earlier in the code, it will terminate and result that value immediately.  Thus, the last result line is only reached under certain conditions.

-----------------------------------
SwAnK
Thu Apr 21, 2005 10:50 pm


-----------------------------------
so then what can i put for my else statment?? :?:

-----------------------------------
Tony
Fri Apr 22, 2005 8:32 am


-----------------------------------
so then what can i put for my else statment?? :?:
well if it's not returning true from the if part, I'm going to assume that you want

if  then
     return true
else
     return false
end if


-----------------------------------
wtd
Fri Apr 22, 2005 5:55 pm


-----------------------------------
so then what can i put for my else statment?? :?:
well if it's not returning true from the if part, I'm going to assume that you want

if  then
     return true
else
     return false
end if


It should be noted that this, while Tony means well, is bad code.

When you write the "if" statement (and it's accompanying "else", the condition is either true or false.  That's your boolean value right there.  So instead of that, let's just say:

result 

Consider a real example:

if 1 = 1 then
   result true
else
   result false
end if

vs. 

result 1 = 1

-----------------------------------
Tony
Fri Apr 22, 2005 6:12 pm


-----------------------------------
It should be noted that this, while Tony means well, is bad code.
Indeed it is. On it's own. That was provided as a reference structure to use, since SwAnK also had some attempts at counter+ type of thing.. :wink:
