I wanted to build a bot for playing Jaipur board game. I started by implementing a simplified version of the game without bot. Want to make sure I'm going in the right direction. Few of the simplifications from the actual game:
- Prices of commodities are fixed
- No camels
- Only one card is visible and can be picked from the market
- Cards are not shuffled at the start
Code:
import random
import numpy as np
DIAMOND = 0
GOLD = 1
SILVER = 2
SILK = 3
SPICE = 4
LEATHER = 5
commodities = [DIAMOND, GOLD, SILVER, SILK, SPICE, LEATHER]
price = [7, 6, 5, 3, 3, 1] # DIAMOND, GOLD, SILVER, SILK, SPICE, LEATHER
TAKE = 0
SELL = 1
actions = [TAKE, SELL]
class Jaipur():
def __init__(self, player1, player2):
self.market = [DIAMOND] * 6 + [GOLD] * 6 + [SILVER] * 6 + [SILK] * 8 + [SPICE] * 8 + [LEATHER] * 10
player1 = globals()[player1]
player2 = globals()[player2]
self.player1 = player1(tag='P1')
self.player2 = player2(tag='P2')
self.winner = None
self.player_turn = self.player1
def play_game(self):
# while len(self.market != 0):
while self.winner is None:
self.state = self.play_move()
self.game_winner()
if self.winner is not None:
print('P1 score: ', self.player1.score)
print('P2 score: ', self.player2.score)
print('Winner is ', self.winner)
def play_move(self, learn=False):
if self.player_turn == self.player1:
self.print_game()
new_market = self.player1.make_move(self.market, self.winner)
self.player_turn = self.player2
elif self.player_turn == self.player2:
self.print_game()
new_market = self.player2.make_move(self.market, self.winner)
self.player_turn = self.player1
def print_game(self):
print(self.market)
print('turn: ', self.player_turn.tag)
print('market: ', self.market)
print('player hand: ', self.player_turn.hand)
print('player score: ', self.player_turn.score)
def game_winner(self):
if len(self.market) == 0:
if self.player1.score > self.player2.score:
self.winner = self.player1.tag
else:
self.winner = self.player2.tag
return self.winner
class Player():
def __init__(self, tag):
self.tag = tag
self.hand = [0] * len(commodities)
self.score = 0
def hand_size(self):
return sum(self.hand)
def take(self, market):
print('taking..')
if self.hand_size() == 7:
return market
if len(market) == 0:
return market
taken = market.pop()
self.hand[taken] += 1
return market
def sell(self, market, commodity=None):
print('selling..')
if commodity is None:
commodity = np.argmax(self.hand)
if commodity in [DIAMOND, GOLD, SILVER] and self.hand[commodity] < 2:
return market
if self.hand[commodity] < 1:
return market
self.score += self.hand[commodity] * price[commodity]
if self.hand[commodity] == 3:
self.score += 2
elif self.hand[commodity] == 4:
self.score += 5
elif self.hand[commodity] >= 5:
self.score += 9
self.hand[commodity] = 0
return market
def make_move(self, market, winner):
# move = int(input('0: Take, 1: Sell. Choose move..'))
move = random.randint(0, 1)
new_market = market
if move == 0:
new_market = self.take(market)
elif move == 1:
new_market = self.sell(market)
return new_market
def play():
game = Jaipur('Player', 'Player')
game.play_game()
play()