-
Notifications
You must be signed in to change notification settings - Fork 1
/
minelaying.py
30 lines (26 loc) · 1.15 KB
/
minelaying.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import random
# Provide several methods to distribute mines
######################################
# interface:
# bool lay_mine_methodName(field_2D_array, mine_number, except_coord)
# input: field_2D_array, should be a rectangle
# mine_number, how many mines should be laying
# except_coordinate,this place can't set mine
# output: laying successful or not
# char 'm' in field_2D_array indicates mine
######################################
# The simplest algorithm is to randomly select a location,
# and if there is no mine, place a mine.
# This method is very similar to the physical means of mine in reality.
def lay_mine_random_place(field_2D_array, mine_number, except_coord):
row_bound = len(field_2D_array) - 1
column_bound = len(field_2D_array[0]) - 1
lay_done_number = 0
while lay_done_number < mine_number:
row = random.randint(0, row_bound)
col = random.randint(0, column_bound)
exp_row, exp_col = except_coord
if row != exp_row or col != exp_col:
if field_2D_array[row][col] != 'm':
field_2D_array[row][col] = 'm'
lay_done_number += 1