Exemplo n.º 1
0
def generate_data():

    possibilities = get_possibilities()
    train = open("NeuralNetwork/train.data", "w")
    test = open("NeuralNetwork/test.data", "w")

    train.write(str(len(possibilities) / 2) + " 9 1\n")

    def nnWrite(file, board, sol):
        file.write(board + "\n" + str(sol) + "\n")

    def getBoardAsString(possibilitie):
        res = ""
        for tab in possibilitie[0]:
            for i in tab:
                res = res + str(i) + " "
        return res

    for i in range(len(possibilities)):
        possibilitie = possibilities[i]
        t = Tictactoe(data=possibilitie)
        ia = IA(t, (t.turn % 2) + 1)
        ia.minimax(ia.game, 0)
        res = ia.choice

        if i < len(possibilities) / 2:
            nnWrite(train, getBoardAsString(possibilitie), res)
        else:
            nnWrite(test, getBoardAsString(possibilitie), res)
Exemplo n.º 2
0
    def future_rewards(self, state):
        """
        Calculates the maximum reward of state

        Args:
            state (nested list (3 x 3)): a Tictactoe board

        Returns:
            float: maximum reward
        """
        # Get all the available ations
        actions = Tictactoe.available_actions(state)

        # Return 0 if not actions available
        if len(actions) == 0:
            return 0

        # Choose the max reward according to the Q table
        max_reward = self.get_q_value(state, actions[0])

        for action in actions:
            value = self.get_q_value(state, action)

            if value > max_reward:
                max_reward = value

        return max_reward
def is_win_strategie_ab():
    '''fonction qui retourne vrai si le jeu du morpion possède une stratégie gagnante
        et faux si non'''
    board = Tictactoe.Board()
    result, counter = max_alphabeta(board, -2, 2)
    if result == 1 or result == 0:
        return True, counter
    return False, counter
Exemplo n.º 4
0
def main():
    x = 0
    game = Tictactoe()
    while True:
        for i in [1, 4, 7]:
            print(game.myfunc(i))
        while True:
            choice = input(
                "Player {} Enter your move(1-9):".format(x + 1)).strip()
            try:
                choice = int(choice)
            except:
                print("Invalid Number!")
                choice = -1
            if 1 <= choice <= 9:
                break
            else:
                print('Please enter valid input between 1 - 9')
        if game.board[choice - 1] == ' ':
            if x == 0:
                game.board[choice - 1] = 'X'
                if game.isVictory('X'):
                    print('\n Player {} is the winner!'.format(x + 1))
                    break
                x = 1
            else:
                game.board[choice - 1] = 'Y'
                if game.isVictory('Y'):
                    print('\n Player {} is the winner!'.format(x + 1))
                    break
                x = 0
        else:
            print("\n Space is taken!")
def is_win_strategie():
    '''fonction qui retourne vrai si le jeu du morpion possède une stratégie gagnante
    et faux si non
        Une partie est considérée comme gagnante si le joueur ne perd pas, donc en cas de match
    nul ou de victoire.'''
    board = Tictactoe.Board()
    result, cpt = max_minimax(board)
    if result == 1 or result == 0:
        return True, cpt
    return False, cpt
Exemplo n.º 6
0
class ClientTic:
    def __init__(self):
        self.client = socket(AF_INET, SOCK_STREAM)
        self.host = gethostname()
        self.port = 5000
        self.tic = Tictactoe()
        self.player = None

    def encodeMsg(self, msg):
        return bytes(msg, 'utf-8')

    def theBoard(slef):
        return self.theBoard

    def connect(self):
        try:
            self.client.connect((self.host, self.port))
            self.client.send(self.encodeMsg("Gracias bro"))
            while True:

                buff = self.client.recv(1024)
                res = str(buff)
                if "[Server Msg]" in res:
                    self.handleServerMsg(res)
                elif "[Player Movement]" in res:
                    self.handlePlayerMovement(res)
                elif "[Player Turn]" in res:
                    self.handlePlayerTurns(res)

                sleep(3)
        except error as e:
            print(e)
            exit()
            raise e

    def handleServerMsg(self, res):
        if not self.player:
            if 'X' in res:
                #print("soy x")
                self.player = Player('X')
            elif 'O':
                #print("soy o")
                self.player = Player('O')
        print(res)

    def handlePlayerMovement(self, res):

        move = res.split(']')
        move = move[1].split("'")

        if (self.player.name == "X"):
            self.tic.printBoard(move[0], 'O')
        else:
            self.tic.printBoard(move[0], 'X')

    def handlePlayerTurns(self, res):
        move = self.player.doPlay(self.tic.theBoard.keys())
        system('cls')
        self.tic.printBoard(move, self.player.name)
        self.client.send(self.encodeMsg(move))
Exemplo n.º 7
0
def get_possibilities():

    t = Tictactoe()
    res = []

    def convertPos(x):
        if x < 3:
            return [0, x]
        else:
            return [(x // 3), (x % 3)]

    def possibilities(game):
        if game.testWin() == 0:
            for move in game.getMoves():
                possibilities(game.getNewState(convertPos(move)))
            res.append(game.export())
        return

    possibilities(t)
    return res
Exemplo n.º 8
0
    def best_action(self, state, epsilon_true=False):
        """
        Chooses the best action according to the state

        Args:
            state (nested list (3 x 3)): a Tictactoe board

        Raises:
            Exception: If no action is available

        Returns:
            tuple (i, j): best action
        """
        # Get all the available ations
        actions = Tictactoe.available_actions(state)

        # Raise if no action available
        if len(actions) == 0:
            raise Exception('No action available')

        # Choose the best action according to its Q value
        if (not epsilon_true) or (epsilon_true and random.random() <
                                  (1 - self.epsilon)):

            best_action = actions[0]
            max_reward = self.get_q_value(state, best_action)

            for action in actions:
                value = self.get_q_value(state, action)

                if value > max_reward or (value == max_reward
                                          and random.random() < 0.5):
                    best_action = action
                    max_reward = value

        # Randomly choose the best action
        else:
            best_action = random.choice(actions)

        return best_action
Exemplo n.º 9
0
 def setUp(self):
     self.board = Tictactoe.Board()
Exemplo n.º 10
0
 def __init__(self):
     self.client = socket(AF_INET, SOCK_STREAM)
     self.host = gethostname()
     self.port = 5000
     self.tic = Tictactoe()
     self.player = None
Exemplo n.º 11
0
def play(args):
    """
    Arguments:
    ----------
    - `args' : List of arguments

    Returns, from a new game parametrized with `args` :
        - Number of moves for each player
        - Total execution time for each player
        - The final state of the game
    """
    if len(args) < 6:
        help()
        return -1

    try:
        n = int(args[1])
    except BaseException:
        help()
        return -1
    try:
        m = int(args[2])
    except BaseException:
        help()
        return -1

    try:
        k = int(args[3])
    except BaseException:
        help()
        return -1

    try:
        p1 = args[4]
        if p1[0] != "h":
            name = p1.split("_")[1]
            p1 = extractAgent(name, 1, k, TIMEOUT)
        else:
            p1 = Human(1, k)
    except BaseException:
        help()
        return -1

    try:
        p2 = args[5]
        if p2[0] != "h":
            name = p2.split("_")[1]
            p2 = extractAgent(name, 2, k, TIMEOUT)
        else:
            p2 = Human(2, k)
    except BaseException:
        help()
        return -1
    env = Tictactoe(n, m, k)
    p = [p1, p2]
    t = [0, 0]
    nact = [0, 0]
    while not env.terminalState():
        _, currentPlayer, _, _ = env.currentState()
        tX, act = f_with_timeout(p[currentPlayer - 1].move, deepcopy(env))
        if act is not None:
            i, j = act
            env.step((currentPlayer, i, j))
        else:
            lst = np.argwhere(env.M == 0)
            move = tuple(lst[np.random.randint(lst.shape[0])])
            env.step((currentPlayer, move[0], move[1]))
        t[currentPlayer - 1] += tX
        nact[currentPlayer - 1] += 1
    return (nact, t, env)
Exemplo n.º 12
0
            if event.type == pygame.MOUSEBUTTONDOWN:
                point = event.pos
                b_ = False

                # Play as X or O
                if playXButton.collidepoint(point):
                    ai_turn = False
                    b_ = True
                elif playOButton.collidepoint(point):
                    ai_turn = True
                    b_ = True

                # Start Play state
                if b_:
                    gamestate = PLAY_STATE
                    ttt = Tictactoe()
                    time.sleep(0.1)
        # Play state
        elif gamestate == PLAY_STATE:

            # Mouse click
            if event.type == pygame.MOUSEBUTTONDOWN:
                point = event.pos

                # If user's trun
                if not ai_turn:

                    # If game is not over
                    if not ttt.gameover():
                        # Cells
                        b_ = True
Exemplo n.º 13
0
 def test_win_colum3(self):
     o = 1
     x = 2
     grid = [[x, 0, o], [o, x, o], [x, o, o]]
     self.assertEqual("O is Winner!", Tictactoe().getStatus(grid))
Exemplo n.º 14
0
 def test_stopped(self):
     o = 1
     x = 2
     grid = [[x, o, o], [o, x, x], [x, x, o]]
     self.assertEqual("Always", Tictactoe().getStatus(grid))
Exemplo n.º 15
0
 def test_win_colum2(self):
     o = 1
     x = 2
     grid = [[o, x, o], [0, x, 0], [o, x, o]]
     self.assertEqual("X is Winner!", Tictactoe().getStatus(grid))
Exemplo n.º 16
0
 def test_playing(self):
     o = 1
     x = 2
     grid = [[0, 0, 0], [0, 0, 0], [x, 0, 0]]
     self.assertEqual("Runing", Tictactoe().getStatus(grid))
Exemplo n.º 17
0
 def test_win_row3(self):
     o = 1
     x = 2
     grid = [[o, 0, o], [o, 0, 0], [x, x, x]]
     self.assertEqual("X is Winner!", Tictactoe().getStatus(grid))
Exemplo n.º 18
0
 def test_win_slash(self):
     o = 1
     x = 2
     grid = [[o, 0, x], [o, x, 0], [x, o, o]]
     self.assertEqual("X is Winner!", Tictactoe().getStatus(grid))
Exemplo n.º 19
0
 def test_win_row1(self):
     o = 1
     x = 2
     grid = [[o, o, o], [x, 0, x], [x, 0, 0]]
     self.assertEqual("O is Winner!", Tictactoe().getStatus(grid))
Exemplo n.º 20
0
    def train(self, num_games):
        """
        Trains the agent by playing games against itself

        Args:
            num_games (int): number of games to train
        """

        # Play num_games games
        for n in range(num_games):

            # Print game number
            if n % 1000 == 0:
                print(f'Game #{n + 1}')

            # Initialize the game
            ttt = Tictactoe()

            # Keep track of last state and actions
            last = {
                'X': {
                    'state': None,
                    'action': None
                },
                'O': {
                    'state': None,
                    'action': None
                }
            }

            # Play the game
            while True:

                # Get the state and action
                state = ttt.get_board()
                action = self.best_action(state, epsilon_true=True)

                # Save as lasts
                last[ttt.get_player()]['state'] = state
                last[ttt.get_player()]['action'] = action

                # Apply action and get the new state
                ttt.action(action)
                new_state = ttt.get_board()

                # Game over
                if ttt.gameover():

                    # Won the game
                    if ttt.get_winner() is not None:

                        # Update q value for winner
                        self.update_q_value(state, action, new_state, 1)

                        # Update q value for loser
                        self.update_q_value(last[ttt.get_player()]['state'],
                                            last[ttt.get_player()]['action'],
                                            new_state, -1)

                    # Draw
                    else:
                        # Update q value
                        self.update_q_value(state, action, new_state, 0)

                    break

                # Game continues
                elif last[ttt.get_player()]['state'] is not None:

                    # Update last action
                    self.update_q_value(last[ttt.get_player()]['state'],
                                        last[ttt.get_player()]['action'],
                                        new_state, 0)

        print('Training done')
Exemplo n.º 21
0
class RoomTic:
    def __init__(self, n_rom):
        self.tic = Tictactoe()
        self.nrom = n_rom
        self.player_1 = None
        self.player_2 = None
        self.turn = 'X'

    '''
		Esta funcion comprueba si la sala esta llena
	'''

    def isFull(self):
        if self.player_1 and self.player_2:
            return True

        return False
        '''
			Esta funcion agrega un jugador a la sala
		'''

    def addPlayer(self, s):
        if self.isFull():
            return False

        if not self.player_1:
            self.player_1 = s
            s.send("[Server Msg] Seras jugador X".encode('utf-8'))
            return self.player_1
        else:
            self.player_2 = s
            s.send("[Server Msg] Seras el jugador O".encode('utf-8'))
            return self.player_2

    def gameLoop(self):

        hasWinner = False
        nturns = 0
        while hasWinner or nturns < 9:

            if self.turn == 'X':
                hasWinner = self.playTurn(self.player_1, self.player_2, 'X')
            else:
                hasWinner = self.playTurn(self.player_2, self.player_1, 'O')

            if hasWinner:
                break

        if nturns == 8:

            player_1.send(
                "[Server Msg] Empate guachin los dos son recontra loosers jajaja giles"
                .encode('utf-8'))
            player_2.send(
                "[Server Msg] Empate guachin los dos son recontra loosers jajaja giles"
                .encode('utf-8'))

    def playTurn(self, turn, opponent, player):

        if self.tic.comprobar(player):
            turn.send("[Server Msg] Has ganado Felicidades".encode('utf-8'))
            opponent.send(
                "[Server Msg] Perdiste Caramono jajaj gil ".encode('utf-8'))

            return True
        else:
            turn.send("[Player Turn]".encode('utf-8'))
            opponent.send(
                "[Server Msg] Waiting for the openents movement....".encode(
                    'utf-8'))
            buff = turn.recv(1024).decode('ascii')
            self.tic.theBoard[buff] = player
            msg = "[Player Movement]" + buff
            opponent.send(msg.encode("utf-8"))
            print("jugada del player 1", buff)

            if player == "X":
                self.turn = "O"
            else:
                self.turn = "X"
        return False

    def encodeMsg(self, msg):
        return msg.encode('utf-8')
Exemplo n.º 22
0
 def __init__(self, n_rom):
     self.tic = Tictactoe()
     self.nrom = n_rom
     self.player_1 = None
     self.player_2 = None
     self.turn = 'X'
Exemplo n.º 23
0
from tictactoe import Tictactoe
board = Tictactoe()
print("\t\tGIVE THE FOLLOWING DETAILS\n")
count = 0
player1 = (raw_input("\t\tFIRST PLAYER   :  "))
player2 = (raw_input("\t\tSECOND PLAYER  :  "))
while (1):
    if (board.isfull()):
        print "\t\tNO MORE MOVE ALLOWED SPACE IS FULL"
        print "\n\t\t\t\tMATCH DRAWN !!!!!"
        break
    count = count % 2
    print "\n\n"
    if (count == 0):
        print("\t\t %s YOUR TURN") % player1
    if (count == 1):
        print("\t\t %s YOUR TURN") % player2
    row = int(raw_input("\t\t ROW     :  "))
    column = int(raw_input("\t\t COLUMN  :  "))
    board.move(row, column, count)
    board.display()
    a = board.result()
    if (a == 1):
        print("\t\t\t\tWINNER   %s  !!!!!!!!!") % player1
        break
    if (a == 2):
        print("\t\t\t\tWINNER   %s  !!!!!!!!!") % player2
        break
    if (a == 0):
        count += 1
        continue