Skip to content

Instantly share code, notes, and snippets.

@ptarjan
Created November 21, 2009 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptarjan/240270 to your computer and use it in GitHub Desktop.
Save ptarjan/240270 to your computer and use it in GitHub Desktop.
Algorithm for swtiching instruments in rockband. http://blog.paulisageek.com/2009/10/rock-band-group-algorithm.html
import random
import sys
instruments = ["Drums", "Vocals", "Guitar", "Bass"]
people = sys.argv[1:]
if len(people) == 0:
print "python rockband.py Name1 Name2 ..."
sys.exit()
if len(people) < len(instruments):
print "If there is less than %d people, just pick some instruments, rotate, and then shuffle. You don't need me for that" % len(instruments)
sys.exit()
random.shuffle(people)
num = len(people)
places = [' Gap']*num
instruments_ids = [0, int(num*0.25), int(num*0.5), int(num*0.75)]
for i, ins in zip(instruments_ids, instruments):
places[i] = ins
while True :
for l,n in zip(places, people):
print "%s:\t%s" % (l, n)
if raw_input("Press <Enter> once you finished a round: ") == "q" :
break
print ""
playing = [people[x] for x in instruments_ids]
not_playing = list(set(people) - set(playing))
if len(not_playing) < len(instruments) :
new_playing = not_playing + random.sample(playing, len(instruments) - len(not_playing))
else :
new_playing = random.sample(not_playing, len(instruments))
new_not_playing = list(set(people) - set(new_playing))
random.shuffle(new_playing)
random.shuffle(new_not_playing)
people = [None]*num
for i, p in zip(instruments_ids, new_playing) :
people[i] = p
count = 0
for i in xrange(len(people)) :
if not people[i] :
people[i] = new_not_playing[count]
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment