I am trying to create a program for the game Connect Four where the board can be anysize. The user is prompted for the number of rows and numbers of columns which through some code creates a 2d list of 0 open spot. So for a 3 column by 4 row board
blankBoard = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
I am trying to create a function that inputs, the list, and column to drop the play piece into, represented by an X... so for example if you chose column 3, the updated board list will appear as such
blankBoard = [[0,0,0],[0,0,0],[0,0,X],[0,0,0]]
My function is suppose to take the input for the column, and find the index inside that column list which is the last one which is blank. My problem is maining deleting and replacing that specific index with out changing all the lists.
def move(blankBoard, column):
newBoard = blankBoard[:]
col = column - 1 # The first column in connect four is 1, not 0
index = -1
i = 0
while i < len(newBoard[col]):
if newBoard[col][i] == 0:
index = index + 1
i = i + 1
newBoard[col][index] = "X" # This line of code I think is bad
print(newBoard)
return ""
with the following output...
newBoard = [[0,0,X],[0,0,X],[0,0,X],[0,0,X]]
but as I said I only want the X to be in the sublist denoted by "col" Why is it looping through all the columns (sublists) in the list if the code line to change the specifc index of the sublist is not in the while loop?