Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
3.4 kB
0
Indexable
Never
import random

grid = []
offsets = [[-1, -1], [0, -1], [1, -1], [-1,  0], [1, 0], [-1, 1], [0, 1], [1, 1]]

x_size = int(input("X scale: "))
y_size = int(input("Y scale: "))
mines = int(input("How many mines do you want? : "))

# set up the squares
for i_1 in range(y_size):
    y_grid = []
    for i_2 in range(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 0 <= y < y_size and 0 <= x < x_size:
        return grid[y][x]
    else:
        return None

def set_square(x, y, info):
    if 0 <= y < y_size and 0 <= x < x_size:
        grid[y][x] = 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 "[  ]"

def get_area_value(x, y):
    area_value = 0
    for offset in offsets:
        adj_square = get_square(x + offset[0], y + offset[1])
        if adj_square:
            area_value += adj_square[1]
    return area_value

def draw_board(board):
    for y in range(y_size):
        string_row = ""
        for x in range(x_size):
            square_info = get_square(x, y)
            if square_info[0] == 0 and square_info[1] == 0:
                string_row += "[  ]"
            elif square_info[0] == 0:
                string_row += "[0" + str(get_area_value(x, y)) + "]"
            else:
                string_row += info_to_string(square_info)
        print(string_row)

def if_in_range(x, y, x2, y2):
    return abs(x2 - x) <= 1 and abs(y2 - y) <= 1

def first_move(x, y, board):
    for _ in range(mines):
        while True:
            x_rand, y_rand = random.randint(0, x_size - 1), random.randint(0, y_size - 1)
            if not if_in_range(x, y, x_rand, y_rand) and get_square(x_rand, y_rand)[1] == 0:
                set_square(x_rand, y_rand, [get_square(x_rand, y_rand)[0], 1])
                break

def open_space(x, y):
    if get_square(x, y)[0] != 0:  # If not already opened
        set_square(x, y, [0, get_square(x, y)[1]])
        if get_area_value(x, y) == 0:
            for offset in offsets:
                new_x, new_y = x + offset[0], y + offset[1]
                if 0 <= new_x < x_size and 0 <= new_y < y_size:
                    if get_square(new_x, new_y)[0] != 0:
                        open_space(new_x, new_y)

run = True
first_turn = 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":
        x, y = int(sep[1]), int(sep[2])
        if first_turn:
            first_move(x, y, grid)
            first_turn = False
        open_space(x, y)
    elif sep[0].lower() == "flag":
        x, y = int(sep[1]), int(sep[2])
        if get_square(x, y)[0] == 1:
            set_square(x, y, [2, get_square(x, y)[1]])

    # Check if the game is over
    unopened_squares = sum(row.count([1, 0]) for row in grid)
    if unopened_squares == 0:
        run = False
        print("You've won!")
    elif any(get_square(x, y)[0] == 0 and get_square(x, y)[1] == 1 for y in range(y_size) for x in range(x_size)):
        run = False
        print("You've hit a mine. Game Over!")

input("Game has ended.")
Leave a Comment