Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # corridors.py
- # This script makes a random expert-sized .mbf board with corridors
- # of lengths 1-MAX_CORRIDOR_LENGTH all around the border. The idea
- # is you start in the top left and go right - down the right hand
- # side - left across the bottom - then up the left hand side. The
- # starts of each of these straights are always safe (except with the
- # side ones you have to start on the 3rd square from the edge)
- # If you play the boards prior knowledge it is a good test of
- # reaction time since if you go too fast you always blast when you
- # get to a mine!
- import sys
- import random
- MAX_CORRIDOR_LENGTH = 7
- class msboard:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.grid = [[0 for x in xrange(width)] for y in xrange(height)]
- def alter(self, x,y):
- if (0 <= x < self.width) and (0 <= y < self.height):
- self.grid[y][x] ^= 1
- else:
- print "Error. Out of range."
- return 0
- def display(self):
- for y in xrange(self.height):
- line = ""
- for val in self.grid[y]:
- if val: line += "m "
- else: line += ". "
- print line
- def mine_count(self):
- return sum([sum(row) for row in self.grid])
- def mbf(self):
- body = ""
- body += chr(self.width) + chr(self.height)
- mines = self.mine_count()
- body += chr(mines/2**8) + chr(mines%2**8)
- for y in xrange(self.height):
- for x in xrange(self.width):
- if self.grid[y][x] == 1: body += chr(x)+chr(y)
- if len(body) != 4+2*mines:
- print "Error! Invalid mbf format."
- return -1
- else: return body
- def main():
- argv = sys.argv
- argc = len(argv)
- file_name = "corridor.mbf"
- my_board = msboard(30, 16)
- for i in xrange(my_board.width):
- my_board.alter(i,1)
- my_board.alter(i,my_board.height-2)
- for i in xrange(2,my_board.height-2):
- my_board.alter(1,i)
- my_board.alter(my_board.width-2,i)
- # rand mines around edge
- #top
- counter = 0
- while 1:
- counter += random.randint(1,MAX_CORRIDOR_LENGTH)
- if counter >= my_board.width: break
- my_board.alter(counter,0)
- counter += 1
- #rhs
- counter = 2
- while 1:
- counter += random.randint(1,MAX_CORRIDOR_LENGTH)
- if counter >= my_board.height-2: break
- my_board.alter(my_board.width-1,counter)
- counter += 1
- #bottom
- counter = 0
- while 1:
- counter += random.randint(1,MAX_CORRIDOR_LENGTH)
- if counter >= my_board.width: break
- my_board.alter(my_board.width-1-counter,my_board.height-1)
- counter += 1
- #lhs
- counter = 2
- while 1:
- counter += random.randint(1,MAX_CORRIDOR_LENGTH)
- if counter >= my_board.height-2: break
- my_board.alter(0,my_board.height-1-counter)
- counter += 1
- file_obj = open(file_name, 'wb')
- file_obj.write(my_board.mbf())
- file_obj.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement