MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / generate_board

Method generate_board

other/word_search.py:316–344  ·  view source on GitHub ↗

Generates a board with a random direction for each word. >>> wt = WordSearch(WORDS, WIDTH, HEIGHT) >>> wt.generate_board() >>> len(list(filter(lambda word: word is not None, sum(wt.board, start=[]))) ... ) == sum(map(lambda word: len(word), WORDS)) T

(self)

Source from the content-addressed store, hash-verified

314 return
315
316 def generate_board(self) -> None:
317 """
318 Generates a board with a random direction for each word.
319
320 >>> wt = WordSearch(WORDS, WIDTH, HEIGHT)
321 >>> wt.generate_board()
322 >>> len(list(filter(lambda word: word is not None, sum(wt.board, start=[])))
323 ... ) == sum(map(lambda word: len(word), WORDS))
324 True
325 """
326 directions = (
327 self.insert_north,
328 self.insert_northeast,
329 self.insert_east,
330 self.insert_southeast,
331 self.insert_south,
332 self.insert_southwest,
333 self.insert_west,
334 self.insert_northwest,
335 )
336 for word in self.words:
337 # Shuffle the row order and column order that is used when brute forcing
338 # the insertion of the word
339 rows, cols = list(range(self.height)), list(range(self.width))
340 shuffle(rows)
341 shuffle(cols)
342
343 # Insert the word via the direction
344 choice(directions)(word, rows, cols)
345
346
347def visualise_word_search(

Callers 1

visualise_word_searchFunction · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected