Skip to content

Instantly share code, notes, and snippets.

@la11111
Last active August 29, 2015 13:56
Show Gist options
  • Save la11111/8972399 to your computer and use it in GitHub Desktop.
Save la11111/8972399 to your computer and use it in GitHub Desktop.
evil word search generator - for fun and to brush up on python, i need to code more
#!/usr/bin/env python
from random import randint as ri
from sys import stdout, exit
import argparse
class EvilWordSearchGenerator(object):
def __init__(self, word, rows=20, cols=20):
self.word = word
if rows < len(word):
self.rows = len(word)
else:
self.rows = rows
if cols < len(word):
self.cols = len(word)
else:
self.cols = cols
self.populate_grid()
self.randomize()
self.add_word()
def populate_grid(self):
self.grid = [
[self.word[ri(0,len(self.word)-1)] for x in xrange(self.cols)]
for x in xrange(self.rows)]
def randomize(self):
for row in xrange(0,self.rows-len(self.word)+1):
for col in xrange(0,self.cols-len(self.word)+1):
word_found = self.find_word(row, col)
if word_found:
self.remove_word(word_found)
def add_word(self):
dir = ri(0,2)
if dir == 0: #hor
row = ri(0,self.rows-1)
col = ri(0,self.cols-len(self.word))
elif dir == 1:#ver
row = ri(0,self.rows-len(self.word))
col = ri(0,self.cols-1)
else:#diag
row = ri(0,self.rows-len(self.word))
col = ri(0,self.cols-len(self.word))
for offset in xrange(0,len(self.word)):
if dir == 0: #hor
self.grid[row][col+offset] = self.word[offset]
elif dir == 1:#ver
self.grid[row+offset][col] = self.word[offset]
else:#diag
self.grid[row+offset][col+offset] = self.word[offset]
print "word location:"
print "row: ", row
print "col: ", col
print "dir: ", dir
def remove_word(self, loc):
row = loc[0]
col = loc[1]
dir = loc[2]
offset = ri(1,len(self.word)-1)
change_loc = ()
if dir == 'h':
change_loc = (row,col+offset)
if dir == 'v':
change_loc = (row+offset,col)
if dir == 'd':
change_loc = (row+offset,col+offset)
old_letter = self.grid[change_loc[0]][change_loc[1]]
new_letter = self.word[ri(0,len(self.word)-1)]
while old_letter == new_letter:
new_letter = self.word[ri(0,len(self.word)-1)]
self.grid[change_loc[0]][change_loc[1]] = new_letter
def find_word(self, row, col):
hor = []
ver = []
diag = []
for n in xrange(0,len(self.word)):
hor.append(self.grid[row][col+n])
ver.append(self.grid[row+n][col])
diag.append(self.grid[row+n][col+n])
if ''.join(hor) == self.word:
return (row, col, 'h')
elif ''.join(ver) == self.word:
return (row, col, 'v')
elif ''.join(diag) == self.word:
return (row, col, 'd')
else:
pass
def print_grid(self):
print "word:", self.word
for row in self.grid:
for c in row:
stdout.write(" "+c)
stdout.write('\n')
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Evil Word Search Puzzle Generator')
parser.add_argument('-w','--word', help='word to use (should probably be all the same case)', required=True)
parser.add_argument('-r','--rows', help='How many rows your puzzle should have', required=False, default=20, type=int)
parser.add_argument('-c','--cols', help='How many columns your puzzle should have', required=False, default=20, type=int)
args = parser.parse_args()
word = args.word
rows = int(args.rows)
cols = int(args.cols)
gen = EvilWordSearchGenerator(word, rows, cols)
gen.print_grid()
@la11111
Copy link
Author

la11111 commented Feb 13, 2014

can word search puzzles have upward diagonals? i maybe did it wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment