コード例 #1
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()
コード例 #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
ファイル: ai_unittests.py プロジェクト: Zulko/easyAI
 def test_json_save_and_restore(self):
     # 1. solve game/save TT
     tt = easyAI.TT()
     winner, depth, best_player_move = easyAI.id_solve(
         examples.Nim,
         range(13, 16),
         tt=tt,
         win_score=80,
         verbose=False
     )
     tt.to_json_file("tt-data.json.temp", use_tuples=True)
     # 2. restore TT from file
     restored_tt = easyAI.TT()
     restored_tt.from_json_file("tt-data.json.temp", use_tuples=True)
     # 3. get first AI move using the TT
     game = examples.Nim([easyAI.Human_Player(), easyAI.AI_Player(restored_tt)])
     game.play_move(best_player_move)  # let the human play
     ai_move = game.get_move()  # get the AI's move based on tt
     self.assertEqual(ai_move, "2,5")
     self.assertEqual(best_player_move, "1,5")
 def test_json_save_and_restore(self):
     # 1. solve game/save TT
     tt = easyAI.TT()
     winner, depth, best_player_move = easyAI.id_solve(examples.Nim,
                                                       range(13, 16),
                                                       tt=tt,
                                                       win_score=80,
                                                       verbose=False)
     tt.to_json_file("tt-data.json.temp", use_tuples=True)
     # 2. restore TT from file
     restored_tt = easyAI.TT()
     restored_tt.from_json_file("tt-data.json.temp", use_tuples=True)
     # 3. get first AI move using the TT
     game = examples.Nim(
         [easyAI.Human_Player(),
          easyAI.AI_Player(restored_tt)])
     game.play_move(best_player_move)  # let the human play
     ai_move = game.get_move()  # get the AI's move based on tt
     self.assertEqual(ai_move, "2,5")
     self.assertEqual(best_player_move, "1,5")
コード例 #5
0
        return self.win()

    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 )])
コード例 #6
0
ファイル: Nim.py プロジェクト: AskarCoursera/easyAI
    def win(self): return max(self.piles) == 0

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

    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 )])
コード例 #7
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 :)
コード例 #8
0
        return self.num_coins <= 0 

    # Stop the game when somebody wins 
    def is_over(self): 
        return self.win() 

    # Compute score
    def scoring(self): 
        return 100 if self.win() else 0

    # Show number of coins remaining in the pile
    def show(self): 
        print(self.num_coins, 'coins left in the pile')

if __name__ == "__main__":
    # Define the transposition table
    tt = TT()

    # Define the method
    LastCoinStanding.ttentry = lambda self: self.num_coins

    # Solve the game
    result, depth, move = id_solve(LastCoinStanding, 
            range(2, 20), win_score=100, tt=tt)
    print(result, depth, move)  

    # Start the game 
    game = LastCoinStanding([AI_Player(tt), Human_Player()])
    game.play() 

コード例 #9
0
ファイル: Gomoku_optimized.py プロジェクト: csabagabor/easyAI
def solve_game():
    tt = TT()
    r, d, m = id_solve(Gomoku_optimized, range(2, 20), win_score=100, tt=tt)
    print r, d, m
コード例 #10
0
ファイル: GameOfBones.py プロジェクト: AskarCoursera/easyAI
    def is_over(self): return self.win() # game stops when someone wins.

    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 :)
コード例 #11
0
def solve_game():
    tt = TT()
    r, d, m = id_solve(Gomoku_Strategic, range(2, 7), win_score=100, tt=tt)
    print r, d, m
コード例 #12
0
ファイル: slovegame.py プロジェクト: vishwachintu/games
    def possible_moves(self):
        return ['1', '2', '3']

    def make_move(self, move):
        self.pile -= int(move)  # remove bones.

    def win(self):
        return self.pile <= 0  # opponent took the last bone ?

    def is_over(self):
        return self.win()  # Game stops when someone wins.

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

    def scoring(self):
        return 100 if self.win() else 0  # For the AI


r, d, m = id_solve(GameOfBones, ai_depths=range(2, 20), win_score=100)

tt = TT()

GameOfBones.ttentry = lambda game: game.pile  # key for the table
r, d, m = id_solve(GameOfBones, range(2, 20), win_score=100, tt=tt)

game = GameOfBones([AI_Player(tt), Human_Player()])
history = game.play()  # you will always lose this game :)
1
コード例 #13
0
        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)]

    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(self) 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()
コード例 #14
0
def solve_game():
    #to run this method we need to modify the max score to 100(check with self.win())
    tt = TT()
    r, d, m = id_solve(Gomoku_Strategic, range(2, 20), win_score=100, tt=tt)
    print r, d, m