Generation

generate functionWed, 07 Jun 2023

Dissenyeu la funció disclose tal que, donada una matriu gràfica m1, una matriu de mines m2 i unes coordenades X i Y, retorni la matriu gràfica modificada en funció de: 1. Si la casella no és un ’?’ no realitzem cap canvi a la matriu gràfica. 2. Si la casella és un ’?’, comptarem quantes mines conté. Si no conté cap mina, comptarem quantes mines té al voltant aquesta casella. Si en té zero, la marcarem com a un ’.” i cridarem la funció del següent apartat per desvetllar totes les mines que aquesta casella x i y tingui al seu voltant. Si en té més de zero, tan sols assignarem la quantitat de mines a la casella de la matriu gràfica.

def disclose(m1, m2, x, y): #m1 is the graphic matrix #m2 is the mine matrix #x and y are the coordinates of the position #if the position is not an unknown if m1[x][y] != '?': return m1 #if the position is an unknown else: #count the number of mines that the position has mines = count_mines(m2, x, y) #if the position has no mines if mines == 0: m1[x][y] = '.' #disclose the mines that the position has around disclose_around(m1, m2, x, y) return m1 #if the position has some mines else: m1[x][y] = mines return m1

Questions about programming?Chat with your personal AI assistant