Advertisement
Guest User

exampleSolver.py

a guest
Apr 19th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. from minesweeper import *
  2.  
  3.  
  4. def exampleSolveGame(grid):
  5.     """
  6.     This is an intentionally crappy AI to solve a game on Minesweeper.  It
  7.     may not play well, but it does illustrate the basics of how to code an AI.
  8.     It'll be easiest if you write your own AI in a function like this one, but
  9.     hopefully it'll be better.
  10.  
  11.     Here's the basic form your function should take.  The input parameter
  12.     `grid' will be a Minefield object (see minesweeper.py for more info):
  13.  
  14.         def myAwesomeSolveGame(grid):
  15.             while not grid.isSolved():
  16.                 <put awesome code here to calculate which row and column coordinates to choose here>
  17.  
  18.                 grid.guess(row, column)
  19.     """
  20.  
  21.     while not grid.isSolved():
  22.         # This dictionary will map the coordinates of a square to
  23.         # an estimate of the probability that the square has a mine
  24.         # on it.  We start with a baseline probability based on
  25.         # how many mines there in total.
  26.         baselineProbability = grid.nMines() / float(grid.nHidden())
  27.         hiddenSquareProbabilities = dict(((r,c), baselineProbability) for r,c in grid.iterHidden())
  28.  
  29.         # For each visible square, check if it has a nonzero number of hidden
  30.         # squares surrounding it.  Use the number of mines surrounding the square
  31.         # as well as the number of curenly hidden squares around to update
  32.         # the estimate of the probability that the square has a mine on it.
  33.         for r,c in grid.iterVisible():
  34.             if grid.nHiddenAround(r,c) > 0:
  35.                 mineProb = grid.nMinesAround(r,c) / float(grid.nHiddenAround(r,c))
  36.                 for r2,c2 in grid.iterNeighbors(r,c):
  37.                     if (r2,c2) in hiddenSquareProbabilities:
  38.                         hiddenSquareProbabilities[(r2,c2)] = max(hiddenSquareProbabilities[(r2,c2)], mineProb)
  39.  
  40.         # Pick the square with the lowest estimated probability.
  41.         availableSquares = [(prob,(r,c)) for (r,c),prob in hiddenSquareProbabilities.iteritems()]
  42.         availableSquares.sort()
  43.         row, column = availableSquares[0][1]
  44.         grid.guess(row, column)
  45.  
  46.  
  47. def main():
  48.     # Set this to True if you want to play a game of Minesweeper with a
  49.     # crappy text interface.  Set this to False if you want to test
  50.     # how good the example AI is (spoiler: not very).
  51.     doYouWantToPlayAGame = False
  52.  
  53.     if doYouWantToPlayAGame:
  54.         playGame(9, 9, 10)
  55.     else:
  56.         testSolver(exampleSolveGame)
  57.  
  58. if __name__ == "__main__":
  59.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement