0

So for a school project we've been asked to code a boggle game on Python (I'm using 2.7.3), and I've come to a dilemma. I'm trying to get my randomly selected letters from an array of 16 hypothetical "cubes" to appear in a 4x4 format (so that there is a square with 16 random letters).

The code can be seen here:

#Boggle array within an array (array of each letter on each cube placed within another array of 16 cube arrays)

DICE = [
["A", "A", "E", "E", "G", "N"],
["A", "B", "B", "J", "O", "O"],
["A", "C", "H", "O", "P", "S"],
["A", "F", "F", "K", "P", "S"],
["A", "O", "O", "T", "T", "W"],
["C", "I", "M", "O", "T", "V"],
["D", "E", "I", "L", "R", "X"],
["H", "L", "N", "N", "R", "Z"],
["D", "I", "S", "T", "T", "Y"],
["E", "E", "G", "H", "N", "W"],
["E", "E", "I", "N", "S", "U"],
["E", "H", "R", "T", "V", "W"],
["E", "I", "O", "S", "S", "T"],
["E", "L", "R", "T", "T", "Y"],
["H", "A", "E", "E", "G", "N"],
["A", "I", "M", "N", "Q", "U"]]



#Code to randomly select letters from the array defined in DICE

from random import choice
for d in DICE:
    print choice(d)

As you can see I've used a for loop to select random letters from each line in the DICE array, now I want those 16 randomly selected letters to display as to create the 4x4 grid.

I.e.

a b c d
e f g h
i j k l
m n o p

Thanks in advance!

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
YashMorar
  • 7
  • 3

2 Answers2

2

I'd do something like:

#this will make it so the first die isn't always in the top left
from random import shuffle
shuffle(dice)

#create four rows of dice and store them in a list
from random import choice
rows = []
for i in range(4):
    #we want the next four dice in the list
    newRow = [choice(dice[i*4 + 0]),
              choice(dice[i*4 + 1]),
              choice(dice[i*4 + 2]),
              choice(dice[i*4 + 3])
             ]
    rows.append(newRow)

#display each row on its own line
for row in rows:
    print row
turbulencetoo
  • 3,447
  • 1
  • 27
  • 50
  • This is more than what I had asked for, thank you so much! You've improved the randomness of the letters selected and which order, this is significant to the nature of the game. Thanks so much! – YashMorar Jan 20 '14 at 17:53
1
for i, d in enumerate(DICE):
    print choice(d),   # comma prevents newline
    if not (i+1) % 4:  # i+1 modulus 4 will evaluate to [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0]
        print ''       # empty string forces new line, when above was 0
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • This is exactly what was needed, and also the comments made it so much easier to understand. As someone who has only had around 10 hours of experience with python, I haven't come across things like enumerate(). I also struggle with grasping the technique of using for loops. Thanks! – YashMorar Jan 20 '14 at 17:42
  • You're very welcome. Take a look at @turbulentcetoo's answer too. Raises a great point as well. – mhlester Jan 20 '14 at 17:46
  • If only StackOverflow could allow for up to 3 selected answers, both of you have helped tremendously, however I've given @turbulentcetoo the accepted answer podium as he did make the point of the shuffle. But on the other hand your answer was the first to be able to reach my intended query ever so simply and easily. – YashMorar Jan 20 '14 at 17:57