Advertisement
Atheuz

Untitled

May 5th, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.09 KB | None | 0 0
  1. class mines:
  2.     def __init__(self):
  3.         self.array = [[],[]]
  4.         self.board = [[],[]]
  5.         self.covered = 0
  6.         self.mined = 0
  7.         self.flagged = 0
  8.         self.rows = 0
  9.         self.cols = 0
  10.         self.ms = 0
  11.  
  12.     def setBoards(self, fn):
  13.         self.array = [[],[]]
  14.         boardStarted = 0                        # splitting lines into tokens separates the
  15.         for line in fn.readlines():          # board data from the dimensions cleanly
  16.             for token in line.split():
  17.                 if token[0] == '.' or token[0] == 'M':
  18.                     self.array[1].append(token)
  19.                 else:
  20.                     self.array[0].append(token)
  21.  
  22.  
  23.     def getNextBoard(self):
  24.         self.mined = 0
  25.         self.rows = int(self.array[0].pop())
  26.         self.cols = int(self.array[0].pop())
  27.         self.covered = self.rows * self.cols
  28.         for i in range (0, self.rows):
  29.             lst = []
  30.             y = self.array[1].pop()
  31.             self.board[0].append(list(y))
  32.             for char in y:
  33.                 if char == 'M':
  34.                     self.ms += 1
  35.                     lst.append('C')
  36.                 self.board[1].append(lst)
  37.         else:
  38.                 self.covered = self.covered - self.mined
  39.         if self.rows == 0:
  40.             return "end_of_file"
  41.         else:
  42.             return self.board
  43.  
  44.     def resetBoard(self):
  45.         self.covered = self.rows * self.cols
  46.         self.ms = 0
  47.         self.board = [[],[]]
  48.         for i in range (0, self.rows):
  49.                 lst = []
  50.                 y = self.array[1].pop()
  51.                 self.board[0].append(list(y))
  52.                 for char in y:
  53.                     if char == 'M':
  54.                         self.ms += 1
  55.                     lst.append('C')
  56.                 self.board[1].append(lst)
  57.         else:
  58.                 self.covered -= self.ms
  59.     def printBoard(self):
  60.         global f, c, m    #adjacent cells
  61.         f = 0             #flagged
  62.         c = 0             #covered
  63.         m = 0             #mined
  64.         for i in self.board[0]:
  65.             l = []
  66.             for j in i:
  67.                 l.append(j)
  68.             print(l)
  69.  
  70.     def checkAdjacent(self, row, col):
  71.         if row == 0:
  72.             if col == 0:
  73.                 self.checkAdjDown(row, col)
  74.                 self.checkAdjRight(row, col)
  75.             if col == (cols-1):
  76.                 self.checkAdjDown(row, col)
  77.                 self.checkAdjLeft(row, col)
  78.             else:
  79.                 self.checkAdjDown(row, col)
  80.                 self.checkAdjLeft(row, col)
  81.                 self.checkAdjRight(row, col)
  82.         if row == (self.rows-1):
  83.             if col == 0:
  84.                 self.checkAdjUp(row, col)
  85.                 self.checkAdjRight(row, col)
  86.             if col == (cols-1):
  87.                 self.checkAdjUp(row, col)
  88.                 self.checkAdjLeft(row, col)
  89.             else:
  90.                 self.checkAdjUp(row, col)
  91.                 self.checkAdjLeft(row, col)
  92.                 self.checkAdjRight(row, col)
  93.         else:
  94.             if col == 0:
  95.                 self.checkAdjUp(row, col)
  96.                 self.checkAdjDown(row, col)
  97.                 self.checkAdjRight(row, col)
  98.             if col == (self.cols-1):
  99.                 self.checkAdjUp(row, col)
  100.                 self.checkAdjDown(row, col)
  101.                 self.checkAdjLeft(row, col)
  102.             else:
  103.                 self.checkAdjUp(row, col)
  104.                 self.checkAdjDown(row, col)
  105.                 self.checkAdjLeft(row, col)
  106.                 self.checkAdjRight(row, col)
  107.  
  108.     def checkAdjUp(self, row, col):
  109.         if col == (self.cols-1):
  110.             if (self.board[1][row-1][col] == 'C'):
  111.                 c = c+1
  112.             if (self.board[1][row-1][col] == 'F'):
  113.                 f = f+1
  114.             if (self.board[0][row-1][col] == 'M'):
  115.                 m = m+1
  116.  
  117.             if (self.board[1][row-1][col-1] == 'C'):
  118.                 c = c+1
  119.             if (self.board[1][row-1][col-1] == 'F'):
  120.                 f = f+1
  121.             if (self.board[0][row-1][col-1] == 'M'):
  122.                 m = m+1
  123.         if col == 0:
  124.             if (self.board[1][row-1][col] == 'C'):
  125.                 c = c+1
  126.             if (self.board[1][row-1][col] == 'F'):
  127.                 f = f+1
  128.             if (self.board[0][row-1][col] == 'M'):
  129.                 m = m+1
  130.  
  131.             if (self.board[1][row-1][col+1] == 'C'):
  132.                 c = c+1
  133.             if (self.board[1][row-1][col+1] == 'F'):
  134.                 f = f+1
  135.             if (self.board[0][row-1][col+1] == 'M'):
  136.                 m = m+1
  137.         else:
  138.             if (self.board[1][row-1][col-1] == 'C'):
  139.                 c = c+1
  140.             if (self.board[1][row-1][col-1] == 'F'):
  141.                 f = f+1
  142.             if (self.board[0][row][col-1] == 'M'):
  143.                 m = m+1
  144.  
  145.             if (self.board[1][row-1][col] == 'C'):
  146.                 c = c+1
  147.             if (self.board[1][row-1][col] == 'F'):
  148.                 f = f+1
  149.             if (self.board[0][row-1][col] == 'M'):
  150.                 m = m+1
  151.  
  152.             if (self.board[1][row-1][col+1] == 'C'):
  153.                 c = c+1
  154.             if (self.board[1][row-1][col+1] == 'F'):
  155.                 f = f+1
  156.             if (self.board[0][row-1][col+1] == 'M'):
  157.                 m = m+1
  158.  
  159.  
  160.     def checkAdjDown(self, x, y):
  161.          if col == (self.cols-1):
  162.             if (self.board[1][row+1][col] == 'C'):
  163.                 c = c+1
  164.             if (self.board[1][row+1][col] == 'F'):
  165.                 f = f+1
  166.             if (self.board[0][row+1][col] == 'M'):
  167.                 m = m+1
  168.  
  169.             if (self.board[1][row+1][col-1] == 'C'):
  170.                 c = c+1
  171.             if (self.board[1][row+1][col-1] == 'F'):
  172.                 f = f+1
  173.             if (self.board[0][row+1][col-1] == 'M'):
  174.                 m = m+1
  175.          if  col == 0:
  176.             if (self.board[1][row+1][col] == 'C'):
  177.                 c = c+1
  178.             if (self.board[1][row+1][col] == 'F'):
  179.                 f = f+1
  180.             if (self.board[0][row+1][col] == 'M'):
  181.                 m = m+1
  182.  
  183.             if (self.board[1][row+1][col+1] == 'C'):
  184.                 c = c+1
  185.             if (self.board[1][row+1][col+1] == 'F'):
  186.                 f = f+1
  187.             if (self.board[0][row+1][col+1] == 'M'):
  188.                 m = m+1
  189.          else:
  190.             if (self.board[1][row+1][col-1] == 'C'):
  191.                 c = c+1
  192.             if (self.board[1][row+1][col-1] == 'F'):
  193.                 f = f+1
  194.             if (self.board[0][row+1][col-1] == 'M'):
  195.                 m = m+1
  196.  
  197.             if (self.board[1][row+1][col] == 'C'):
  198.                 c = c+1
  199.             if (self.board[1][row+1][col] == 'F'):
  200.                 f = f+1
  201.             if (self.board[0][row+1][col] == 'M'):
  202.                 m = m+1
  203.  
  204.             if (self.board[1][row+1][col+1] == 'C'):
  205.                 c = c+1
  206.             if (self.board[1][row+1][col+1] == 'F'):
  207.                 f = f+1
  208.             if (self.board[0][row+1][col+1] == 'M'):
  209.                 m = m+1
  210.  
  211.  
  212.     def checkAdjLeft(self, row, col):
  213.         if (self.board[1][row][col-1] == 'C'):
  214.             c = c+1
  215.         if (self.board[1][row][col-1] == 'F'):
  216.             f = f+1
  217.         if (self.board[0][row][col-1] == 'M'):
  218.             m = m+1
  219.     def checkAdjRight(self, row, col):
  220.         if (self.board[1][row][col+1] == 'C'):
  221.             c = c+1
  222.         if (self.board[1][row][col+1] == 'F'):
  223.             f = f+1
  224.         if (self.board[0][row][col+1] == 'M'):
  225.             m = m+1
  226.  
  227.  
  228.     def solveBoard(self):
  229.         for i in self.board[0]:
  230.             for j in i:
  231.                 self.checkAdjacent(i, j)
  232.                 print(j)
  233.                 if f == m:
  234.                     self.clearAllCovered(row, col)
  235.                 if f + c == m:
  236.                     self.flagAllCovered(row, col)
  237.  
  238.  
  239.     def clearAllCovered(self, row, col):
  240.         if row == 0:
  241.             if (col == 0):
  242.                 self.clearDown(row, col, 'U')
  243.                 self.clearRight(row, col, 'U')
  244.             if (col == (self.cols-1)):
  245.                 self.clearDown(row, col, 'U')
  246.                 self.clearLeft(row, col, 'U')
  247.             else:
  248.                 self.clearDown(row, col, 'U')
  249.                 self.clearRight(row, col, 'U')
  250.                 self.clearLeft(row, col, 'U')
  251.  
  252.         if row == (rows - 1):
  253.             if (col == 0):
  254.                 self.clearUp(row, col, 'U')
  255.                 self.clearRight(row, col, 'U')
  256.             if (col == (self.cols-1)):
  257.                 self.clearUp(row, col, 'U')
  258.                 self.clearLeft(row, col, 'U')
  259.             else:
  260.                 self.clearUp(row, col, 'U')
  261.                 self.clearLeft(row, col, 'U')
  262.                 self.clearRight(row, col, 'U')
  263.         else:
  264.             if (col == 0):
  265.                 self.clearRight(row, col, 'U')
  266.                 self.clearUp(row, col, 'U')
  267.                 self.clearDown(row, col, 'U')
  268.             if (col == (self.cols-1)):
  269.                 self.clearLeft(row, col, 'U')
  270.                 self.clearUp(row, col, 'U')
  271.                 self.clearDown(row, col, 'U')
  272.             else:
  273.                 self.clearUp(row, col, 'U')
  274.                 self.clearDown(row, col, 'U')
  275.                 self.clearLeft(row, col, 'U')
  276.                 self.clearRight(row, col, 'U')
  277.  
  278.     def flagAllCovered(self, row, col):
  279.         if row == 0:
  280.             if (col == 0):
  281.                 self.clearDown(row, col, 'F')
  282.                 self.clearRight(row, col, 'F')
  283.             if (col == (cols-1)):
  284.                 self.clearDown(row, col, 'F')
  285.                 self.clearLeft(row, col, 'F')
  286.             else:
  287.                 self.clearDown(row, col, 'F')
  288.                 self.clearRight(row, col, 'F')
  289.                 self.clearLeft(row, col, 'F')
  290.  
  291.         if row == (self.rows - 1):
  292.             if (col == 0):
  293.                 self.clearUp(row, col, 'F')
  294.                 self.clearRight(row, col, 'F')
  295.             if (col == (cols-1)):
  296.                 self.clearUp(row, col, 'F')
  297.                 self.clearLeft(row, col, 'F')
  298.             else:
  299.                 self.clearUp(row, col, 'F')
  300.                 self.clearLeft(row, col, 'F')
  301.                 self.clearRight(row, col, 'F')
  302.         else:
  303.             if (col == 0):
  304.                 self.clearRight(row, col, 'F')
  305.                 self.clearUp(row, col, 'F')
  306.                 self.clearDown(row, col, 'F')
  307.             if (col == (cols-1)):
  308.                 self.clearLeft(row, col, 'F')
  309.                 self.clearUp(row, col, 'F')
  310.                 self.clearDown(row, col, 'F')
  311.             else:
  312.                 self.clearUp(row, col, 'F')
  313.                 self.clearDown(row, col, 'F')
  314.                 self.clearLeft(row, col, 'F')
  315.                 self.clearRight(row, col, 'F')
  316.  
  317.     def changeUp(self, row, col, char):
  318.         if col == 0:
  319.             if self.board[1][row-1][col] == 'C':
  320.                 self.board[1][row-1][col] = char
  321.             if self.board[1][row-1][col+1] == 'C':
  322.                 self.board[1][row-1][col+1] = char
  323.         if col == (self.cols-1):
  324.             if self.board[1][row-1][col] == 'C':
  325.                 self.board[1][row-1][col] = char
  326.             if self.board[1][row-1][col-1] == 'C':
  327.                 self.board[1][row-1][col-1] = char
  328.         else:
  329.             if self.board[1][row-1][col] == 'C':
  330.                 self.board[1][row-1][col] = char
  331.             if self.board[1][row-1][col-1] == 'C':
  332.                 self.board[1][row-1][col-1] = char
  333.             if self.board[1][row-1][col+1] == 'C':
  334.                 self.board[1][row-1][col+1] = char
  335.  
  336.     def changeDown(self, row, col, char):
  337.         if col == 0:
  338.             if self.board[1][row-1][col] == 'C':
  339.                 self.board[1][row+1][col] = char
  340.             if self.board[1][row-1][col+1] == 'C':
  341.                 self.board[1][row+1][col+1] = char
  342.         if col == (self.cols-1):
  343.             if self.board[1][row-1][col] == 'C':
  344.                 self.board[1][row+1][col] = char
  345.             if self.board[1][row-1][col-1] == 'C':
  346.                 self.board[1][row+1][col-1] = char
  347.         else:
  348.             if self.board[1][row-1][col] == 'C':
  349.                 self.board[1][row-1][col] = char
  350.             if self.board[1][row-1][col-1] == 'C':
  351.                 self.board[1][row-1][col-1] = char
  352.             if self.board[1][row-1][col+1] == 'C':
  353.                 self.board[1][row-1][col+1] = char
  354.  
  355.     def changeLeft(self, row, col, char):
  356.         if self.board[row][col-1] == 'C':
  357.              self.board[row][col-1] = char
  358.  
  359.     def changeRight(self, row, col, char):
  360.         if self.board[row][col+1] == 'C':
  361.              self.board[row][col+1] = char
  362.  
  363.     def main(self):
  364.         f = open(r'mines.in', "r")
  365.         g = open(r'mines.out', "w")
  366.         self.setBoards(f)
  367.         self.array[0].reverse()
  368.         self.array[1].reverse()
  369.         x = self.getNextBoard()
  370.         self.resetBoard()
  371.         print("Rows: " + str(self.rows) + " Cols: "+str(self.cols))
  372.         print("Covered: " + str(self.covered) + " Mines: " + str(self.ms))
  373.         print x
  374.         x.printBoard()
  375.         x.solveBoard()
  376.         f.close()
  377.  
  378. mx = mines()
  379. mx.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement