def test_o_win(self): #diagonal game = tictactoe.Game() game.move('02') game.move('00') game.move('10') game.move('11') game.move('20') results = game.move('22') self.assertEqual(results['game_state'],'o') #middle game = tictactoe.Game() game.move('02') game.move('01') game.move('10') game.move('11') game.move('20') results = game.move('21') self.assertEqual(results['game_state'],'o') #side game = tictactoe.Game() game.move('02') game.move('00') game.move('11') game.move('10') game.move('21') results = game.move('20') self.assertEqual(results['game_state'],'o')
def randomtraining2(beginn=False): training = [] g = t.Game() while True: rand = r.randint(0, 8) if g.play(rand) >= 0: break g.printgrid() while True: rand = r.randint(0, 8) if g.play(rand) >= 0: break g.printgrid() while True: inp = input() if not inp: break training.append([g.field[:], int(inp)]) print(training[-1]) if g.play(int(inp)) == 1: g.clear() g.printgrid() while True: rand = r.randint(0, 8) if g.play(rand) < 0: print(rand) else: break g.printgrid() return training
def train(agent): for e in range(1, NUM_EPSIODES + 1): for i in range(1, NUM_ITERS + 1): myGame = tictactoe.Game(DIM) state = agent.extractState(myGame) tempMemory = [] while (not myGame.isEnd()): action = agent.getAction(myGame) myGame.move(action) next_state = agent.extractState(myGame) winner = myGame.getWinner() tempMemory.append((state, next_state)) state = next_state for j, (state, next_state) in enumerate(tempMemory): weight = myGame.getWinner() * (agent.gamma **(len(tempMemory) - 1 - j)) agent.remember(state, next_state, weight) agent.replay(BATCH_SIZE) print("episode: {}/{}, epsilon: {}".format(e, NUM_EPSIODES, agent.epsilon)) print('Loss:', agent.history.history['loss'][0]) agent.model.save('dqnModel.h5')
def play_game(): player1 = UCTPlayer(100) player2 = ticplayer.BasicPlayer() large = True global game_instance game_instance = tictactoe.Game(large, player1, player2) result = game_instance.play() # if result != 1 if player1.getResult() == 1.0 else 2: # pass return result
def ply(terminal, command): if len(command) < 2: terminal.typeout('Give a game type with the PLY command.\n') return if command[1] != 'TIC': terminal.typeout('No such game ' + command[1] + '\n') return terminal.typeout('Playing tic-tac-toe\n\n') game = tictactoe.Game(terminal) game.reset() status = game.play_game() if status == tictactoe.GameStatus.x_win: finale.death_throws(terminal)
def __init__(self, move=None, parent=None, state=tictactoe.Game(), agent=None): self.move = move # the move that got us to this node self.parentNode = parent self.childNodes = [] self.wins = 0 self.visits = 0 self.state = state self.agent = agent self.untriedMoves = copy.deepcopy(state.getMoves())
def simulate_game(self, funct_p1, funct_p2): g = t.Game() run = True while run: # Zug Spieler 1 funct_p1(g) if g.turn == 8 or g.winner != 0: break # Zug Spieler 2 funct_p2(g) if g.turn == 8 or g.winner != 0: break # g.printgrid() return g.winner
def simulateGames(agent1, agent2, n=100): wins1 = 0 wins2 = 0 for i in range(n): myGame = tictactoe.Game() myGame.currPlayer = 1 # First player to move (count = 0) is agent 1 while (myGame.getMoves() and not myGame.isEnd()): if myGame.getNumMoves() % 2 == 0: myGame.move(agent1.getAction(myGame)) else: myGame.move(agent2.getAction(myGame)) if myGame.getWinner() == 1: wins1 += 1 elif myGame.getWinner() == -1: wins2 += 1 print('Player 1 won {} games, player 2 won {} games, {} ties'.format( wins1, wins2, n - wins1 - wins2)) f.write('{},{},{}\n'.format(wins1, wins2, n - wins1 - wins2))
def step(self): myGame = tictactoe.Game() tempMemory = [] while not myGame.isEnd(): state = self.agent.extractState(myGame) action = self.agent.getAction(myGame) myGame.move(action) newState = self.agent.extractState(myGame) tempMemory.append((state, newState)) # Store states/newstates in memory for i, (state, newState) in enumerate(tempMemory): discounted_win = myGame.getWinner() * (self.gamma **(len(tempMemory) - i - 1)) winProb = (1 + discounted_win) / 2 self.agent.remember(state, newState, winProb) with tf.GradientTape() as tape: total_loss = self.computeLoss()
#!/usr/bin/env python3 """ train3t.py train the current bot; pass a number of training rounds on the command line """ import sys import player import tictactoe trainer = player.Dullard() champion = player.AI(read_save=True) game_turn1 = tictactoe.Game(champion, trainer) game_turn2 = tictactoe.Game(trainer, champion) if __name__ == "__main__": for _ in range(int(sys.argv[1])): # every round, play a game as X and a game as O game_turn1.play() game_turn2.play() total_games = champion.wins + trainer.wins + trainer.draws print("Victory rate: %.2f%%" % (champion.wins / total_games * 100)) print("Draw rate: %.2f%%" % (champion.draws / total_games * 100)) champion.freeze()
import tictactoe # curses is in the python standard library, but it may not be available # on Windows because it depends on the GNU program "ncurses". import curses if __name__ == "__main__": game = tictactoe.Game("game.log") # curses.wrapper "wraps" the game in a curses terminal window application # environment, passing the window in as the first argument. This method # ensures that control is properly handed back to the native terminal # application upon exit or error. curses.wrapper(game)
#!/usr/bin/env python3 """ versus3t.py play a round against the current bot; it learns from these games as well """ import random import tictactoe import player ai_player = player.AI(read_save=True) if random.randint(0, 1): print("Go first") game = tictactoe.Game(player.Human(), ai_player) else: print("Go second") game = tictactoe.Game(ai_player, player.Human()) if __name__ == "__main__": game.play() ai_player.freeze()
import random p1score = 0 p2score = 0 ties = 0 print('Welcome to Tic-Tac-Toe!') while True: print() print('Please decide who is Player 1 and who is Player 2.') print() p1, p2 = tictactoe.inputPlayerLetters() print() print('Selecting a random player to begin the first turn.') game = tictactoe.Game(p1, p2, random.randint(0, 1)) while game.game_status == 1: if game.getCurrentPlayer() == 0: # Player 1's turn. game.printBoard() move = game.getPlayerMove() game.makeMove(game.p1, move) if game.isWinner(game.p1): game.printBoard() print('Player 1 has won the game!') p1score += 1 game.endGame() else: if game.isBoardFull():
def setUp(self): self.game = tictactoe.Game()
import tictactoe as t game = t.Game() game.start()
def setUp(self): # called before every test self.game = tictactoe.Game()
return 'NONE' print('---Rules---') print('1. The player who makes a move first will get X') print( '2. Table orientation is given below. Choose one number between 0-8 to mark the position.' ) print('\n') print(' 0 | 1 | 2 ') print('---|---|---') print(' 3 | 4 | 5 ') print('---|---|---') print(' 6 | 7 | 8 ') print(' ') game = tictactoe.Game() start = (input('Who starts?(P/C): ')) print('\n') while (game.checkWinner() == 0 and len(game.getEmptyCells()) != 0): if (start == 'C'): game.botMakeMove('X') game.printBoard() if (checkWinOrDraw(game) != 'NONE'): if (checkWinOrDraw(game) == 'WINS'): print('Computer Wins!') break else: print('Game Drawn!') break var = int(input('\nEnter position:')) if (var in game.getEmptyCells()):
from flask import Flask, request, redirect, url_for, render_template import tictactoe as ttt import time app = Flask(__name__) game = ttt.Game() @app.route('/') @app.route('/index') def index(): return render_template("index.html", board_state=game.get_board(), result=game.get_result(), level=game.get_level()) @app.route('/index/<int:id>') def input(id): print(id) p = game.set_move(id) r = game.get_result() print(r) print(game.level) game.get_move() if not r: game.get_move() r = game.get_result()