'Values in my array change despite being correct 3 lines earlier
I am making a code for a connect 4 game but you can twist a row after your go, I am very early in development and am trying to make the twist element of the game, however after I complete the twist function the values inside the array storing the game data just gives me zeros when I run the twist function the first time I print "GameArrays[y][Height - row]" it gives me the correct values however when I print it again it gives me the incorrect values. It is meant to produce the Y and Rs in the same array position but in the array indexed one more or less depending on the direction chosen
Twist Function
def twistRow(row, direction):
rowArray = []
for x in range(Width):
rowArray.append(GameArrays[x][Height - row])
twistArray = rowArray.copy()
if direction == "L":
firstItem = twistArray.pop(0)
twistArray.append(firstItem)
elif direction == "R":
lastItem = twistArray.pop()
twistArray.insert(0, lastItem)
for y in range(Width):
GameArrays[y][Height - row] = twistArray[y]
print(GameArrays[y][Height - row])
for z in range(Width):
print(GameArrays[z][Height - row])
Code that calls it
check = 0
goTrack = 0
gameFinished = False
while check == 0:
playsFirst = input("Who is going first? R- Red or Y- Yellow\n")
if playsFirst == "R":
print("Ok, Red goes first")
check = 1
elif playsFirst == "Y":
print("Ok, Yellow goes first")
check = 1
goTrack = 1
else:
print("Invalid- Please Enter either R for Red or Y for Yellow")
while not gameFinished:
if goTrack == 0:
columnChosen = input("Red - Which column would you like to play in\n")
playsCounter("R", int(columnChosen))
goTrack += 1
elif goTrack == 1:
columnChosen = input("Yellow - Which column would you like to play in\n")
playsCounter("Y", int(columnChosen))
goTrack -= 1
twistChoice = input("Which row would you like to twist? (Enter 0 for no twist)\n")
if twistChoice == "0":
print("Ok then")
else:
directionChoice = input("Which direction would you like to twist? (Enter L or R)\n")
if directionChoice == "L":
twistRow(int(twistChoice), directionChoice)
elif directionChoice == "R":
twistRow(int(twistChoice), directionChoice)
Plays Counter Function
def playsCounter(colour, column):
counter = 0
if 0 in GameArrays[column - 1]:
playedIn = GameArrays[column - 1].copy()
for x in playedIn:
if x != 0:
break
else:
counter += 1
print(counter)
playedIn[counter - 1] = colour
GameArrays[column - 1] = playedIn
else:
print("Invalid- That Column is full")
playAgain(colour)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
