import sys
from Training.observer_abilities import *
from Training.cortex_3x3_caddy import *
from Scenery.announcer_chair import *

class Player(Observer):

    def __init__(self, marker_code):
        self.announcer = Announcer()
        self.marker_code = marker_code

    def get_enemy_code(self):
        if self.marker_code == 10:
            return 1
        return 10

    def move(self, board):
        choice = self.choose(board)
        board[choice] = self.marker_code
        return board

    def choose(self, board):
        options = self.get_legal_moves(board)
        return options[0]

    def get_legal_moves(self, board):
        legal_moves = []
        for i in range(0, len(board)):
            if board[i] != 1 and board[i] != 10:
                legal_moves.append(i)
        return legal_moves

class Human(Player):

    name = 'human'
    strikes = 0

    def choose(self, board):
        choice = self.get_good_input(board)
        if self.check_conscience(choice, board):
            return self.redo_move(board)
        else:
            self.reset_strikes()
            return choice

    def get_good_input(self, board):
        try:
            self.announcer.show(self.announcer.question)
            return int(raw_input("> ")) -1
        except(ValueError):
            return self.redo_move(board)

    def check_conscience(self, choice, board):
        if choice not in self.get_legal_moves(board):
            return True
            
    def redo_move(self, board):
        self.add_a_strike()
        self.announcer.show(self.announcer.bad_move)
        return self.choose(board)

    def add_a_strike(self):
        self.strikes += 1
        if self.strikes == 3:
            self.announcer.show(self.announcer.strike_3)
            sys.exit()

    def reset_strikes(self):
        self.strikes = 0

class Computer(Player):

    name = 'computer'
    cortex = Cortex_3x3()

    def choose(self, board):
        intel = self.get_intelligence(board)
        return self.cortex.direct_move(intel)

    def get_intelligence(self, board):
        intel = { 'board': board, 
                  'options': self.get_legal_moves(board),
                  'analysis': self.scan_board(board),
                  'marker_code': self.marker_code, 
                  'enemy_code': self.get_enemy_code() }
        return intel

@@ -1987,30 +1987,32 @@
 rd)%0A        
-return
+choice =
  self.cortex
@@ -2030,16 +2030,75 @@
 e(intel)
+%0A        self.announce_choice(choice)%0A        return choice
 %0A%0A    de
@@ -2406,8 +2406,218 @@
 n intel%0A
+%0A    def announce_choice(self, choice):%0A        pre = self.announcer.pre_choice%0A        post = self.announcer.post_choice%0A        statement = pre + str(choice + 1) + post%0A        self.announcer.show(statement)%0A

add annoying speeches by the computer about itself