Null_Cat

minesweeper.png.py (Terminal [Python])

Mar 27th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.65 KB | None | 0 0
  1. # If you want to play this abomination... https://repl.it/@Null_Cat/Bad-Minesweeper
  2. # Part of the Minesweeper Project [https://pastebin.com/Yftb7QWL]
  3.  
  4. import random
  5. import os
  6. import traceback
  7. import placeBoardNumbers as pn
  8. import datetime
  9.  
  10. def clear():
  11.   # definitely not stolen from stack overflow
  12.   os.system('cls' if os.name=='nt' else 'clear')
  13.  
  14. def insertDifficulty():
  15.   inputDifficulty = input("Select a Difficulty\nEasy, Intermediate, Advanced, Expert, Marathon, Custom, or Daily?\n> ")
  16.   return inputDifficulty
  17.  
  18. inputMines = None
  19. inputSeed = ""
  20.  
  21. while inputMines == None:
  22.   inputDifficulty = insertDifficulty().lower()
  23.  
  24.   if inputDifficulty == "easy":
  25.     inputWidth = 8
  26.     inputHeight = 8
  27.     inputMines = 10
  28.   elif inputDifficulty == "intermediate":
  29.     inputWidth = 16
  30.     inputHeight = 16
  31.     inputMines = 40
  32.   elif inputDifficulty == "advanced":
  33.     inputWidth = 16
  34.     inputHeight = 32
  35.     inputMines = 99
  36.   elif inputDifficulty == "expert":
  37.     inputWidth = 24
  38.     inputHeight = 32
  39.     inputMines = 249
  40.   elif inputDifficulty == "marathon":
  41.     inputWidth = 32
  42.     inputHeight = 32
  43.     inputMines = 165
  44.   elif inputDifficulty == "marathon+":
  45.   9  inputWidth = 50
  46.     inputHeight = 50
  47.     inputMines = 512
  48.   elif inputDifficulty == "daily":
  49.     inputWidth = 16
  50.     inputHeight = 16
  51.     inputMines = 40
  52.     inputSeed = str(datetime.datetime.today().year) + str(datetime.datetime.today().month) + str(datetime.datetime.today().day)
  53.   elif inputDifficulty == "custom":
  54.     inputWidth, inputHeight, inputMines = "","",""
  55.     clear()
  56.     while inputWidth == "":
  57.       inputWidth = input("Insert Width\n>")
  58.       try:
  59.         inputWidth = int(inputWidth)
  60.         clear()
  61.         if inputWidth <= 0:
  62.           print("Please insert a valid POSITIVE number.\n")
  63.           inputWidth = ""
  64.       except:
  65.         clear()
  66.         print("Please insert a valid number.\n")
  67.         inputWidth = ""
  68.     while inputHeight == "":
  69.       inputHeight = input("Insert Height\n>")
  70.       try:
  71.         inputHeight = int(inputHeight)
  72.         clear()
  73.         if inputHeight <= 0:
  74.           print("Please insert a valid POSITIVE number.\n")
  75.           inputHeight = ""
  76.       except:
  77.         clear()
  78.         print("Please insert a valid number.\n")
  79.         inputHeight = ""
  80.     while inputMines == "":
  81.       inputMines = input("Insert Mine Count\n>")
  82.       try:
  83.         inputMines = int(inputMines)
  84.         clear()
  85.         if inputMines > inputWidth * inputHeight:
  86.           inputMines = ""
  87.           print("What did Minesweeper do to you for you to put more mines than spaces on the board, huh?\n")
  88.         elif inputMines <= 0:
  89.           print("Please insert a valid POSITIVE number.\n")
  90.           inputMines = ""
  91.       except:
  92.         inputMines = ""
  93.         clear()
  94.         print("Please insert a valid number.\n")
  95.      
  96.   else:
  97.     clear()
  98.     print("Please Insert A Valid Difficulty\n")
  99. clear()
  100.  
  101. if inputSeed == "":
  102.   inputSeed = input("Insert seed (insert nothing to make it random)\n> ")
  103.   if inputSeed == "":
  104.     inputSeed = random.randint(-4000000000, 4000000000)
  105.   clear()
  106.  
  107. inputMode = ""
  108. while inputMode == "":
  109.   inputMode = input("Insert Mode. Insert nothing for Normal.\nNormal (That's it)\n>").lower()
  110.   if inputMode == "":
  111.     inputMode = "normal"
  112. clear()
  113.  
  114. print('\n')
  115. # "omg wat r yuo doin puttin object in teh arrey?!?!?//11"
  116. class Board:
  117.   'Creates the Board'
  118.   def __init__(self, size, contents, seed):
  119.     self.Size = size
  120.     self.Contents = contents
  121.     self.Seed = seed
  122.   def addRow(self, rowToAdd):
  123.     self.Contents.append(rowToAdd)
  124.  
  125. class Space:
  126.   'Creates an Empty Space'
  127.   def __init__(self, position=[0,0], minesAround=0, uncovered = False, flagged=False):
  128.     self.Pos = position
  129.     self.MinesAround = minesAround
  130.     self.Uncovered = uncovered
  131.     self.Flagged = flagged
  132.   def __del__(self):
  133.     self = 0
  134.  
  135. class Mine:
  136.   'Creates A Mine'
  137.   def __init__(self, position=[0,0], uncovered = False, flagged=False):
  138.     self.Pos = position
  139.     self.Uncovered = uncovered
  140.     self.Flagged = flagged
  141. # isinstance(someMine, Mine) -- is the variable/object "someMine" part of the "Mine" class?
  142.  
  143. def createBoard(width, height, mineCount, seed, type="Normal"):
  144.   'Creates a Board. The Numbers around mines will change with the type.'
  145.   newSeed = seed
  146.   random.seed(newSeed)
  147.   newBoard = Board([width, height, mineCount], [], newSeed)
  148.   #Generates the board while placing mines in the process.
  149.   for i in range(height):
  150.     newRow = []
  151.     for j in range(width):
  152.       if random.randint(1, 10) == 1 and mineCount != 0:
  153.         newRow.append(Mine([j,i]))
  154.         mineCount -= 1
  155.       else:
  156.         newRow.append(Space([j,i], 0))
  157.     newBoard.addRow(newRow)
  158.   #Checking if there are not enough mines and placing more.
  159.   while (mineCount != 0):
  160.     for i in range(height):
  161.       for j in range(width):
  162.         if random.randint(1,16) == 1 and mineCount != 0 and isinstance(newBoard.Contents[i][j], Mine) == False:
  163.           del newBoard.Contents[i][j]
  164.           newBoard.Contents[i].insert(j, Mine([j,i]))
  165.           mineCount -= 1
  166.   # Adding the numbers around the mines.
  167.   pn.placeNumbersNormal(newBoard, Mine, Space)
  168.   return newBoard
  169.  
  170. try:
  171.   theBoard = createBoard(inputWidth,inputHeight,inputMines, inputSeed)
  172. except Exception as e:
  173.   print("The board failed to create due to",e)
  174.   print("Seed:",inputSeed)
  175.   print("Difficulty:",inputDifficulty.upper())
  176.   if inputDifficulty == "custom":
  177.     print("Size:",inputWidth,"by",inputHeight,"with",inputMines,"mines.")
  178.   print("Please report this!")
  179.   print("\n\nFull Error:\n")
  180.   print(traceback.format_exc())
  181.   exit()
  182.  
  183. exploded = False
  184. while not exploded:
  185.   print("Seed:",theBoard.Seed)
  186.   print("Board Size:",theBoard.Size[0],"by",theBoard.Size[1],"with",theBoard.Size[2],"mines.")
  187.  
  188.   xPosGuide = "  "
  189.   for i in range(theBoard.Size[1]):
  190.     xPosGuide += str(i)+" "
  191.   print(xPosGuide+"\n")
  192.   try:
  193.     for i in range(theBoard.Size[0]):
  194.       newLinePrint = ""+str(i)+" "
  195.       for j in range(theBoard.Size[1]):
  196.         if isinstance(theBoard.Contents[j][i], Space) and theBoard.Contents[j][i].Uncovered == True:
  197.           newLinePrint += str(theBoard.Contents[j][i].MinesAround)+" "
  198.         elif isinstance(theBoard.Contents[j][i], Mine) and theBoard.Contents[j][i].Uncovered == True:
  199.           newLinePrint += "X "
  200.         elif theBoard.Contents[j][i].Flagged == True:
  201.           newLinePrint += "◼ "
  202.         else:
  203.           newLinePrint += "☐ "
  204.       print(newLinePrint)
  205.     spaceToUncover = input("Uncover or Flag?\nSyntax: <U(ncover):F(lag)> <x> <y>\n>")
  206.     if spaceToUncover[0].lower() == "f":
  207.       print("Flag")
  208.       spaceToUncover = [int(s) for s in spaceToUncover.split() if s.isdigit()]
  209.       try:
  210.         if theBoard.Contents[spaceToUncover[0]][spaceToUncover[1]].Flagged == False:
  211.           theBoard.Contents[spaceToUncover[0]][spaceToUncover[1]].Flagged = True
  212.         else:
  213.           theBoard.Contents[spaceToUncover[0]][spaceToUncover[1]].Flagged = False
  214.       except IndexError:
  215.         pass
  216.     elif spaceToUncover[0].lower() == "u":
  217.       print("Uncover")
  218.       try:
  219.         spaceToUncover = [int(s) for s in spaceToUncover.split() if s.isdigit()]
  220.         if theBoard.Contents[spaceToUncover[0]][spaceToUncover[1]].Flagged == False:
  221.           theBoard.Contents[spaceToUncover[0]][spaceToUncover[1]].Uncovered = True
  222.           if isinstance(theBoard.Contents[spaceToUncover[0]][spaceToUncover[1]], Mine):
  223.             exploded = True
  224.       except IndexError:
  225.         pass
  226.   except IndexError:
  227.     pass
  228.   clear()
  229.  
  230.  
  231. print("""
  232. _____________
  233. |            |
  234. |    ________|
  235. |    |_____
  236. |         |
  237. |    _____|
  238. |    |
  239. |    |
  240. |____|
  241. """)
Advertisement
Add Comment
Please, Sign In to add comment