OK, so I made a FastScrabble game.
My code is currently:
from random import *
from itertools import *
scrabbletiles = []
showntiles = []
def drawtile():
nexttile = sample(scrabbletiles, 1)
scrabbletiles.remove(nexttile[0])
showntiles += nexttile
def possibilities():
possi = []
for i in range(2, len(showntiles)+1):
for word in permutations(showntiles, i):
wordstring = ""
for letter in word:
wordstring += letter
for WORD in f:
if wordstring+'\n' == WORD:
possi.append(wordstring)
f.seek(0)
def makechoice():
print "1: draw new tile"
print "2: attempt to form a word"
choice = raw_input("Make choice! Make here: ")
if choice == "1":
drawtile()
elif choice == "2":
guess()
drawtile()
else:
makechoice()
def guess():
scoreadd = 0
get = False
guess = raw_input("Make guess! Make here: ").upper()
for WORD in f:
if guess+'\n' == WORD:
get = True
if get == True:
for letter in guess:
scoreadd += scrabbleD[letter][2]
showntiles.remove()
playerscore += scoreadd
print "Yes! Now you have", playerscore, "points. Turn is us."
else:
tryagain = raw_input("Try again yes/no make choice! Make here: ")
if tryagain[0].upper() == "Y":
guess()
else:
scoreadd = 10
compuscore += scoreadd
print "Oh noes! You are fail, we get points. Turn is us!"
def computurn():
print "Turn is us! We take turn."
choicetime = randint(37, 100)
if choicetime == 77:
"We see word! We make word."
possibilities()
if len(possi) == 0:
"Oops. We no see word. You get points. Turn is you."
playerscore += 10
else:
scoreadd = 0
choice(possi)
for letter in guess:
scoreadd += scrabbleD[letter][2]
showntiles.remove()
compuscore += scoreadd
print "Yes! Now we have", compuscore, "points. Turn is you."
drawtile()
else:
print "We no see word. We make draw. Turn is you."
drawtile()
def finchoice():
if raw_input("Last guess yes/no make choice! Make here: ")[0].upper() == 'Y':
guess()
else:
pass
def fingame():
print "You has", playerscore, "points."
print "We has", compuscore, "points."
scoredict = {playerscore: "You", compuscore: "We"}.items().sort()
print scoredict[0][1], "is win!"
play = raw_input("Play again make choice! Make here: ")
scrabbleD = {
'A': (9, 1),
'B': (2, 3),
'C': (2, 3),
'D': (4, 2),
'E': (12, 1),
'F': (2, 4),
'G': (3, 2),
'H': (2, 4),
'I': (9, 1),
'J': (1, 8),
'K': (1, 5),
'L': (4, 1),
'M': (2, 3),
'N': (6, 1),
'O': (8, 1),
'P': (2, 3),
'Q': (1, 10),
'R': (6, 1),
'S': (4, 1),
'T': (6, 1),
'U': (4, 1),
'V': (2, 4),
'W': (2, 4),
'X': (1, 8),
'Y': (2, 4),
'Z': (1, 10)
}
f = open('C:\Python26\dictionary.txt')
play = 'Y'
while play[0].upper() == 'Y':
scrabbletiles = []
showntiles = []
playerscore = 0
compuscore = 0
for letter, (freq, val) in scrabbleD.iteritems():
for i in range(freq):
scrabbletiles.append(letter)
drawtile()
drawtile()
drawtile()
print showtiles
while len(scrabbletiles) > 0:
print showtiles
makechoice()
if len(scrabbletiles) > 0:
print showtiles
computurn()
else:
break
finchoice()
fingame()
And when I run it, it... it gives me this.
Traceback (most recent call last):
File "S:\Bell Work\FastScrabble.py", line 127, in <module>
drawtile()
File "S:\Bell Work\FastScrabble.py", line 10, in drawtile
showntiles += nexttile
UnboundLocalError: local variable 'showntiles' referenced before assignment
I researched UnboundLocalError... and based on what I found, it doesn't make sense. It says that it is referred to before being given a value...
Well, unless I'm wrong, I made it an empty list not only in my main while loop, but before I even defined my functions...
Uhm...
This could be a bug, or I could have made a mistake in my assumption. Or worse yet I have no true understanding of the error in question.
... Help, please?