Last active
November 4, 2021 09:40
-
-
Save jampekka/7eadcdc8db9b2ac0db959854eebafd5a to your computer and use it in GitHub Desktop.
Simple The Mind type game
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 | |
import selectors | |
import sys | |
total_cards = 100 | |
cards_per_player = 3 | |
deck = list(range(1, total_cards + 1)) | |
random.shuffle(deck) | |
player_hand, deck = deck[:cards_per_player], deck[cards_per_player:] | |
machine_hand, deck = deck[:cards_per_player], deck[cards_per_player:] | |
player_hand.sort() | |
machine_hand.sort() | |
top_card = 0 | |
machine_seconds_per_point = 0.5 | |
sel = selectors.DefaultSelector() | |
sel.register(sys.stdin, selectors.EVENT_READ) | |
while player_hand and machine_hand: | |
print(f"Table: {top_card}. Your hand: {player_hand}. Press enter to play {player_hand[0]}.") | |
machine_diff = machine_hand[0] - top_card | |
machine_wait = machine_diff*machine_seconds_per_point | |
event = sel.select(machine_wait) | |
if not event: | |
card, *machine_hand = machine_hand | |
if card > player_hand[0]: | |
print(f"Machine played {card}, but you have {player_hand[0]}! Sorry!") | |
break | |
else: | |
top_card = card | |
print(f"Machine plays {card}.") | |
else: | |
sys.stdin.readline() | |
card, *player_hand = player_hand | |
if card > machine_hand[0]: | |
print(f"You played {card}, but the machine has {machine_hand[0]}! Sorry!") | |
break | |
else: | |
top_card = card | |
print(f"You play {card}.") | |
else: | |
print(f"Yay, you won! Machine hand: {machine_hand}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment