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 assess(self, other): for c1 in self.chromosomes: for c2 in other.chromosomes: # print("new battle") for games in range(10): p1 = Players.Chromo("O", c1) p2 = Players.Chromo("X", c2) players = [p1, p2] gb = GameBoard() while True: active, nextP = players[gb.turnState], players[(gb.turnState + 1) % 2] move = active.make_move(gb) # print(move) # gb.print_board() gb.place_piece(move) gb.increment_turn() r = gb.get_results() if r == "O": # gb.print_board() c1.update(1) c2.update(-1) break if r == "X": # gb.print_board() c1.update(-1) c2.update(1) break if r == "-": c1.update(.2) c2.update(.2) break 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 test_is_valid_move(): print("Testing is_valid_move...") gb = GameBoard() gb.place_piece([1,1]) if (not(gb.is_valid_move([1,1]))): print(" Success") if (gb.is_valid_move([1, 2])): print(" Success") if (not(gb.is_valid_move([1,3]))): print(" Success")
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
def play(numberOfGames): p1 = Players.Player("O") p2 = Players.Player("X") players = [p1, p2] for i in range(numberOfGames): gb = GameBoard() while True: print(gb.valid_actions()) gb.print_board() move = players[gb.turnNumber].make_move(gb) gb.place_piece(move) if (gb.get_results() != None): break gb.switch_turn()
def test_has_won(): print("\nTesting has_won...") gb = GameBoard() gb.place_piece([0,0]) gb.place_piece([1,0]) gb.place_piece([1,1]) gb.place_piece([2,0]) gb.place_piece([2,2]) gb.place_piece([2,1]) if (gb.has_won(gb.pieces[gb.turnNumber])): print("Success")
# --- Game logic mx = pygame.mouse.get_pos()[0] my = pygame.mouse.get_pos()[1] mbd = pygame.mouse.get_pressed()[0] # print(get_cell(mx, my)) loc = get_cell(mx, my) p1.update(gb, loc) 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()