'''This is the main class of the game.'''
import datetime
import os
import random
import sys
from blessings import Terminal
import time
from bureaucrat import Bureaucrat
from character import Character
import text
t = Terminal()
class Game:
'''The main logic of the game.'''
def __init__(self):
self.clear_screen()
input(t.underline('Welcome to LAM-40 Alpha!') + '\n\n' +
text.intro + text.disclaimer) # the first screen of the game
self.clear_screen()
self.character = Character() # creates an instance of the player class
# sets the date and the time of the game
self.timer = datetime.datetime(2023, 9, 17, 8, 0)
for number, floor in enumerate(range(39)):
self.bureaucrats_number = random.choice((range(1),
range(2),
range(3)))
for bureaucrat in self.bureaucrats_number:
self.bureaucrat = Bureaucrat()
self.act()
self.clear_screen()
print('One of the visitors slowly passes by and says:\n' +
random.choice(text.visitor_remarks) + '\n\n' +
'You reach ' + t.bold('the floor number {}.'
.format(number + 2)) + '\n')
time.sleep(2)
self.game_completed()
def act(self):
'''An action taken by the player.'''
# the following loop numerically lists all available actions
for number, action in enumerate(self.character.actions):
number = str(number + 1)
if int(number) < 10:
print('0' + number + '. ' + action)
else:
print(number + '. ' + action)
self.action = input('\nIt is ' +
t.bold('{}'.format(self.timer.strftime('%H:%M'))) +
'. What will you do? ').lower()
if self.action == 'q': # quits the game
self.quit()
elif self.action == 'w': # creates a new bureaucrat
self.timer_add(random.choice(range(800, 1001)))
print('\nYou wait for the next bureaucrat to appear.\n')
time.sleep(1)
self.evaluate_time() # checks if the player ran out of time
self.bureaucrat = Bureaucrat()
self.act()
elif self.action in 'abcdefhijklmpstyw':
self.react() # finds out what type of action was chosen
self.calculate_odds() # alters the odds of success
self.evaluate_success() # determines success or failure
else: # incorrect input is given
print('\nIncorrect action. Please try again.\n')
time.sleep(1)
def react(self):
'''A reaction to the choice of the player.'''
if self.action == 'bt': # character.py for reference
self.character.probability_is_low()
self.character.asc_rank_asc_mood()
self.timer_add(random.choice(range(200, 401)))
elif self.action in 'cp':
self.character.probability_is_high()
self.character.desc_rank_desc_mood()
self.timer_add(random.choice(range(500, 701)))
elif self.action in 'dm':
self.character.probability_is_low()
self.character.desc_rank_desc_mood()
self.timer_add(random.choice(range(350, 551)))
elif self.action in 'efj':
self.character.probability_is_high()
self.character.asc_rank_asc_mood()
self.timer_add(random.choice(range(650, 851)))
elif self.action in 'hkly':
self.character.probability_is_low()
self.character.desc_rank_asc_mood()
self.timer_add(random.choice(range(350, 551)))
elif self.action in 'is':
self.character.probability_is_high()
self.character.asc_rank_desc_mood()
self.timer_add(random.choice(range(500, 701)))
else: # 'a' (ask)
self.character.probability_is_high()
self.character.desc_rank_asc_mood()
self.timer_add(random.choice(range(650, 851)))
def calculate_odds(self):
'''Calculates the odds of a successful action.'''
# odds of success are randomly assigned to be 25-75% at first
self.odds = float(random.choice(range(25, 76)))
if self.bureaucrat.rank == 'low':
if self.character.asc_rank is True: # character.py for reference
self.odds /= self.character.divisor
else:
self.odds *= self.character.multiplier
elif self.bureaucrat.rank == 'high':
if self.character.asc_rank is True:
self.odds *= self.character.multiplier
else:
self.odds /= self.character.divisor
else:
pass # medium rank doesn't affect the odds
if self.bureaucrat.mood == 'bad':
if self.character.asc_mood is True:
self.odds /= self.character.divisor
else:
self.odds *= self.character.multiplier
elif self.bureaucrat.mood == 'good':
if self.character.asc_mood is True:
self.odds *= self.character.multiplier
else:
self.odds /= self.character.divisor
else:
pass # average mood doesn't affect the odds
def evaluate_success(self):
'''Evaluates whether the action is successful or not.'''
# the higher self.odds value
# the higher the chance the action will succeed
self.luck = random.choice(range(0, 101))
if self.luck > self.odds:
self.react_negatively()
if self.action in 'bkty': # your risky action was unsuccessful
self.game_over()
else:
self.react_positively()
def react_negatively(self):
'''A negative reaction to the choice of the player.'''
self.clear_screen()
print(t.bold("It doesn't affect the bureaucrat!\n") +
random.choice(text.negative_reactions) + '\n')
time.sleep(1.5)
self.evaluate_time()
self.act()
def react_positively(self):
'''A positive reaction to the choice of the player.'''
self.clear_screen()
print(t.bold("It's super effective!\n") +
random.choice(text.positive_reactions) + '\n')
time.sleep(1.5) # a new bureaucrat is generated
self.evaluate_time()
def timer_add(self, seconds_passed):
'''Adds the time the action took to the timer.'''
self.timer += datetime.timedelta(seconds=seconds_passed)
def evaluate_time(self):
'''Checks whether you still have time left.'''
if self.timer - datetime.datetime(2023, 9, 17, 8, 0) >= \
datetime.timedelta(seconds=43200):
self.game_over()
else:
pass
def quit(self):
'''Quits the game.'''
self.clear_screen()
sys.exit('Thank you for playing LAM-40.\n')
def game_over(self):
'''Called when the game is over.'''
self.clear_screen()
if self.action in 'bkty':
print('Game over! You were handed over to the police ' +
'and arrested.\nYou will have to try again next day.')
else: # the time is up
print('Game over! The Instituion is now closed for today.' +
'\nYou will have to try again next day.')
self.choice = input("\nType 'r' if you want to restart: ").lower()
if self.choice == 'r': # restarts the game from the beginning
os.execl(sys.executable, sys.executable, * sys.argv)
else:
self.quit()
def game_completed(self):
'''Ends the game after showing the ending.'''
self.clear_screen()
if self.timer - datetime.datetime(2023, 9, 17, 8, 0) >= \
datetime.timedelta(seconds=21600):
print(text.ending)
else: # the player reaches the floor number 40 in 6 hours
print(text.secret_ending)
self.clear_screen()
sys.exit('Congratulations! You beat LAM-40. See you next time!\n')
def clear_screen(self):
'''Clears the screen of the terminal.'''
if os.name == 'nt': # 'nt' equals Windows
os.system('cls')
else:
os.system('clear') # Mac OS and Linux
Game()