Advertisement
Darker666

headless_reversi_creator.py

Nov 27th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.74 KB | None | 0 0
  1. import random_player
  2. from game_board import GameBoard
  3. import time, getopt, sys
  4.  
  5. class HeadlessReversiCreator(object):
  6.     '''
  7.    Creator of the Reversi game without the GUI.
  8.    '''
  9.  
  10.     def __init__(self, player1, player1_color, player2, player2_color, board_size=8):
  11.         '''
  12.        :param player1: Instance of first player
  13.        :param player1_color: color of player1
  14.        :param player2: Instance of second player
  15.        :param player1_color: color of player2
  16.        :param boardSize: Board will have size [boardSize x boardSize]
  17.        '''
  18.         self.board = GameBoard(board_size, player1_color, player2_color)
  19.         self.player1 = player1
  20.         self.player2 = player2
  21.         self.current_player = self.player1
  22.         self.current_player_color = player1_color
  23.         self.player1_color = player1_color
  24.         self.player2_color = player2_color
  25.         self.silent = False
  26.  
  27.     def play_game(self):
  28.         '''
  29.        This function contains game loop that plays the game.
  30.        '''
  31.         # TODO: Time limit for move
  32.         correct_finish = True
  33.         while self.board.can_play(self.current_player,self.current_player_color):
  34.             startTime = time.time()
  35.             move = self.current_player.move(self.board.get_board_copy())
  36.             endTime = time.time()
  37.             moveTime = (endTime - startTime) * 1000
  38.             if move is None:
  39.                 if not self.silent:
  40.                     print('Player %d reurns None istead of a valid move. Move takes %.3f ms.' % (self.current_player_color, moveTime))
  41.                 correct_finish = False
  42.                 break
  43.             else:
  44.                 if not self.silent:
  45.                     print('Player %d wants move [%d,%d]. Move takes %.3f ms.' % (self.current_player_color, move[0], move[1], moveTime))
  46.  
  47.             if self.board.is_correct_move(move, self.current_player,self.current_player_color):
  48.                 if not self.silent:
  49.                     print('Move is correct')
  50.                 self.board.play_move(move, self.current_player,self.current_player_color)
  51.  
  52.             else:
  53.                 if not self.silent:
  54.                     print('Player %d made the wrong move [%d,%d]' % (self.current_player_color, move[0], move[1]))
  55.                 correct_finish = False
  56.                 break
  57.                
  58.             self.change_player()
  59.             if not self.board.can_play(self.current_player,self.current_player_color):
  60.                 if not self.silent:
  61.                     print('No possible move for Player %d' % (self.current_player_color))
  62.                 self.change_player()
  63.                 if self.board.can_play(self.current_player,self.current_player_color):
  64.                     if not self.silent:
  65.                         print('Player %d plays again ' % (self.current_player_color))
  66.                 else:
  67.                     if not self.silent:
  68.                         print('Game over')
  69.             if not self.silent:
  70.                 self.board.print_board()
  71.         if correct_finish:
  72.             return self.printFinalScore()
  73.         else:
  74.             if not self.silent:
  75.                 print('Game over.')
  76.             if self.current_player_color == self.player1_color:
  77.                 if not self.silent:
  78.                     print('Winner is player %d.' % (self.player2_color))
  79.                 return self.player2
  80.             else:
  81.                 if not self.silent:
  82.                     print('Winner is player %d.' % (self.player1_color))
  83.                 return self.player1
  84.  
  85.  
  86.     def change_player(self):
  87.         '''
  88.        Change the current_player
  89.        '''
  90.         if self.current_player == self.player1:
  91.             self.current_player = self.player2
  92.             self.current_player_color = self.player2_color
  93.         else:
  94.             self.current_player = self.player1
  95.             self.current_player_color = self.player1_color
  96.  
  97.     def printFinalScore(self):
  98.         p1Stones = 0
  99.         p2Stones = 0
  100.         for x in range(self.board.board_size):
  101.             for y in range(self.board.board_size):
  102.                 if self.board.board[x][y] == 0:
  103.                     p1Stones += 1
  104.                 if self.board.board[x][y] == 1:
  105.                     p2Stones += 1
  106.         if not self.silent:
  107.             print('\n\n-----------------------------\n')
  108.             print('Final score:\n\nPlayer%d:Player%d\n\t[%d:%d]\n' % (self.player1_color, self.player2_color, p1Stones, p2Stones))
  109.         if p1Stones > p2Stones:
  110.             if not self.silent:
  111.                 print('Player %d wins!' % (self.player1_color))
  112.             return self.player1
  113.         elif p2Stones > p1Stones:
  114.             if not self.silent:
  115.                 print('Player %d wins!' % (self.player2_color))
  116.             return self.player2
  117.         else:
  118.             if not self.silent:
  119.                 print('Draw')
  120.             return None
  121.         if not self.silent:
  122.             print('\n-----------------------------\n\n')
  123.  
  124. if __name__ == "__main__":
  125.     (choices,args) = getopt.getopt(sys.argv[1:],"")
  126.     p1_color = 0
  127.     p2_color = 1
  128.  
  129.     if len(args) == 0:
  130.         print('No arguments given.\nRunning game with two random players.')
  131.         p1 = random_player.MyPlayer(p1_color, p2_color)
  132.         p2 = random_player.MyPlayer(p2_color, p1_color)
  133.         game = HeadlessReversiCreator(p1, p1_color, p2, p2_color, 8)
  134.         game.play_game()
  135.  
  136.     elif len(args) == 1:
  137.         print('One player given in argument.\nRunning game with given player aginst the random player.')
  138.         p1 = random_player.MyPlayer(p1_color, p2_color)
  139.         try:
  140.             player_module = __import__(args[0])
  141.             p2 = player_module.MyPlayer(p2_color, p1_color)
  142.            
  143.             game = HeadlessReversiCreator(p1, p1_color, p2, p2_color, 8)
  144.             game.play_game()
  145.  
  146.         except ImportError:
  147.             print('Error: Cannot import given player: %s.' %(args[0]))
  148.  
  149.     else:
  150.         if len(args) > 2:
  151.             print('More than two arguments given. Ignoring other arguments and using only the first and the second as players.')
  152.  
  153.         importsCorrect = True
  154.         try:
  155.             player_module = __import__(args[0])
  156.             p1 = player_module.MyPlayer(p1_color, p2_color)
  157.         except ImportError:
  158.             importsCorrect = False
  159.             print('Error: Cannot import given player: %s.' %(args[0]))
  160.  
  161.         try:
  162.             player_module = __import__(args[1])
  163.             p2 = player_module.MyPlayer(p2_color, p1_color)
  164.         except ImportError:
  165.             importsCorrect = False
  166.             print('Error: Cannot import given player: %s.' %(args[1]))
  167.  
  168.         if importsCorrect:
  169.             game = HeadlessReversiCreator(p1, p1_color, p2, p2_color, 8)
  170.             game.play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement