I'm trying to create a simple connect-4 game using NumPy's 2D arrays, The idea is to when a player chooses a column, It will replace the largest number in that column with a piece
import numpy as np
R_count = 6
C_count = 7
M1 = ([[0,0,0,0,0,0,0],[1,1,1,1,1,1,1],[2,2,2,2,2,2,2],[3,3,3,3,3,3,3],[4,4,4,4,4,4,4],[5,5,5,5,5,5,5]])
def M2():
mymatrix = np.array(M1)
return mymatrix
Board = M2()
def drop_piece(Board,row,col,piece):
np.max(Board[row][col]) = piece
But when I use the np.max function to replace the number with a piece, the function will give an error:
np.max(Board[row][col]) = piece
^
SyntaxError: cannot assign to function call
Are there any solutions for this or I cannot change the largest number in a column using a function?
CodePudding user response:
The output of np.max is a scalar value, and is not your matrix. You want to use np.argmax. This will output the position of the maximum value (or list of positions, if many).
You can use the following code to implement the desired behavior:
pos=np.argmax(Board[:,col].squeeze())
Board[pos,col]=piece