Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Minesweeper in Discord using the new /spoiler feature
- # Copy/paste the output from the .txt file into your chat!
- from noise import pnoise2
- '''
- I've been told that the noise package doesn't always work, if this is the case please replace:
- 'pnoise2(x / scale + offset, y / scale + offset, octaves=octaves)'
- with:
- '0.5'
- On line 18
- '''
- import random
- import numpy as np
- import matplotlib.pyplot as plt
- def create_map(width, height, scale, octaves):
- offset = random.random() * 100000
- map = np.array([[pnoise2(x / scale + offset, y / scale + offset, octaves=octaves) for x in range(width)] for y in range(height)])
- for y in range(height):
- for x in range(width):
- if map[x][y] > .2:
- map[x][y] = 0
- else:
- map[x][y] = .5
- return map
- def add_tile(map, x, y):
- try:
- map[x][y] += 1
- except IndexError:
- pass
- return map
- def add_up_tiles(map, x, y):
- """
- Add 1 to every square in the following shape:
- |1|1|1|
- |1|0|1|
- |1|1|1|
- """
- map = add_tile(map, x + 1, y + 1)
- map = add_tile(map, x, y + 1)
- map = add_tile(map, x - 1, y + 1)
- map = add_tile(map, x - 1, y)
- map = add_tile(map, x - 1, y - 1)
- map = add_tile(map, x, y - 1)
- map = add_tile(map, x + 1, y - 1)
- map = add_tile(map, x + 1, y)
- return map
- def place_bombs(map, n_bombs):
- print('Placing Bombs')
- bombs_placed = 0
- while bombs_placed < n_bombs:
- x = random.randint(0, map.shape[0] - 1)
- y = random.randint(0, map.shape[1] - 1)
- if map[x][y] == .5:
- print('Placing At ({0}, {1})'.format(x, y))
- bombs_placed += 1
- map[x][y] = 9
- map = add_up_tiles(map, x, y)
- return map
- num_to_text = {0: '||:black_large_square:||',
- 1: '||:one:||',
- 2: '||:two:||',
- 3: '||:three:||',
- 4: '||:four:||',
- 5: '||:five:||',
- 6: '||:six:||',
- 7: '||:seven:||',
- 8: '||:eight:||',
- 9: '||:bomb:||',
- 10: '||:bomb:||'}
- def integer_convert(map):
- for y in range(map.shape[1]):
- for x in range(map.shape[0]):
- map[x][y] = int(map[x][y])
- return map
- def convert_to_text(map, file_name):
- f = open(file_name, 'w')
- for y in range(map.shape[1]):
- for x in range(map.shape[0]):
- f.write(num_to_text[map[x][y]])
- f.write('\n')
- f.close()
- return text_map
- def show_map(map, res):
- fig, ax = plt.subplots()
- im = ax.pcolormesh(range(res + 1), range(res + 1), map, cmap='viridis')
- plt.show()
- def main():
- res = 10
- scale = int(res / 2)
- map = create_map(res, res, scale, 1)
- print(map.shape)
- print('Created Map')
- map = place_bombs(map, res)
- map = integer_convert(map)
- convert_to_text(map, 'text_map.txt')
- # show_map(map, res)
- # Uncomment the line above to show what the generated map would look like
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement