Exemple #1
0
    def __init__(self, training_mode, restore_net):
        # the dice
        self.dice = Dice()
        # internal board, which is the state before the current move
        self.board = Board()
        
        # the neural network used by the players, both players share the same net
        if not restore_net:
            self.neural_network = NeuralNetwork(input_size=198, hidden_size=40, \
                                                                output_size=2)
        elif restore_net:
            self.neural_network = NeuralNetwork(restore_from_file=True)
        
        # list of players
        if training_mode:
            self.players = [Player('white', self.neural_network, learning_mode=True), \
                            Player('black', self.neural_network, learning_mode=True)]
        # let white play against RandomPlayer black for evaluating performance
        elif not training_mode:
            self.players = [Player('white', self.neural_network, learning_mode=False), \
                            RandomPlayer('black')]
        
        # the current player of this instance
        self.current_player = None
        # winner of this game
        self.winner = None

        self.reset()
Exemple #2
0
def main(parent=None):
    root = Tk()
    root.geometry("1366x700")
    root.configure(bg="white")

    dice = Dice()
    game_divider = Divider(
        root)  # will occupy left of main window, contains monopoly board
    interface_divider = Divider(
        root,
        dice)  # will occupy right of main window, contains controls and info

    game_divider.pack(side=LEFT)
    interface_divider.pack(side=BOTTOM)

    board_dim = 700  #this is the side dimension for the monopoly board e.g. 600 x 600 square
    img_path = "centre_img.png"

    #initialise board values
    #p_colours = ["magenta", "cyan", "black", "green"]
    pawns = {}

    #for i in range(0, 4):
    #    pawns.append(Pawn(i, 0, p_colours[i]))

    tiles = []
    tiles.append(Tile(0, 'go', 'Go'))
    tiles.append(Tile(1, 'property', 'Nubar', 'brown', '60'))
    tiles.append(Tile(2, 'community_chest', 'C. Chest'))
    tiles.append(Tile(3, 'property', 'Larkfield', 'brown', '60'))
    tiles.append(Tile(4, 'tax', 'Inc. Tax', None, '200'))
    tiles.append(Tile(5, 'transport', 'Helix Bus Stop', None, '200'))
    tiles.append(Tile(6, 'property', 'Business School', 'cyan', '100'))
    tiles.append(Tile(7, 'chance', 'Chance'))
    tiles.append(Tile(8, 'property', 'Henry Grattan', 'cyan', '100'))
    tiles.append(Tile(9, 'property', 'T. Larkin Theatre', 'cyan', '100'))
    tiles.append(Tile(10, 'jail', 'Jail'))
    tiles.append(Tile(11, 'property', 'DCU Canteen', 'magenta', '140'))
    tiles.append(Tile(12, 'transport', 'DCU Library', None, '150'))
    tiles.append(Tile(13, 'property', 'Interfaith', 'magenta', '140'))
    tiles.append(Tile(14, 'property', 'Albert College', 'magenta', '160'))
    tiles.append(Tile(15, 'transport', 'St Pats Bus Stop', None, '200'))
    tiles.append(Tile(16, 'property', 'Nursing', 'orange', '180'))
    tiles.append(Tile(17, 'community_chest', 'C. Chest'))
    tiles.append(Tile(18, 'property', 'Lonsdale', 'orange', '180'))
    tiles.append(Tile(19, 'property', 'Estates Office', 'orange', '200'))
    tiles.append(Tile(20, 'free_parking', 'Free Parking'))
    tiles.append(Tile(21, 'property', 'Londis', 'red', '220'))
    tiles.append(Tile(22, 'chance', 'Chance'))
    tiles.append(Tile(23, 'property', 'The U', 'red', '220'))
    tiles.append(Tile(24, 'property', 'Hampstead', 'red', '240'))
    tiles.append(Tile(25, 'transport', 'Ballymun Bus Stop', None, '200'))
    tiles.append(Tile(26, 'property', 'Purcell House', 'yellow', '260'))
    tiles.append(Tile(27, 'transport', 'DCU Sport', None, '150'))
    tiles.append(Tile(28, 'property', 'All Hallows', 'yellow', '260'))
    tiles.append(Tile(29, 'property', 'St Pats', 'yellow', '280'))
    tiles.append(Tile(30, 'goto_jail', 'Go To Jail!'))
    tiles.append(Tile(31, 'property', 'Stokes', 'green', '300'))
    tiles.append(Tile(32, 'property', 'Campus Store', 'green', '300'))
    tiles.append(Tile(33, 'community_chest', 'C. Chest'))
    tiles.append(Tile(34, 'property', 'The Helix', 'green', '320'))
    tiles.append(Tile(35, 'transport', 'Collins Av Bus Stop', None, '200'))
    tiles.append(Tile(36, 'chance', 'Chance'))
    tiles.append(Tile(37, 'property', 'McNulty Build.', 'blue', '350'))
    tiles.append(Tile(38, 'tax', 'Super Tax', None, '100'))
    tiles.append(Tile(39, 'property', 'College Park', 'blue', '400'))

    board_frame = BoardDisplay(game_divider, board_dim, board_dim, img_path,
                               tiles, pawns)

    #interface_divider = parent, board_frame = internal reference of board for using commands on
    controls_frame = Controls(interface_divider, board_frame, parent)
    parent.ui_controls = controls_frame
    parent.ui_root = root
    information_frame = Information(interface_divider)

    parent.ui_ready = True
    root.mainloop()
Exemple #3
0
class Backgammon(object):
    """ This class wraps all of the backgammon functionality. Basically,
        the use model for this class is to build a new Backgammon with
        two players, execute backgammon.run(), which runs the game, and
        the call backgammon.reset(), backgammon.run() if you
        want to play again. """
    def __init__(self, training_mode, restore_net):
        # the dice
        self.dice = Dice()
        # internal board, which is the state before the current move
        self.board = Board()
        
        # the neural network used by the players, both players share the same net
        if not restore_net:
            self.neural_network = NeuralNetwork(input_size=198, hidden_size=40, \
                                                                output_size=2)
        elif restore_net:
            self.neural_network = NeuralNetwork(restore_from_file=True)
        
        # list of players
        if training_mode:
            self.players = [Player('white', self.neural_network, learning_mode=True), \
                            Player('black', self.neural_network, learning_mode=True)]
        # let white play against RandomPlayer black for evaluating performance
        elif not training_mode:
            self.players = [Player('white', self.neural_network, learning_mode=False), \
                            RandomPlayer('black')]
        
        # the current player of this instance
        self.current_player = None
        # winner of this game
        self.winner = None

        self.reset()
    
    def save_network(self):
        """ Saves the Neural Network of this Backgammon instance to a file. """
        self.neural_network.save_network()
    
    def reset(self):
        """ Resets this backgammon instance to the initial state, with
            a new board and determines starting player. """
        self.board.reset_board()
        self.dice.roll()
        
        # decide which player starts the game by rolling dice
        # die1 > die2 player 0 starts and vice versa
        # if die1==die2, roll until different
        if self.dice.get_die1() != self.dice.get_die2():
            # determine starting player:
            # die1 rolls for white and die2 rolls for black
            self.current_player = (0 if self.dice.get_die1() >
                                    self.dice.get_die2() else 1)
        # make sure that dice dont show same number
        elif self.dice.get_die1() == self.dice.get_die2():
            same = True
            # roll until different
            while same:
                self.dice.roll()
                if self.dice.get_die1() != self.dice.get_die2():
                    self.current_player = (0 if self.dice.get_die1() >
                                            self.dice.get_die2() else 1)
                    same = False
        
        # if black starts game, reverse players list
        # because white is first in the initial list
        if self.current_player == 1:
            self.players = list(reversed(self.players))
                            
    def run(self):
        """ Runs a game of backgammon, and does not return until the game
            is over. Returns the player who won the game. """
        while not self.board.is_gameover():
            # request players to choose a board
            self.get_move(self.players[0])
            if self.board.is_gameover():
                break

            self.get_move(self.players[1])
            if self.board.is_gameover():
                break

        # check whether a player has all checkers beared off
        # and return it as winner. 
        if self.board.get_off(Board.WHITE) == 15:
            for player in self.players:
                if player.color == Board.WHITE:
                    player.won(self.board)
                    self.winner = player
                else:
                    player.lost(self.board)
        
        elif self.board.get_off(Board.BLACK) == 15:
            for player in self.players:
                if player.color == Board.BLACK:
                    player.won(self.board)
                    self.winner = player
                else:
                    player.lost(self.board)
        
        return self.winner
        
    def get_move(self, player):
        """ Receives a board from the player and applies the move. """
        #print player.color
        new_board = player.choose_move(self)
        self.apply_move(new_board)

    def apply_move(self, new_board):
        """ Updates the board according to chosen move
            and initiates the next turn. """
        # update board according to chosen board
        self.board = new_board
        # roll new dice
        self.dice.roll()
        # update player
        self.current_player = Board.get_opponent(self.current_player)

    @staticmethod
    def progress(count, total, suffix=''):
        bar_len = 60
        filled_len = int(round(bar_len * count / float(total)))

        percents = round(100.0 * count / float(total), 1)
        bar = '=' * filled_len + '-' * (bar_len - filled_len)

        sys.stdout.write("\r[%s] %s%s %s" %(bar, percents, '%', suffix))
        
        sys.stdout.flush()