d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help In Python > For A Mastermind
123Next
Add Reply New Topic New Poll
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 23 2014 11:22pm
This is simple. Player have to pick 5 colors in an order. After that, computer have to find right colors in right order.


So... A list with all the possibility in generated (8^5 = around 32 000). Pc random in that list and if it dont find the answer it will keep that combination. For exemple if the comp get 2 right color right place and 2 right color wrong place, it will delete every color lists in the big list without 2 right color right place and 2 right color wrong place then pc rand again in the big list and check for combinations until the answer is found



The problem is: I got Index out or range exception while comparing 2 lists
I'm using pycharm on win7 64bits. So, is the code wrong or could this be a pycharm bug or could this be a win7/ram issue because it looks like a var ref to another one or content deleted without reason



here is the code:


Code
import random
import sys

def lChoice2():
print "Here is the colors"
print "1 = Black"
print "2 = White"
print "3 = Yellow"
print "4 = Orange"
print "5 = Red"
print "6 = Green"
print "7 = Brown"
print "8 = Blue"
lChoice = []
nbTry = 0

Colors = "0"
i = 0
while i < 5:
while Colors != "1" and Colors != "2" and Colors != "3" and Colors != "4" and Colors != "5" and Colors != "6" and Colors != "7" and Colors != "8":
Colors = raw_input("Chooose a color")
lChoice.insert(i, Colors)
i = i +1
Colors = "0"
totall = fRempliPoss()
nbTry = 0
rcolrplace, rcolorwplace = 0,0
rcolrplace2, rcolorwplace2 = 0,0
z = 0
lLength = len(totall)
lTry2 = []
nbrand = 0
lTry = []
lChoiceCopy = []
lelem = []
vari = 0


while rcolrplace != 5:
vari = vari + 1
print (vari)
del lTry[:]
lLength = len(totall)
nbrand = random.randint(0, lLength-1)
lTry = totall[nbrand]

del lChoiceCopy[:]
lChoiceCopy = lChoice[:]
lTry2 = lTry[:]
brcolorwplace, rcolrplace = 0, 0
sortloop = 0
x = 0
while x < len(lTry):
if lTry2[x] == lChoice[x]:
lTry2.pop(x)
lChoiceCopy.pop(x)
rcolrplace = rcolrplace +1
else:
x = x + 1
i, j, x = 0, 0, 0
nbTry = nbTry + 1


if rcolrplace != 5:
while j < len(lChoiceCopy):
while i < len(lChoice) and sortloop == 0:
if lTry2[i] == lChoiceCopy[j]:
lTry2.pop(i)
lChoiceCopy.pop(j)
rcolorwplace = rcolorwplace + 1
sortloop = 1
else:
i = i + 1
i = 0
sortloop = 0
j = j + 1
x = 0

lelem = []
while z < lLength:
brcolorwplace2, rcolrplace2 = 0, 0
sortloop = 0
del lTry2[:]
lTry2 = lTry[:]
del lelem [:]
lelem = totall[z]

while x <len(lelem):
if lTry2[x] == lelem[x]:
lTry2.pop(x)
lelem.pop(x)
rcolrplace2 = rcolrplace2 +1
else:
x = x + 1
i, j = 0, 0

while j <len(lelem):
while i <len(lelem) and sortloop == 0:
if lTry2[i] == lelem[j]: #--------------------------------------getting index out of range--------------------------------------------------just there
lTry2.pop(i)
lelem.pop(j)
rcolorwplace2 = rcolorwplace2 + 1
sortloop = 1
else:
i = i +1
i = 0
sortloop = 0
j = j + 1
if rcolrplace != rcolrplace2 or rcolorwplace != rcolorwplace2:
del totall[z]
z = z - 1
lLength = lLength - 1
z = z + 1

print "You win"
return

def fRempliPoss():
tempo = []
totall = []
i, j, k, l, m = 0, 0, 0, 0, 0
while i < 8:
while j < 8:
while k < 8:
while l < 8:
while m < 8:
tempo.insert(0, i)
tempo.insert(1, j)
tempo.insert(2, k)
tempo.insert(3, l)
tempo.insert(4, m)
totall.append(tempo)
tempo = []
m = m +1
l = l + 1
m = 0
k = k + 1
l = 0
j = j + 1
k = 0
i = i + 1
j = 0
return totall
endprogram = 0

while endprogram == 0:
print "####MASTERMIND### \n\n You have to match right colors in right place"
print "Please choose between these 2 options:\n\n"
print "1- You match right colors in right place\n"
print "2- Computer match right colors in right place"

lChoice = input("Make your Choice\n")

if lChoice == 1:
print 'You match right colors in right place'
lChoice1()
elif lChoice == 2:
print 'Computer match right colors in right placel'
lChoice2()
continu = "a"
while continu != "y" and continu != "n":
continu = raw_input("Another game? y/n")
if continu == "n":
endprogram = 1

Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
May 24 2014 03:59am
What a mess, this is not something I even want to read through, but I'm going to try. :(

Maybe totall is empty.

This post was edited by killg0re on May 24 2014 04:20am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 24 2014 09:10am
i assume lTry2 is the array in question? why dont you try debugging? i see no logging in your code. you're only gonna hurt yourself.

log the length of your array before checking, and log every time your array changes. then you can determine if you're not adding when you think you are or if you're removing too many. or if something else is wrong.
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 24 2014 03:45pm
Quote (carteblanche @ 24 May 2014 10:10)
i assume lTry2 is the array in question? why dont you try debugging? i see no logging in your code. you're only gonna hurt yourself.

log the length of your array before checking, and log every time your array changes. then you can determine if you're not adding when you think you are or if you're removing too many. or if something else is wrong.


yes I debugged but values in list are right just before entering the loop so i just cant understand
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 24 2014 03:53pm
Quote (eric838 @ May 24 2014 05:45pm)
yes I debugged but values in list are right just before entering  the loop so i just cant understand

for what value of i and j is it failing, and what are the lengths of both arrays respectively? is it always failing at the same time?
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 24 2014 04:26pm
Quote (carteblanche @ 24 May 2014 16:53)
for what value of i and j is it failing, and what are the lengths of both arrays respectively? is it always failing at the same time?


yes

while z < lLength: #I never exit that loop
and this is what I get in ltry -->
Code
[]




####################### while z < lLength: LOOP with break points#########################

Code
while z < lLength:
brcolorwplace2, rcolrplace2 = 0, 0
sortloop = 0
del lTry2[:]
lTry2 = lTry[:]
del lelem [:]
lelem = totall[z]

print ("test 0")
print (lTry)
print (lTry2)
print (totall[z])
print (lelem)

while x <len(lelem):
if lTry2[x] == lelem[x]:
lTry2.pop(x)
lelem.pop(x)
rcolrplace2 = rcolrplace2 +1
else:
x = x + 1
i, j = 0, 0

print ("test 1")
print (lTry)
print (lTry2)
print (totall[z])
print (lelem)

while j <len(lelem):
while i <len(lelem) and sortloop == 0:
if lTry2[i] == lelem[j]: #--------------------------------------getting index out of range--------------------------------------------------just there
lTry2.pop(i)
lelem.pop(j)
rcolorwplace2 = rcolorwplace2 + 1
sortloop = 1
else:
i = i +1
i = 0
sortloop = 0
j = j + 1
if rcolrplace != rcolrplace2 or rcolorwplace != rcolorwplace2:
del totall[z]
z = z - 1
lLength = lLength - 1
z = z + 1



##############################DEBUG########################
[CODE
test 0
[7, 3, 3, 1, 0]#lTry
[7, 3, 3, 1, 0]#lTry2
[7, 3, 3, 0, 7]#totall[z]
[7, 3, 3, 0, 7]#lelem
]test 1
[7, 3, 3, 1, 0] #lTry
[7, 3, 3, 1, 0] #lTry2
[7, 3, 3, 0, 7] #totall[z]
[7, 3, 3, 0, 7] #lelem
test 0
[7, 3, 3, 1, 0] #lTry
[7, 3, 3, 1, 0] #lTry2
[7, 3, 3, 1, 0] #totall[z]
[7, 3, 3, 1, 0] #lelem
test 1
[7, 3, 3, 1, 0] #lTry
[7, 3, 3, 1, 0] #lTry2
[7, 3, 3, 1, 0] #totall[z]
[7, 3, 3, 1, 0] #lelem
test 0
[] #lTry
[3, 1] #lTry2
[7, 3, 3, 1, 1] #totall[z]
[7, 3, 3, 1, 1] #lelem
test 1
[] #lTry
[3, 1] #lTry2
[7, 3, 3, 1, 1] #totall[z]
[7, 3, 3, 1, 1] #lelem[/CODE]

This post was edited by eric838 on May 24 2014 04:29pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 24 2014 04:51pm
Code
while j <len(lelem):
while i <len(lelem) and sortloop == 0:
print ('j=' + j + ', i=' + i + ', lTry2=' + lTry2 + ', lelem=' + lelem)
if lTry2[i] == lelem[j]: #--------------------------------------getting index out of range--------------------------------------------------just there


since you didnt answer my question, what does that print out? and does the printout look correct to you?

first thing you gotta determine is which array is bad. second thing to figure out is why it's bad.

This post was edited by carteblanche on May 24 2014 04:52pm
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 24 2014 06:42pm
Quote (carteblanche @ 24 May 2014 17:51)
Code
while j <len(lelem):
    while i <len(lelem) and sortloop == 0:
        print ('j=' + j + ', i=' + i + ', lTry2=' + lTry2 + ', lelem=' + lelem)
        if lTry2[i] == lelem[j]: #--------------------------------------getting index out of range--------------------------------------------------just there


since you didnt answer my question, what does that print out? and does the printout look correct to you?

first thing you gotta determine is which array is bad. second thing to figure out is why it's bad.


lTry and lTry2 are bad and supposed to be [7, 3, 3, 1, 0] but got [] and [3, 1]
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 24 2014 06:56pm
Quote (eric838 @ May 24 2014 08:42pm)
lTry and lTry2 are bad and supposed to be [7, 3, 3, 1, 0] but got [] and [3, 1]


sounds like a great starting point. now go add logging statements to find out why they're not correct.
Member
Posts: 27,086
Joined: Mar 7 2008
Gold: 685.00
May 24 2014 07:32pm
Quote (carteblanche @ 24 May 2014 19:56)
sounds like a great starting point. now go add logging statements to find out why they're not correct.


tried but cant find out why lTry get empty w/o ref or del
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll