示例#1
0
class BoardController:
  def __init__(self):
    self.board = Board(None)
    self.view  = ConsoleBoardView(self.board)

  def init_game(self):
  #  self.white_player = Core1Player(Board.WHITE)
    self.white_player = HumanPlayer(Board.WHITE)
    self.black_player = Core2Player(Board.BLACK)
  #  self.black_player = RandomPlayer(Board.BLACK)
    self.atual_player = self.black_player

    finish_game = 0

    self.view.update_view()
    

    while finish_game != 2:
      #raw_input("")
      atual_color = self.atual_player.color
      print 'Jogador: ' + atual_color +' ('+ self.atual_player.name() +')'

      if self.board.valid_moves(atual_color).__len__() > 0:
        self.board.play(self.atual_player.play(self.board.get_clone()), atual_color)
        self.view.update_view()
        finish_game = 0
      else:
        print 'Sem movimentos para o jogador: ' + atual_color
        finish_game += 1
      self.atual_player = self._opponent(self.atual_player)

    return self._end_game()


  def _end_game(self):
    score = self.board.score()
    print ''
    print'-----------------PLACAR DESTA RODADA---------------------'
    print self.white_player.name() , " = " , score[0]
    print self.black_player.name() , " = " , score[1]
    if score[0] > score[1]:
      print ""
      print 'Jogador ' + self.white_player.name() + ' Ganhou'
      return 1
    elif score[0] < score[1]:
      print ""
      print 'Jogador ' + self.black_player.name() + ' Ganhou'
      return 2
    else:
      print ""
      print 'Jogo terminou empatado'
      return 0
    print'---------------------------------------------'
    
    
  def _opponent(self, player):
    if player.color == Board.WHITE:
      return self.black_player

    return self.white_player
    def __init__(self):
        self.board = Board(None)
        self.view = ConsoleBoardView(self, self.board)

        self.white_player = None
        self.black_player = None
        self.atual_player = None
        self.finish_game = 0
class BoardController:
  def __init__(self):
    self.board = Board(None)
    self.view  = ConsoleBoardView(self.board)

  def init_game(self):
    self.white_player = CornerPlayer(Board.WHITE)
    self.black_player = RandomPlayer(Board.BLACK)
    self.atual_player = self.black_player

    finish_game = 0

    self.view.update_view()

    while finish_game != 2:
      raw_input("")
      atual_color = self.atual_player.color
      print 'Jogador: ' + atual_color
      if self.board.valid_moves(atual_color).__len__() > 0:
        self.board.play(self.atual_player.play(self.board.get_clone()), atual_color)
        self.view.update_view()
        finish_game = 0
      else:
        print 'Sem movimentos para o jogador: ' + atual_color
        finish_game += 1
      self.atual_player = self._opponent(self.atual_player)

    self._end_game()


  def _end_game(self):
    score = self.board.score()
    if score[0] > score[1]:
      print ""
      print 'Jogador ' + Board.WHITE + ' Ganhou'
    elif score[0] < score[1]:
      print ""
      print 'Jogador ' + Board.BLACK + ' Ganhou'
    else:
      print ""
      print 'Jogo terminou empatado'

  def _opponent(self, player):
    if player.color == Board.WHITE:
      return self.black_player

    return self.white_player
示例#4
0
 def __init__(self, white, black, p):
     self.board = Board(None)
     self.white = white
     self.black = black
     self.view = ConsoleBoardView(self.board)
     self.win = 0
     self.p1 = p[0]
     self.p2 = p[1]
     self.p3 = p[2]
 def __init__(self):
   self.board = Board(None)
   self.view  = ConsoleBoardView(self.board)
 def __init__(self):
     self.board = Board(None)
     self.view = ConsoleBoardView(self.board)
class BoardController:
    def __init__(self):
        self.board = Board(None)
        self.view = ConsoleBoardView(self.board)

    def init_game(self):

        self.white_player = self._select_player(Board.WHITE)
        self.black_player = self._select_player(Board.BLACK)

        self.atual_player = self.black_player

        finish_game = 0

        self.view.update_view()

        while finish_game != 2:
            raw_input("")
            atual_color = self.atual_player.color
            print 'Jogador: ' + atual_color
            if self.board.valid_moves(atual_color).__len__() > 0:
                self.board.play(self.atual_player.play(self.board.get_clone()),
                                atual_color)
                self.view.update_view()
                finish_game = 0
            else:
                print 'Sem movimentos para o jogador: ' + atual_color
                finish_game += 1
            self.atual_player = self._opponent(self.atual_player)

        self._end_game()

    def _end_game(self):
        score = self.board.score()
        if score[0] > score[1]:
            print ""
            print 'Jogador ' + self.white_player.__class__.__name__ + '(' + Board.WHITE + ') Ganhou'
        elif score[0] < score[1]:
            print ""
            print 'Jogador ' + self.black_player.__class__.__name__ + '(' + Board.BLACK + ') Ganhou'
        else:
            print ""
            print 'Jogo terminou empatado'

    def _opponent(self, player):
        if player.color == Board.WHITE:
            return self.black_player

        return self.white_player

    def _select_player(self, color):
        players = glob.glob('./models/players/*_player.py')
        print 'Selecione um dos players abaixo para ser o jogador ' + color

        for idx, player in enumerate(players):
            print idx.__str__() + " - " + player

        player = raw_input("Digite o numero do player que voce deseja: ")
        module_globals = {}
        execfile(players[int(player)], module_globals)
        print module_globals.keys()
        return module_globals[module_globals.keys()[len(module_globals.keys())
                                                    - 1]](color)
示例#8
0
class BoardController:
  def __init__(self):
    self.board = Board(None)
    self.view  = ConsoleBoardView(self.board)

  def init_game(self):


    self.white_player = self._select_player(Board.WHITE)
    self.black_player = self._select_player(Board.BLACK)

    self.atual_player = self.black_player

    finish_game = 0

    self.view.update_view()

    while finish_game != 2:
      raw_input("")
      atual_color = self.atual_player.color
      print 'Jogador: ' + atual_color
      if self.board.valid_moves(atual_color).__len__() > 0:
        self.board.play(self.atual_player.play(self.board.get_clone()), atual_color)
        self.view.update_view()
        finish_game = 0
      else:
        print 'Sem movimentos para o jogador: ' + atual_color
        finish_game += 1
      self.atual_player = self._opponent(self.atual_player)

    self._end_game()


  def _end_game(self):
    score = self.board.score()
    if score[0] > score[1]:
      print ""
      print 'Jogador ' + self.white_player.__class__.__name__ + '('+Board.WHITE+') Ganhou'
    elif score[0] < score[1]:
      print ""
      print 'Jogador ' + self.black_player.__class__.__name__ + '('+Board.BLACK+') Ganhou'
    else:
      print ""
      print 'Jogo terminou empatado'

  def _opponent(self, player):
    if player.color == Board.WHITE:
      return self.black_player

    return self.white_player

  def _select_player(self, color):
    players = glob.glob('./models/players/*_player.py')
    print 'Selecione um dos players abaixo para ser o jogador '+color

    for idx, player in enumerate(players):
      print idx.__str__() + " - " + player

    player = raw_input("Digite o numero do player que voce deseja: ")
    module_globals = {}
    execfile(players[int(player)], module_globals)
    print module_globals.keys()

    return module_globals[module_globals.keys()[len(module_globals.keys()) - 1]](color)
class BoardController(object):
    """Objeto do controlador de jogo de Othelo."""

    def __init__(self):
        self.board = Board(None)
        self.view = ConsoleBoardView(self, self.board)

        self.white_player = None
        self.black_player = None
        self.atual_player = None
        self.finish_game = 0

    def init_game(self):
        """Inicio o jogo Othelo."""
        self.view.carregar_jogadores_possiveis(self._possible_players_list())
        self.view.put_view_in_main_loop()

    def restart_game(self):
        """Reinicia o jogo Othelo."""
        self.board = Board(None)
        self.view.reiniciar_jogo(self.board)

        self.white_player = None
        self.black_player = None
        self.atual_player = None
        self.finish_game = 0

    def next_round(self):
        """Permite que a IA realize a jogada seguinte."""
        if self.finish_game == 3:
            self.restart_game()
            return

        atual_color = self.atual_player.color
        if self.board.valid_moves(atual_color).__len__() > 0:
            self.board.play(self.atual_player.play(self.board.get_clone()), atual_color)
            self.view.atualizar_discos()
            self.finish_game = 0
        else:
            self.finish_game += 1
        self.atual_player = self._opponent(self.atual_player)

        self.view.atualizar_jogador_atual(self.atual_player.color)

        if self.finish_game == 2:
            self._end_game()

    def _possible_players_list(self):
        return glob.glob('./models/players/*_player.py')

    def select_player(self, player, color):
        """Carrega o arquivo de um jogador para certa cor e retorna seu modulo."""
        module_globals = {}
        execfile(player, module_globals)
        return module_globals[module_globals.keys()[len(module_globals.keys()) - 1]](color)

    def _end_game(self):
        score = self.board.score()
        if score[0] > score[1]:
            self.view.anunciar_vitorioso(self.white_player.__class__.__name__,
                                         self.black_player.__class__.__name__,
                                         score[0], score[1])
        elif score[0] < score[1]:
            self.view.anunciar_vitorioso(self.black_player.__class__.__name__,
                                         self.white_player.__class__.__name__,
                                         score[1], score[0])

        self.finish_game = 3

    def _opponent(self, player):
        if player.color == Board.WHITE:
            return self.black_player

        return self.white_player
示例#10
0
 def __init__(self, white, black):
   self.board = Board(None)
   self.white = white
   self.black = black
   self.view  = ConsoleBoardView(self.board)
   self.win = 0
示例#11
0
class TestBoardController:
  def __init__(self, white, black):
    self.board = Board(None)
    self.white = white
    self.black = black
    self.view  = ConsoleBoardView(self.board)
    self.win = 0

  def init_game(self):

    self.white_player = self._select_player(Board.WHITE)
    self.black_player = self._select_player(Board.BLACK)

    self.atual_player = self.black_player

    finish_game = 0

    self.view.update_view()

    while finish_game != 2:
      # raw_input("")
      atual_color = self.atual_player.color
      print('Jogador: ' + atual_color)

      if self.board.valid_moves(atual_color).__len__() > 0:
        self.board.play(self.atual_player.play(self.board.get_clone()), atual_color)
        self.view.update_view()
        finish_game = 0
      else:
        print('Sem movimentos para o jogador: ' + atual_color)
        finish_game += 1
      
      self.atual_player = self._opponent(self.atual_player)

    ##begin added
    self.view.update_view()
    ##end added
    self._end_game()
    return self.win

  def _score(self):
    return self.board.score()

  def _end_game(self):
    score = self.board.score()
    if score[0] > score[1]:
      print("")
      print('Jogador ' + self.white_player.__class__.__name__ + '('+Board.WHITE+') Ganhou')
      self.win = 1
    elif score[0] < score[1]:
      print("")
      print('Jogador ' + self.black_player.__class__.__name__ + '('+Board.BLACK+') Ganhou')
      self.win = 2
    else:
      print("")
      print('Jogo terminou empatado')

  def _opponent(self, player):
    if player.color == Board.WHITE:
      return self.black_player

    return self.white_player

  def _select_player(self, color):
    players = glob.glob('./models/players/*_player.py')

    player = self.white if color == Board.WHITE else self.black
    module_globals = {}
    exec(compile(open(players[int(player)], "rb").read(), players[int(player)], 'exec'), module_globals)
    print(list(module_globals.keys()))
    return module_globals[list(module_globals.keys())[len(list(module_globals.keys())) - 1]](color)