Untitled
unknown
plain_text
a year ago
3.1 kB
4
Indexable
import random grid = [] offsets = [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 1], [-1, 1], [0, 1], [1, 1],] x_size = input("X scale: ") y_size = input("Y scale: ") mines = input("How many mines do you want? : ") # set up the squares for i_1 in range(0, int(y_size)): y_grid = [] for i_2 in range(0, int(x_size)): x_grid = [1, 0] # open/close state, mine count y_grid.append(x_grid) grid.append(y_grid) def get_square(x, y): if grid[y - 1][x - 1]: return grid[y - 1][x - 1] else: return "none" def set_square(x, y, info): grid[y - 1][x - 1] = info def info_to_string(info): if info[0] == 1: return "[BL]" elif info[0] == 2: return "[FL]" if info[0] == 0: if info[1] == 1: return "[MI]" else: return "TBD" def get_area_value(x, y): area_value = 0 for i in range(0, 8): area_value += get_square(offsets[i][0], offsets[i][1])[1] return area_value def draw_board(board): for y_slice in range(0, int(y_size)): string_row = "" for x_slice in range(0, int(x_size)): if info_to_string(get_square(x_slice, y_slice)) == "TBD": string_row = string_row + "[0" + str(get_area_value(x_slice, y_slice)) + "]" else: string_row = string_row + info_to_string(get_square(x_slice, y_slice)) if not string_row == 0 and not string_row == "0": print(string_row) def if_in_range(x, y, x2, y2): if x2 < x + 1 and x2 > x - 1: if y2 < y + 1 and y2 > y - 1: return True else: return False else: return False def first_move(x, y, board): for i in range(0, int(mines)): mine_picked = False tries = 10 while tries > 0: x_rand, y_rand = random.randint(1, int(x_size)), random.randint(1, int(y_size)) if get_square(x_rand, y_rand): if not if_in_range(x, y, x_rand, y_rand): if get_square(x_rand, y_rand)[1] == 0: set_square(x_rand, y_rand, [get_square(x_rand, y_rand)[0], 1]) tries = 0 if tries > 0: tries -= 1 def open_space(x, y): set_square(x, y, [0, get_square(x, y)[1]]) if get_area_value(x, y) == 0: for i in range(0, 8): if int(x_size) >= x + offsets[i][0] > 0 and int(y_size) >= y + offsets[i][1] > 0: open_space(x + offsets[i][0], y + offsets[i][1]) run = True blank = True # commands while run: draw_board(grid) inp_str = input("(open x y), (flag x y): ") sep = inp_str.split(" ") if sep[0].lower() == "open": open_space(int(sep[1]), int(sep[2])) first_move(int(sep[1]), int(sep[2]), grid) def first_move(x, y, grid): blank == False # elif sep[0].lower() == "flag": input("Game has ended. ")
Editor is loading...
Leave a Comment