minesweeper.py (1645B)
1 import random 2 grid = [ 3 [0,0,0,0,0,0], 4 [0,0,0,0,0,0], 5 [0,0,0,0,0,0], 6 [0,0,0,0,0,0], 7 [0,0,0,0,0,0], 8 [0,0,0,0,0,0], 9 ] 10 11 def getValidPos(text): 12 inp = -1 13 while inp < 1 or inp > 6: 14 inp = int(input(text)) 15 return inp 16 17 def printGrid(): 18 for i in grid: 19 for j in i: 20 if j == "B": 21 print("0", end = " ") 22 else: 23 print(j, end = " ") 24 print() 25 26 def placeBombs(amount): 27 for i in range(amount): 28 grid[random.randint(0,5)][random.randint(0,5)] = "B" 29 30 def isMineNear(row, collumn): 31 row = row - 1 32 collumn = collumn - 1 33 try: 34 if grid[row][collumn+1] == "B": 35 print("a bomb is near") 36 elif grid[row][collumn-1] == "B": 37 print("a bomb is near") 38 elif grid[row+1][collumn] == "B": 39 print("a bomb is near") 40 elif grid[row-1][collumn] == "B": 41 print("a bomb is near") 42 elif grid[row+1][collumn+1] == "B": 43 print("a bomb is near") 44 elif grid[row-1][collumn-1] == "B": 45 print("a bomb is near") 46 elif grid[row-1][collumn+1] == "B": 47 print("a bomb is near") 48 elif grid[row+1][collumn-1] == "B": 49 print("a bomb is near") 50 except: 51 pass 52 53 54 placeBombs(10) 55 56 score = 0 57 while True: 58 printGrid() 59 row = getValidPos("row: ") 60 collumn = getValidPos("collumn: ") 61 if grid[row-1][collumn-1] == "B": 62 print("you blew up, your score was", score) 63 break 64 grid[row-1][collumn-1] = "G" 65 score = score + 1 66 isMineNear(row, collumn)