コード例 #1
0
ファイル: quarto.py プロジェクト: victorsmits/ProjetIA
 def _nextmove(self, State):
     AIClient.ttentry = lambda self: State  # Send State to the Transposition tables
     AI = Negamax(6, tt=TT(), win_score=90
                  )  # Algorithm(depth, scoring=None, win_score=inf,tt=None)
     Quarto = AIClient([AI_Player(AI), AI_Player(AI)], State)
     Move = Quarto.get_move()  # find the best move possible
     return json.dumps(Move)  # send the Move
コード例 #2
0
        def make_move(self, move):
            self.num_coins -= int(move)

            def win_game(self):
                return self.num_coins <= 0

            def is_over(self):
                return self.win()

            def score(self):
                return 100 if self.win_game() else 0

            def show(self):
                print(self.num_coins, 'coins left in the pile')

            if __name__ == "__main__":
                tt = TT()
                LastCoin_game.ttentry = lambda self: self.num_coins
                r, d, m = id_solve(LastCoin_game,
                                   range(2, 20),
                                   win_score=100,
                                   tt=tt)
                print(r, d, m)
                game = LastCoin_game([AI_Player(tt), Human_Player()])
                game.play()
コード例 #3
0
class LastCoin_game(TwoPlayersGame):
	def __init__(self, players):
		self.players = players
		self.nplayer = 1
		self.num_coins = 15
		self.max_coins = 4

	def possible_moves(self):
	return [str(a) for a in range(1, self.max_coins + 1)]
#Define the removal of the coins .
	def make_move(self, move):
		self.num_coins -= int(move)
#Define who took the last coin.
	def win_game(self):
		return self.num_coins <= 0
#Define when to stop the game, that is when somebody wins.
	def is_over(self):
		return self.win()
#Define how to compute the score.
	def score(self):
		return 100 if self.win_game() else 0
#Define number of coins remaining in the pile.
	def show(self):
		print(self.num_coins, 'coins left in the pile')
if __name__ == "__main__":
	tt = TT()
#LastCoin_game.ttentry = lambda self: self.num_coins
#Solving the game with the following code block:
	r, d, m = id_solve(LastCoin_game,
	range(2, 20), win_score=100, tt=tt)
	print(r, d, m)
	#Deciding who will start the game
	game = LastCoin_game([AI_Player(tt), Human_Player()])
	game.play()
コード例 #4
0
    def _nextmove(self, state):
        visible = state._state['visible']
        move = {}

        #List of the number of free places on the board
        verif = []
        for i in range(4):
            verif.append([visible['board'][4 * i + e]
                          for e in range(4)].count(None))
            verif.append([visible['board'][4 * e + i]
                          for e in range(4)].count(None))
        verif.append([visible['board'][5 * e] for e in range(4)].count(None))
        verif.append([visible['board'][3 + 3 * e]
                      for e in range(4)].count(None))

        #First is a random AI
        if verif.count(1) == 0:
            ls_test = [i for i, x in enumerate(visible['board']) if x == None]

            # select a random position

            if visible['pieceToPlay'] is not None:
                move['pos'] = random.choice(ls_test)

            # select a random piece

            test1 = visible['remainingPieces']
            a = random.randint(0, len(test1) - 2)
            move['nextPiece'] = a

            # applymove will raise if we announce a quarto while there is not
            move['quarto'] = True
            try:
                state.applymove(move)
            except:
                del (move['quarto'])

            return json.dumps(move)  # send the move

        #When the number of free place per line/column/diag is brought down to 1, begin easyAI
        else:
            easyAI.ttentry = lambda self: state
            ai_algo_neg = Negamax(6, tt=TT(), win_score=90)  # Algorithm
            Quarto = easyAI([AI_Player(ai_algo_neg),
                             AI_Player(ai_algo_neg)], state)
            best_move = Quarto.get_move()  # find the best move possible
            print("BEST MOVE", best_move)
            return json.dumps(best_move)  # send the Move
コード例 #5
0
    def scoring(self):
        return 100 if self.win() else 0

    def ttentry(self):
        return tuple(self.piles)  # optional, speeds up AI


if __name__ == "__main__":
    # IN WHAT FOLLOWS WE SOLVE THE GAME AND START A MATCH AGAINST THE AI

    from easyAI import AI_Player, Human_Player, Negamax, id_solve
    from easyAI.AI import TT
    # we first solve the game
    w, d, m, tt = id_solve(Nim, range(5, 20), win_score=80)
    print
    w, d, len(tt.d)
    # the previous line prints -1, 16 which shows that if the
    # computer plays second with an AI depth of 16 (or 15) it will
    # always win in 16 (total) moves or less.

    # Now let's play (and lose !) against the AI
    ai = Negamax(16, tt=TT())
    game = Nim([Human_Player(), AI_Player(tt)])
    game.play()  # You will always lose this game !
    print("player %d wins" % game.nplayer)

    # Note that with the transposition table tt generated by id_solve
    # we can setup a perfect AI which doesn't have to think:
    # >>> game = Nim( [ Human_Player(), AI_Player( tt )])
    # >>> game.play() # You will always lose this game too!
コード例 #6
0
    def scoring(self):
        return 100 if self.win() else 0

    def show(self):
        print("%d bones left in the pile" % (self.pile))


if __name__ == "__main__":
    """
    Start a match (and store the history of moves when it ends)
    ai = Negamax(10) # The AI will think 10 moves in advance 
    game = GameOfBones( [ AI_Player(ai), Human_Player() ] )
    history = game.play()
    """

    # Let's solve the game

    from easyAI import id_solve, Human_Player, AI_Player
    from easyAI.AI import TT

    tt = TT()
    GameOfBones.ttentry = lambda self: self.pile
    r, d, m = id_solve(GameOfBones, range(2, 20), win_score=100, tt=tt)
    print(r, d, m)  # see the docs.

    # Unbeatable AI !

    game = GameOfBones([AI_Player(tt), Human_Player()])
    game.play()  # you will always lose this game :)
コード例 #7
0
ファイル: quarto.py プロジェクト: victorsmits/ProjetIA
        QuartoRandom(args.name, (args.host, args.port), verbose=args.verbose)

    if args.component == 'prof':
        ProfAI(args.name, (args.host, args.port), verbose=args.verbose)

    if args.component == 'ai':
        state = QuartoState()

        if args.algo == 'solve':
            if args.tt:
                AIClient.ttentry = lambda self: state  # Send State to the Transposition tables
                result, depth, move = id_solve(AIClient([], state),
                                               ai_depths=range(
                                                   2, int(args.depth)),
                                               win_score=90,
                                               tt=TT())
            else:
                result, depth, move = id_solve(AIClient([], state),
                                               ai_depths=range(
                                                   2, int(args.depth)),
                                               win_score=90)
            print('result =', result)
            print('depth =', depth)
            print('move =', move)

        if args.algo == 'Negamax':
            if args.tt:
                AI = Negamax(int(args.depth), tt=TT())
                AIClient.ttentry = lambda self: state  # Send State to the Transposition tables
            else:
                AI = Negamax(int(args.depth))
コード例 #8
0
        self.num_coins = 15
        self.max_coins = 4

    def possible_moves(self):
        return [str(a) for a in range(1, self.max_coins + 1)]

    def make_move(self, move):
        self.num_coins -= int(move)

    def win_game(self):
        return self.num_coins <= 0

    def is_over(self):
        return self.win()

    def score(self):
        return 100 if self.win_game() else 0

    def show(self):
        print(self.num_coins, 'coins left in the pile')


if __name__ == "__main__":
    tt: object = TT()
    LastCoin_game.ttentry = lambda self: self.num_coins
    r, d, m = id_solve(LastCoin_game,
                       range(2, 20), win_score=100, tt=tt)
    print(r, d, m)
    game = LastCoin_game([AI_Player(tt), Human_Player()])
    game.play()