Exemplo n.º 1
0
class federatedplayer:
	def __init__(self, p):	
		# get the model from the server
		model = self._download_model()

		# create an evaluator with these weights, and an ai agent using it
		evaluator = TutorialEvaluator(p, model)

                # TTT's constructor takes in the piece
                # that STARTS first (not the player's piece)
		self.board = TTT(3, START_PIECE, evaluator)

		# self.agent = AlphaBeta(4)
		self.agent = AlphaBeta()

	def update(self, move):
		self._apply(move)
	def move(self):
		move = self._move()
		self._apply(move)
		return move
	
	def _apply(self, move):
		self.board.update(move)

	def _move(self):
		return self.agent.next_move(self.board)
	
	def _download_model(self):
		server = flip.flip()
		server.connect()
		server.send_check()
		model = server.recv_model()
		server.disconnect()
		return model
Exemplo n.º 2
0
def main():
    '''The primary assignment functionality.'''

    print("Running main game..\n\n")

    # Play with with negamaxIDS
    playGameNegamax(TTT(False), IDS=True, ab=False)

    # Play with negamaxIDSab
    playGameNegamax(TTT(False), IDS=True, ab=True)
Exemplo n.º 3
0
	def __init__(self, p):	
		# get the model from the server
		model = self._download_model()

		# create an evaluator with these weights, and an ai agent using it
		evaluator = TutorialEvaluator(p, model)

                # TTT's constructor takes in the piece
                # that STARTS first (not the player's piece)
		self.board = TTT(3, START_PIECE, evaluator)

		# self.agent = AlphaBeta(4)
		self.agent = AlphaBeta()
Exemplo n.º 4
0
def playGameNegamaxMultipleRounds(rounds=100):
    '''Extra credit problem-- 100 games, average win ratio for 'X'. I used
    negamaxIDS with alpha-beta prunning because it's faster (but less effective
    sometimes). Can specify any number of rounds..'''

    print("Playing {} games and finding average # of 'X' wins.".format(rounds))

    xWins = 0
    value = 0

    for round in range(rounds):
        game = TTT(False)
        print(game)
        while not game.isOver():
            if game.player == 'X':
                value, move = negamaxIDS(game, 9, ab=True)
            else:
                move = opponentRandom(game.board)
            if move == None:
                print("move is None. Stopping")
                break
            game.makeMove(move)
            print("Player", game.player, "to", move, "for value", value)
            if not debug: print()
            print(game)
            game.changePlayer()
        if game.winner == 'X':
            xWins = xWins + 1

    msg = "'X' won {0} out of {1} games for {2}% wins.".format(
        xWins, rounds, (xWins / rounds) * 100)
    print(msg)
def playGameNegamaxMultipleRounds(rounds=100):
    '''Extra credit problem-- 100 games, average win ratio for 'X'. I used
    negamaxIDS with alpha-beta prunning because it's faster (but less effective
    sometimes). Can specify any number of rounds..'''
    
    print("Playing {} games and finding average # of 'X' wins.".format(rounds))
    
    xWins = 0
    value = 0
    
    for round in range(rounds):
	game = TTT(False)
	print(game)
	while not game.isOver():
	    if game.player == 'X':
		value,move = negamaxIDS(game, 9, ab = True)
	    else:
		move = opponentRandom(game.board)
	    if move == None:
		print("move is None. Stopping")
		break
	    game.makeMove(move)
	    print("Player",game.player,"to",move,"for value",value)
	    if not debug: print()
	    print(game)
	    game.changePlayer()
	if game.winner == 'X':
	    xWins = xWins + 1
        
    msg = "'X' won {0} out of {1} games for {2}% wins.".format(xWins, rounds, (xWins/rounds) * 100)    
    print(msg)
Exemplo n.º 6
0
def main(args):
    parser = ap.ArgumentParser(
        description=
        "A Game of Tic Tac Toe that gradually learns how to play over time")
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        default=False,
                        help="Increase verbosity")
    parser.add_argument("--gametype",
                        default=PLAYER_VS_PLAYER,
                        choices=[
                            PLAYER_VS_PLAYER, PLAYER_VS_COMPUTER,
                            PLAYER_VS_LEARNER, LEARNER_VS_COMPUTER,
                            LEARNER_VS_LEARNER
                        ])
    args = parser.parse_args(args)

    if args.gametype in [PLAYER_VS_PLAYER, PLAYER_VS_COMPUTER]:
        game = TTT(args.verbose)

        p1 = HumanPlayer(PLAYER_1)
        if args.gametype == PLAYER_VS_PLAYER:
            p2 = HumanPlayer(PLAYER_2)
        else:
            p2 = Player(PLAYER_2)

        curr_player = p1
        while True:

            print "\nPlayer " + ("1" if curr_player.id == PLAYER_1 else
                                 "2") + ", your move: "
            move = curr_player.move(game.board)
            game.move(curr_player.id, move)

            print
            game.draw_board()

            check = game.check_win()
            if check == PLAYER_1:
                print "Player 1 wins!"
                break
            elif check == PLAYER_2:
                print "Player 2 wins!"
                break
            elif check == CAT_GAME:
                print "It's a tie!"
                break

            curr_player = p1 if curr_player == p2 else p2
    else:
        raise NotImplementedError()
Exemplo n.º 7
0
 def setup_prenetwork_ttt(self, **kwargs):
     #add weight init!
     ttt = TTT(args.nlayer, nz=args.latent, arch=args.arch)
     ttt.apply(self.init_weights)
     args['ttt'] = ttt.cuda()
Exemplo n.º 8
0
 def setUp(self):
     self.game = TTT()
Exemplo n.º 9
0
class TestGameOver(unittest.TestCase):
    def setUp(self):
        self.game = TTT()

    def test_won_across(self):
        for i in range(1, 4):
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()
        for i in range(4, 7):
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()
        for i in range(7, 10):
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())

    def test_won_down(self):
        for i in [1, 4, 7]:
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()
        for i in [2, 5, 8]:
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()
        for i in [3, 6, 9]:
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()

    def test_won_across(self):
        for i in [1, 5, 9]:
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()
        for i in [3, 5, 7]:
            self.game[i] = 'X'
        self.assertTrue(self.game.game_won())
        self.game._reset()

    def test_stalemate(self):
        moves = ['X', 'O', 'X', 'O', 'X', 'O', 'O', 'X', 'O']
        for i, m in enumerate(moves):
            self.game[i + 1] = m
        self.assertTrue(self.game.stalemate())