Advertisement
Guest User

Minesweeper

a guest
Mar 4th, 2010
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # minesweeper.py
  4. # Resolve the problem minesweeper from the book programming challenges
  5.  
  6.  
  7. def readFile(input):
  8.     x, y = input.readline().rstrip().split()
  9.     x = int(x)
  10.     y = int(y)
  11.     field = []
  12.  
  13.     if x == 0 or y == 0:
  14.         return None
  15.  
  16.     for i in range(x):
  17.         line = list(input.readline().rstrip())
  18.         field.append(line)
  19.  
  20.     return field
  21.  
  22. def check(coord):
  23.     if coord == '*':
  24.         return 1
  25.     return 0
  26.        
  27.  
  28. def processField(field):
  29.     for i in range(len(field)):
  30.         for j in range(len(field[i])):
  31.             count = 0
  32.             if field[i][j] == '*':
  33.                 continue
  34.             if i == 0:
  35.                 count += check(field[i+1][j]) # Check Down            
  36.                 if j == 0:
  37.                     count += check(field[i][j+1]) # Check Right
  38.                     count += check(field[i+1][j+1]) # Check DownRight
  39.                 elif j == len(field[i])-1:
  40.                     count += check(field[i][j-1]) # Check Left
  41.                     count += check(field[i+1][j-1]) # Check DownLeft    
  42.                 else:
  43.                     count += check(field[i][j-1]) # Check Left
  44.                     count += check(field[i][j+1]) # Check Right
  45.                     count += check(field[i+1][j-1]) # Check DownLeft
  46.                     count += check(field[i+1][j+1]) # Check DownRight            
  47.             elif i == len(field)-1:
  48.                 count += check(field[i-1][j]) # Check Up
  49.                 if j == 0:
  50.                     count += check(field[i][j+1]) # Check Right
  51.                     count += check(field[i-1][j+1]) # Check UpRight
  52.                 elif j == len(field[i])-1:
  53.                     count += check(field[i][j-1]) # Check Left
  54.                     count += check(field[i-1][j-1]) # Check UpLeft
  55.                 else:
  56.                     count += check(field[i][j+1]) # Check Right
  57.                     count += check(field[i-1][j+1]) # Check UpRight
  58.                     count += check(field[i][j-1]) # Check Left
  59.                     count += check(field[i-1][j-1]) # Check UpLeft
  60.                  
  61.             else:
  62.                 count += check(field[i-1][j]) # Check Up
  63.                 count += check(field[i+1][j]) # Check Down
  64.                
  65.                 if j == 0 or j != len(field[i])-1:
  66.                     count += check(field[i-1][j+1]) # Check UpRight
  67.                     count += check(field[i][j+1]) # Check Right
  68.                     count += check(field[i+1][j+1]) # Check DownRight
  69.                 if j == len(field[i])-1 or j != 0:                  
  70.                     count += check(field[i-1][j-1]) # Check UpLeft
  71.                     count += check(field[i][j-1]) # Check Left
  72.                     count += check(field[i+1][j-1]) # Check DownLeft
  73.            
  74.             field[i][j] = count
  75.            
  76.     return field
  77.  
  78. def writeFile(field, output, numField):
  79.     print >> output,"Field #%s" % numField
  80.     for line in field:
  81.         for charac in line:
  82.             output.write(str(charac))
  83.         print >> output
  84.     print >> output
  85.  
  86.  
  87. def main():
  88.     try:    
  89.         input = open("input.in")
  90.         output = open("output.out", "w")
  91.     except IOError:
  92.         return "Cannot open input file"
  93.  
  94.     try:
  95.         field = readFile(input)
  96.         numField = 1
  97.         while field:
  98.             field = processField(field)
  99.             writeFile(field, output, numField)
  100.             field = readFile(input)
  101.             numField += 1
  102.  
  103.         input.close()
  104.         output.close()
  105.         return "Finishing the program"
  106.    
  107.     except StandardError:
  108.         return "Something unexpected happened"
  109. raw_input(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement