I'm trying to build Nim "mathematical game of strategy" in Python, I have problem with removing element from specific list from the matrix, I started with getting game mode from user and then start removing elements.
def RemoveMatches(Stacks,StackNum,Matches):
if not_empty(Stacks[StackNum]):
lenStack=len(Stacks[StackNum])
try:
val=int(Matches)
except ValueError:
print ("Wrong input,try only with numbers")
return False
if val>lenStack:
print "try again with smaller number"
return False
else :
for i in range(Matches):
Stacks[StackNum].pop()
return True
else:
print "Stack that you have chose is already empty,try other satck"
return False
Stacks
is the matrix that i build in main.
StackNum
number of list that i want to remove from it elements.
Matches
number of elements that i want to remove.
There is another function called ManageGame
which control user's input "if it's 2 players or 1, getting StackNum
/Matches":
def ManageGame(Stack,gameMode):
if gameMode=='2':
while(lastDot(Stack)):
stackNum,matchesNum=raw_input('select number of the Stack and number of Matches between 1-10. keep a space between two numbers:').split(' ')
check=RemoveMatches(Stack,int(stackNum),int(matchesNum))
if check:
DrawStacks(Stack)
else:
print "try again."
lastDot(Stack)
is a function which checks if the matrix has more then 1 Dot.
The problem is when I'm trying to remove number of elements from StackNum I get somthing like this:
from:
When I type StackNum=1,Matches=2
I get:
Somehow I remove 2 Dots from each matrix line, I cannot see the problem in my code.
The return in the line:
for i in range(Matches):
Stacks[StackNum].pop()
return True
is situated in the wrong location. you have to move it after the for
. Otherwise, after the first iteration it will exit and not run all the iterations:
for i in range(Matches):
Stacks[StackNum].pop()
return True
the problem here with the list l=[]
,here how I build my matrix:
l=[]
tmp=[]
for i in range(10):
l.append('*')
for i in range(5):
tmp.append(l)
python works with pointers so I'm appending same pointer for tmp, when I make a change to one of them the rest going to be the same .
Here how I did solve it :
for i in range(5):
tmp.append(10)
When I want to pop an element I subtract Matches number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.