Advertisement
Guest User

Untitled

a guest
Aug 29th, 2009
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. # corridors.py
  2.  
  3. # This script makes a random expert-sized .mbf board with corridors
  4. # of lengths 1-MAX_CORRIDOR_LENGTH all around the border. The idea
  5. # is you start in the top left and go right - down the right hand
  6. # side - left across the bottom - then up the left hand side. The
  7. # starts of each of these straights are always safe (except with the
  8. # side ones you have to start on the 3rd square from the edge)
  9. # If you play the boards prior knowledge it is a good test of
  10. # reaction time since if you go too fast you always blast when you
  11. # get to a mine!
  12.  
  13. import sys
  14. import random
  15.  
  16. MAX_CORRIDOR_LENGTH = 7
  17.  
  18. class msboard:
  19. def __init__(self, width, height):
  20. self.width = width
  21. self.height = height
  22. self.grid = [[0 for x in xrange(width)] for y in xrange(height)]
  23. def alter(self, x,y):
  24. if (0 <= x < self.width) and (0 <= y < self.height):
  25. self.grid[y][x] ^= 1
  26. else:
  27. print "Error. Out of range."
  28. return 0
  29. def display(self):
  30. for y in xrange(self.height):
  31. line = ""
  32. for val in self.grid[y]:
  33. if val: line += "m "
  34. else: line += ". "
  35. print line
  36. def mine_count(self):
  37. return sum([sum(row) for row in self.grid])
  38. def mbf(self):
  39. body = ""
  40. body += chr(self.width) + chr(self.height)
  41. mines = self.mine_count()
  42. body += chr(mines/2**8) + chr(mines%2**8)
  43. for y in xrange(self.height):
  44. for x in xrange(self.width):
  45. if self.grid[y][x] == 1: body += chr(x)+chr(y)
  46. if len(body) != 4+2*mines:
  47. print "Error! Invalid mbf format."
  48. return -1
  49. else: return body
  50.  
  51. def main():
  52. argv = sys.argv
  53. argc = len(argv)
  54.  
  55. file_name = "corridor.mbf"
  56.  
  57. my_board = msboard(30, 16)
  58. for i in xrange(my_board.width):
  59. my_board.alter(i,1)
  60. my_board.alter(i,my_board.height-2)
  61. for i in xrange(2,my_board.height-2):
  62. my_board.alter(1,i)
  63. my_board.alter(my_board.width-2,i)
  64.  
  65. # rand mines around edge
  66. #top
  67. counter = 0
  68. while 1:
  69. counter += random.randint(1,MAX_CORRIDOR_LENGTH)
  70. if counter >= my_board.width: break
  71. my_board.alter(counter,0)
  72. counter += 1
  73. #rhs
  74. counter = 2
  75. while 1:
  76. counter += random.randint(1,MAX_CORRIDOR_LENGTH)
  77. if counter >= my_board.height-2: break
  78. my_board.alter(my_board.width-1,counter)
  79. counter += 1
  80. #bottom
  81. counter = 0
  82. while 1:
  83. counter += random.randint(1,MAX_CORRIDOR_LENGTH)
  84. if counter >= my_board.width: break
  85. my_board.alter(my_board.width-1-counter,my_board.height-1)
  86. counter += 1
  87. #lhs
  88. counter = 2
  89. while 1:
  90. counter += random.randint(1,MAX_CORRIDOR_LENGTH)
  91. if counter >= my_board.height-2: break
  92. my_board.alter(0,my_board.height-1-counter)
  93. counter += 1
  94.  
  95.  
  96. file_obj = open(file_name, 'wb')
  97. file_obj.write(my_board.mbf())
  98. file_obj.close()
  99.  
  100. if __name__ == "__main__":
  101. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement