def play_tic_tac_toe(): gb = GameBoard() gb.turnNumber = random.choice([0, 1]) # gb.print_board() while True: # print() currPlayer, otherPlayer = activePlayer[gb.turnNumber], activePlayer[(gb.turnNumber + 1) % 2] coord = currPlayer.make_move(gb) gb.place_piece(coord) results = gb.get_results() if results == currPlayer.piece: currPlayer.update(1, gb) otherPlayer.update(-1, gb) break if results == "-": # tie game currPlayer.update(0.5, gb) otherPlayer.update(0.5, gb) break otherPlayer.update(0, gb) if player[0] != "persona" or player[1] != "persona": gb.print_board() gb.switch_turn()
def train(numberOfGames): gb = GameBoard() p1 = Players.Merlin("O") p2 = Players.Merlin("X") players = [p1, p2] for i in range(numberOfGames): if i % 100 == 0: print(i) gb = GameBoard() gb.turnNumber = random.choice([0, 1]) while True: # gb.print_board() active, nextP = players[gb.turnNumber], players[(gb.turnNumber + 1) % 2] move = active.make_move(gb) gb.place_piece(move) r = gb.get_results() # print(r) if r == "O" or r == "X": active.update(1, gb) nextP.update(-1, gb) break if r == "-": active.update(0, gb) nextP.update(0, gb) break active.update(0, gb) nextP.update(0, gb) gb.switch_turn() p2.write_history()
def play_vs_merlin(numberOfGames): p1 = Players.Player("O") p2 = Players.Merlin("X", learning = False) players = [p1, p2] for i in range(numberOfGames): gb = GameBoard() gb.turnNumber = random.choice([0, 1]) while True: gb.print_board() move = players[gb.turnNumber].make_move(gb) gb.place_piece(move) gb.switch_turn() if (gb.get_results() != None): break
turn = gb.turnNumber # print(gb.turnNumber) if mbd and not mbdPrev or False: if gb.is_valid_move(players[turn].make_move(gb)): gb.place_piece(players[turn].make_move(gb)) results = gb.get_results() if results != None: print(results) # p2.end_game(gb, results) # p1.end_game(gb, results) gb = GameBoard() gb.turnNumber = random.choice([0, 1]) if count == 1000: done = True else: gb.switch_turn() # --- Drawing code screen.fill(WHITE) for i in range(3): for j in range(3): pygame.draw.rect(screen, colors[cellStates.index(gb.board[i][j])], [i * SQUARE_SIZE, j * SQUARE_SIZE, 200, 200], 0)