For building bots to play two player games in AI, we need to install the easyAI library. It is an artificial intelligence framework that provides all the functionality to build two-player games. You can download it with the help of the following command:
A Bot to Play Last Coin Standing
In this game, there would be a pile of coins. Each player has to take a number of coins from that pile. The goal of the game is to avoid taking the last coin in the pile. We will be using the class LastCoinStanding inherited from the TwoPlayersGame class of the easyAI library. The following code shows the Python code for this game:
from easyAI import TwoPlayersGame, id_solve, Human_Player, AI_Player
from easyAI.AI import TTNow, inherit the class from the TwoPlayerGame class to handle all operations of the game:
class LastCoin_game(TwoPlayersGame):
def __init__(self, players):Now, define the players and the player who is going to start the game.
self.players = players
self.nplayer = 1
self.nplayer = 1
Now, define the number of coins in the game, here we are using 15 coins for the game.
self.num_coins = 15
Define the maximum number of coins a player can take in a move.
self.max_coins = 4
Now there are some certain things to define as shown in the following code. Define possible moves.
def possible_moves(self):
return [str(a) for a in range(1, self.max_coins + 1)]
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)
self.num_coins -= int(move)
Define who took the last coin.
def win_game(self):
return self.num_coins <= 0
return self.num_coins <= 0
Define when to stop the game, that is when somebody wins.
def is_over(self):
return self.win()
return self.win()
Define how to compute the score.
def score(self):
return 100 if self.win_game() else 0
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()
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)
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()
game.play()
You can find the following output and a simple play of this game:
d:2, a:0, m:1
d:3, a:0, m:1
d:4, a:0, m:1
d:5, a:0, m:1
d:6, a:100, m:4
1 6 4
15 coins left in the pile
Move #1: player 1 plays 4 :
11 coins left in the pile
Player 2 what do you play ? 2
Move #2: player 2 plays 2 :
9 coins left in the pile
Move #3: player 1 plays 3 :
6 coins left in the pile
Player 2 what do you play ? 1
Move #4: player 2 plays 1 :
5 coins left in the pile
Move #5: player 1 plays 4 :
1 coins left in the pile
Player 2 what do you play ? 1
Move #6: player 2 plays 1 :
0 coins left in the pile
d:3, a:0, m:1
d:4, a:0, m:1
d:5, a:0, m:1
d:6, a:100, m:4
1 6 4
15 coins left in the pile
Move #1: player 1 plays 4 :
11 coins left in the pile
Player 2 what do you play ? 2
Move #2: player 2 plays 2 :
9 coins left in the pile
Move #3: player 1 plays 3 :
6 coins left in the pile
Player 2 what do you play ? 1
Move #4: player 2 plays 1 :
5 coins left in the pile
Move #5: player 1 plays 4 :
1 coins left in the pile
Player 2 what do you play ? 1
Move #6: player 2 plays 1 :
0 coins left in the pile
0 comments:
Post a Comment