mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
Reformat to match pylint recommendations
This commit is contained in:
Regular → Executable
+13
-12
@@ -1,38 +1,39 @@
|
||||
#!/bin/python
|
||||
from sys import argv
|
||||
from random import randint, shuffle
|
||||
from math import sqrt
|
||||
|
||||
def readDiceFile(path):
|
||||
with open(path, 'r+') as f:
|
||||
def read_dice_file(path):
|
||||
with open(path, 'r+', encoding="utf-8") as file:
|
||||
# Ignore first two lines
|
||||
f.readline()
|
||||
f.readline()
|
||||
file.readline()
|
||||
file.readline()
|
||||
dice = []
|
||||
line = ''.join(f.readline().rstrip().split(','))
|
||||
line = ''.join(file.readline().rstrip().split(','))
|
||||
while(line):
|
||||
dice.append(line)
|
||||
line = ''.join(f.readline().rstrip().split(','))
|
||||
line = ''.join(file.readline().rstrip().split(','))
|
||||
|
||||
return dice
|
||||
|
||||
def rollDie(die):
|
||||
def roll_die(die):
|
||||
return str(die[randint(0, len(die) - 1)])
|
||||
|
||||
def rollDice(dice):
|
||||
def roll_dice(dice):
|
||||
rolls = []
|
||||
for die in dice:
|
||||
roll = rollDie(die)
|
||||
roll = roll_die(die)
|
||||
rolls.append(roll)
|
||||
|
||||
return rolls
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(argv) != 2:
|
||||
print('Usage: python3 board_randomizer DICE_FILE')
|
||||
print('Usage: python3 <board_randomizer.py> <dice_file>')
|
||||
else:
|
||||
dice = readDiceFile(argv[1])
|
||||
dice = read_dice_file(argv[1])
|
||||
shuffle(dice)
|
||||
rolls = rollDice(dice)
|
||||
rolls = roll_dice(dice)
|
||||
|
||||
# Format dice rolls for boards file
|
||||
board_size = int(sqrt(len(rolls)))
|
||||
|
||||
Regular → Executable
+54
-37
@@ -1,72 +1,87 @@
|
||||
import sys
|
||||
from dictionary_utils import WordTree
|
||||
from copy import deepcopy
|
||||
|
||||
def readBoggleFile(file):
|
||||
with open(file, 'r') as f:
|
||||
return [x.rstrip() for x in f.readlines()]
|
||||
def read_boggle_file(file):
|
||||
'''Return list of rows from Boggle board file'''
|
||||
with open(file, 'r', encoding='utf-8') as file:
|
||||
return [x.rstrip() for x in file.readlines()]
|
||||
|
||||
def readWordlist(file):
|
||||
with open(file, 'r') as f:
|
||||
return {k:1 for k in f.read().split()}
|
||||
def read_wordlist(file):
|
||||
'''Return dictionary of words with associated word count (1 by default)'''
|
||||
with open(file, 'r', encoding='utf-8') as file:
|
||||
return {k:1 for k in file.read().split()}
|
||||
|
||||
def getAdjacentIndexes(board, row, col):
|
||||
maxHeight = len(board) - 1
|
||||
maxWidth = len(board[0]) - 1
|
||||
def get_adjacent_indexes(board, row, col):
|
||||
'''Return adjecency list for matrix (Boggle board) of size `row x col`'''
|
||||
max_height = len(board) - 1
|
||||
max_width = len(board[0]) - 1
|
||||
indexes = []
|
||||
if row > 0:
|
||||
indexes.append((row-1, col)) # up
|
||||
if col > 0:
|
||||
indexes.append((row-1, col-1)) # up-left
|
||||
if col < maxWidth:
|
||||
if col < max_width:
|
||||
indexes.append((row-1, col+1)) # up-right
|
||||
if row < maxHeight:
|
||||
if row < max_height:
|
||||
indexes.append((row+1, col)) # down
|
||||
if col > 0:
|
||||
indexes.append((row+1, col-1)) # down-left
|
||||
if col < maxWidth:
|
||||
if col < max_width:
|
||||
indexes.append((row+1, col+1)) # down-right
|
||||
if col > 0:
|
||||
indexes.append((row, col-1)) # left
|
||||
if col < maxWidth:
|
||||
if col < max_width:
|
||||
indexes.append((row, col+1)) # right
|
||||
|
||||
return indexes
|
||||
return indexes
|
||||
|
||||
def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix = '', path = [], foundWords = {}, prefixes = None, maxDepth = 5, prefixLen = 0):
|
||||
if depth == maxDepth:
|
||||
def find_words_by_index(board, wordlist, row, col, depth, adjacent_indexes, prefix = '', path = [], found_words = {}, prefixes = None, max_depth = 5, prefix_len = 0):
|
||||
if depth == max_depth:
|
||||
return {}
|
||||
|
||||
if prefixes != None and depth == prefixLen and prefix not in prefixes:
|
||||
return {}
|
||||
if prefixes != None and depth == prefix_len and prefix not in prefixes:
|
||||
return {}
|
||||
prefix = prefix + board[row][col]
|
||||
|
||||
foundWords = deepcopy(foundWords)
|
||||
found_words = deepcopy(found_words)
|
||||
path = deepcopy(path)
|
||||
|
||||
path.append((row, col))
|
||||
if prefix in wordlist:
|
||||
foundWords[prefix] = 1
|
||||
found_words[prefix] = 1
|
||||
|
||||
adjacents = [x for x in adjacentIndexes[(row, col)] if x not in path]
|
||||
for a in adjacents:
|
||||
newWords = findWordsByIndex(board, wordlist, a[0], a[1], depth+1, adjacentIndexes, prefix, path, foundWords, prefixes, maxDepth, prefixLen)
|
||||
for k,v in newWords.items():
|
||||
if k in wordlist and k in foundWords:
|
||||
adjacents = [x for x in adjacent_indexes[(row, col)] if x not in path]
|
||||
for adj in adjacents:
|
||||
new_words = find_words_by_index(board,
|
||||
wordlist,
|
||||
adj[0],
|
||||
adj[1],
|
||||
depth+1,
|
||||
adjacent_indexes,
|
||||
prefix,
|
||||
path,
|
||||
found_words,
|
||||
prefixes,
|
||||
max_depth,
|
||||
prefix_len)
|
||||
for k, v in new_words.items():
|
||||
if k in wordlist and k in found_words:
|
||||
# Add and increment word counts
|
||||
#foundWords[k] = foundWords[k] + v
|
||||
foundWords[k] = foundWords[k]
|
||||
found_words[k] = found_words[k]
|
||||
elif k in wordlist.keys():
|
||||
foundWords[k] = v
|
||||
found_words[k] = v
|
||||
|
||||
return foundWords
|
||||
return found_words
|
||||
|
||||
def findWords(board, adjacentIndexes, wordlistPath, maxDepth = 5, prefixes = None, prefixLen = 0):
|
||||
def find_words(board, adjacent_indexes, wordlist_path, max_depth = 5, prefixes = None, prefix_len = 0):
|
||||
words = {}
|
||||
for row in range(len(board)):
|
||||
for col in range(len(board[0])):
|
||||
# Find words for each row/col starting points
|
||||
wordlist = readWordlist(wordlistPath + 'words_' + board[row][col] + '.txt')
|
||||
newWords = findWordsByIndex(board, wordlist, row, col, 0, adjacentIndexes, prefixes = prefixes, maxDepth = maxDepth, prefixLen = prefixLen)
|
||||
wordlist = read_wordlist(wordlist_path + 'words_' + board[row][col] + '.txt')
|
||||
newWords = find_words_by_index(board, wordlist, row, col, 0, adjacent_indexes, prefixes = prefixes, max_depth = max_depth, prefix_len = prefix_len)
|
||||
for w,v in newWords.items():
|
||||
# Add and increment word counts
|
||||
words[w] = words[w] + v if w in words.keys() else v
|
||||
@@ -76,19 +91,21 @@ def findWords(board, adjacentIndexes, wordlistPath, maxDepth = 5, prefixes = No
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) != 5:
|
||||
print('Usage: python3 blogger.py BOARD_FILE MAX_WORD_LENGTH PREFIX_FILE WORDLISTS_PATH')
|
||||
print('Usage: python3 blogger.py <BOARD_FILE> <MAX_WORD_LENGTH> <PREFIX_FILE> <WORDLISTS>')
|
||||
else:
|
||||
board = readBoggleFile(sys.argv[1])
|
||||
board = read_boggle_file(sys.argv[1])
|
||||
print(board)
|
||||
adjacentIndexes = {}
|
||||
for r in range(len(board)):
|
||||
for c in range(len(board)):
|
||||
adjacentIndexes[(r,c)] = getAdjacentIndexes(board, r, c)
|
||||
adjacentIndexes[(r,c)] = get_adjacent_indexes(board, r, c)
|
||||
|
||||
adjacentLetters = {k:[board[pos[0]][pos[1]] for pos in v] for (k,v) in adjacentIndexes.items()}
|
||||
adjacentLetters = {
|
||||
k:[board[pos[0]][pos[1]] for pos in v] for (k,v) in adjacentIndexes.items()
|
||||
}
|
||||
|
||||
maxSize = int(sys.argv[2], 10)
|
||||
prefixes = readWordlist(sys.argv[3])
|
||||
words = findWords(board, adjacentIndexes, sys.argv[4], maxSize, prefixes, 4)
|
||||
prefixes = read_wordlist(sys.argv[3])
|
||||
words = find_words(board, adjacentIndexes, sys.argv[4], maxSize, prefixes, 4)
|
||||
for w in words:
|
||||
print(w)
|
||||
|
||||
Reference in New Issue
Block a user