Ejemplo n.º 1
0
def test_player_move():
    board = g.Board()
    player = g.Player(board, 1)
    board.set_state([1, 0, 1, 0, 2, 0, 0, 0, 0])
    player.set_Q(1, 10)
    player.epsilon = 0
    assert player.move() == 1
Ejemplo n.º 2
0
def test_player_rewards():
    board = g.Board()
    player = g.Player(board, 1)
    assert player.reward(3) == 0
    board.set_state([1, 1, 1, 0, 0, 0, 0, 0, 0])
    assert player.reward(3) == 100
    board.set_state([2, 2, 2, 0, 0, 0, 0, 0, 0])
    assert player.reward(3) == -100
Ejemplo n.º 3
0
def test_board_set_get():
    board = g.Board()
    board.set_state(np.array([1, 2, 0, 0, 0, 1, 2, 1, 2]).reshape((3, 3)))
    assert np.array_equal(
        board.get_state(),
        np.array([1, 2, 0, 0, 0, 1, 2, 1, 2]).reshape((3, 3)))
    empty = board.get_empty()
    assert np.array_equal(empty, np.array([2, 3, 4]))
Ejemplo n.º 4
0
def test_board_available_states():
    board = g.Board()
    board.set_state([2, 1, 0, 1, 2, 1, 2, 0, 2])
    assert np.array_equal(
        board.available_states(1),
        np.array([
            np.array([2, 1, 1, 1, 2, 1, 2, 0, 2]).reshape(3, 3),
            np.array([2, 1, 0, 1, 2, 1, 2, 1, 2]).reshape(3, 3)
        ]))
Ejemplo n.º 5
0
def test_player_move():
    board = g.Board()
    player = g.Player(board, 1)
    player.epsilon = 1
    board.set_state([2, 1, 2, 1, 2, 1, 2, 0, 2])
    assert player.move() == 7
    player = g.Player(board, 2)
    player.epsilon = 1
    board.set_state([2, 1, 2, 1, 2, 1, 2, 0, 2])
    assert player.move() == 7
Ejemplo n.º 6
0
def test_player_bestmove():
    board = g.Board()
    player = g.Player(board, 1)
    board.set_state([2, 1, 2, 1, 2, 1, 2, 0, 2])
    player.set_Q(7, 10)
    assert np.array_equal(player.eval_board(),
                          [0., 0., 0., 0., 0., 0., 0., 10., 0.])
    board.set_state([0, 1, 2, 1, 2, 1, 2, 0, 2])
    player.set_Q(0, 20)
    player.set_Q(7, 10)
    assert np.array_equal(player.eval_board(),
                          [20., 0., 0., 0., 0., 0., 0., 10., 0.])
Ejemplo n.º 7
0
def test_board_cond():
    board = g.Board()

    board.set_state(np.array([1, 1, 1, 0, 0, 0, 0, 0, 0]).reshape((3, 3)))
    cond = board.get_condition()
    assert cond == 1

    board.set_state(np.array([2, 0, 0, 0, 2, 0, 0, 0, 2]).reshape((3, 3)))
    cond = board.get_condition()
    assert cond == 2

    board.set_state(np.array([2, 1, 0, 1, 0, 1, 2, 0, 0]).reshape((3, 3)))
    cond = board.get_condition()
    assert cond == 4

    board.set_state(np.array([1, 2, 1, 2, 2, 1, 1, 1, 2]).reshape((3, 3)))
    cond = board.get_condition()
    assert cond == 3

    board.set_state(np.array([2, 2, 1, 2, 2, 1, 1, 1, 2]).reshape((3, 3)))
    cond = board.get_condition()
    assert cond == 2
Ejemplo n.º 8
0
def test_player_Q():
    board = g.Board()
    player = g.Player(board, 1)
    player.set_Q(1, 56)
    assert player.get_Q(1) == 56
Ejemplo n.º 9
0
def test_board_update():
    board = g.Board()
    board.update(4, 1)
    assert board.get_state()[1, 1] == 1
Ejemplo n.º 10
0
def test_board_empty():
    board = g.Board()
    board.set_state(np.array([0, 0, 0, 0, 0, 0, 0, 0, 0]).reshape((3, 3)))
    empty = board.get_empty()
    assert np.array_equal(empty, np.arange(9))
import deeptactoe
from deeptactoe import game as g
from pprint import pprint as pp
import numpy as np
board = g.Board()
player1 = g.Player(board, 1)
player2 = g.Player(board, 2, beh='human')
player1.epsilon = 1
try:
    player1.load_Q()
except:
    print("Not possible to load Q matrix. Computer will play at random")


def sanitise(board, move):
    if (np.any(np.equal(board.get_empty(), int(move)))):
        return False
    else:
        print("Illegal move!")
        return True


def resolution(board):
    case = board.get_condition()
    if (case == 1):
        print("The Computer wins")
    elif (case == 2):
        print("Human wins")
    else:
        print("Tie")