-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Class Constructor with arguments using __init__ method (#2775)
- Loading branch information
Showing
4 changed files
with
282 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from lpython import i32 | ||
class Character: | ||
def __init__(self:"Character", name:str, health:i32, attack_power:i32): | ||
self.name :str = name | ||
self.health :i32 = health | ||
self.attack_power : i32 = attack_power | ||
self.is_immortal : bool = False | ||
|
||
def attack(self:"Character", other:"Character") -> str: | ||
other.health -= self.attack_power | ||
return self.name+" attacks "+ other.name+" for "+str(self.attack_power)+" damage." | ||
|
||
def is_alive(self:"Character")->bool: | ||
if self.is_immortal: | ||
return True | ||
else: | ||
return self.health > 0 | ||
|
||
def main(): | ||
hero : Character = Character("Hero", 10, 20) | ||
monster : Character = Character("Monster", 50, 15) | ||
print(hero.attack(monster)) | ||
print(monster.health) | ||
assert monster.health == 30 | ||
print(monster.is_alive()) | ||
assert monster.is_alive() == True | ||
print("Hero gains temporary immortality") | ||
hero.is_immortal = True | ||
print(monster.attack(hero)) | ||
print(hero.health) | ||
assert hero. health == -5 | ||
print(hero.is_alive()) | ||
assert hero.is_alive() == True | ||
print("Hero's immortality runs out") | ||
hero.is_immortal = False | ||
print(hero.is_alive()) | ||
assert hero.is_alive() == False | ||
print("Restarting") | ||
hero = Character("Hero", 10, 20) | ||
print(hero.is_alive()) | ||
assert hero.is_alive() == True | ||
|
||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.