Exemplo n.º 1
0
  def one_turn( self ):
    #Draw big board
    self.draw()

    #First turn.  Select small game then select a spot on small game
    if self.small_game == None:
      msg = 'No inner game chosen.  Please select one using a small board as a reference: '
      print( '\n\n' + 2*self.smallseparator + '\n\n' )
      tictactoe().draw()
      print( )
      self.small_game = self.select_int( msg ) 

    self.games[self.small_game].current_player = self.current_player
          
    # prev choice leads to completed board
    if self.games[self.small_game].winner != None:
      #Find which small_games are won, create small board to match 
      tmp = tictactoe()
      for _ in range(len(self.games)):
        if self.games[_].winner == 'X':
          tmp.values[_] = 'X'
        elif self.games[_].winner == 'O':
          tmp.values[_] = 'O'
      print( "Your next move would be in a completed board.  Please select the next board to move from." )
      print( "The small board below shows where any games have been won.  Please choose a a free one" )
      tmp.draw()
      self.small_game = tmp.select( self.current_player )

    print()
    self.small_game = self.games[self.small_game].one_turn( self.current_player )
    print()
    self.games[self.small_game].is_winning()
    if self.is_winning():
      exit()
    self.swap_players()
Exemplo n.º 2
0
    def testColumnsWinCases(self):
        print '3: Testing Winning returns for all Column win types.'
        print '-' * 50

        # Making all possible column wins
        for k in range(1, 3):
            for i in range(0, 3):
                game = tictactoe()
                agent = game.agent
                x = []
                for j in range(0, 3):
                    agent[j][i] = k
                game.agent = agent
                try:
                    assert game.gameWin() == False
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '6 Columns checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True
Exemplo n.º 3
0
    def testRowsWinCases(self):
        print '2: Testing Winning returns for all Row win types.'
        print '-' * 50

        # Making all possible row wins
        for k in range(1, 3):
            for i in range(0, 3):
                game = tictactoe()
                agent = game.agent
                x = []
                for j in range(0, 3):
                    x.append(k)
                agent[i] = x
                game.agent = agent
                try:
                    assert game.gameWin() == False
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '6 Rows checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True
Exemplo n.º 4
0
 def test3row(self):
     for i in range(3):
         ttt = tictactoe.tictactoe()
         ttt.playMove(0, i)
         ttt.playMove(0, (i + 1) % 3, player=2)
         ttt.playMove(1, i)
         ttt.playMove(1, (i + 1) % 3, player=2)
         ttt.playMove(2, i)
         self.assertTrue(ttt.checkThree(0, i, down=1) == 1)
Exemplo n.º 5
0
 def test_run_game_using_choose_next_position_using_current_state(self):
     """
     Test if function choose_next_move_using_current_state
     """
     self.train_naive_rl()
     # TODO Export the csv using the db
     self.ttt = tictactoe.tictactoe()
     print("Test run npucs", self.ttt.board)
     self.ttt.play((1, 0))
     print("Test run npucs after first move", self.ttt.board)
     print(self.rs.choose_next_position_using_board(self.ttt, self.port))
Exemplo n.º 6
0
    def test_export_table_to_csv(self):
        """
        Test if the function export_table_to_csv works
        """

        # fufill db with states
        for i in range(10):
            self.ttt = tictactoe.tictactoe()
            states, winner = self.ttt.run(random_game=True)
            assert self.rs.insert_new_state(states, winner, self.port) == 0
        assert dbt.export_table_to_csv("State", test=True) == 0
def agent_play_against_heuristics(filename):
    game = tictactoe()
    game.reset()
    input_size = game.obs_space_size
    output_size = game.action_space_size
    hidden_layer_size = 128
    # agent = AlphaZero(input_size, hidden_layer_size, output_size)
    # agent = AlphaZeroResidual(input_size, hidden_layer_size, output_size)

    agent = AlphaZeroConv(input_size, hidden_layer_size, output_size)
    agent.load_state_dict(torch.load(filename, map_location=device))
Exemplo n.º 8
0
    def testDiagonalWinCases(self):
        print '4: Testing Winning returns for all Diagonal win types.'
        print '-' * 50

        # Making all possible Diagonal wins
        for k in range(1, 3):
            game = tictactoe()
            agent = game.agent
            x = []
            for j in range(0, 3):
                agent[j][j] = k
            game.agent = agent
            try:
                assert game.gameWin() == False
            except AssertionError as e:
                print '-' * 50
                print 'Test Cases Failed'
                print '\n\n'
                return e

        for k in range(1, 3):
            game = tictactoe()
            agent = game.agent
            x = []
            for j in range(0, 3):
                agent[j][2 - j] = k
            game.agent = agent
            try:
                assert game.gameWin() == False
            except AssertionError as e:
                print '-' * 50
                print 'Test Cases Failed'
                print '\n\n'
                return e

        print '-' * 50
        print '4 Diagonals checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True
Exemplo n.º 9
0
def test_tictactoe():
    with patch(
            '__builtin__.raw_input',
            MagicMock(side_effect=['0 0', '0 1', '1 1', '0 2', '2 2', '1 0'
                                   ])) as mock_raw_input:
        tictactoe.tictactoe()
    with patch(
            '__builtin__.raw_input',
            MagicMock(side_effect=['2 0', '0 1', '1 1', '1 0', '0 2', '1 0'
                                   ])) as mock_raw_input:
        tictactoe.tictactoe()
    with patch(
            '__builtin__.raw_input',
            MagicMock(side_effect=['2 0', '1 1', '2 1', '1 2', '2 2', '1 0'
                                   ])) as mock_raw_input:
        tictactoe.tictactoe()
    with patch(
            '__builtin__.raw_input',
            MagicMock(side_effect=[
                '0 2', '0 1', '1 2', 'xxxx', 'ads233 j33j3j', '-1 -1', '500 2',
                '0 0', '0 0', '2 2', '1 0'
            ])) as mock_raw_input:
        with patch('__builtin__.exit', MagicMock()):
            #tictactoe.tictactoe()
            runpy.run_module('tictactoe', run_name='__main__')

    try:
        tictactoe.TicTacToeBoard(1, 0, '_')
    except Exception as e:
        pass

    try:
        tictactoe.HumanPlayer('Josh', tictactoe.TicTacToeBoard.EMPTY_SPOT)
    except Exception as e:
        pass
Exemplo n.º 10
0
    def testWin(self):
        players = [1, 2]
        for pi in range(2):
            player = players[pi]
            otherPlayer = players[(pi + 1) % 2]
            print(player)
            print(otherPlayer)
            print(" ")
            for i in range(3):
                ttt = tictactoe.tictactoe()
                self.assertFalse(ttt.detectWin())
                ttt.playMove(0, i, player=player)
                ttt.playMove(0, (i + 1) % 3, player=otherPlayer)
                self.assertFalse(ttt.detectWin())
                ttt.playMove(1, i, player=player)
                ttt.playMove(1, (i + 1) % 3, player=otherPlayer)
                self.assertFalse(ttt.detectWin())
                ttt.playMove(2, i, player=player)
                ttt.drawboard()
                self.assertTrue(ttt.detectWin())

            for i in range(3):
                ttt = tictactoe.tictactoe()
                self.assertFalse(ttt.detectWin())
                ttt.playMove(i, 0, player=player)
                self.assertFalse(ttt.detectWin())
                ttt.playMove(i, 1, player=player)
                self.assertFalse(ttt.detectWin())
                ttt.playMove(i, 2, player=player)
                self.assertTrue(ttt.detectWin())

            ttt = tictactoe.tictactoe()
            ttt2 = tictactoe.tictactoe()
            for i in range(3):
                self.assertFalse(ttt.detectWin())
                ttt.playMove(i, i, player=player)
                self.assertFalse(ttt2.detectWin())
                ttt2.playMove(i, 2 - i, player=player)
            self.assertTrue(ttt.detectWin())
            self.assertTrue(ttt2.detectWin())
Exemplo n.º 11
0
def test_tictactoe():
    with patch(
        "__builtin__.raw_input", MagicMock(side_effect=["0 0", "0 1", "1 1", "0 2", "2 2", "1 0"])
    ) as mock_raw_input:
        tictactoe.tictactoe()
    with patch(
        "__builtin__.raw_input", MagicMock(side_effect=["2 0", "0 1", "1 1", "1 0", "0 2", "1 0"])
    ) as mock_raw_input:
        tictactoe.tictactoe()
    with patch(
        "__builtin__.raw_input", MagicMock(side_effect=["2 0", "1 1", "2 1", "1 2", "2 2", "1 0"])
    ) as mock_raw_input:
        tictactoe.tictactoe()
    with patch(
        "__builtin__.raw_input",
        MagicMock(
            side_effect=["0 2", "0 1", "1 2", "xxxx", "ads233 j33j3j", "-1 -1", "500 2", "0 0", "0 0", "2 2", "1 0"]
        ),
    ) as mock_raw_input:
        with patch("__builtin__.exit", MagicMock()):
            # tictactoe.tictactoe()
            runpy.run_module("tictactoe", run_name="__main__")

    try:
        tictactoe.TicTacToeBoard(1, 0, "_")
    except Exception as e:
        pass

    try:
        tictactoe.HumanPlayer("Josh", tictactoe.TicTacToeBoard.EMPTY_SPOT)
    except Exception as e:
        pass
Exemplo n.º 12
0
 def setUp(self):
     self.rs = RL_scratch.RL_scratch()
     self.ttt = tictactoe.tictactoe()
     self.port = 5433
     # create image db
     name = "postgres"
     assert dbt.create_image_using_dockerfile(name) == 0
     # check if image exist
     assert dbt.dtt.is_image_exist("c_ttt_" + name)
     # run container db on port 5433
     assert dbt.run_db(port=self.port) == 0
     # wait db connection
     assert dbt.wait_db_connection(port=self.port) == 0
Exemplo n.º 13
0
    def __init__(self, model, discount=.85, explore=.4):
        self.game = tictactoe.tictactoe()
        self.model = model

        # attempts to load a model and keep previous training
        try:
            self.load_model()
            print("model loaded")
        except:
            print("model not trained")

        # discount factor
        self.g = discount
        # exploration rate
        self.e = explore

        self.device = 'cpu'
Exemplo n.º 14
0
    def testInvalidMoves(self):
        print '6. Testing Invalid Moves'
        print '-' * 50

        for i in range(-1, 5):
            for j in range(-1, 5):
                if i < 0 or i > 2 or j < 0 or j > 2:
                    game = tictactoe()
                    print i, j
                    try:
                        assert game.registerMove(i, j) == 'Invalid Move'
                    except AssertionError, e:
                        # game.displayBoard()
                        print '-' * 50
                        print 'Test Cases Failed'
                        print '\n\n'
                        return
def load_and_play(filename, agent_plays=1, use_heuristic_agent=False):
    game = tictactoe()
    input_size = game.obs_space_size
    output_size = game.action_space_size
    hidden_layer_size = 128

    if use_heuristic_agent:
        agent = None
    else:
        agent = AlphaZeroResidual(input_size, hidden_layer_size, output_size)
        # agent = AlphaZeroResidual(input_size, hidden_layer_size, output_size)
        # agent = AlphaZeroConv(input_size, hidden_layer_size, output_size)
        agent.load_state_dict(torch.load(filename, map_location=device))
    agent.eval()
    play_with_agent(agent,
                    verbose=True,
                    agent_plays=agent_plays,
                    use_heuristic_agent=use_heuristic_agent)
Exemplo n.º 16
0
    def testPassAllValues(self):

        print '1: Testing Register Move for all values.'
        print '-' * 50
        for i in range(0, 3):
            for j in range(0, 3):
                print 'Testing registerMove(' + str(i) + ', ' + str(j) + ')'
                game = tictactoe()
                try:
                    assert game.registerMove(i, j) == True
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '9 Values checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True
Exemplo n.º 17
0
    def testNotWinCases(self):
        print '5. Checking possible cases that are not a Win'
        print '-' * 50

        for i in range(0, 3):
            for j in range(0, 3):
                game = tictactoe()
                game.agent[i][j] = 1
                game.agent[j][i] = 2
                game.displayBoard()
                try:
                    assert game.gameWin() == True
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '9 No Win cases checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True
Exemplo n.º 18
0
from discord.ext.commands import Bot
import discord
import asyncio
from BotFileHandler import BotFileHandler
from tictactoe import tictactoe

#insert token:
TOKEN = ''
BOT_PREFIX = '&'
DEFAULT_ACTIVITY = discord.Game(name="around with humans")
client = Bot(BOT_PREFIX)

point_handler = BotFileHandler("tunak_greetings_counter.txt")
ttt_game = tictactoe()


@client.command(pass_context=True)
async def hello(context):
    await client.say('Hello to you too, ' + context.message.author.mention)
    point_handler.add_points(context.message.author.name, 1)
    print("Bot: said hi to " + context.message.author.name)


@client.command()
async def topfriends():
    await client.say(point_handler.get_top_friends_str())
    print("Bot: revealed his best friends")


@client.command()
async def ttt(tag, x, y):
Exemplo n.º 19
0
from armory import fillBackpack

fillBackpack()
slowdown(
    "Well, now that you are equipped and have your destination ready, we will journey to your first destination.  I wish you luck, Agent."
)
"""importing modules for level 1: Planet Wilderness"""
import wilderness
from wilderness import wildIntro
from wilderness import seehowmany
from wilderness import visitForest
from wilderness import wateringHole
from wilderness import marketplace
from wilderness import forest
from ship_class import welcomeback
from tictactoe import tictactoe
"""Start level 1 with introduction and choices"""
wildIntro()
"""interact with villagers in marketplace"""
seehowmany()
"""interact with villagers at Watering Hole"""
wateringHole()
"""travel to new destination in on Planet Wilderness and explore forest"""
visitForest()
"""player must beat minigame to move forward"""
tictactoe()
slowdown(
    "Now that you've decoded the clue, you should head back to your ship as quickly as possible!"
)
welcomeback()
Exemplo n.º 20
0
def main():
    game = tictactoe()
    input_size = game.obs_space_size
    output_size = game.action_space_size
    hidden_layer_size = 128
    agent = AlphaZeroResidual(input_size, hidden_layer_size, output_size)
    #agent = AlphaZero(input_size, hidden_layer_size, output_size)
    # agent = AlphaZero(input_size, hidden_layer_size, output_size)
    # agent = AlphaZeroConvLarge(input_size, hidden_layer_size, output_size)

    agent.to(device)
    print(agent)
    agent.eval()  # sets batch norm properly
    print(f"Parameters: {sum([p.nelement() for p in agent.parameters()])}")

    iterations = 25  # how many times we want to make training data, update a model
    num_games = 30  # play certain number of games to generate examples each iteration (batches)
    search_steps = 30  # for each step of each game, consider this many possible outcomes
    optimization_steps = 10  # once we have generated training data, how many epochs do we do on this data

    num_faceoff_games = 2  # when comparing updated model and old model, how many games they play to determine winner

    lr = .01
    optimizer = torch.optim.Adam(agent.parameters(), lr=lr, weight_decay=0.01)
    lr_decay = .1
    lr_decay_period = 200

    replay_buffer_size = 800
    best_loss = .1
    training_data = None
    new_agent = None
    do_compare_agents = False
    play_vs_heuristics = True

    with open("saved_models/saved_data.csv", "w+") as f:
        f.write(
            f"total loss, value loss, policy loss, Agent wins, Heuristic wins, Ties\n"
        )
        for itr in range(iterations):
            print(f"Starting iteration {itr + 1} / {iterations}")

            # if itr != 0 and itr % lr_decay_period == 0:
            #     lr *= lr_decay
            #     print(f"Decayed lr to {lr}")

            print(f"Generating training data...")

            # sliding window
            if training_data is None:
                training_data = generate_training_data(game, num_games,
                                                       search_steps, agent)
            else:  # we generate more data if our improved agent fails to beat the old one
                print("Generating additional data...")
                more_data = generate_training_data(game, num_games,
                                                   search_steps, agent)
                s = torch.cat((training_data[0], more_data[0]))
                pi = torch.cat((training_data[1], more_data[1]))
                z = torch.cat((training_data[2], more_data[2]))
                training_data = [s, pi, z]

            # Clip the training data to hold last states
            # Acts as a replay buffer with a sliding window
            _s = training_data[0][-replay_buffer_size:, :]
            _pi = training_data[1][-replay_buffer_size:, :]
            _z = training_data[2][-replay_buffer_size:]

            training_data = [_s, _pi, _z]

            #training_data = generate_training_data(game, num_games, search_steps, agent)
            print(
                f"Generated training data: {training_data[0].size(0)} states")

            # if new_agent is None:  # keep the progress on the new model if we failed to beat the old one
            #     new_agent = copy.deepcopy(agent)

            print(f"Improving model...")
            agent.train()
            loss, value_loss, policy_loss = improve_model(agent,
                                                          training_data,
                                                          optimization_steps,
                                                          optimizer,
                                                          verbose=False)
            agent.eval()

            print(
                f"Finished improving model, total loss: {loss}, value loss: {value_loss}, policy loss: {policy_loss}"
            )

            if loss < best_loss:  # model is pretty good. lets stop and check it out
                torch.save(agent.state_dict(),
                           f"saved_models/tictactoe_agent_{loss}.pt")
                best_loss = loss

            # if do_compare_agents:
            #     # 1. We can compare agents and keep the best one, or  2. just keep improving the same network repeatedly
            #     # AlphaGo Zero 1. (the specialized version of the algortihm for Go, a predecessor to the general purpose AlhpaZero)
            #     # AlphaZero uses 2.
            #     print(f"Comparing agents...")
            #     new_wins, old_wins, ties = compare_agents(agent, new_agent, game, num_faceoff_games)
            #     print(f"New wins: {new_wins}, old wins: {old_wins}, ties: {ties}")
            #     if new_wins > old_wins:
            #         agent = new_agent
            #         new_agent = None
            # else:
            #     agent = new_agent

            if play_vs_heuristics:
                agent_wins_first = 0
                agent_wins_second = 0
                heursitic_wins_first = 0
                heursitic_wins_second = 0
                ties = 0
                for i in range(num_faceoff_games // 2):
                    game.reset()
                    r = play_against_heuristics(game, agent, agent_plays=1)
                    if r == 0:
                        ties += 1
                    elif r == 1:
                        agent_wins_first += 1
                    elif r == -1:
                        heursitic_wins_second += 1

                for i in range(num_faceoff_games // 2):
                    game.reset()
                    r = play_against_heuristics(game, agent, agent_plays=2)
                    if r == 0:
                        ties += 1
                    elif r == 1:
                        heursitic_wins_first += 1
                    elif r == -1:
                        agent_wins_second += 1

                print(
                    f"Agent wins: ({agent_wins_first},{agent_wins_second}) , Heuristic wins: ({heursitic_wins_first},{heursitic_wins_second}) , "
                    f"Ties: {ties}")
                f.write(
                    f"{loss}, {value_loss}, {policy_loss}, {agent_wins_first+agent_wins_second}, {heursitic_wins_first+heursitic_wins_second}, {ties}\n"
                )

            # training_data = None # reset training data every time, no replay buffer

        print("Saving agent")
        torch.save(agent.state_dict(), "saved_models/tictactoe_agent.pt")
        print(f"Best loss seen: {best_loss}")

    return agent
Exemplo n.º 21
0
def pipe():
    global players
    global rooms
    if request.environ.get("wsgi.websocket"):
        ws = request.environ["wsgi.websocket"]
        cp = None
        oponent_ws = None
        room_id = None
        mode = None
        ws.send(json.dumps({"cmd": "init"}))
        while True:
            time.sleep(1)
            data = ws.receive()
            if data:
                data = json.loads(data)
                if data["cmd"] == "init":
                    players[data["id"]] = ws
                elif data["cmd"] == "room":
                    if data["mode"] == "bot":
                        mode = "bot"
                        cp = tt.tictactoe()
                        ws.send(json.dumps({"cmd": "start", "turn": 1}))
                    elif data["mode"] == "vs":
                        players[data["id"]] = ws
                        mode = "vs"
                        room_id = str(len(rooms) + 1)
                        rooms[room_id] = {}
                        rooms[room_id]["player1"] = data["id"]
                        response = {"cmd": "new_room", "room": room_id}
                        for elem in players:
                            players[elem].send(json.dumps(response))
                elif data["cmd"] == "join":
                    mode = "vs"
                    room_id = str(data["room"])
                    if len(rooms[room_id]) > 1:
                        response = {"cmd": "error", "msg": "room is full"}
                        ws.send(json.dumps(response))
                    else:
                        rooms[room_id]["player2"] = data["id"]
                        response1 = {"cmd": "start", "turn": 1}
                        response2 = {"cmd": "start", "turn": 2}
                        oponent_ws = players[rooms[room_id]["player1"]]
                        oponent_ws.send(json.dumps(response1))
                        ws.send(json.dumps(response2))
                elif data["cmd"] == "move":
                    move = data["move"]
                    if mode == "bot":
                        cp.mark(1, move[0], move[1])
                        if cp.winner() == tt.NO_WINNER:
                            best_move = cp.get_best_move(2)
                            cp.mark(2, best_move[0], best_move[1])
                            response = {"cmd": "move", "move": best_move}
                            ws.send(json.dumps(response))
                    elif mode == "vs":
                        if not oponent_ws:
                            oponent_ws = players[rooms[room_id]["player2"]]
                        response = {"cmd": "move", "move": move}
                        oponent_ws.send(json.dumps(response))
                elif data["cmd"] == "leave_room":
                    if room_id in rooms.keys():
                        del rooms[room_id]
                        oponent_ws.send(json.dumps({"cmd": "leave_room"}))
                    break
    ws.send(json.dumps({"cmd": "finish"}))
    return ""
Exemplo n.º 22
0
 def resetGame(self):
     self.game = tictactoe.tictactoe()
Exemplo n.º 23
0
from game_check import game_check
from tictactoe import tictactoe

wins_one = 0
wins_two = 0
draws = 0

while True:

    winner = tictactoe()  #play the game
    if winner == 'X':
        wins_one += 1
    elif winner == 'O':
        wins_two += 1
    else:
        draws += 1

    again = input('Would you like to play again?[y/n]\n')

    if again != 'y':
        break

print('\nPlayer 1 won ', wins_one, ' games\nPlayer 2 won ', wins_two,
      ' games\nThere were ', draws, ' draws')
Exemplo n.º 24
0
 def __init__( self ):
   self.games = [ tictactoe() for i in range(9) ]
Exemplo n.º 25
0
 def train_naive_rl(self, nb_game=10):
     # fufill db with states
     for i in range(nb_game):
         self.ttt = tictactoe.tictactoe()
         states, winner = self.ttt.run(random_game=True)
         assert self.rs.insert_new_state(states, winner, self.port) == 0
Exemplo n.º 26
0
            userMove = int(userInput)
            if (userMove < 0 or userMove > 8
                    or not (game.GAMEBOARD[userMove] == ' ')):
                continue
            game.changeGAMEBOARDbyMove(userMove)
            game.turnChange()
        else:
            start = time()
            value, move = gs.negamax(game, -inf, inf, None,
                                     None)  #=gs.pvSplit(game,-inf,inf) #
            #print "grid sent, score ",value
            #print game
            #print "move ",move
            if move == None:
                print "move is None. Stopping"
                break
            game.changeGAMEBOARDbyMove(move)
            print "Player", game.player, "to", move, "for value", value,
            if not debug: print
            print game
            finish = time()
            print "Time ", finish - start
            game.turnChange()


gameObj = tictactoe.tictactoe(debug)
print gameObj.player
#gameObj.turnChange()
#print gameObj.player
playGameNegamax(gameObj)
Exemplo n.º 27
0
 def setUp(self):
     self.ttt = tictactoe.tictactoe()
Exemplo n.º 28
0
class TestCases(tictactoe):
    def __init__(self):
        print '\n\n'
        print 'Initialised Unit Testing for TicTacToe.\n\n'
        print '-' * 50

    def testPassAllValues(self):

        print '1: Testing Register Move for all values.'
        print '-' * 50
        for i in range(0, 3):
            for j in range(0, 3):
                print 'Testing registerMove(' + str(i) + ', ' + str(j) + ')'
                game = tictactoe()
                try:
                    assert game.registerMove(i, j) == True
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '9 Values checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True

    def testRowsWinCases(self):
        print '2: Testing Winning returns for all Row win types.'
        print '-' * 50

        # Making all possible row wins
        for k in range(1, 3):
            for i in range(0, 3):
                game = tictactoe()
                agent = game.agent
                x = []
                for j in range(0, 3):
                    x.append(k)
                agent[i] = x
                game.agent = agent
                try:
                    assert game.gameWin() == False
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '6 Rows checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True

    def testColumnsWinCases(self):
        print '3: Testing Winning returns for all Column win types.'
        print '-' * 50

        # Making all possible column wins
        for k in range(1, 3):
            for i in range(0, 3):
                game = tictactoe()
                agent = game.agent
                x = []
                for j in range(0, 3):
                    agent[j][i] = k
                game.agent = agent
                try:
                    assert game.gameWin() == False
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '6 Columns checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True

    def testDiagonalWinCases(self):
        print '4: Testing Winning returns for all Diagonal win types.'
        print '-' * 50

        # Making all possible Diagonal wins
        for k in range(1, 3):
            game = tictactoe()
            agent = game.agent
            x = []
            for j in range(0, 3):
                agent[j][j] = k
            game.agent = agent
            try:
                assert game.gameWin() == False
            except AssertionError as e:
                print '-' * 50
                print 'Test Cases Failed'
                print '\n\n'
                return e

        for k in range(1, 3):
            game = tictactoe()
            agent = game.agent
            x = []
            for j in range(0, 3):
                agent[j][2 - j] = k
            game.agent = agent
            try:
                assert game.gameWin() == False
            except AssertionError as e:
                print '-' * 50
                print 'Test Cases Failed'
                print '\n\n'
                return e

        print '-' * 50
        print '4 Diagonals checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True

    def testNotWinCases(self):
        print '5. Checking possible cases that are not a Win'
        print '-' * 50

        for i in range(0, 3):
            for j in range(0, 3):
                game = tictactoe()
                game.agent[i][j] = 1
                game.agent[j][i] = 2
                game.displayBoard()
                try:
                    assert game.gameWin() == True
                except AssertionError as e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return e

        print '-' * 50
        print '9 No Win cases checked ...'
        print 'Test Case passed.'
        print '\n\n'
        return True

    def testInvalidMoves(self):
        print '6. Testing Invalid Moves'
        print '-' * 50

        for i in range(-1, 5):
            for j in range(-1, 5):
                if i < 0 or i > 2 or j < 0 or j > 2:
                    game = tictactoe()
                    print i, j
                    try:
                        assert game.registerMove(i, j) == 'Invalid Move'
                    except AssertionError, e:
                        # game.displayBoard()
                        print '-' * 50
                        print 'Test Cases Failed'
                        print '\n\n'
                        return
                        # raise e

        for i in range(0, 3):
            for j in range(0, 3):
                game = tictactoe()
                game.agent[i][j] = random.randint(1, 2)
                try:
                    game.displayBoard()
                    assert game.registerMove(i, j) == 'Invalid Move'
                except AssertionError, e:
                    print '-' * 50
                    print 'Test Cases Failed'
                    print '\n\n'
                    return
Exemplo n.º 29
0
def setup():
    t = tictactoe.tictactoe()
    t.board[:] = '-'
    return t
Exemplo n.º 30
0
        return winner


def save_model(player):
    # saves model with trained weights
    torch.save(player.state_dict(), "ev.pth")


def load_model(player):
    # loads a saved model from a .pth file
    player.load_state_dict(torch.load('ev.pth'))
    return player


if __name__ == "__main__":
    player = Player()
    try:
        player = load_model(player)
        print("Model loaded")
    except:
        print("Model not trained")

    game = tictactoe.tictactoe()
    for i in range(200):
        print(i)
        print(game)
        players = make_children(player, num_children=100)
        player = tournament(players, game)

    save_model(player)
def play_with_agent(agent,
                    verbose=False,
                    agent_plays=1,
                    use_heuristic_agent=False):
    game = tictactoe()
    obs, reward, done = game.reset()
    game.render()
    while not done:
        if agent_plays == 1:

            if use_heuristic_agent:
                agent_action = game.get_computer_move()
            else:
                agent_action = get_agent_action(agent,
                                                game,
                                                obs,
                                                verbose=verbose)

            obs, reward, done = game.step(agent_action)
            game.render()
            # player takes turn
            if not done:

                action = int(input("Input play space (0-8): "))

                while not game.isLegalAction(action):
                    print("Illegal action")
                    action = int(input("Input play space (0-8): "))

                obs, reward, done = game.step(action)
                game.render()

        elif agent_plays == 2:

            action = int(input("Input play space (0-8): "))

            while not game.isLegalAction(action):
                print("Illegal action")
                action = int(input("Input play space (0-8): "))

            obs, reward, done = game.step(action)
            game.render()
            if not done:
                if use_heuristic_agent:
                    agent_action = game.get_computer_move()
                else:
                    agent_action = get_agent_action(agent,
                                                    game,
                                                    obs,
                                                    verbose=verbose)
                obs, reward, done = game.step(agent_action)
                game.render()

        else:
            raise Exception(
                "Invalid value for agent plays parameter. Choose 1 or 2")

    if reward == 0:
        print("Tie game")
    elif (reward == 1 and agent_plays == 1) or (reward == -1
                                                and agent_plays == 2):
        print("Agent won")
    else:
        print("You won!")
Exemplo n.º 32
0
import random
import sys
import time
import rockpaperscissors
import tictactoe
import guess

typing_speed = 350
def slow_type(msg): #maybe include special OWO syntax here -> this has lotsa potential hehe
    for letter in msg:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(random.random()*10.0/typing_speed)
    print('')


slow_type("As you're coding your CSK project, you see a strange popup. Someone programmed an owobot to infiltrate your computer and fill it with OWOs! It's up to you to defeat owobot before it takes over.")
slow_type("OWO TEST 1: THE NOT-SO-STRATEGIC SELECTION OF AN ITEM (aka rock paper scissors).")
rockpaperscissors.rockpaperscissors()
slow_type("OWO TEST 2: THE STRATEGIC SELECTION OF AN ARBITRARY NUMBER (aka guess the number). Guess the number that owobot is thinking of!")
guess.func()
slow_type("OWO TEST 3: THE STRATEGIC PLACING OF CERTAIN ARBITRARY LETTERS. (aka tic-tac-toe).")
tictactoe.tictactoe()
print('OWOBOT CONGRATULATES YOU! YOU HAVE SUCCESSFULLY PROTECTED YOUR COMPUTER')