arrays - Change variable to be used inside loop (Python) -
so programming checkers game, , problem having creating several pieces in loop. have class creation part of code, won't post here, , rest, i'm posting, don't know how change variable loop use. if can lend me hand , clear out, thankful.
sorry posting code image, i'm new website ( , programming) , couldn't format website accept post. hope it's ok guys me!
thanks help!
further clarification: need use different "piece" creation everytime loop runs. means first loop has create piece1, piece2, piece3... , forward
edit: posting whole code. know format wrong, can't it. so, hope can fix it.
class piece: def __init__(self, kind, yposition, xposition): self.color = kind self.ypos = xposition self.xpos = yposition def getcolor(self): return self.getcolor def adjusty(self, change): self.ypos = self.ypos + change def adjustx(self, change): self.xpos = self.xpos + change def gety(self): return self.ypos def getx(self): return self.xpos def mover(self, direction): self.direc = direction if self.direc == "right" , self.color == "white": n in alist: if n.gety == (self.gety - 1) , n.getx == (self.getx + 1): pass # not yet implemented else: self.adjusty(-1) self.adjustx(+1) elif self.direc == "left" , self.color == "white": n in alist: if n.gety == (self.gety - 1) , n.getx == (self.getx - 1): pass # not yet implemented else: self.adjusty(-1) self.adjustx(-1) elif self.direc == "right" , self.color == "black": n in alist: if n.gety == (self.gety + 1) , n.getx == (self.getx + 1): pass # not yet implemented else: self.adjusty(+1) self.adjustx(+1) else: n in alist: if n.gety == (self.gety + 1) , n.getx == (self.getx - 1): pass # not yet implemented else: self.adjusty(+1) self.adjustx(-1) piece1 = 0 piece2 = 0 piece3 = 0 piece4 = 0 piece5 = 0 piece6 = 0 piece7 = 0 piece8 = 0 piece9 = 0 piece10 = 0 piece11 = 0 piece12 = 0 alistb1 = [piece1,piece2,piece3,piece4,piece5,piece6,piece7,piece8,piece9,piece10,piece11,piece12] k = 2 in range(0,11): if >= 0 , <5: j = 8 m = 0 elif >= 5 , < 9: j = 7 m = 1 else: j = 6 m = 0 alistb1[i] = piece("white",j,(m + 1 + * k)) print(alistb1[i].gety()) # print(piece7.gety()) test reasons
ps: def mover not ready yet.
you not need assign variable each piece. using list pieces. instead of writing piece1
, can write pieces[0]
. (you need note lists start index 0.)
range
has exclusive right bound. means not included, range ends 1 less value. want use range(0,12)
.
in python, can add lists dynamically. not need allocate enough spaces fit pieces. can use .append()
method of lists.
one way write code this:
pieces = [] in range(0, 12): # 0-11 if < 5: pieces.append(piece("white", 8, 1 + i*2)) elif < 9: pieces.append(piece("white", 7, 2 + i*2)) else: pieces.append(piece("white", 6, 1 + i*2))
i took liberty of simplifying conditional statements (i
>= 0
, if i < 5
false, inverse, i >= 5
, true, don't need restate in elif
) , getting rid of j
, k
, , m
unnecessary variables , can replaced literals save memory.
one more thing: implementation of getcolor
return function object itself. think wanted do:
def getcolor(): return self.color
Comments
Post a Comment