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

Username:   Password: 
 RegisterRegister   
 Monsters
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
magicman




PostPosted: Sat Jul 09, 2005 9:31 am   Post subject: Monsters

ok, so this is what i have for code sofar, with the monsters attacking.
code:

var ans : string
var mhp : int := 25
var hp : int := 100
var lvl1a : int
var att : int
proc lvl1
    randint (lvl1a, 1, 6)
    put "he attacks you for ", lvl1a
    hp := hp - lvl1a
    put "your health is now ", hp
    delay (400)
end lvl1
proc menu
    put "What do you want to do?"
    put "(A)ttack"
    put "(D)efend"
    get ans
end menu
proc attack
    randint (att, 1, 6)
    put "You hit him for ", att
    mhp := mhp - att
    put "The monsters health is now ", mhp
end attack

loop
    menu ()
    if ans = "a"| ans = "A" then
        attack ()
        put "Hit any key to continue..."
        var ch4 : string (1)
        getch (ch4)
        cls
        lvl1 ()
    elsif ans = "d"| ans = "D" then
        put "You defend from the monster."
        put "he attacks you for ", lvl1a - 2
    end if
    if mhp = 0 then
        exit
    end if
end loop


the only problem is when i want the program to exit the loop when "mhp" will equal zero. for some reason, i think this is how you do it, but its not working. am i doing something thats not possible?
Sponsor
Sponsor
Sponsor
sponsor
Bacchus




PostPosted: Sat Jul 09, 2005 10:56 am   Post subject: (No subject)

try:
code:
exit when mhp<=0
Delos




PostPosted: Sat Jul 09, 2005 1:34 pm   Post subject: Re: Monsters

Let's clean this code up a bit why don't we? We'll start by declaring a character type. This can apply to both hero's and monsters:

Turing:

type character :
  record
    HP : int
    Atk : int
    Def : int
  end record

var myHero : character
var aMonster : character

function initCharacter (_HP, _Atk, _Def : int) : character
  var _char:character
  _char.HP := _HP
  _char.Atk := _Atk
  _char.Def := _Def
  result _char
end initCharacter

myHero := initCharacter (100, 20, 10)
aMonster := initCharacter (50, 5, 0)


Notice a few things: a single type will contain all the information about the character that you need, in a single variable. This saves having to declare a bunch of unnecassary variables. Also, you can have multiple characteristics for a single character. Possibilities are endless.
I've used a function to initialize the variables as opposed to a procedure. This feels more intuitive, since 'myHero' gets the characteristics of '(100, 20, 10)'.

For your action calls, you can now use Rand.Int()'s that incorporate the charcter's specific Atk. This means you don't have to have a set value of '6' for any attack.

Here's a sample of what you can now accomplish:
Turing:

function attack (atk_char, def_char : character) : int
  var temp : int := 0
  temp := (Rand.Int (1, atk_char.Atk) - def_char.Def)
  if temp > 0 then
    result temp
  else
    result temp
  end if
end attack

aMonster.HP -= attack (myHero, aMonster)


See what happened? I made a function that used a random number in the first character's attack range, subtracted the defense of the second character (this could be random too); it then made sure that the result was greater than zero. Why?
Later on, I subtract the result from 'aMonster's HP. If the result were ever -ve, then we would add HP to it by attacking!
There are likely better ways of doing this little attack - but this shows you the power of types.
wtd




PostPosted: Sat Jul 09, 2005 5:46 pm   Post subject: (No subject)

I can't effectively give bits to another moderator, but if I could I'd give Delos 100 bits for that reply. Smile
Delos




PostPosted: Sat Jul 09, 2005 5:53 pm   Post subject: (No subject)

Now, here's the clincher...I wrote that entire post in a certain style reminiscent of a certain..."duck"...
wtd




PostPosted: Sat Jul 09, 2005 6:57 pm   Post subject: (No subject)

Delos wrote:
Now, here's the clincher...I wrote that entire post in a certain style reminiscent of a certain..."duck"...


Hexley isn't a duck, dammit! Smile
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  [ 6 Posts ]
Jump to:   


Style:  
Search: