Last active
August 29, 2015 14:15
-
-
Save zeimusu/50fd54fb5988d07984fd to your computer and use it in GitHub Desktop.
A little guessing game, "Mentalist poker" http://www.benefactum.ca/?p=274
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
#!/usr/bin/env python3 | |
import random | |
import os | |
import sys | |
""" | |
Mentalist Poker http://www.benefactum.ca/?p=274 | |
1. Each player starts with 20 chips or counters of some sort, a screen to hide | |
them, and a “betting line,” perhaps a piece of string or a pencil, placed | |
horizontally in front of him (but behind the screen). Chips placed in front of | |
the line are being bet, while those behind are not. | |
2. Determine at random which player starts with the First Bettor button. | |
3. Each round begins with both players making an ante of one chip. | |
4. The First Bettor secretly decides on a bet, which can be anywhere between | |
zero chips and all of his chips. He places these in front of the line, being | |
sure to disguise his motions. | |
5. The opponent attempts to guess how many chips he has bet, stating verbally | |
“Zero,” or “Five,” or whatever number he likes. | |
6. The bettor’s screen is lifted and his bet revealed. If the opponent’s guess | |
was correct, the opponent immediately wins the pot and the bettor’s bet. | |
7. If the opponent’s guess was incorrect, the bet is added to the pot, the | |
screen is replaced, and now it is the opponent’s turn to make a bet, and the | |
First Bettor’s turn to guess. | |
8. If it is a player’s turn to bet and he has no chips left, the opponent is | |
assumed to guess Zero, and automatically wins the pot and the game. | |
9. Play continues back and forth until the pot is won. Assuming both players | |
have at least one chip left, the First Bettor button is passed to the opponent, | |
both players ante again, and a new round is begun. | |
""" | |
class player(): | |
def __init__(self, human=False, chips=20): | |
self.human = human | |
self.chips = chips | |
def bet(self): | |
return self.human_bet() if self.human else self.computer_bet() | |
def human_bet(self): | |
return self._input_int( | |
"How much to bet (0-{})".format(self.chips), self.chips) | |
def computer_bet(self): | |
bet = 0 | |
for i in range(self.chips): | |
if random.random() < 0.5: | |
bet += 1 | |
else: | |
break | |
return min(bet, self.chips) | |
def guess(self, maxguess): | |
return self.human_guess(maxguess) if self.human\ | |
else self.computer_guess(maxguess) | |
def human_guess(self, maxguess): | |
return self._input_int( | |
"How much to guess (0-{})".format(maxguess), maxguess) | |
def computer_guess(self, maxguess): | |
guess = 0 | |
for i in range(maxguess): | |
if random.random() < 0.5: | |
guess += 1 | |
else: | |
break | |
return min(maxguess, guess) | |
def win(self, chips): | |
self.chips += chips | |
def _input_int(self, prompt, max): | |
while True: | |
try: | |
i = int(input(prompt)) | |
except ValueError: | |
pass | |
else: | |
if i <= max: | |
break | |
return i | |
class table(): | |
def __init__(self, *players): | |
self.pot = 0 | |
self.players = players | |
print(self.players) | |
def ante(self, bet=1): | |
self.pot = 0 | |
self.players = [player for player in self.players if player.chips > 0] | |
if len(self.players) == 1: | |
raise ValueError | |
for p in self.players: | |
p.chips -= 1 | |
self.pot += 1 | |
def show_table(self): | |
os.system('clear') | |
for i, player in enumerate(self.players): | |
print("Player {} ({}) has £{}".format(i, "Human" if player.human | |
else "Computer", | |
player.chips)) | |
print("Pot has £{}".format(self.pot)) | |
def main(): | |
h = player(human=True) | |
c = player(human=False) | |
t = table(h, c) | |
t.ante() | |
while True: | |
for i, better in enumerate(t.players): | |
t.show_table() | |
guesser = t.players[(i+1) % len(t.players)] | |
bet = better.bet() | |
guess = guesser.guess(better.chips) | |
print("Bet is {} and guess is {}".format(bet, guess)) | |
if guess == bet: | |
# guesser wins pot and bet | |
print("The pot is won") | |
guesser.win(t.pot + bet) | |
better.chips -= bet | |
t.pot = 0 | |
try: | |
t.ante() | |
except ValueError: | |
game_over() | |
else: | |
print("Guessed wrong") | |
better.chips -= bet | |
t.pot += bet | |
input("press enter") | |
def game_over(): | |
print("Game over") | |
sys.exit() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment