Created
April 14, 2018 10:27
-
-
Save antonio-catalano/bb2e9999b6a5dc4ef75f4db9b8eb9b9a to your computer and use it in GitHub Desktop.
The BomberMan Game
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
''' Here the description of the problem: https://www.hackerrank.com/challenges/bomber-man/problem ''' | |
from random import choice | |
from time import sleep | |
R, C, N = map(int, input().split()) # we take the number of rows, columns and seconds of the loop | |
Lista = [] # we create the grid where each element is randomnly chosen between '.'(free cell) and 'M'(marked bomb) | |
for r in range(R): | |
Lista.append([choice(['.','M']) for c in range(C)]) # M is a marked bomb | |
for r in range(R): # we print the Grid with the initial marked bombs | |
for c in range(C): | |
print(' ',Lista[r][c],' ', end='') | |
print('\n') | |
print() | |
''' We build the function for the detonation algorithm ''' | |
def effect_detonation(matrix, AllBomb): | |
y = len(matrix[0]) | |
L = AllBomb | |
for r in range(len(matrix)): | |
for c in range(len(matrix[r])): | |
try: | |
if matrix[r][c] == 'M': | |
L[r][c] = '.' | |
continue | |
except IndexError: | |
pass | |
try: | |
if matrix[r][c+1] == 'M': | |
L[r][c] = '.' | |
continue | |
except IndexError: | |
pass | |
try: | |
if matrix[r+1][c] == 'M': | |
L[r][c] = '.' | |
continue | |
except IndexError: | |
pass | |
if c-1 >= 0: | |
try: | |
if matrix[r][c-1] == 'M': | |
L[r][c] = '.' | |
except IndexError: | |
pass | |
if r-1 >= 0: | |
try: | |
if matrix[r-1][c] == 'M': | |
L[r][c] = '.' | |
except IndexError: | |
pass | |
return L | |
sleep(1) # first second passed and nothing happened | |
counter = 1 # so we initialize a counter variable with value 1 | |
''' We build a function that repeats the detonation algorithm in a loop cycle according to the problem instructions (steps 3 and 4 of the text) ''' | |
def stateOfSituation(N): | |
global Lista | |
global counter | |
while counter <= N: | |
sleep(1) | |
counter += 1 | |
''' Now starts the third second and the bombMan starts to work....''' | |
print('Second {} terminates: the situation is this: ALL BOMB IN THE GRID\n'.format(counter)) | |
print('/----/\n') | |
AllBomb = [['B' for s in range(C)] for f in range(R)] | |
if counter == N: | |
return AllBomb | |
sleep(1) | |
counter += 1 | |
AfterNseconds = effect_detonation(Lista, AllBomb) | |
print('Second {} terminates: the situation is this:\n'.format(counter)) | |
for r in range(R): | |
for c in range(C): | |
print(' ',AfterNseconds[r][c],' ',end='') | |
print('\n') | |
print('/------/\n\n') | |
AfterNseconds = [['M' if elem[i] == 'B' else elem[i] for i in range(C)] for elem in AfterNseconds] | |
Lista = AfterNseconds | |
if counter == N: | |
return AfterNseconds | |
Final = stateOfSituation(N) | |
Final = [['B' if elem[i] == 'M' else elem[i] for i in range(C)] for elem in Final] | |
print('After {} seconds the situation is this:\n'.format(N)) | |
for r in range(R): | |
for c in range(C): | |
print(' ',Final[r][c],' ', end='') | |
print('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment