2016-02-29 19:16:12 +08:00
|
|
|
import logging
|
2016-04-30 19:27:41 +08:00
|
|
|
from datetime import datetime
|
2016-02-29 19:16:12 +08:00
|
|
|
|
2016-02-29 06:57:24 +08:00
|
|
|
from deck import Deck
|
|
|
|
import card as c
|
|
|
|
|
|
|
|
|
|
|
|
class Game(object):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" This class represents a game of UNO """
|
2016-02-29 06:57:24 +08:00
|
|
|
current_player = None
|
|
|
|
reversed = False
|
2016-02-29 19:16:12 +08:00
|
|
|
draw_counter = 0
|
2016-02-29 06:57:24 +08:00
|
|
|
choosing_color = False
|
2016-03-11 16:23:53 +08:00
|
|
|
started = False
|
2016-04-26 23:53:29 +08:00
|
|
|
owner = None
|
|
|
|
open = True
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-04-24 08:11:37 +08:00
|
|
|
def __init__(self, chat):
|
|
|
|
self.chat = chat
|
2016-02-29 06:57:24 +08:00
|
|
|
self.deck = Deck()
|
|
|
|
self.last_card = self.deck.draw()
|
2016-04-19 06:41:48 +08:00
|
|
|
|
|
|
|
while self.last_card.special:
|
|
|
|
self.deck.dismiss(self.last_card)
|
|
|
|
self.deck.shuffle()
|
|
|
|
self.last_card = self.deck.draw()
|
|
|
|
|
2016-02-29 19:16:12 +08:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-04-26 23:53:29 +08:00
|
|
|
@property
|
|
|
|
def players(self):
|
|
|
|
players = list()
|
|
|
|
if not self.current_player:
|
|
|
|
return players
|
|
|
|
|
|
|
|
current_player = self.current_player
|
|
|
|
itplayer = current_player.next
|
|
|
|
players.append(current_player)
|
|
|
|
while itplayer and itplayer is not current_player:
|
|
|
|
players.append(itplayer)
|
|
|
|
itplayer = itplayer.next
|
|
|
|
return players
|
|
|
|
|
2016-02-29 06:57:24 +08:00
|
|
|
def reverse(self):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Reverse the direction of play """
|
2016-02-29 06:57:24 +08:00
|
|
|
self.reversed = not self.reversed
|
|
|
|
|
|
|
|
def turn(self):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Mark the turn as over and change the current player """
|
2016-03-01 08:25:26 +08:00
|
|
|
self.logger.debug("Next Player")
|
2016-02-29 06:57:24 +08:00
|
|
|
self.current_player = self.current_player.next
|
2016-03-01 08:25:26 +08:00
|
|
|
self.current_player.drew = False
|
2016-04-30 19:27:41 +08:00
|
|
|
self.current_player.turn_started = datetime.now()
|
2016-02-29 06:57:24 +08:00
|
|
|
|
|
|
|
def play_card(self, card):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Play a card and trigger its effects """
|
2016-02-29 06:57:24 +08:00
|
|
|
self.deck.dismiss(self.last_card)
|
|
|
|
self.last_card = card
|
2016-02-29 19:16:12 +08:00
|
|
|
|
|
|
|
self.logger.info("Playing card " + repr(card))
|
|
|
|
if card.value == c.SKIP:
|
2016-03-01 08:25:26 +08:00
|
|
|
self.turn()
|
2016-02-29 19:16:12 +08:00
|
|
|
elif card.special == c.DRAW_FOUR:
|
2016-02-29 06:57:24 +08:00
|
|
|
self.draw_counter += 4
|
2016-02-29 19:16:12 +08:00
|
|
|
self.logger.debug("Draw counter increased by 4")
|
|
|
|
elif card.value == c.DRAW_TWO:
|
2016-02-29 06:57:24 +08:00
|
|
|
self.draw_counter += 2
|
2016-02-29 19:16:12 +08:00
|
|
|
self.logger.debug("Draw counter increased by 2")
|
|
|
|
elif card.value == c.REVERSE:
|
2016-03-08 09:50:24 +08:00
|
|
|
# Special rule for two players
|
2016-03-08 06:50:39 +08:00
|
|
|
if self.current_player is self.current_player.next.next:
|
|
|
|
self.turn()
|
|
|
|
else:
|
|
|
|
self.reverse()
|
2016-02-29 06:57:24 +08:00
|
|
|
|
2016-03-08 09:50:24 +08:00
|
|
|
# Don't turn if the current player has to choose a color
|
2016-02-29 06:57:24 +08:00
|
|
|
if card.special not in (c.CHOOSE, c.DRAW_FOUR):
|
2016-03-01 08:25:26 +08:00
|
|
|
self.turn()
|
2016-02-29 06:57:24 +08:00
|
|
|
else:
|
2016-02-29 19:16:12 +08:00
|
|
|
self.logger.debug("Choosing Color...")
|
2016-02-29 06:57:24 +08:00
|
|
|
self.choosing_color = True
|
|
|
|
|
|
|
|
def choose_color(self, color):
|
2016-03-08 09:50:24 +08:00
|
|
|
""" Carries out the color choosing and turns the game """
|
2016-02-29 06:57:24 +08:00
|
|
|
self.last_card.color = color
|
2016-03-01 08:25:26 +08:00
|
|
|
self.turn()
|
2016-02-29 06:57:24 +08:00
|
|
|
self.choosing_color = False
|