Ejemplo n.º 1
0
class TicTacToe:
    def __init__(self, player1, player2):
        self.board = Board()
        self.players = [player1, player2]
        self.current_player = random.randrange(0, len(self.players))
        print("Player", self.players[self.current_player].get_mark(), "starts")

    def do_move(self):
        if not self.check_someone_won():
            move = self.players[self.current_player].do_move(self.board)
            if self.check_legal(move) and not self.board_full():
                self.board.place_move(
                    move, self.players[self.current_player].get_mark())
                self.change_player()

    def check_someone_won(self):
        for player in self.players:
            if self.board.check_win(player.get_mark()):
                return True
        return False

    def check_legal(self, move):
        return self.board.move_is_legal(move)

    def check_win(self):
        marks = []
        for player in self.players:
            marks.append(player.getMark())
        win = self.board.check_win(marks)
        if win:
            self.update_table(self.board)
        return win

    def change_player(self):
        self.current_player = (self.current_player + 1) % len(self.players)

    def board_full(self):
        return self.board.board_full()

    def reset(self):
        self.board = Board()

    def get_board(self):
        return self.board.get_board()

    def row_length(self):
        return self.board.row_length

    def get_current_player(self):
        return self.current_player

    def game_ended(self):
        return self.check_win() or self.board_full()
Ejemplo n.º 2
0
class Match(UIInterface):
    def __init__(self, home, away, message_delay=MESSAGE_DELAY):
        UIInterface.__init__(self)
        self._home = home
        self._home.set_symbol('X')
        self._away = away
        self._away.set_symbol('O')
        self._board = Board()
        self._turn = home
        self._message_delay = message_delay

        self.set_w_h(50, 24)
        self._board.set_x_y(1, 1)
        self._log = MessageWindow.with_xywh(
            self._board.position().x() + self._board.size().width() + 1,
            self._board.position().y(),
            self.size().width() - self._board.size().width() - 2,
            self._board.size().height())
        self.add_child(self._log)

        self._home_deck = []
        self._away_deck = []

        self.add_child(self._board)

    def home(self):
        return self._home

    def away(self):
        return self._away

    def add_message(self, msg, color):
        self._log.add_message(msg, color)
        return self

    def disqualify(self, message):
        self.add_message(message, UIColor.light_red())
        winner = self._home
        if self._turn == self._home:
            winner = self._away
        self.render()
        time.sleep(2)
        return winner

    def play(self):
        while self._board.check_win() is None and not self._board.full():
            self.render()
            self.add_message(self._turn.name() + '\'s turn:',
                             UIColor.light_yellow())
            time.sleep(self._message_delay)
            x = -1
            y = -1

            tc = TimeoutCall(3, self._turn.play, self._board.read_only())
            if not tc.start():
                return self.disqualify(self._turn.name() +
                                       ' developed a crappy AI, disqualifying')
            else:
                try:
                    x, y = tc.result()
                except:
                    return self.disqualify(
                        self._turn.name() +
                        ' returned an ill-formed touple, disqualifying')

            if self._board.play(self._turn, x, y):
                self.add_message(
                    self._turn.name() + ' played ' + self._turn.symbol() +
                    ' at (' + str(x) + ', ' + str(y) + ')', self._turn.color())
            else:
                return self.disqualify(self._turn.name() +
                                       ' made an ilegal move, disqualifying')
            if self._turn == self._home:
                self._turn = self._away
            else:
                self._turn = self._home
        winner = self._away
        if self._board.check_win() == self._home.symbol():
            winner = self._home
        elif self._board.check_win() is None:
            winner = None
        if winner is not None:
            self.add_message(winner.name() + ' has won the match!',
                             winner.color())
        else:
            self.add_message('It\'s a tie', UIColor.dark_gray())
        self.render()
        time.sleep(self._message_delay * 2)
        return winner