Last active
October 7, 2015 01:38
-
-
Save zokis/3084984 to your computer and use it in GitHub Desktop.
Campo Minado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CampoMinado: | |
def __init__(self, linha, coluna): | |
self.linha = linha | |
self.coluna = coluna | |
self.matriz = [[0 for i in range(self.coluna)] for x in range(self.linha)] | |
def insere_bomba(self, linha, coluna): | |
if self._celula_existe(linha,coluna): | |
self.matriz[linha][coluna] = -1 | |
else: | |
return False | |
vizinhos = [(linha-1,coluna),(linha+1,coluna),(linha,coluna+1),(linha,coluna-1),(linha-1,coluna-1),(linha+1,coluna+1),(linha-1,coluna+1),(linha+1,coluna-1)] | |
for i in vizinhos: | |
if self._celula_existe(i[0],i[1]): | |
if not self._e_bomba(i[0],i[1]): | |
self.matriz[i[0]][i[1]]+=1 | |
return True | |
def _e_bomba(self, linha, coluna): | |
return self.matriz[linha][coluna] == -1 | |
def _celula_existe(self, linha, coluna): | |
return (linha<self.linha and coluna<self.coluna) and (linha > -1 and coluna > -1) | |
def mostrar(self): | |
for linhas in self.matriz: | |
for coluna in linhas: | |
if coluna == -1: | |
print('*', end='') | |
else: | |
print(coluna, end='') | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment