View difference between Paste ID: uXeu6PTv and UfM5FqAq
SHOW: | | - or go back to the newest paste.
1
#!/usr/bin/env python
2
3
# Generate a word search grid from hell
4
# usage: wordgrid.py w h word
5
# The resulting word grid of w x h letters will be made up from the letters in
6
# word but will never actually contain the word neither forward nor backwards
7
# No warranty and all that noise
8
9
10
import sys
11
import random
12
13-
def _checkForWord(grid, word, x, y):
13+
def _checkForWord(grid, word, w, h, x, y):
14
    l = len(word) - 1
15
    forward, backward = False, False
16
    # forward
17
    if x >= l and all(grid[y][x-l+o] == word[o] for o in range(l)):
18
        forward = True
19
    elif y >= l and all(grid[y-l+o][x] == word[o] for o in range(l)):
20
        forward = True
21
    elif x >= l and y >= l and all(grid[y-l+o][x-l+o] == word[o] for o in range(l)):
22
        forward = True
23
    elif x < w-l and y >= l and all(grid[y-l+o][x+l-o] == word[o] for o in range(l)):
24
        forward = True
25
    
26
    # backward
27
    if x >= l and all(grid[y][x-o] == word[o] for o in range(1, l+1)):
28
        backward = True
29
    elif y >= l and all(grid[y-o][x] == word[o] for o in range(1, l+1)):
30
        backward = True
31
    elif x >= l and y >= l and all(grid[y-o][x-o] == word[o] for o in range(1, l+1)):
32
        backward = True
33
    elif x < w-l and  y >= l and all(grid[y-o][x+o] == word[o] for o in range(1,l+1)):
34
        backward = True
35
    
36
    return forward, backward
37
38
39
def generate(w, h, word):
40
    word = word.upper()
41
    letters = set(word)
42
    letter = list(letters - set(word[-1]))
43
    etters = list(letters - set(word[0]))
44
    etter = list(letters - set(word[0]) - set(word[-1]))
45
    letters = list(letters)
46
    
47
    grid = list()
48
    
49-
            check = _checkForWord(grid, word, x, y)
49+
50
        line = list()
51
        grid.append(line)
52
        for x in range(w):
53
            check = _checkForWord(grid, word, w, h, x, y)
54
            if check[0] and check[1]:
55
                line.append(random.choice(etter))
56
            elif check[0]:
57
                line.append(random.choice(letter))
58
            elif check[1]:
59
                line.append(random.choice(etters))
60
            else:
61
                line.append(random.choice(letters))
62
    return grid    
63
64
def printgrid(grid):
65
    for line in grid:
66
        print(" ".join(line))
67
        
68
if __name__ == "__main__":
69
    if len(sys.argv) != 4:
70
        print("Usage: wordgrid.py w h word")
71
        print("The grid will be made up of the letters in word, but word will never appear in it")
72
        sys.exit(1)
73
    
74
    w = int(sys.argv[1])
75
    h = int(sys.argv[2])
76
77
    word = sys.argv[3]
78
    if len(word) < 3:
79
        print("The word must have at least three letters")
80
        sys.exit(1)
81
    
82
    print("Find %s!" % word)
83
    printgrid(generate(w,h,word))