Advertisement
101prairiedogs

Minesweeper in Discord

Feb 1st, 2019
1,519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. # Minesweeper in Discord using the new /spoiler feature
  2. # Copy/paste the output from the .txt file into your chat!
  3. from noise import pnoise2
  4. '''
  5. I've been told that the noise package doesn't always work, if this is the case please replace:
  6.     'pnoise2(x / scale + offset, y / scale + offset, octaves=octaves)'
  7. with:
  8.     '0.5'
  9. On line 18
  10. '''
  11. import random
  12. import numpy as np
  13. import matplotlib.pyplot as plt
  14.  
  15.  
  16. def create_map(width, height, scale, octaves):
  17.     offset = random.random() * 100000
  18.     map = np.array([[pnoise2(x / scale + offset, y / scale + offset, octaves=octaves) for x in range(width)] for y in range(height)])
  19.     for y in range(height):
  20.         for x in range(width):
  21.             if map[x][y] > .2:
  22.                 map[x][y] = 0
  23.             else:
  24.                 map[x][y] = .5
  25.     return map
  26.  
  27.  
  28. def add_tile(map, x, y):
  29.     try:
  30.         map[x][y] += 1
  31.     except IndexError:
  32.         pass
  33.     return map
  34.  
  35.  
  36. def add_up_tiles(map, x, y):
  37.     """
  38.    Add 1 to every square in the following shape:
  39.    |1|1|1|
  40.    |1|0|1|
  41.    |1|1|1|
  42.    """
  43.     map = add_tile(map, x + 1, y + 1)
  44.     map = add_tile(map, x, y + 1)
  45.     map = add_tile(map, x - 1, y + 1)
  46.     map = add_tile(map, x - 1, y)
  47.     map = add_tile(map, x - 1, y - 1)
  48.     map = add_tile(map, x, y - 1)
  49.     map = add_tile(map, x + 1, y - 1)
  50.     map = add_tile(map, x + 1, y)
  51.     return map
  52.  
  53.  
  54. def place_bombs(map, n_bombs):
  55.     print('Placing Bombs')
  56.     bombs_placed = 0
  57.     while bombs_placed < n_bombs:
  58.         x = random.randint(0, map.shape[0] - 1)
  59.         y = random.randint(0, map.shape[1] - 1)
  60.         if map[x][y] == .5:
  61.             print('Placing At ({0}, {1})'.format(x, y))
  62.             bombs_placed += 1
  63.             map[x][y] = 9
  64.             map = add_up_tiles(map, x, y)
  65.     return map
  66.  
  67.  
  68. num_to_text = {0: '||:black_large_square:||',
  69.                1: '||:one:||',
  70.                2: '||:two:||',
  71.                3: '||:three:||',
  72.                4: '||:four:||',
  73.                5: '||:five:||',
  74.                6: '||:six:||',
  75.                7: '||:seven:||',
  76.                8: '||:eight:||',
  77.                9: '||:bomb:||',
  78.                10: '||:bomb:||'}
  79.  
  80.  
  81. def integer_convert(map):
  82.     for y in range(map.shape[1]):
  83.         for x in range(map.shape[0]):
  84.             map[x][y] = int(map[x][y])
  85.     return map
  86.  
  87.  
  88. def convert_to_text(map, file_name):
  89.     f = open(file_name, 'w')
  90.     for y in range(map.shape[1]):
  91.         for x in range(map.shape[0]):
  92.             f.write(num_to_text[map[x][y]])
  93.         f.write('\n')
  94.     f.close()
  95.     return text_map
  96.  
  97.  
  98. def show_map(map, res):
  99.     fig, ax = plt.subplots()
  100.     im = ax.pcolormesh(range(res + 1), range(res + 1), map, cmap='viridis')
  101.     plt.show()
  102.  
  103.  
  104. def main():
  105.     res = 10
  106.     scale = int(res / 2)
  107.     map = create_map(res, res, scale, 1)
  108.     print(map.shape)
  109.     print('Created Map')
  110.     map = place_bombs(map, res)
  111.     map = integer_convert(map)
  112.     convert_to_text(map, 'text_map.txt')
  113.     # show_map(map, res)
  114.     # Uncomment the line above to show what the generated map would look like
  115.  
  116.  
  117. if __name__ == '__main__':
  118.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement