dimanche 7 février 2016

Memory Game - Selecting and printing random words [on hold]

I am creating a memory game which displays a 3 x 3 grid of words loaded from a .txt file which contains 10 words. The game should remove one word and replace it with the 10th, unused word. I have been working on a solution in Python but I can’t quite get some bits right. So far I can load the words from the file into a list and display them (but not in a 3x3 grid). It then replaces one of the words but not with the

The areas I need some help with are: - Placing the words into a 3x3 grid - Replacing a word with a new word, not one already in the grid - Correctly stating whether the user has guessed correctly or not

Help would be greatly appreciated.

Here is what I have so far

import random
import time, os

os.system('clear')

wordlist=[]
f = open("Words.txt","r")
for line in f:
    enter code herewordlist.append(line)
print wordlist

words = random.sample(wordlist,9)
grid = [words[i:i+3] for i in range(0, len(words), 3)]

for x,y,z in grid:
    print x, y, z

time.sleep(5)
os.system('clear')

ri = random.randint(0, 8)  # random index

old_word = grid[ri // 3][ri % 3]

grid[ri // 3][ri % 3] = random.sample(wordlist, 1)[0]

for x, y, z in grid:
    print x, y, z

g = raw_input(">> Enter old word that changed:\n")
if g == old_word:
    print "You won!"
else:
    print "No!"




Aucun commentaire:

Enregistrer un commentaire