Advertisement
SafariMonkey

mine.py

Apr 21st, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.68 KB | None | 0 0
  1. from engine import *
  2. from random import randint
  3. import globals
  4.  
  5. def mySolveGame(grid):
  6.  
  7.     globals.flagsdict = dict(((r,c), False) for r,c in grid.iterSquares())
  8.     #grid.guess(0,0)
  9.     grid.guess(grid.nRows()//2, grid.nCols()//2)
  10.     changed = True
  11.     while not grid.isSolved():
  12.  
  13.         # Each method sets changed true if it changes the state.
  14.         # This means that once that method finishes the board,
  15.         # all subsequent (harder/riskier) methods will be skipped.
  16.         changed = False
  17.  
  18.         # For each square, do basic checks:
  19.         #
  20.         #   - if the number of adjacent hidden squares
  21.         #     equals the mine count of that square,
  22.         #     flag all adjacent squares.
  23.         #
  24.         #   - if the number of flags around the square
  25.         #     equals the mine count of that square,
  26.         #     click all adjacent squares.
  27.  
  28.         for r,c in grid.iterVisible():
  29.             hidden = grid.nHiddenAround(r,c)
  30.             mines = grid.nMinesAround(r,c)
  31.             flags = sum(globals.flagsdict[rN,cN] for (rN,cN) in grid.iterNeighbors(r,c))
  32.             if hidden == mines:
  33.                 for rN, cN in grid.iterNeighbors(r,c):
  34.                     if grid.isVisible(rN,cN):
  35.                         continue
  36.                     if globals.flagsdict[rN,cN] == False:
  37.                         globals.flagsdict[rN,cN] = True
  38.                         changed = True
  39.             if flags == mines:
  40.                 for rN, cN in grid.iterNeighbors(r,c):
  41.                     if grid.isVisible(rN,cN) or globals.flagsdict[rN,cN]:
  42.                         continue
  43.                     grid.guess(rN,cN)
  44.                     changed = True
  45.  
  46.         # For each visible square with nonzero mine count, iterate
  47.         # over its visible neighbors with nonzero mine count.
  48.         # For each neighbor, count its hidden un-flagged neighbors
  49.         # which are not adjacent to the original square. (nhidden)
  50.         # Then iterate through neighbors of the original square
  51.         # which are not adjacent to the currently selected neighbor,
  52.         # and count flagged (oflags) and hidden unflagged (ohidden)
  53.         # squares.
  54.  
  55.         if not changed:
  56.             for r,c in grid.iterVisible():
  57.                 mines = grid.nMinesAround(r,c)
  58.                 if mines == 0:
  59.                     continue
  60.                 for rN, cN in grid.iterNeighbors(r,c):
  61.                     if not grid.isVisible(rN, cN):
  62.                         continue
  63.                     minesN = grid.nMinesAround(rN,cN)
  64.                     if minesN == 0:
  65.                         continue
  66.                     dmines = minesN - mines
  67.                     nhidden = 0
  68.                     ohidden = 0
  69.                     oflags = 0
  70.                     new_hidden = False
  71.                     for rN2, cN2 in grid.iterNeighbors(rN,cN):
  72.                         if grid.isVisible(rN2, cN2):
  73.                             continue
  74.                         if not (rN2 > r + 1 or rN2 < r - 1 or cN2 > c + 1 or cN2 < c - 1):
  75.                             continue
  76.                         if not globals.flagsdict[rN2,cN2]:
  77.                             new_hidden = True
  78.                         nhidden += 1
  79.                     for rF, cF in grid.iterNeighbors(r,c):
  80.                         if not (rF > rN + 1 or rF < rN - 1 or cF > cN + 1 or cF < cN - 1):
  81.                             continue
  82.                         if globals.flagsdict[rF,cF]:
  83.                             oflags += 1
  84.                             continue
  85.                         if not grid.isVisible(rF,cF):
  86.                             ohidden += 1
  87.  
  88.  
  89.                     if nhidden == dmines + oflags and new_hidden:
  90.                         for rN2, cN2 in grid.iterNeighbors(rN,cN):
  91.                             if not (rN2 > r + 1 or rN2 < r - 1 or cN2 > c + 1 or cN2 < c - 1):
  92.                                 continue
  93.                             if grid.isVisible(rN2, cN2):
  94.                                 continue
  95.                             if globals.flagsdict[rN2,cN2] == False:
  96.                                 globals.flagsdict[rN2,cN2] = True
  97.                             changed = True
  98.  
  99.                     if (mines - (oflags + ohidden)) == minesN:
  100.                         for rN2, cN2 in grid.iterNeighbors(rN,cN):
  101.                             if not (rN2 > r + 1 or rN2 < r - 1 or cN2 > c + 1 or cN2 < c - 1):
  102.                                 continue
  103.                             if grid.isVisible(rN2, cN2):
  104.                                 continue
  105.                             grid.guess(rN2,cN2)
  106.                             #print("Guessed")
  107.                             changed = True
  108.  
  109.         if not changed:
  110.             baselineProbability = (grid.nMines() - sum(globals.flagsdict.values())) / float(grid.nHidden() - sum(globals.flagsdict.values()))
  111.             hiddenSquareProbabilities = dict(((r,c), baselineProbability) for r,c in grid.iterHidden())
  112.             '''
  113.            for r,c in grid.iterHidden():
  114.                if r == 0 or r == grid.nRows() - 1:
  115.                    hiddenSquareProbabilities[(r,c)] *= 0.99
  116.                if c == 0 or c == grid.nCols() - 1:
  117.                    hiddenSquareProbabilities[(r,c)] *= 0.99
  118.            '''
  119.             for r,c in grid.iterVisible():
  120.                 flags = sum(globals.flagsdict[rN,cN] for (rN,cN) in grid.iterNeighbors(r,c))
  121.                 if grid.nHiddenAround(r,c) > flags:
  122.                     mineProb = (grid.nMinesAround(r,c) - flags) / float(grid.nHiddenAround(r,c) - flags)
  123.                     for r2,c2 in grid.iterNeighbors(r,c):
  124.                         if (r2,c2) in hiddenSquareProbabilities:
  125.                             hiddenSquareProbabilities[(r2,c2)] = max(hiddenSquareProbabilities[(r2,c2)], mineProb)
  126.             availableSquares = [(prob,(r,c)) for (r,c),prob in hiddenSquareProbabilities.items()]
  127.             availableSquares.sort()
  128.             row, column = availableSquares[0][1]
  129.             i = 0
  130.             while globals.flagsdict[row,column]:
  131.                 i += 1
  132.                 row, column = availableSquares[i][1]
  133.             changed = True
  134.             #print("Probability")
  135.             grid.guess(row, column)
  136.         if not changed:
  137.             #print("Random")
  138.             grid.guess(randint(0, grid.nRows()-1), randint(0,grid.nCols()-1))
  139.  
  140.  
  141.  
  142.  
  143.  
  144. def main():
  145.     # Set this to True if you want to play a game of Minesweeper with a
  146.     # crappy text interface.  Set this to False if you want to test
  147.     # how good the example AI is (spoiler: not very).
  148.     doYouWantToPlayAGame = False
  149.  
  150.     if doYouWantToPlayAGame:
  151.         playGame(9, 9, 10)
  152.     else:
  153.         testSolver(mySolveGame)
  154.  
  155. if __name__ == "__main__":
  156.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement