“Guess the film” revisited
Yes, I know I posted the other entry last night, but after that I kept messing around with the code. One of my friend requests was to have a GUI (seriously?). Ok, after thinking about it, and modifying some code here is the version 0.7 of this magnificent and almighty implementation of guess the film.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #!/usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright (c) 2010 Oscar Carballal Prego <info@oscarcp.com> # # Distributed under terms of the MIT license. # TODO: # - Solve the codification problem in Windows. # - Improve the films list to have a director and description. # FIXME: # - _get_film(self) does not print 5 spaces before text in format string # Import python modules import sys import random import datetime class Game(): def __init__(self): """ Start everything. Declare dictionaries and lists, open files and get all the content ready. """ self.films_file = open('films.txt', 'r') self.films = [] self.players = {} [self.films.append([line]) for line in self.films_file] self.films_file.close() def _end(self): """ This function terminates the game, after showing a basic statistic of the game. """ self.end_time = datetime.datetime.now() self.t = str((self.end_time - self.start_time)).split(':') self.counter = len(self.players) - 1 self.p = self.players.items() self.fwinner = max(self.players, key = lambda a: self.players.get(a)) print "\n\n In a game of {0} players, results are:\n".format(len(self.players)) print " Time: {0} hours, {1} minutes, {2} seconds.\n".format(self.t[0], self.t[1], self.t[2].split('.')[0]) print " Points\n ------\n" while self.counter >= 0: print " {0} won {1} points.".format(self.p[self.counter][0], self.p[self.counter][1]) self.counter -= 1 print "\n ******* Winner: {0} *******\n".format(self.fwinner) # Clear everything del self.films[:] self.players.clear() sys.exit(0) def _get_players(self): """ This function asks the number of players and their names. Names are stored in a dictionary as a key with a value of zero for every player. """ self.i = 0 self.check = 0 while self.check == 0: try: self.n_players = int(raw_input('* How many players? ')) self.check = 1 except ValueError: print '* ERROR: Must type a number!' if self.n_players == 1: print '* Are you kidding? This can\'t be played alone!.' sys.exit(0) while self.i < self.n_players: self.player_name = raw_input('Player {0} name: '.format(self.i)) self.players[self.player_name] = 0 self.i += 1 def _get_points(self): """ This function stores the points earned by a player. It simply increases the number of the value by one in the pair [key,value]. """ try: self.winner = raw_input('* Who guessed? (player name, case sensitive): ') self.players[self.winner] += 1 except: self._get_points() def _get_film(self): """ This function gets a random film from the film list. """ print '\n {0:>5}'.format(random.choice(self.films)[0]) def start(self): """ This function starts the game. It gets the time when the game is started and enters a loop asking to generate a new film. If the player does not want a new film _end() is executed. """ self._get_players() self.start_time = datetime.datetime.now() while True: self.generate = raw_input('* Generate film? (s/n) ') if self.generate == 'n' or self.generate == 'no': self._end() else: self._get_film() self._get_points() if __name__ == '__main__': game = Game() game.start() |