Skip to content

Instantly share code, notes, and snippets.

@Artanis
Created July 5, 2011 00:31
Show Gist options
  • Save Artanis/1064103 to your computer and use it in GitHub Desktop.
Save Artanis/1064103 to your computer and use it in GitHub Desktop.
Exceedingly simple life and poison counter tracker.
"""Simple class to track a player's life total and poison counts in
Magic: the Gathering.
Keeps a history of life total changes, and has some simple logic to
determine when a player has lost by on either life or poison counts--
not accounting for cards that alter the life and poison rules, however.
These 'deaths' are not enforced, so a player may be healed up to 1 or
more life, or have the tenth poison counter removed and the class will
happily report that the player is Still Alive.
While the license below grants you permission to use this as you see fit
(while retaining the copyright notice, of course), if your goal is a
more complete implementation of this or additional facets of the game,
it may be more effective to write your own.
Copyright (c) 2011 Erik Youngren
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
def closure(func):
"""Simple decorator to complete function closures.
Also causes closures to be self-documented.
"""
return func()
class Player:
"""A player, with life and poison counter tracking.
"""
def __init__(self, name, *, life=None, history=None, poison=0):
"""Create a new player object.
The `life` and `history` arguments are mutually exclusive, with
`history` taking precedence (as it contains more data).
By default, `life` defaults to 20, and `history` to an empty
list (which immediately gains an element when initial life total
is set).
"""
self.name = name
self.poison = poison
if history is not None:
self.life_history = list(history)
else:
self.life_history = list()
if life is not None:
self.life = life
else:
self.life = 20
@ property
def life(self):
"""The life total.
Actually is a total, calculated by summing up the life_history
list.
Setting `life` to a value will calculate the difference between
the current total and the new one, and append that to the
history list.
The `+=` and `-=` operators function properly, and well heal or
hurt, respectively, by the proper amount.
"""
return sum(self.life_history)
@life.setter
def life(self, value):
self.life_history.append(value - self.life)
@property
def alive(self):
"""Determine if the player satisfies the 'alive' requirements.
A player is alive if she has more than 0 life and less than 10
poison counters.
As there are cards that alter these rules, do not use this to
automate loss detection.
"""
if self.life > 0 and self.poison < 10:
return True
else:
return False
@property
def poisoned(self):
return self.poison >= 1
@closure
def __str__():
unpoisoned = "{name}: {status} with {life} LIFE".format
poisoned = "{name}: {status}, POISONED({poison}) with {life} LIFE".format
def __str__(self):
formatting = poisoned if self.poisoned else unpoisoned
return formatting(name=self.name, life=self.life,
status='LIVING' if self.alive else 'DEAD',
poison=self.poison)
return __str__
def __repr__(self):
return "Player({name}, life={life}, poison={poison})".format(
name=repr(self.name), life=self.life, poison=self.poison)
def main():
p1 = Player("Player 1")
p1.poison = 1
p1.life -= 1
print(p1)
p1.poison += 1
print(p1)
p1.poison += 9
p1.life -= 9
print(p1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment