def main(): global board_size board_size = 3 global t t = ttt.TTT(board_size) console.log(t.get_board()) display_board(t, board_size, None, None)
def test_1(self): tic = ttt.TTT() assert len(tic.board) == 9 assert all([s == '.' for s in tic.board]) assert tic.state is ttt.game_state(tic) assert tic.display( ) == '. | . | .\n---------\n. | . | .\n---------\n. | . | .' assert tic._nelem('.') == 9 assert tic._nelem('x') == 0 assert tic._nelem('o') == 0 self.assertRaises(AssertionError, lambda: tic._nelem('p')) assert tic.state is ttt.GameStates.unfinished tic.board = ['x', 'x', 'x', 'x', 'x'] assert tic.state is ttt.GameStates.invalid tic.board = ['x'] * 9 assert tic.state is ttt.GameStates.invalid tic.board = ['o'] * 9 assert tic.state is ttt.GameStates.invalid tic.board = ['x', 'x', '.', '.', '.', '.', '.', '.', '.'] assert tic.state is ttt.GameStates.invalid tic.board = ['x', 'o', 'o', '.', '.', '.', '.', '.', '.'] assert tic.state is ttt.GameStates.invalid tic.board = ['x', 'x', 'x', 'o', 'o', '.', '.', '.', '.'] assert tic.state is ttt.GameStates.x_wins tic.board = ['o', 'x', 'x', 'o', 'o', '.', 'x', 'x', 'o'] assert tic.state is ttt.GameStates.o_wins tic.board = ['x', 'o', 'x', 'x', 'o', 'o', 'o', 'x', '.'] assert tic.state is ttt.GameStates.draw # tic.board = ['o', 'x', 'o', 'x', 'o', '.', 'x', 'x', '.'] # assert tic.state is ttt.GameStates.o_wins tac = ttt.TTT() tac.play('NE') assert tac.state is ttt.GameStates.unfinished print(tac.display())
def tournament(pls, n=1000): """ Play tournament of players (everybody against everybody as long as they play opposite same side) """ print("Entering tournament with players:") for p in pls: print(" " + p.name + " playing " + {1: "x", 4: "o"}[p.pl]) print() for pair in itertools.combinations(pls, 2): if pair[0].pl == pair[1].pl: # Invalid pairing continue p1, p2 = sorted(pair, key=lambda x: x.pl) nx, no, nd = 0, 0, 0 for i in range(n): t = ttt.TTT() p1.initGame() p2.initGame() while True: m = p1.move(t, p="greedy") w = t.move(m, p1.pl) if not w is None: break m = p2.move(t, p="greedy") w = t.move(m, p2.pl) if not w is None: break if w == 1: nx += 1 elif w == -1: nd += 1 else: no += 1 print("Performance of " + p1.name + " (x) against " + p2.name + " (o)") print(" x wins %6.2f %% and o wins %6.2f %%" % (nx / n * 100, no / n * 100)) print(" draws %6.2f %%" % (nd / n * 100)) print(" x looses %6.2f %% and o looses %6.2f %%" % ((n - nx - nd) / n * 100, (n - no - nd) / n * 100)) print()
episode = max(episode, 1) # to avoid divide by zero odds = 10 / episode odds = max(odds, 0.01) odds = min(odds, 1) return odds controller = ttt.TTT_vsRandoAI() qlrn = qlearning.TFQLearning(controller, comp_randact, neural.neural, future_discount=0.5) (player, results) = qlrn.runEpisodes(100000) #Play vs human controller = ttt.TTT() done = False while not done: state = controller.reset() controller.printBoard() while controller._isActive: moves = player.computeQState(state) move = np.argmax(moves) print(f"AI played move {move}") state, _, _ = controller.step(move) controller.printBoard() if controller._isActive: move = int(input("Enter your move [0-8]: ")) state, _, _ = controller.step(move) assert (not controller._isActive) controller.printBoard()
def init(): global t global login_sys t = ttt.TTT(3) login_sys = login.Login_Sys(2) return json.dumps({'result': 'OK'})
def train(pls, rounds): # Rewards for win, loose, and draw rw, rl, rd = 1., 0., 0.5 print("Entering training with players:") for p in pls: print(" " + p.name + " playing " + {1: "x", 4: "o"}[p.pl]) print() for pair in itertools.combinations(pls, 2): if pair[0].pl == pair[1].pl: # Invalid pairing continue p1, p2 = sorted(pair, key=lambda x: x.pl) print("Training rounds: ", p1, " vs. ", p2) for j in range(rounds): # Training can begin if not silent: print("Game no. %5d" % j) t = ttt.TTT() p1.initGame() p2.initGame() # Make moves until victory while True: m = p1.move(t) w = t.move(m, p1.pl) if not w is None: break m = p2.move(t) w = t.move(m, p2.pl) if not w is None: break if not silent: t.printHistory() # Determine winner if (w == 1): if not silent: print("Winner is 'x'") p1.update(rw) p2.update(rl) elif (w == 4): if not silent: print("Winner is 'o'") p1.update(rl) p2.update(rw) elif w == -1: if not silent: print("Draw") p1.update(rd) p2.update(rd) else: raise (ValueError("OOpps: " + str(w))) if not silent: print() print("-" * 80) print()