Last active
December 17, 2015 17:49
-
-
Save Lysander/5648698 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # coding: utf-8 | |
| """ | |
| ~~~~~~~~ | |
| DiceGame | |
| ~~~~~~~~ | |
| Umsetzung des "Würfelspiels 1.0" aus einem Thread bei ubuntuusers.de | |
| http://forum.ubuntuusers.de/post/5643272/ | |
| .. moduleauthor: Christian Hausknecht <christian.hausknecht@gmx.de> | |
| """ | |
| import random | |
| import itertools | |
| import argparse | |
| def get_guessable_number(): | |
| return random.randint(1, 6) | |
| def guess_number_by_human_player(): | |
| while True: | |
| try: | |
| return int(input("Wähle eine Zahl von 1 bis 6 >>> ")) | |
| except ValueError: | |
| print("Bitte nur ganze Zahlen zwischen 1 und 6 eingeben!") | |
| def create_clever_ai(): | |
| """ | |
| fabric method that generates a function, that just returns | |
| constantly the same number once initialized. This is probably the | |
| best method to guess a randomly generated number which follows | |
| the laplace distribution. | |
| :returns: an integer within 1 to 6 | |
| """ | |
| number = get_guessable_number() | |
| def guess(): | |
| return number | |
| return guess | |
| def play(get_guessable, guessing_func): | |
| """ | |
| this function implements the core of the game. It gets a guessable | |
| number each turn and will get a guess from a callable. It ends if | |
| the guess was correct. | |
| :param get_guessable: callable to get a guessing number | |
| :param guessing_func: callable to get a guess | |
| :returns: the turns needed to guess a right number | |
| """ | |
| for turn in itertools.count(1): | |
| guessable_number = get_guessable() | |
| guessed_number = guessing_func() | |
| if guessable_number == guessed_number: | |
| break | |
| print("{} ist leider falsch, die gesuchte Zahl war {}".format( | |
| guessed_number, guessable_number)) | |
| print("Glückwunsch, die gesuchte Zahl war {}! " | |
| "Du hast {} Versuche benötigt".format(guessable_number, turn)) | |
| return turn | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Ein einfaches Ratespiel.') | |
| parser.add_argument("-ai", action="store_true", default=False, | |
| help="Let the Computer play") | |
| parser.add_argument("-turns", type=int, default=1, | |
| help="define the turns you wanna play") | |
| args = parser.parse_args() | |
| turns_needed = [] | |
| for turn in range(1, args.turns+1): | |
| guessing_func = create_clever_ai() if args.ai else \ | |
| guess_number_by_human_player | |
| print() | |
| print("Durchgang {}:".format(turn)) | |
| turns_needed.append(play(get_guessable_number, guessing_func)) | |
| print() | |
| print("Du hast im Schnitt {} Versuche benötigt!".format( | |
| sum(turns_needed) / len(turns_needed))) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment