Optimized pick-random-elements-from-list method.
This commit is contained in:
@@ -32,7 +32,9 @@ from __future__ import print_function
|
||||
import math
|
||||
import random
|
||||
import itertools
|
||||
|
||||
from robotgame.logic.direction import *
|
||||
import robotgame.misc as misc
|
||||
|
||||
class Blocker(object):
|
||||
pass
|
||||
@@ -89,6 +91,9 @@ def generate_simple_playfield(width, height, nturns, nstones,
|
||||
|
||||
'steps' is the number of steps used by the generated solution. It is not
|
||||
necessarily the lowest number of steps the playfield can be completed in.
|
||||
|
||||
This generator favours increasing the turn density the closer to the goal
|
||||
it gets.
|
||||
"""
|
||||
|
||||
min_width, min_height = _min_play_size(nturns)
|
||||
@@ -151,13 +156,10 @@ def generate_simple_playfield(width, height, nturns, nstones,
|
||||
del turns[-1]
|
||||
for p, d in turns:
|
||||
playfield[p] = d
|
||||
emptys = set(itertools.product(range(width),
|
||||
range(height))) - set(used_fields)
|
||||
for _ in range(nstones):
|
||||
if not emptys:
|
||||
break
|
||||
pos = random.choice(list(emptys))
|
||||
emptys.remove(pos)
|
||||
|
||||
for pos in misc.pick_random_elements(
|
||||
list(set(itertools.product(range(width), range(height)))
|
||||
- set(used_fields)), nstones):
|
||||
playfield[pos] = Blocker
|
||||
return playfield, len(used_fields) - 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user