class Othello: def __init__(self): self.gameBoard = GameBoard() self.player1 = None self.player2 = None self.turn = 0 def createPlayer(self, symbol, playerNum): if symbol == 'O': if playerNum == '1': return Human(symbol) else: return Computer(symbol) else: if playerNum == '1': return Human(symbol) else: return Computer(symbol) def startGame(self): #basic logic self.player1 = self.createPlayer('O', choice1) self.player2 = self.createPlayer('X', choice2) self.gameBoard.init_gameBoard() self.gameBoard.printGameBoard() while not self.gameBoard.check_ending(): current_player = [self.player1, self.player2][self.turn] print("Player %s's turn." % current_player.playerSymbol) if self.gameBoard.check_legal_move(current_player.playerSymbol): pos = current_player.nextMove(self.gameBoard.board) self.gameBoard.execute_flip(pos, current_player.playerSymbol) else: print("There is no valid move for Player %s." % current_player.playerSymbol) self.turn = 1 - self.turn self.gameBoard.printGameBoard() s1, s2 = self.gameBoard.check_winner() if s1 > s2: winner = 'O' # Black elif s1 < s2: winner = 'X' # White elif s1 == s2: winner = ' ' # Tie print('Count O : {}'.format(s1)) print('Count X : {}'.format(s2)) if winner != ' ': print('Player {} won!\n'.format(winner)) else: print('A tie')
class Othello: def __init__(self): self.gameBoard = GameBoard() self.player1 = None self.player2 = None self.turn = 0 def create_player(self, symbol, player_num): print("Please choose player " + str(player_num) + " (" + symbol + "):") print("1. Human") print("2. Computer Player") player_type = int(input("Your Choice is: ")) if player_type == 1: print("Player " + symbol + " is " + 'Human.') return Human(symbol) else: print("Player " + symbol + " is " + 'Computer(AI).') return ComputerAI(symbol) def start_game(self): self.player1 = self.create_player('O', 1) self.player2 = self.create_player('X', 2) self.gameBoard.init_game_board() self.gameBoard.print_game_board() while not self.gameBoard.check_ending(): current_player = [self.player1, self.player2][self.turn] if self.gameBoard.check_legal_move(current_player.player_symbol): pos = current_player.next_move(self.gameBoard.board) self.gameBoard.execute_flip(pos, current_player.player_symbol) else: print("There is no valid move for Player " + current_player.player_symbol + ".") self.turn = 1 - self.turn self.gameBoard.print_game_board() s1, s2 = self.gameBoard.check_winner() if s1 > s2: winner = 'O' # Black elif s1 < s2: winner = 'X' # White elif s1 == s2: winner = ' ' # Tie print('Count O : {}'.format(s1)) print('Count X : {}'.format(s2)) if winner != ' ': print('Player {} won!\n'.format(winner)) else: print('A tie')
class Othello: def __init__(self): self.gameBoard = GameBoard() self.player1 = None self.player2 = None self.turn = 0 def createPlayer(self, symbol, playerNum): pass def startGame(self): #basic logic self.player1 = self.createPlayer('O', 1) self.player2 = self.createPlayer('X', 2) self.gameBoard.init_gameBoard() self.gameBoard.printGameBoard() while not self.gameBoard.check_ending(): current_player = [self.player1,self.player2][self.turn] if self.gameBoard.check_legal_move(current_player.playerSymbol): pos = current_player.nextMove(self.gameBoard.board) self.gameBoard.execute_flip(pos, current_player.playerSymbol) self.turn = 1 - self.turn self.gameBoard.printGameBoard() s1, s2 = self.gameBoard.check_winner() if s1 > s2: winner = 'O' # Black elif s1 < s2: winner = 'X' # White elif s1 == s2: winner = ' ' # Tie print('Count O : {}'.format(s1)) print('Count X : {}'.format(s2)) if winner != ' ': print('Player {} won!\n'.format(winner)) else: print('A tie')
class Othello: def __init__(self): self.gameBoard = GameBoard() self.player1 = None self.player2 = None self.turn = 0 def createPlayer(self, symbol, playerNum): while True: p1 = input( 'Please choose player {} ({}): \n1. Human \n2. Computer Player \nYour choice is: ' .format(playerNum, symbol)) try: p1 = int(p1) if p1 == 1: if playerNum == 1: print('Player O is Human.') return Human(symbol) elif playerNum == 2: print('Player X is Human.') return Human(symbol) else: print('Invalid Input.') elif p1 == 2: if playerNum == 1: print('Player O is Computer.') return Computer(symbol) elif playerNum == 2: print('Player X is Computer.') return Computer(symbol) else: print('Invalid Input.') else: print('Invalid Input.') except ValueError: print('Invalid Input.') def startGame(self): #basic logic self.player1 = self.createPlayer('O', 1) self.player2 = self.createPlayer('X', 2) self.gameBoard.init_gameBoard() self.gameBoard.printGameBoard() while not self.gameBoard.check_ending(): current_player = [self.player1, self.player2][self.turn] if self.gameBoard.check_legal_move(current_player.playerSymbol): pos = current_player.nextMove(self.gameBoard.board) self.gameBoard.execute_flip(pos, current_player.playerSymbol) self.turn = 1 - self.turn self.gameBoard.printGameBoard() s1, s2 = self.gameBoard.check_winner() if s1 > s2: winner = 'O' # Black elif s1 < s2: winner = 'X' # White elif s1 == s2: winner = ' ' # Tie print('Count O : {}'.format(s1)) print('Count X : {}'.format(s2)) if winner != ' ': print('Player {} won!\n'.format(winner)) else: print('A tie')