Skip to content

Instantly share code, notes, and snippets.

@jameslin101
Last active November 2, 2018 02:01
Show Gist options
  • Save jameslin101/583af1cf7ba66d3fb1cdb7235b6a54d8 to your computer and use it in GitHub Desktop.
Save jameslin101/583af1cf7ba66d3fb1cdb7235b6a54d8 to your computer and use it in GitHub Desktop.
import random
class Slice():
def __init__(self, pie_type, loc, size):
self.pie_type = pie_type;
self.loc = loc;
self.size = size;
def __str__(self):
return "type:{0} loc:{1} size:{2}".format(self.pie_type, self.loc, self.size)
class DestPie():
def __init__(self):
self.locs = [False, False, False, False, False, False]
self.pie_type = None
def clear(self):
points = self.locs.count(True)
self.locs = [False, False, False, False, False, False]
self.pie_type = None
return points
def complete(self):
return all(self.locs[i] == True for i in self.locs)
def add_slice(self,slice):
#check if pie size is correct
if self.pie_type != None and self.pie_type != slice.pie_type:
return False
#check if each space we need are empty
for i in range(slice.size):
loc_to_check = slice.loc + i;
if loc_to_check > 5:
loc_to_check = loc_to_check - 6;
if self.locs[loc_to_check] != False:
print("cant fit")
return False
#if empty fill in pie_type, locs and return slice_size
self.pie_type = slice.pie_type
for i in range(slice.size):
loc_to_check = slice.loc + i;
if loc_to_check > 5:
loc_to_check = loc_to_check - 6;
self.locs[loc_to_check] = True
return slice.size
class Game():
#settings
def __init__(self, pie_types, slice_size_probs, target_points = 500):
self.pie_types = pie_types
self.slice_sizes = [1,2,3,4,5]
self.slice_size_probs = slice_size_probs
self.dest_pies = []
for i in range(6):
self.dest_pies.append(DestPie())
self.slices_placed = 0
self.points = 0
self.target_points = target_points
def generate_random_slice(self):
slice_size = random.choices(
population=self.slice_sizes,
weights=self.slice_size_probs,
k=1)[0]
slice = Slice(random.choice(self.pie_types), random.choice(range(6)), slice_size)
print(slice)
return slice
def play(self):
turn = 0
no_more_moves = False
while turn < 100 and self.points < self.target_points and no_more_moves == False:
turn += 1
slice = self.generate_random_slice()
found_place = False
print("Turn: {0}, Slice {1}:, Points: {2}".format(str(turn), slice, self.points))
#find a pie to place the slice
for idx, pie in enumerate(self.dest_pies):
result = pie.add_slice(slice)
if result != False:
found_place = True
self.slices_placed += result
print("slice added to pie {0}".format(idx))
break;
if not found_place:
print("Game over on turn {0}!".format(turn))
no_more_moves = True;
#check if pies clear
for idx, pie in enumerate(self.dest_pies):
if pie.complete():
self.points += pie.clear()
print("pie {0} cleared!".format(idx))
idx_after = idx + 1
idx_before = idx - 1
if idx == 5:
idx_after = 0
if idx == 0:
idx_before = 5
print("adjacent pie {0} cleared!".format(idx_after))
self.points += self.dest_pies[idx_after].clear()
print("adjacent pie {0} cleared!".format(idx_before))
self.points += self.dest_pies[idx_before].clear()
return self.points
game = Game(['a'],[.4,.3,.2,0,0])
game.play()
@jameslin101
Copy link
Author

type:a loc:5 size:1
Turn: 1, Slice type:a loc:5 size:1:, Points: 0
slice added to pie 0
type:a loc:0 size:1
Turn: 2, Slice type:a loc:0 size:1:, Points: 0
slice added to pie 0
type:a loc:2 size:3
Turn: 3, Slice type:a loc:2 size:3:, Points: 0
slice added to pie 0
type:a loc:1 size:1
Turn: 4, Slice type:a loc:1 size:1:, Points: 0
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:1 size:1
Turn: 5, Slice type:a loc:1 size:1:, Points: 6
slice added to pie 0
type:a loc:4 size:2
Turn: 6, Slice type:a loc:4 size:2:, Points: 6
slice added to pie 0
type:a loc:5 size:2
Turn: 7, Slice type:a loc:5 size:2:, Points: 6
cant fit
slice added to pie 1
type:a loc:2 size:1
Turn: 8, Slice type:a loc:2 size:1:, Points: 6
slice added to pie 0
type:a loc:3 size:1
Turn: 9, Slice type:a loc:3 size:1:, Points: 6
slice added to pie 0
type:a loc:3 size:2
Turn: 10, Slice type:a loc:3 size:2:, Points: 6
cant fit
slice added to pie 1
type:a loc:3 size:1
Turn: 11, Slice type:a loc:3 size:1:, Points: 6
cant fit
cant fit
slice added to pie 2
type:a loc:2 size:2
Turn: 12, Slice type:a loc:2 size:2:, Points: 6
cant fit
cant fit
cant fit
slice added to pie 3
type:a loc:3 size:1
Turn: 13, Slice type:a loc:3 size:1:, Points: 6
cant fit
cant fit
cant fit
cant fit
slice added to pie 4
type:a loc:0 size:1
Turn: 14, Slice type:a loc:0 size:1:, Points: 6
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:3 size:2
Turn: 15, Slice type:a loc:3 size:2:, Points: 16
slice added to pie 0
type:a loc:5 size:2
Turn: 16, Slice type:a loc:5 size:2:, Points: 16
slice added to pie 0
type:a loc:2 size:3
Turn: 17, Slice type:a loc:2 size:3:, Points: 16
cant fit
slice added to pie 1
type:a loc:3 size:1
Turn: 18, Slice type:a loc:3 size:1:, Points: 16
cant fit
cant fit
cant fit
cant fit
cant fit
slice added to pie 5
type:a loc:0 size:3
Turn: 19, Slice type:a loc:0 size:3:, Points: 16
cant fit
cant fit
slice added to pie 2
pie 2 cleared!
adjacent pie 3 cleared!
adjacent pie 1 cleared!
type:a loc:1 size:2
Turn: 20, Slice type:a loc:1 size:2:, Points: 25
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:0 size:2
Turn: 21, Slice type:a loc:0 size:2:, Points: 32
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:0 size:2
Turn: 22, Slice type:a loc:0 size:2:, Points: 34
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:3 size:1
Turn: 23, Slice type:a loc:3 size:1:, Points: 36
slice added to pie 0
type:a loc:0 size:2
Turn: 24, Slice type:a loc:0 size:2:, Points: 36
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:2 size:1
Turn: 25, Slice type:a loc:2 size:1:, Points: 39
slice added to pie 0
type:a loc:3 size:1
Turn: 26, Slice type:a loc:3 size:1:, Points: 39
slice added to pie 0
type:a loc:5 size:1
Turn: 27, Slice type:a loc:5 size:1:, Points: 39
slice added to pie 0
type:a loc:4 size:1
Turn: 28, Slice type:a loc:4 size:1:, Points: 39
slice added to pie 0
type:a loc:4 size:2
Turn: 29, Slice type:a loc:4 size:2:, Points: 39
cant fit
slice added to pie 1
type:a loc:1 size:3
Turn: 30, Slice type:a loc:1 size:3:, Points: 39
cant fit
slice added to pie 1
type:a loc:0 size:2
Turn: 31, Slice type:a loc:0 size:2:, Points: 39
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:2 size:1
Turn: 32, Slice type:a loc:2 size:1:, Points: 50
slice added to pie 0
type:a loc:2 size:3
Turn: 33, Slice type:a loc:2 size:3:, Points: 50
cant fit
slice added to pie 1
type:a loc:4 size:3
Turn: 34, Slice type:a loc:4 size:3:, Points: 50
slice added to pie 0
type:a loc:3 size:3
Turn: 35, Slice type:a loc:3 size:3:, Points: 50
cant fit
cant fit
slice added to pie 2
type:a loc:0 size:1
Turn: 36, Slice type:a loc:0 size:1:, Points: 50
cant fit
slice added to pie 1
type:a loc:0 size:1
Turn: 37, Slice type:a loc:0 size:1:, Points: 50
cant fit
cant fit
slice added to pie 2
type:a loc:5 size:1
Turn: 38, Slice type:a loc:5 size:1:, Points: 50
cant fit
slice added to pie 1
type:a loc:3 size:1
Turn: 39, Slice type:a loc:3 size:1:, Points: 50
slice added to pie 0
type:a loc:2 size:1
Turn: 40, Slice type:a loc:2 size:1:, Points: 50
cant fit
cant fit
slice added to pie 2
type:a loc:0 size:2
Turn: 41, Slice type:a loc:0 size:2:, Points: 50
cant fit
cant fit
cant fit
slice added to pie 3
pie 3 cleared!
adjacent pie 4 cleared!
adjacent pie 2 cleared!
type:a loc:1 size:1
Turn: 42, Slice type:a loc:1 size:1:, Points: 58
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:5 size:2
Turn: 43, Slice type:a loc:5 size:2:, Points: 69
slice added to pie 0
type:a loc:0 size:1
Turn: 44, Slice type:a loc:0 size:1:, Points: 69
cant fit
slice added to pie 1
type:a loc:4 size:2
Turn: 45, Slice type:a loc:4 size:2:, Points: 69
cant fit
slice added to pie 1
type:a loc:2 size:2
Turn: 46, Slice type:a loc:2 size:2:, Points: 69
slice added to pie 0
type:a loc:2 size:3
Turn: 47, Slice type:a loc:2 size:3:, Points: 69
cant fit
cant fit
slice added to pie 2
type:a loc:1 size:1
Turn: 48, Slice type:a loc:1 size:1:, Points: 69
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:4 size:2
Turn: 49, Slice type:a loc:4 size:2:, Points: 77
slice added to pie 0
type:a loc:0 size:2
Turn: 50, Slice type:a loc:0 size:2:, Points: 77
slice added to pie 0
pie 0 cleared!
adjacent pie 1 cleared!
adjacent pie 5 cleared!
type:a loc:2 size:1
Turn: 51, Slice type:a loc:2 size:1:, Points: 81
slice added to pie 0
type:a loc:1 size:3
Turn: 52, Slice type:a loc:1 size:3:, Points: 81
cant fit
slice added to pie 1
type:a loc:3 size:2
Turn: 53, Slice type:a loc:3 size:2:, Points: 81
slice added to pie 0
type:a loc:1 size:2
Turn: 54, Slice type:a loc:1 size:2:, Points: 81
cant fit
cant fit
cant fit
slice added to pie 3
type:a loc:3 size:1
Turn: 55, Slice type:a loc:3 size:1:, Points: 81
cant fit
cant fit
cant fit
slice added to pie 3
type:a loc:5 size:2
Turn: 56, Slice type:a loc:5 size:2:, Points: 81
slice added to pie 0
type:a loc:3 size:1
Turn: 57, Slice type:a loc:3 size:1:, Points: 81
cant fit
cant fit
cant fit
cant fit
slice added to pie 4
type:a loc:1 size:2
Turn: 58, Slice type:a loc:1 size:2:, Points: 81
cant fit
cant fit
cant fit
cant fit
slice added to pie 4
type:a loc:1 size:3
Turn: 59, Slice type:a loc:1 size:3:, Points: 81
cant fit
cant fit
cant fit
cant fit
cant fit
slice added to pie 5
type:a loc:5 size:1
Turn: 60, Slice type:a loc:5 size:1:, Points: 81
cant fit
slice added to pie 1
type:a loc:2 size:1
Turn: 61, Slice type:a loc:2 size:1:, Points: 81
cant fit
cant fit
cant fit
cant fit
cant fit
cant fit
Game over on turn 61!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment