Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class mines:
- def __init__(self):
- self.array = [[],[]]
- self.board = [[],[]]
- self.covered = 0
- self.mined = 0
- self.flagged = 0
- self.rows = 0
- self.cols = 0
- self.ms = 0
- def setBoards(self, fn):
- self.array = [[],[]]
- boardStarted = 0 # splitting lines into tokens separates the
- for line in fn.readlines(): # board data from the dimensions cleanly
- for token in line.split():
- if token[0] == '.' or token[0] == 'M':
- self.array[1].append(token)
- else:
- self.array[0].append(token)
- def getNextBoard(self):
- self.mined = 0
- self.rows = int(self.array[0].pop())
- self.cols = int(self.array[0].pop())
- self.covered = self.rows * self.cols
- for i in range (0, self.rows):
- lst = []
- y = self.array[1].pop()
- self.board[0].append(list(y))
- for char in y:
- if char == 'M':
- self.ms += 1
- lst.append('C')
- self.board[1].append(lst)
- else:
- self.covered = self.covered - self.mined
- if self.rows == 0:
- return "end_of_file"
- else:
- return self.board
- def resetBoard(self):
- self.covered = self.rows * self.cols
- self.ms = 0
- self.board = [[],[]]
- for i in range (0, self.rows):
- lst = []
- y = self.array[1].pop()
- self.board[0].append(list(y))
- for char in y:
- if char == 'M':
- self.ms += 1
- lst.append('C')
- self.board[1].append(lst)
- else:
- self.covered -= self.ms
- def printBoard(self):
- global f, c, m #adjacent cells
- f = 0 #flagged
- c = 0 #covered
- m = 0 #mined
- for i in self.board[0]:
- l = []
- for j in i:
- l.append(j)
- print(l)
- def checkAdjacent(self, row, col):
- if row == 0:
- if col == 0:
- self.checkAdjDown(row, col)
- self.checkAdjRight(row, col)
- if col == (cols-1):
- self.checkAdjDown(row, col)
- self.checkAdjLeft(row, col)
- else:
- self.checkAdjDown(row, col)
- self.checkAdjLeft(row, col)
- self.checkAdjRight(row, col)
- if row == (self.rows-1):
- if col == 0:
- self.checkAdjUp(row, col)
- self.checkAdjRight(row, col)
- if col == (cols-1):
- self.checkAdjUp(row, col)
- self.checkAdjLeft(row, col)
- else:
- self.checkAdjUp(row, col)
- self.checkAdjLeft(row, col)
- self.checkAdjRight(row, col)
- else:
- if col == 0:
- self.checkAdjUp(row, col)
- self.checkAdjDown(row, col)
- self.checkAdjRight(row, col)
- if col == (self.cols-1):
- self.checkAdjUp(row, col)
- self.checkAdjDown(row, col)
- self.checkAdjLeft(row, col)
- else:
- self.checkAdjUp(row, col)
- self.checkAdjDown(row, col)
- self.checkAdjLeft(row, col)
- self.checkAdjRight(row, col)
- def checkAdjUp(self, row, col):
- if col == (self.cols-1):
- if (self.board[1][row-1][col] == 'C'):
- c = c+1
- if (self.board[1][row-1][col] == 'F'):
- f = f+1
- if (self.board[0][row-1][col] == 'M'):
- m = m+1
- if (self.board[1][row-1][col-1] == 'C'):
- c = c+1
- if (self.board[1][row-1][col-1] == 'F'):
- f = f+1
- if (self.board[0][row-1][col-1] == 'M'):
- m = m+1
- if col == 0:
- if (self.board[1][row-1][col] == 'C'):
- c = c+1
- if (self.board[1][row-1][col] == 'F'):
- f = f+1
- if (self.board[0][row-1][col] == 'M'):
- m = m+1
- if (self.board[1][row-1][col+1] == 'C'):
- c = c+1
- if (self.board[1][row-1][col+1] == 'F'):
- f = f+1
- if (self.board[0][row-1][col+1] == 'M'):
- m = m+1
- else:
- if (self.board[1][row-1][col-1] == 'C'):
- c = c+1
- if (self.board[1][row-1][col-1] == 'F'):
- f = f+1
- if (self.board[0][row][col-1] == 'M'):
- m = m+1
- if (self.board[1][row-1][col] == 'C'):
- c = c+1
- if (self.board[1][row-1][col] == 'F'):
- f = f+1
- if (self.board[0][row-1][col] == 'M'):
- m = m+1
- if (self.board[1][row-1][col+1] == 'C'):
- c = c+1
- if (self.board[1][row-1][col+1] == 'F'):
- f = f+1
- if (self.board[0][row-1][col+1] == 'M'):
- m = m+1
- def checkAdjDown(self, x, y):
- if col == (self.cols-1):
- if (self.board[1][row+1][col] == 'C'):
- c = c+1
- if (self.board[1][row+1][col] == 'F'):
- f = f+1
- if (self.board[0][row+1][col] == 'M'):
- m = m+1
- if (self.board[1][row+1][col-1] == 'C'):
- c = c+1
- if (self.board[1][row+1][col-1] == 'F'):
- f = f+1
- if (self.board[0][row+1][col-1] == 'M'):
- m = m+1
- if col == 0:
- if (self.board[1][row+1][col] == 'C'):
- c = c+1
- if (self.board[1][row+1][col] == 'F'):
- f = f+1
- if (self.board[0][row+1][col] == 'M'):
- m = m+1
- if (self.board[1][row+1][col+1] == 'C'):
- c = c+1
- if (self.board[1][row+1][col+1] == 'F'):
- f = f+1
- if (self.board[0][row+1][col+1] == 'M'):
- m = m+1
- else:
- if (self.board[1][row+1][col-1] == 'C'):
- c = c+1
- if (self.board[1][row+1][col-1] == 'F'):
- f = f+1
- if (self.board[0][row+1][col-1] == 'M'):
- m = m+1
- if (self.board[1][row+1][col] == 'C'):
- c = c+1
- if (self.board[1][row+1][col] == 'F'):
- f = f+1
- if (self.board[0][row+1][col] == 'M'):
- m = m+1
- if (self.board[1][row+1][col+1] == 'C'):
- c = c+1
- if (self.board[1][row+1][col+1] == 'F'):
- f = f+1
- if (self.board[0][row+1][col+1] == 'M'):
- m = m+1
- def checkAdjLeft(self, row, col):
- if (self.board[1][row][col-1] == 'C'):
- c = c+1
- if (self.board[1][row][col-1] == 'F'):
- f = f+1
- if (self.board[0][row][col-1] == 'M'):
- m = m+1
- def checkAdjRight(self, row, col):
- if (self.board[1][row][col+1] == 'C'):
- c = c+1
- if (self.board[1][row][col+1] == 'F'):
- f = f+1
- if (self.board[0][row][col+1] == 'M'):
- m = m+1
- def solveBoard(self):
- for i in self.board[0]:
- for j in i:
- self.checkAdjacent(i, j)
- print(j)
- if f == m:
- self.clearAllCovered(row, col)
- if f + c == m:
- self.flagAllCovered(row, col)
- def clearAllCovered(self, row, col):
- if row == 0:
- if (col == 0):
- self.clearDown(row, col, 'U')
- self.clearRight(row, col, 'U')
- if (col == (self.cols-1)):
- self.clearDown(row, col, 'U')
- self.clearLeft(row, col, 'U')
- else:
- self.clearDown(row, col, 'U')
- self.clearRight(row, col, 'U')
- self.clearLeft(row, col, 'U')
- if row == (rows - 1):
- if (col == 0):
- self.clearUp(row, col, 'U')
- self.clearRight(row, col, 'U')
- if (col == (self.cols-1)):
- self.clearUp(row, col, 'U')
- self.clearLeft(row, col, 'U')
- else:
- self.clearUp(row, col, 'U')
- self.clearLeft(row, col, 'U')
- self.clearRight(row, col, 'U')
- else:
- if (col == 0):
- self.clearRight(row, col, 'U')
- self.clearUp(row, col, 'U')
- self.clearDown(row, col, 'U')
- if (col == (self.cols-1)):
- self.clearLeft(row, col, 'U')
- self.clearUp(row, col, 'U')
- self.clearDown(row, col, 'U')
- else:
- self.clearUp(row, col, 'U')
- self.clearDown(row, col, 'U')
- self.clearLeft(row, col, 'U')
- self.clearRight(row, col, 'U')
- def flagAllCovered(self, row, col):
- if row == 0:
- if (col == 0):
- self.clearDown(row, col, 'F')
- self.clearRight(row, col, 'F')
- if (col == (cols-1)):
- self.clearDown(row, col, 'F')
- self.clearLeft(row, col, 'F')
- else:
- self.clearDown(row, col, 'F')
- self.clearRight(row, col, 'F')
- self.clearLeft(row, col, 'F')
- if row == (self.rows - 1):
- if (col == 0):
- self.clearUp(row, col, 'F')
- self.clearRight(row, col, 'F')
- if (col == (cols-1)):
- self.clearUp(row, col, 'F')
- self.clearLeft(row, col, 'F')
- else:
- self.clearUp(row, col, 'F')
- self.clearLeft(row, col, 'F')
- self.clearRight(row, col, 'F')
- else:
- if (col == 0):
- self.clearRight(row, col, 'F')
- self.clearUp(row, col, 'F')
- self.clearDown(row, col, 'F')
- if (col == (cols-1)):
- self.clearLeft(row, col, 'F')
- self.clearUp(row, col, 'F')
- self.clearDown(row, col, 'F')
- else:
- self.clearUp(row, col, 'F')
- self.clearDown(row, col, 'F')
- self.clearLeft(row, col, 'F')
- self.clearRight(row, col, 'F')
- def changeUp(self, row, col, char):
- if col == 0:
- if self.board[1][row-1][col] == 'C':
- self.board[1][row-1][col] = char
- if self.board[1][row-1][col+1] == 'C':
- self.board[1][row-1][col+1] = char
- if col == (self.cols-1):
- if self.board[1][row-1][col] == 'C':
- self.board[1][row-1][col] = char
- if self.board[1][row-1][col-1] == 'C':
- self.board[1][row-1][col-1] = char
- else:
- if self.board[1][row-1][col] == 'C':
- self.board[1][row-1][col] = char
- if self.board[1][row-1][col-1] == 'C':
- self.board[1][row-1][col-1] = char
- if self.board[1][row-1][col+1] == 'C':
- self.board[1][row-1][col+1] = char
- def changeDown(self, row, col, char):
- if col == 0:
- if self.board[1][row-1][col] == 'C':
- self.board[1][row+1][col] = char
- if self.board[1][row-1][col+1] == 'C':
- self.board[1][row+1][col+1] = char
- if col == (self.cols-1):
- if self.board[1][row-1][col] == 'C':
- self.board[1][row+1][col] = char
- if self.board[1][row-1][col-1] == 'C':
- self.board[1][row+1][col-1] = char
- else:
- if self.board[1][row-1][col] == 'C':
- self.board[1][row-1][col] = char
- if self.board[1][row-1][col-1] == 'C':
- self.board[1][row-1][col-1] = char
- if self.board[1][row-1][col+1] == 'C':
- self.board[1][row-1][col+1] = char
- def changeLeft(self, row, col, char):
- if self.board[row][col-1] == 'C':
- self.board[row][col-1] = char
- def changeRight(self, row, col, char):
- if self.board[row][col+1] == 'C':
- self.board[row][col+1] = char
- def main(self):
- f = open(r'mines.in', "r")
- g = open(r'mines.out', "w")
- self.setBoards(f)
- self.array[0].reverse()
- self.array[1].reverse()
- x = self.getNextBoard()
- self.resetBoard()
- print("Rows: " + str(self.rows) + " Cols: "+str(self.cols))
- print("Covered: " + str(self.covered) + " Mines: " + str(self.ms))
- print x
- x.printBoard()
- x.solveBoard()
- f.close()
- mx = mines()
- mx.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement