Exemplo n.º 1
0
    def testEmptyBoard(self):
        expected = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

        state = game.State(logId=0)
        ttt = game.TTT()

        if (state.board == expected):
            return 0

        return 1
Exemplo n.º 2
0
    def testBotMarkAny(self):
        state = game.State(logId=0)
        ttt = game.TTT()

        res = ttt.botMark(state)

        if (res > 0) and res < 10:
            return 0

        return 1
Exemplo n.º 3
0
    def testBotMarkLastSpot(self):
        state = game.State(logId=0, board=[X, X, X, X, X, X, X, X, " "])
        ttt = game.TTT()

        res = ttt.botMark(state)

        if (res == 9):
            return 0

        return 1
Exemplo n.º 4
0
    def testMarkBoard(self):
        expected = [X, " ", " ", " ", " ", " ", " ", " ", " "]

        state = game.State(logId=0)
        ttt = game.TTT()

        ttt.markBoard(1, X, state)

        if (state.board == expected):
            return 0

        return 1
Exemplo n.º 5
0
    def testCheckGameNotFinished(self):
        expected = 1

        state = game.State()
        ttt = game.TTT()

        state.gamestate = ttt.checkGame(state)

        if (state.gamestate == expected):
            return 0

        return 1
Exemplo n.º 6
0
    def testMarkSameSpot(self):
        expected = [O, " ", " ", " ", " ", " ", " ", " ", " "]

        state = game.State()
        ttt = game.TTT()

        ttt.markBoard(1, X, state)
        ttt.markBoard(1, O, state)

        if (state.board == expected):
            return 0

        return 1
Exemplo n.º 7
0
    def testValidateMarkSameSpot(self):
        expected = [X, " ", " ", " ", " ", " ", " ", " ", " "]

        state = game.State(logId=0)
        ttt = game.TTT()

        ttt.markBoard(1, X, state)
        if (ttt.validateMark(1, state)):
            ttt.markBoard(1, O, state)

        if (state.board == expected):
            return 0

        return 1
Exemplo n.º 8
0
    def testBotMarkAll(self):
        expected = [O, O, O, O, O, O, O, O, O]
        board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

        state = game.State(logId=0, board=board)
        ttt = game.TTT()

        i = 0
        while i < 9:
            res = ttt.botMark(state)
            state.board[res - 1] = O
            i += 1

        if (state.board == expected):
            return 0

        return 1
Exemplo n.º 9
0
    def testCheckGameXWin(self):
        expected = 2

        #  X | X | X
        # ---+---+---
        #    |   |
        # ---+---+---
        #    |   |
        state = game.State(turn=0,
                           board=[X, X, X, " ", " ", " ", " ", " ", " "])
        ttt = game.TTT()

        state.gamestate = ttt.checkGame(state)

        if (state.gamestate == expected):
            return 0

        return 1
Exemplo n.º 10
0
    def testCheckGameDraw(self):
        expected = 4

        # Turns + gametate
        state1 = game.State(turn=10, gamestate=1)
        # Board is filled. Draw match:
        #  X | X | O
        # ---+---+---
        #  O | O | X
        # ---+---+---
        #  X | O | X
        state2 = game.State(board=[X, X, O, O, O, X, X, O, X])
        ttt = game.TTT()

        state1.gamestate = ttt.checkGame(state1)
        state2.gamestate = ttt.checkGame(state2)

        if (state1.gamestate == expected and state2.gamestate == expected):
            return 0

        return 1
Exemplo n.º 11
0
    def testCheckGameOWin(self):
        expected = 3

        #  O | O | O
        # ---+---+---
        #    |   |
        # ---+---+---
        #    |   |
        state = game.State(turn=1,
                           board=[O, O, O, " ", " ", " ", " ", " ", " "])

        # This would also work, and since checkGame checks on listindex and turn, not symbol, anything except a single space (" ") would work
        # state = game.State(turn = 1, board = [X, X, X, " ", " ", " ", " ", " ", " "])

        ttt = game.TTT()

        state.gamestate = ttt.checkGame(state)

        if (state.gamestate == expected):
            return 0

        return 1
Exemplo n.º 12
0
from __future__ import print_function

import keras, game
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

ttt = game.TTT()

model = Sequential()
model.add(Dense(81, activation='relu', input_shape=(27, )))
model.add(Dense(81, activation='relu'))
model.add(Dense(27, activation='relu'))
model.add(Dense(27, activation='relu'))
model.add(Dense(9, activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='RMSprop')
model.load_weights('weights_200000.h5')

numpad_to_grid = [0, 6, 7, 8, 3, 4, 5, 0, 1, 2]
num_games = input('how many games do you want to play?')

for episode in range(num_games):
    player = input('which player do you want to play? (1 or -1)')
    ttt.initialize_game()
    game_over = False
    turn = player
    while not game_over:
        if turn == 1:
            print(np.reshape(ttt.board, (3, 3)))
            move = numpad_to_grid[input('make your move.')]
Exemplo n.º 13
0

'''
def custom_loss3(y_true, y_pred):
	return np.sum((y_true+1)*(y_true-2)/(-2) * (y_true - y_pred)**2, axis = 1)
'''
model = Sequential()
model.add(Dense(81, activation='relu', input_shape=(27, )))
model.add(Dense(81, activation='relu'))
model.add(Dense(27, activation='relu'))
model.add(Dense(27, activation='relu'))
model.add(Dense(9, activation='softmax'))
model.compile(loss=custom_loss2, optimizer='RMSprop')
model.load_weights('weights_1000000.h5')

ttt = game.TTT(EPISODES)

board_batch = []
label_batch = []

file = open('game_lengths.txt', 'w')

for episode in range(EPISODES):
    board_data = np.empty((9, 27))
    move_data = [None] * 9
    ttt.initialize_game()
    game_over = False

    # play a game
    while not game_over:
Exemplo n.º 14
0
    def before(logId):
        print("Preparing unit test...")

        state = game.State(logId=0)
        ttt = game.TTT()