Skip to content

Instantly share code, notes, and snippets.

@samm81
Created September 3, 2017 23:56
Show Gist options
  • Save samm81/eedc781e1a53fd5b897e0321cede345b to your computer and use it in GitHub Desktop.
Save samm81/eedc781e1a53fd5b897e0321cede345b to your computer and use it in GitHub Desktop.
Start to an implementation of splendor
from random import randrange, choice, sample
colors = [ 'red', 'blue', 'green', 'black', 'white' ]
# example cost gemstone points
card = ( {'red': 2, 'green': 2 }, 'white', 1 )
def genDeck1Card():
cost = { color: 0 for color in colors }
num_gemstones = randrange(3, 5+1)
for i in range(num_gemstones):
c = choice(colors)
cost[c] = cost[c] + 1
gemstone = choice(colors)
points = 1 if num_gemstones is 5 else 0
return ( cost, gemstone, points )
deck2Points = { 5: 1, 6: 2, 7: 3 }
def genDeck2Card():
cost = { color: 0 for color in colors }
num_gemstones = randrange(5, 7+1)
for i in range(num_gemstones):
c = choice(colors)
cost[c] = cost[c] + 1
gemstone = choice(colors)
points = deck2Points[num_gemstones]
return ( cost, gemstone, points )
deck3Points = { 8: 3, 9:3, 10: 4, 11: 4, 12: 5 }
def genDeck3Card():
cost = { color: 0 for color in colors }
num_gemstones = randrange(8, 12+1)
for i in range(num_gemstones):
c = choice(colors)
cost[c] = cost[c] + 1
gemstone = choice(colors)
points = deck3Points[num_gemstones]
return ( cost, gemstone, points )
# example noble "cost" points gained
noble = ( { 'red' : 4, 'white': 4 }, 3 )
noblesPoints = { 8: 3, 9:3, 10: 4, 11: 4, 12: 5 }
def genNoble():
cost = { color: 0 for color in colors }
num_gemstones = randrange(2, 3+1)
if num_gemstones is 2:
color1, color2 = sample(colors, 2)
cost[color1] = 4
cost[color2] = 4
else: # it's 3
color1, color2, color3 = sample(colors, 3)
cost[color1] = 3
cost[color2] = 3
cost[color3] = 3
return ( cost, 3 )
board = (
[ genDeck1Card(), genDeck1Card(), genDeck1Card() ],
[ genDeck2Card(), genDeck2Card(), genDeck2Card() ],
[ genDeck3Card(), genDeck3Card(), genDeck3Card() ],
[ genNoble(), genNoble(), genNoble() ] )
coins = { 'red': 4, 'blue': 4, 'green': 4, 'black': 4, 'white': 4, 'gold': 4 }
p1 = ('p1', { 'red': 0, 'blue': 0, 'green': 0, 'black': 0, 'white': 0, 'gold': 0 }, [], 0 )
p2 = ('p2', { 'red': 0, 'blue': 0, 'green': 0, 'black': 0, 'white': 0, 'gold': 0 }, [], 0 )
def cardToStr(card):
cost, color, points = card
return '|{}|{}| {}'.format(color, points, ' '.join( [ '{}{}'.format(num, color.capitalize()) for color, num in cost.items() if num is not 0 ] ))
def ppBoard(board):
def nobleToStr(noble):
cost, points = noble
return '[N] {}'.format(' '.join( [ '{}{}'.format(num, color.capitalize()) for color, num in cost.items() if num is not 0 ] ))
print ' '.join([ nobleToStr(noble) for noble in board[3] ])
print
for i in range(3,0,-1):
print '[{}]\n{}'.format(i, '\n'.join( [ cardToStr(card) for card in board[i-1] ] ))
def ppCoins(coins):
print ' '.join( '{}: {}'.format(color, num) for color, num in coins.items() )
def ppPlayer(player):
name, coins, cards, score = player
print '{}: {} points\n{}\n{}'.format(name, score,
' | | '.join( '{} {}'.format(color, num) for color, num in coins.items() ),
'\n'.join( [ cardToStr(card) for card in cards ] ) )
def ppGame(board, coins, p1, p2):
print '---------------------------------------------'
ppBoard(board)
print
ppCoins(coins)
print
print '----------------------------------------------'
print
print '```````````````````````'
ppPlayer(p1)
print '```````````````````````'
print
print '```````````````````````'
ppPlayer(p2)
print '```````````````````````'
def getValidInput(prompt, options):
inp = raw_input(prompt)
while inp not in options:
print 'invalid input'
inp = raw_input(prompt)
return inp
def takeTurn(player):
opts = [ 'buy', 'take2', 'take3', 'reserve' ]
name, pcoins, cards, score = player
print '{}\'s go'.format(name)
action = getValidInput('choose an action: {} '.format(' '.join(opts)), opts)
if action == 'take2':
color = getValidInput('which color to take two of: ', colors)
while coins[color] < 4:
print 'must choose color with 4 or more coins'
color = getValidInput('which color to take two of: ', colors)
pcoins[color] += 2
coins[color] -= 2
elif action == 'take3':
def opts3(colors):
opts = []
for i, c in enumerate(colors):
for j, cc in enumerate(colors):
for k, ccc in enumerate(colors):
if j > i and k > j:
opts.append( (c, cc, ccc) )
opts.append( (c, ccc, cc) )
opts.append( (cc, c, ccc) )
opts.append( (cc, ccc, c) )
opts.append( (ccc, c, cc) )
opts.append( (ccc, cc, c) )
return opts
color_opts = [ ' '.join(cs) for cs in opts3(colors) ]
c1, c2, c3 = getValidInput('which colors to take: ', color_opts).split(' ')
while coins[c1] < 1 or coins[c2] < 1 or coins[c2] < 1:
c1, c2, c3 = getValidInput('which colors to take: ', color_opts).split(' ')
pcoins[c1] += 1
coins[c1] -= 1
pcoins[c2] += 1
coins[c2] -= 1
pcoins[c3] += 1
coins[c3] -= 1
elif action == 'buy':
rcs = []
for r in range(1, 4):
for c in range(1, 5):
rcs.append('{} {}'.format(r, c))
rc_str = getValidInput('which card to purchase (level number): ', rcs)
r, c = map(lambda i: int(i), rc_str.split())
card = board[r-1][c-1]
cost, gemstone, points = card
if not all([ pcoins[color] > cost[color] for color in colors ]):
print 'not able to purchase card'
return (name, pcoins, cards, score)
def updateScore(player):
pass
def checkWinner(p1, p2):
pass
gameOver = False
while not gameOver:
ppGame(board, coins, p1, p2)
p1 = takeTurn(p1)
#p1 = updateScore(p1)
ppGame(board, coins, p1, p2)
p2 = takeTurn(p2)
#p2 = updateScore(p2)
gameOver = checkWinner(p1, p2)
print 'GAME OVER'
ppGame(board, coins, p1, p2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment