Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from engine import *
- from random import randint
- import globals
- def mySolveGame(grid):
- globals.flagsdict = dict(((r,c), False) for r,c in grid.iterSquares())
- #grid.guess(0,0)
- grid.guess(grid.nRows()//2, grid.nCols()//2)
- changed = True
- while not grid.isSolved():
- # Each method sets changed true if it changes the state.
- # This means that once that method finishes the board,
- # all subsequent (harder/riskier) methods will be skipped.
- changed = False
- # For each square, do basic checks:
- #
- # - if the number of adjacent hidden squares
- # equals the mine count of that square,
- # flag all adjacent squares.
- #
- # - if the number of flags around the square
- # equals the mine count of that square,
- # click all adjacent squares.
- for r,c in grid.iterVisible():
- hidden = grid.nHiddenAround(r,c)
- mines = grid.nMinesAround(r,c)
- flags = sum(globals.flagsdict[rN,cN] for (rN,cN) in grid.iterNeighbors(r,c))
- if hidden == mines:
- for rN, cN in grid.iterNeighbors(r,c):
- if grid.isVisible(rN,cN):
- continue
- if globals.flagsdict[rN,cN] == False:
- globals.flagsdict[rN,cN] = True
- changed = True
- if flags == mines:
- for rN, cN in grid.iterNeighbors(r,c):
- if grid.isVisible(rN,cN) or globals.flagsdict[rN,cN]:
- continue
- grid.guess(rN,cN)
- changed = True
- # For each visible square with nonzero mine count, iterate
- # over its visible neighbors with nonzero mine count.
- # For each neighbor, count its hidden un-flagged neighbors
- # which are not adjacent to the original square. (nhidden)
- # Then iterate through neighbors of the original square
- # which are not adjacent to the currently selected neighbor,
- # and count flagged (oflags) and hidden unflagged (ohidden)
- # squares.
- if not changed:
- for r,c in grid.iterVisible():
- mines = grid.nMinesAround(r,c)
- if mines == 0:
- continue
- for rN, cN in grid.iterNeighbors(r,c):
- if not grid.isVisible(rN, cN):
- continue
- minesN = grid.nMinesAround(rN,cN)
- if minesN == 0:
- continue
- dmines = minesN - mines
- nhidden = 0
- ohidden = 0
- oflags = 0
- new_hidden = False
- for rN2, cN2 in grid.iterNeighbors(rN,cN):
- if grid.isVisible(rN2, cN2):
- continue
- if not (rN2 > r + 1 or rN2 < r - 1 or cN2 > c + 1 or cN2 < c - 1):
- continue
- if not globals.flagsdict[rN2,cN2]:
- new_hidden = True
- nhidden += 1
- for rF, cF in grid.iterNeighbors(r,c):
- if not (rF > rN + 1 or rF < rN - 1 or cF > cN + 1 or cF < cN - 1):
- continue
- if globals.flagsdict[rF,cF]:
- oflags += 1
- continue
- if not grid.isVisible(rF,cF):
- ohidden += 1
- if nhidden == dmines + oflags and new_hidden:
- for rN2, cN2 in grid.iterNeighbors(rN,cN):
- if not (rN2 > r + 1 or rN2 < r - 1 or cN2 > c + 1 or cN2 < c - 1):
- continue
- if grid.isVisible(rN2, cN2):
- continue
- if globals.flagsdict[rN2,cN2] == False:
- globals.flagsdict[rN2,cN2] = True
- changed = True
- if (mines - (oflags + ohidden)) == minesN:
- for rN2, cN2 in grid.iterNeighbors(rN,cN):
- if not (rN2 > r + 1 or rN2 < r - 1 or cN2 > c + 1 or cN2 < c - 1):
- continue
- if grid.isVisible(rN2, cN2):
- continue
- grid.guess(rN2,cN2)
- #print("Guessed")
- changed = True
- if not changed:
- baselineProbability = (grid.nMines() - sum(globals.flagsdict.values())) / float(grid.nHidden() - sum(globals.flagsdict.values()))
- hiddenSquareProbabilities = dict(((r,c), baselineProbability) for r,c in grid.iterHidden())
- '''
- for r,c in grid.iterHidden():
- if r == 0 or r == grid.nRows() - 1:
- hiddenSquareProbabilities[(r,c)] *= 0.99
- if c == 0 or c == grid.nCols() - 1:
- hiddenSquareProbabilities[(r,c)] *= 0.99
- '''
- for r,c in grid.iterVisible():
- flags = sum(globals.flagsdict[rN,cN] for (rN,cN) in grid.iterNeighbors(r,c))
- if grid.nHiddenAround(r,c) > flags:
- mineProb = (grid.nMinesAround(r,c) - flags) / float(grid.nHiddenAround(r,c) - flags)
- for r2,c2 in grid.iterNeighbors(r,c):
- if (r2,c2) in hiddenSquareProbabilities:
- hiddenSquareProbabilities[(r2,c2)] = max(hiddenSquareProbabilities[(r2,c2)], mineProb)
- availableSquares = [(prob,(r,c)) for (r,c),prob in hiddenSquareProbabilities.items()]
- availableSquares.sort()
- row, column = availableSquares[0][1]
- i = 0
- while globals.flagsdict[row,column]:
- i += 1
- row, column = availableSquares[i][1]
- changed = True
- #print("Probability")
- grid.guess(row, column)
- if not changed:
- #print("Random")
- grid.guess(randint(0, grid.nRows()-1), randint(0,grid.nCols()-1))
- def main():
- # Set this to True if you want to play a game of Minesweeper with a
- # crappy text interface. Set this to False if you want to test
- # how good the example AI is (spoiler: not very).
- doYouWantToPlayAGame = False
- if doYouWantToPlayAGame:
- playGame(9, 9, 10)
- else:
- testSolver(mySolveGame)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement