Last active
January 16, 2019 23:25
-
-
Save Camto/735a45eebaab7a9337c240ad9b24a4f5 to your computer and use it in GitHub Desktop.
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
import random | |
class Player: | |
def __init__(self): | |
self.bullets = 0 | |
self.blocking = False | |
self.being_shot = False | |
def is_alive(self): | |
return self.blocking or not self.being_shot | |
def round_reset(self): | |
self.blocking = False | |
self.being_shot = False | |
def load(player, _, is_player): | |
player.bullets += 1 | |
if not is_player: | |
print("Robot loads.") | |
def shoot(player, opponent, is_player): | |
if player.bullets > 0: | |
player.bullets -= 1 | |
opponent.being_shot = True | |
elif is_player: | |
print("Cannot shoot without a bullet.") | |
if not is_player: | |
print("Robot shoots.") | |
def block(player, _, is_player): | |
player.blocking = True | |
if not is_player: | |
print("Robot blocks.") | |
actions = { | |
"l": load, | |
"s": shoot, | |
"b": block | |
} | |
beginning_text = """ | |
Welcome to The Gun Game! | |
You and the robot each start with 0 bullets. | |
Every turn you and the robot will get to choose an action: load, shoot, or block. Then they will be performed simultaneously. | |
Load gives you a bullet. | |
Shoot can only be used if you have at least 1 bullet. You lose a bullet. Your opponent loses the game if they're not blocking. | |
Block makes you invincible that round. | |
To choose an attack, simply write it's name and press enter. | |
""" | |
action_help = """ | |
Select one of: | |
(l)oad - Get one bullet. | |
(s)hoot - Shoot your opponent. You lose one bullet and he dies unless he's blocking. | |
(b)lock - Cannot get killed this round. | |
""" | |
def main(): | |
print(beginning_text) | |
player = Player() | |
robot = Player() | |
while player.is_alive() and robot.is_alive(): | |
player.round_reset() | |
robot.round_reset() | |
print(action_help) | |
print("You have {} bullets".format(player.bullets)) | |
print("The robot has {} bullets".format(robot.bullets)) | |
try: | |
actions[input()[0]](player, robot, True) | |
robot_choice = random.choices(list(actions.values()), [0.2, 0.1, 0.7])[0] | |
robot_choice(robot, player, False) | |
except Exception as e: | |
# print(e) | |
print("That's not an action.") | |
if player.is_alive() and not robot.is_alive(): | |
print("You win!") | |
elif not player.is_alive() and robot.is_alive(): | |
print("You lose!") | |
else: | |
print("It's a tie!") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment