Skip to content

Instantly share code, notes, and snippets.

@visuve
Last active November 25, 2021 14:07
Show Gist options
  • Select an option

  • Save visuve/c1501a7e9d47d2cf38e7ac74330b6e7b to your computer and use it in GitHub Desktop.

Select an option

Save visuve/c1501a7e9d47d2cf38e7ac74330b6e7b to your computer and use it in GitHub Desktop.
A very annoying game
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" roulette.py - dare to play?"""
__author__ = "visuve"
__credits__ = ["visuve"]
__license__ = "0BSD"
import ctypes
import os
import random
import shutil
import sys
import time
# https://en.wikipedia.org/wiki/Comparison_of_command_shells
def resolve_command_shell_paths():
yield shutil.which("sh")
yield shutil.which("bash")
yield shutil.which("csh")
yield shutil.which("tcsh")
yield shutil.which("scsh")
yield shutil.which("ksh")
yield shutil.which("zsh")
yield shutil.which("fish")
yield shutil.which("ion")
def is_admin() -> bool:
if sys.platform == "win32":
return ctypes.windll.shell32.IsUserAnAdmin() != 0
return os.getuid() == 0
class Roulette:
def __init__(self):
self.cylinder = list(resolve_command_shell_paths())
self.chamber = None
self.round = 1
def roll(self):
print("Rolling cylinder...")
time.sleep(1.0)
self.chamber = random.choice(self.cylinder)
def fire(self):
self.round += 1
if (self.chamber):
self._hit()
else:
self._miss()
def _hit(self):
print(f'The chamber had "{self.chamber}" ...')
try:
if os.path.exists(self.chamber):
os.remove(self.chamber)
print(f'"{self.chamber}" was removed. Bad luck for you.')
else:
print(f'"{self.chamber}" was already fired. You were lucky this time.')
except OSError:
print(f'Could not remove "{self.chamber}". You were lucky this time.')
def _miss(self):
print("The chamber was empty. You were lucky this time.")
def run():
print("*** Welcome to Russian Roulette ***")
if not is_admin():
print("Administrator privileges required. Please restart the program with other credentials.")
return
roulette = Roulette()
while input(f"\nRound {roulette.round}. Press enter to roll...") != "x":
roulette.roll()
roulette.fire()
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment