예제 #1
0
def play():
    print('Welcome to Tic Tac Toe!\n')

    sample_board = Board(Board.sample)
    print(f'{sample_board}\n\n')

    while True:
        board = Board()
        players = create_players()

        print()
        for player in players:
            print(f'{player} go with {player.marker}')
        print()

        game_on = True
        while game_on:
            for player in players:
                player.move(board)
                print(f'\n{board}\n')

                if board.win_check(player.marker):
                    game_on = False
                    print(f'{player} won')
                    break

                if board.full_check():
                    game_on = False
                    print('The board is full')
                    break

        print()
        if not replay():
            break
예제 #2
0
 def __init__(self,
              players: List[Player],
              boardsize: int = 4,
              learning_model_step: int = 1,
              update_target_step: int = 1):
     self.boardsize = boardsize
     self.board = Board(self.boardsize)
     self.numBoxes = 0
     self.players = players
예제 #3
0
class Game:
    def __init__(self, players: list, boardsize: int = 4):
        self.boardsize = boardsize
        self.board = Board(boardsize)
        self.numBoxes = 0
        self.players = players

    def is_valid(self, idx: int) -> bool:
        return not self.board.vectorBoard[
            idx]  # (1==True gives False, 0 == False gives True)

    def play(self):

        currentPlayer = self.players[0]
        otherPlayer = self.players[1]
        turn = 0
        PlayerTurn = 0
        N = self.board.size
        newNumBoxes = 0
        invalids = 0

        # self.board.print_board()

        while turn < (2 * N + 2) * N:

            move = currentPlayer.get_move(self.board.vectorBoard)

            while not self.is_valid(move):
                currentPlayer.invalidMove()
                invalids += 1
                move = currentPlayer.get_move(self.board.vectorBoard)
                # print("Invalid Move")

            self.board.set_board(move)
            turn += 1

            newNumBoxes = self.board.count_boxes()
            # print(newNumBoxes)

            if newNumBoxes - self.numBoxes == 0:
                PlayerTurn += 1
                currentPlayer = self.players[PlayerTurn % 2]
                otherPlayer = self.players[(PlayerTurn + 1) % 2]
            else:
                currentPlayer.scored(newNumBoxes - self.numBoxes)
                self.numBoxes = newNumBoxes

            #self.board.print_board()
        return invalids  # print("Players score: " + str([p.score for p in self.players]))

    def reset(self):
        self.board = Board(self.boardsize)
        self.numBoxes = 0
        for player in self.players:
            player.score = 0
예제 #4
0
 def reset(self):
     self.board = Board(self.boardsize)
     self.numBoxes = 0
     for player in self.players:
         player.score = 0
예제 #5
0
class GameTraining:
    boardsize: int
    board: Board
    numBoxes: int
    players: List[Player]
    learning_model_step: int
    update_target_step: int

    def __init__(self,
                 players: List[Player],
                 boardsize: int = 4,
                 learning_model_step: int = 1,
                 update_target_step: int = 1):
        self.boardsize = boardsize
        self.board = Board(self.boardsize)
        self.numBoxes = 0
        self.players = players

    def is_valid(self, idx: int) -> bool:
        return not self.board.vectorBoard[
            idx]  # (1==True gives False, 0 == False gives True)

    def play(self, train: bool):

        currentPlayer = self.players[0]
        currentPlayer.state = self.board.vectorBoard.copy()
        otherPlayer = self.players[1]
        turn = 0
        PlayerTurn = 0
        N = self.board.size
        while turn < (2 * N + 2) * N:

            action = currentPlayer.get_move(self.board.vectorBoard)

            if not self.is_valid(action):
                currentPlayer.invalidMove()
                return 1

            self.board.set_board(action)

            newNumBoxes = self.board.count_boxes()

            if newNumBoxes - self.numBoxes == 0:
                if turn > 0:
                    currentPlayer.no_score_move()
                    otherPlayer.add_record(self.board.vectorBoard, False)
                    if train:
                        otherPlayer.train_model_network()

                PlayerTurn += 1
                currentPlayer = self.players[PlayerTurn % 2]
                otherPlayer = self.players[(PlayerTurn + 1) % 2]
            else:
                currentPlayer.scored(newNumBoxes - self.numBoxes)
                self.numBoxes = newNumBoxes
                otherPlayer.opponentScored(newNumBoxes - self.numBoxes)

            turn += 1

        currentPlayer.endGameReward(currentPlayer.score > otherPlayer.score)
        currentPlayer.add_record(self.board.vectorBoard, True)
        otherPlayer.endGameReward(otherPlayer.score > currentPlayer.score)
        otherPlayer.add_record(self.board.vectorBoard, True)
        if train:
            currentPlayer.train_model_network()
        if train:
            otherPlayer.train_model_network()
        return 0

    def reset(self):
        self.board = Board(self.boardsize)
        self.numBoxes = 0
        for player in self.players:
            player.score = 0
예제 #6
0
 def __init__(self, players: list, boardsize: int = 4):
     self.boardsize = boardsize
     self.board = Board(boardsize)
     self.numBoxes = 0
     self.players = players
예제 #7
0
파일: main.py 프로젝트: prathamdesai13/UTTT
    def __init__(self):

        self.Board = Board()
        self.game_state = True
예제 #8
0
파일: main.py 프로젝트: prathamdesai13/UTTT
class UltimateTicTacToe:
    def __init__(self):

        self.Board = Board()
        self.game_state = True

    def reset_board(self):
        self.Board = Board()
        self.game_state = True

    def global_input(self):

        print("Player {} please enter global coordinates: ".format(
            1 if self.Board.player else 2))
        gx = int(input())
        gy = int(input())

        return gx, gy

    def pvp(self):
        # assuming every move made is legal (really this is just meant for the ai)
        gx, gy = self.global_input()

        self.Board.set_global_coord(gx, gy)

        while self.game_state:

            self.Board.draw_board()
            while (self.Board.gx == -1 and self.Board.gy == -1):

                # ask to enter global coords again and check if they are valid
                gx, gy = self.global_input()

                self.Board.set_global_coord(gx, gy)

            print("Player {}, enter local coordinates for global spot {}, {}".
                  format(1 if self.Board.player else 0, self.Board.gx,
                         self.Board.gy))

            spot = self.Board.board[self.Board.gx][self.Board.gy]

            x = int(input())
            y = int(input())

            spot.make_move(x, y, 'X' if self.Board.player else 'O')

            self.Board.resolve_ttt_boards()
            self.Board.set_global_coord(x, y)

            state = self.Board.get_board_state()

            if state == 1 or state == -1:

                self.game_state = False

                print("Game is over!")

                if state == 1:
                    print("Player {} wins!".format(
                        1 if self.Board.player else 2))

                else:
                    print("Draw!")

            self.Board.change_player()

        self.Board.draw_board()

    def pvc(self, agent):

        gx, gy = self.global_input()
        self.Board.set_global_coord(gx, gy)

        while self.game_state:

            self.Board.draw_board()
            while (self.Board.gx == -1 and self.Board.gy == -1):
                if self.Board.player:  # human player
                    gx, gy = self.global_input()
                else:  # not human player
                    gx, gy = agent.make_global_move(self.Board)

                self.Board.set_global_coord(gx, gy)
                print("Global Move perfomed by Player {} is : {}, {}".format(
                    1 if self.Board.player else 0, self.Board.gx,
                    self.Board.gy))
            print("Player {}, enter local coordinates for global spot {}, {}".
                  format(1 if self.Board.player else 0, self.Board.gx,
                         self.Board.gy))
            spot = self.Board.board[self.Board.gx][self.Board.gy]

            if self.Board.player:  # if human player
                x = int(input())
                y = int(input())

            else:
                x, y = agent.make_local_move(self.Board)

            spot.make_move(x, y, 'X' if self.Board.player else 'O')
            print()
            print("Local Move perfomed by Player {} is : {}, {}".format(
                1 if self.Board.player else 0, x, y))
            self.Board.resolve_ttt_boards()
            self.Board.set_global_coord(x, y)

            state = self.Board.get_board_state()

            if state == 1 or state == -1:
                self.game_state = False
                print("Game is over!")

                if state == 1:
                    print("Player {} wins!".format(
                        1 if self.Board.player else 2))

                else:
                    print("Draw!")
            self.Board.change_player()

        self.Board.draw_board()

    def train(self, agent1, agent2, num_episodes=10):
        episode = 0
        episodes_won = 0
        while episode < num_episodes:
            gx, gy = agent1.make_global_move(self.Board)
            self.Board.set_global_coord(gx, gy)

            while self.game_state:

                self.Board.draw_board()
                while (self.Board.gx == -1 and self.Board.gy == -1):
                    if self.Board.player:  # agent 1
                        gx, gy = agent1.make_global_move(self.Board)
                    else:  # agent2
                        gx, gy = agent2.make_global_move(self.Board)

                    self.Board.set_global_coord(gx, gy)
                    print(
                        "Global Move perfomed by Agent {} is : {}, {}".format(
                            1 if self.Board.player else 2, self.Board.gx,
                            self.Board.gy))
                print(
                    "Agent {}, enter local coordinates for global spot {}, {}".
                    format(1 if self.Board.player else 2, self.Board.gx,
                           self.Board.gy))
                spot = self.Board.board[self.Board.gx][self.Board.gy]

                if self.Board.player:  # if human player
                    x, y = agent1.make_local_move(self.Board)
                else:
                    x, y = agent2.make_local_move(self.Board)

                spot.make_move(x, y, 'X' if self.Board.player else 'O')
                print()
                print("Local Move perfomed by Agent {} is : {}, {}".format(
                    1 if self.Board.player else 2, x, y))
                self.Board.resolve_ttt_boards()
                self.Board.set_global_coord(x, y)

                state = self.Board.get_board_state()

                if state == 1 or state == -1:
                    self.game_state = False
                    print("Game {} is over!".format(episode))
                    episode += 1
                    if state == 1:
                        print("Agent {} wins!".format(
                            1 if self.Board.player else 2))
                        if not self.Board.player:
                            # add rewards and gradient defs here for nancy
                            episodes_won += 1
                            pass
                        else:
                            #idk
                            pass
                    else:
                        print("Draw!")
                self.Board.change_player()
            self.reset_board()
        print(episodes_won)
        self.Board.draw_board()
예제 #9
0
파일: main.py 프로젝트: prathamdesai13/UTTT
 def reset_board(self):
     self.Board = Board()
     self.game_state = True