Skip to content

Instantly share code, notes, and snippets.

@sxv
Last active May 14, 2018 17:51
Show Gist options
  • Save sxv/6b7a349fcea3bf4e32fa76765fa2d67a to your computer and use it in GitHub Desktop.
Save sxv/6b7a349fcea3bf4e32fa76765fa2d67a to your computer and use it in GitHub Desktop.
script to run simulations of settlers of catan board tile setups to find the average number of games played before encountering a duplicate arrangement.
import sys, random
# board is defined as list of 19 land tiles: Forest (4), Sheep (4), Wheat (4), Brick (3), Rocks (3), Desert (1)
def new_board(board=''):
available = list('ffffsssswwwwbbbrrrd')
for i in range(19):
random.shuffle(available)
board += available.pop()
return board
def rotate(board):
return board[7] + board[3] + board[0] + board[12] + board[8] + board[4] + board[1] + board[16] + board[13] + board[9] + board[5] + board[2] + board[17] + board[14] + board[10] + board[6] + board[18] + board[15] + board[11]
results = []
for test in range(int(sys.argv[1])):
boards = {}
while len(results) == test:
permutations = [new_board()]
for _ in range(5):
permutations.append(rotate(permutations[-1]))
if any(permutation in boards for permutation in permutations):
results.append(len(boards)+1)
boards[permutations[-1]] = 1
print 'test %s: duplicate board found after %s games. average: %s' % (test+1, len(boards), sum(results)/len(results))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment