Skip to content

Instantly share code, notes, and snippets.

@heypoom
Created March 31, 2020 19:08
Show Gist options
  • Save heypoom/f0e9624f706ff5324dfc84c341f935b5 to your computer and use it in GitHub Desktop.
Save heypoom/f0e9624f706ff5324dfc84c341f935b5 to your computer and use it in GitHub Desktop.
from random import randint
# Randomize an integer between 0 to n.
def randomize(maximum):
return randint(0, maximum)
class OpenClosedGame:
is_playing = False
def __init__(self):
self.game_loop()
def game_loop(self):
while True:
print("Welcome to the open-closed game.")
if self.ask_yes_no("Would you like to see the rules first?"):
self.show_rules()
self.start_gameplay_loop()
if not self.ask_yes_no("Do you want to play again?"): break
def ask_yes_no(self, question):
answer = input("{} (y/n) ".format(question))
if answer is 'y': return True
def start_gameplay_loop(self):
is_win = False
is_player_predicting = True
while not is_win:
is_win = self.play_turn(is_player_predicting)
is_player_predicting = not is_player_predicting
def get_prediction_pattern(self, prediction):
if prediction is 0: return 'cc'
if prediction is 2: return 'oo'
if randomize(10) > 5: return 'oc'
return 'co'
def play_turn(self, is_player_predicting):
predictor_name = 'You' if is_player_predicting else 'AI'
print("The predictor is {}.".format(predictor_name))
player_open_hands, player_predict = self.get_turn_input(is_player_predicting)
ai_turn = randomize(2)
print("AI:", self.get_prediction_pattern(ai_turn))
if is_player_predicting:
prediction = player_predict
else:
# If the AI is predicting. AI predict the player's hand.
prediction = randomize(4)
print("Player:", self.get_prediction_pattern(prediction))
return self.check_turn_result(
is_player_predicting,
player_open_hands,
ai_turn,
prediction
)
# Check the turn result. True is winning and False is losing.
def check_turn_result(
self,
is_player_predicting,
player_open_hands,
ai_turn,
prediction
):
predictor_name = 'You' if is_player_predicting else 'AI'
# TODO: Explain this logic.
if player_open_hands + ai_turn is prediction:
print("{} Win!".format(predictor_name))
return True
print("No winner.")
return False
def get_turn_input(self, is_player_predicting):
raw_input = input("What is your input? ")
hands = raw_input[0:2]
hands = hands.lower()
predict = 0
# If player is predicting, allow them to guess the AI's hand.
if is_player_predicting:
predict = int(raw_input[2])
open_hands = hands.count('o')
return open_hands, predict
def show_rules(self):
print("Rule: ")
print("You need to play this game against the computer.")
print("You will always be the predictor first.")
print("For each round, the computer will expect player input in the following format if you are the predictor: OC")
print("Or if you are not the predictor: CO")
print("That is, the first two characters will show how you will play your hands, O for open or C for closed.")
print("If you are the predictor, you also need to enter a third character which is your prediction for how many open hands in total.")
print("The game will end if one player win.")
game = OpenClosedGame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment