def train(self, player=Player('X'), board=Board(), previous_key=None, previous_move=None): memory_key = Model.MEMORY_KEY.format(player.char, board.key) if memory_key not in self.memory.keys(): self.memory[memory_key] = { 'weights': [0, 0, 0, 0, 0, 0, 0, 0, 0], 'childs': [], 'previous_key': previous_key, 'previous_move': previous_move } self._set_all_related(memory_key) for index in range(9): _board = Board(board.key) done, winner = _board.add(player, index) if done: if winner: self._set_weight( memory_key, index, 3.0 if winner.char == 'X' else -2.0 if winner.char == 'O' else -1.0) if winner is False: self.train( player=Player('X' if player.char == 'O' else 'O'), board=_board, previous_key=memory_key, previous_move=index) if not previous_key: self._cleanup()
def test_minimax(self): """ Tests that computer can block human. """ board = Board() computer = Player(board, 'C', 'computer', is_human=False, ai_strat=Minimax(board)) human = Player(board, 'H', 'human') # set up human about to win board.spaces[0] = human board.spaces[1] = human # set up computer to not have win cond board.spaces[4] = computer board.spaces[8] = computer """ |H|H|2| |3|C|5| |6|7|C| """ board.CURRENT_PLAYER = computer board.WAITING_PLAYER = human self.assertEqual(2, computer.make_play())
def test_player_move(self, monkeypatch): """Test Player.move method.""" config = {'char': 'X', 'ai': False} player = Player(config) # Monkeypatch Python's input function for this test's runtime. monkeypatch.setattr('builtins.input', lambda: 'test input') i = player.move() assert i == ('test input')
def handleServerMsg(self, res): if not self.player: if 'X' in res: #print("soy x") self.player = Player('X') elif 'O': #print("soy o") self.player = Player('O') print(res)
def test_player_init(self): """Test Player initialization.""" config = {'char': 'X', 'ai': False} player = Player(config) assert player.char is 'X' and player.ai is False config = {'char': '@', 'ai': True} player = Player(config) assert player.char is '@' and player.ai is True
def test(): game = Game() game.add_player(Player(15)) game.add_player(Player(0)) game.start() game.add_mark(15, 0, 0) add_available_mark(game) game.add_mark(15, 1, 0) add_available_mark(game) game.add_mark(15, 2, 0) print game.get_won_marks(15)
def start(bot, update): chat_id = update.message.chat_id if chat_id not in games: game = Game() game.add_player(Player(chat_id)) game.add_player(Player(0)) game.start() games[chat_id] = game reply_markup = get_keyboard(game, False) bot.sendMessage(chat_id=chat_id, text="Let's the game begin!", reply_markup=reply_markup)
def test_ai_move(self): """Test Player.ai_move method.""" config = {'char': '@', 'ai': True} player = Player(config) grid = Grid(3) i = player.ai_move(grid) # Assert AI input is in correct format. assert re.match(r'^\d\,\d$', i) # Assert AI input is within the grid. i = i.split(',') x = int(i[0]) y = int(i[1]) assert 0 <= x <= grid.size assert 0 <= y <= grid.size
class ClientTic: def __init__(self): self.client = socket(AF_INET, SOCK_STREAM) self.host = gethostname() self.port = 5000 self.tic = Tictactoe() self.player = None def encodeMsg(self, msg): return bytes(msg, 'utf-8') def theBoard(slef): return self.theBoard def connect(self): try: self.client.connect((self.host, self.port)) self.client.send(self.encodeMsg("Gracias bro")) while True: buff = self.client.recv(1024) res = str(buff) if "[Server Msg]" in res: self.handleServerMsg(res) elif "[Player Movement]" in res: self.handlePlayerMovement(res) elif "[Player Turn]" in res: self.handlePlayerTurns(res) sleep(3) except error as e: print(e) exit() raise e def handleServerMsg(self, res): if not self.player: if 'X' in res: #print("soy x") self.player = Player('X') elif 'O': #print("soy o") self.player = Player('O') print(res) def handlePlayerMovement(self, res): move = res.split(']') move = move[1].split("'") if (self.player.name == "X"): self.tic.printBoard(move[0], 'O') else: self.tic.printBoard(move[0], 'X') def handlePlayerTurns(self, res): move = self.player.doPlay(self.tic.theBoard.keys()) system('cls') self.tic.printBoard(move, self.player.name) self.client.send(self.encodeMsg(move))
def natural_selection(self): new_players = [] # First child of new array is the best player of previous generation without mutation best_player = self.select_best_player() # Create new player with previous players brain to remove move history and such new_best_player = Player(best_player.brain) new_players.append(new_best_player) crossing_players = True parents = self.select_mating_pool(int(len(self.population)) + 1) index = 0 while crossing_players: parent1 = parents[index] index += 1 parent2 = parents[index] index += 1 children = parent1.crossover(parent2) for child in children: if len(new_players) < self.popSize: child.mutate() new_players.append(child) else: crossing_players = False break self.population = new_players
def __init__(self, popSize, spread): self.popSize = popSize self.count = 0 self.spread = spread #self.population = [Match([[0, 0, 0], [0, 0, 0], [0, 0, 0]], [Player(1), Player(2)]) for i in range(popSize)] self.population = [Player() for i in range(popSize)] self.popFitness = 1
def test_minimax_failing_block(self): """ |H|1|H| |3|C|5| |6|7|8| """ board = Board() computer = Player(board, 'C', 'computer', is_human=False, ai_strat=Minimax(board)) computer.ai_strat.MINIMAX_DEPTH = 1 human = Player(board, 'H', 'human') board.spaces[0] = human board.spaces[2] = human board.spaces[4] = computer board.CURRENT_PLAYER = computer board.WAITING_PLAYER = human self.assertEqual(1, computer.make_play())
def test_board_get_win_detection(self): """ Tests that a board can detect when a win is present. """ board = Board() self.assertFalse(board.get_win()) # create a player and mark a winning move player = Player(board, 'H', 'human') winning_move = choice(board.WINNING_POSITIONS) for space in winning_move: board.spaces[space] = player self.assertTrue(board.get_win())
def test_computer_player(): game = Game(RandomPlayer(), Player()) assert [space for space in game.grid if space is not None] == [game.player_x]
def game(): player_x = Player() player_o = Player() return Game(player_x, player_o)
def test_player(): player = Player() assert player.is_computer is False
def test_computer_player_autoplay_false(): game = Game(RandomPlayer(), Player(), is_autoplay=False) assert [space for space in game.grid if space is not None] == []