@ADoctor/

Christmas Wordsearch Creator

Python

Original name: FluffyNeighboringPhase

fork
loading
Files
  • main.py
  • ChristmasWords.txt
main.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import random

DOWN = 1
RIGHT = 2
UP = 3
LEFT = 4

MAX_BOARD=15
MIN_BOARD=0
WORD_AMOUNT=10

def ArrayHexCheck(array):
	for i in range(0,len(array)):
		for j in range(0,2):
			k = array[i][j]
			if k > 9:
				if k == 10:
					k = 'A'
				elif k == 11:
					k = 'B'
				elif k == 12:
					k = 'C'
				elif k == 13:
					k = 'D'
				elif k == 14:
					k = 'E'
				elif k == 15:
					k = 'F'
			array[i][j] = str(k)
	return array

def CreateBoard():
	WordSearchBoard = []
	for i in range(MIN_BOARD,MAX_BOARD+1):
		WordSearchLine = []
		for i in range(MIN_BOARD,MAX_BOARD+1):
			WordSearchLine.append(" ")
		WordSearchBoard.append(WordSearchLine)
	return WordSearchBoard

def addToBoard(StartRow, StartColumn, Board, Direction, Word, doAdd, StartLocations, EndLocations, WordsUsed):
	if Word in WordsUsed:
		return False
	currentRow = StartRow
	currentColumn = StartColumn

	if Direction == DOWN:
		StartRow += 1
	elif Direction == RIGHT:
		StartColumn += 1
	elif Direction == UP:
		StartRow -= 1
	elif Direction == LEFT:
		StartColumn -= 1

	for i in range(0,len(Word)):
		try:
			if Direction == DOWN:
				currentRow += 1
			elif Direction == RIGHT:
				currentColumn += 1
			elif Direction == UP:
				currentRow -= 1
			elif Direction == LEFT:
				currentColumn -= 1
			else:
				print("**** ERROR BAD Direction = " + Direction)
				quit()
			
			if currentRow < 0 or currentColumn < 0:
				return False
			
			if Board[currentRow][currentColumn] != ' ' and Board[currentRow][currentColumn] != Word[i]:
				return False
			if doAdd == True:
				Board[currentRow][currentColumn] = Word[i]
		except IndexError:
			print("**** ERROR Unexpected Index ERROR")
			return False

	if [StartRow,StartColumn] not in StartLocations and [currentRow,currentColumn] not in EndLocations:
		StartLocations.append([StartRow,StartColumn])
		EndLocations.append([currentRow,currentColumn])
	return True

def SetUpWords(WSBoard):
	File = open('ChristmasWords.txt','r').readlines()
	Words = []
	for i in File:
		i = i.strip('\n').upper()
		Words.append(i)
	return Words

def FinishCreation(Words,Board):
	WordPlaceSuccesses = 0
	StartLocations = []
	EndLocations = []
	WordsUsed = []
	while WordPlaceSuccesses < WORD_AMOUNT:
		WordNum = random.randint(1,len(Words))
		Word = Words[WordNum-1]
		Direction = random.randint(1,4)
		colStart = MIN_BOARD
		colEnd = MAX_BOARD
		rowStart = MIN_BOARD
		rowEnd = MAX_BOARD

		if Direction == DOWN:
			rowEnd = rowEnd-len(Word)
		elif Direction == RIGHT:
			colEnd = colEnd-len(Word)
		elif Direction == UP:
			rowStart = rowStart+len(Word)
		elif Direction == LEFT:
			colStart = colStart+len(Word)
		else:
			print("**** ERROR BAD Direction = " + Direction)
			quit()

		if colEnd - colStart <= 0:
			StartColumn = MIN_BOARD
		else:
			StartColumn = random.randint(colStart,colEnd)

		if rowEnd - rowStart <= 0:
			StartRow = MIN_BOARD
		else:
			StartRow = random.randint(rowStart,rowEnd)

		canAdd = addToBoard(StartRow, StartColumn, Board, Direction, Word, False, StartLocations, EndLocations, WordsUsed)
		if canAdd == True:
			addToBoard(StartRow, StartColumn, Board, Direction, Word, True, StartLocations, EndLocations, WordsUsed)
			WordPlaceSuccesses = WordPlaceSuccesses + 1
			WordsUsed.append(Word)

	ArrayHexCheck(StartLocations)
	ArrayHexCheck(EndLocations)
	print("""
Words: """)
	for i in range(0,WORD_AMOUNT):
		print(WordsUsed[i])
	print("")
	return Board,StartLocations,EndLocations,WordsUsed

def CounterCheck(counter):
	try:
		if counter < 9:
			return counter + 1
		else:
			return 'A'
	except TypeError:
		if counter == 'A':
			return 'B'
		elif counter == 'B':
			return 'C'
		elif counter == 'C':
			return 'D'
		elif counter == 'D':
			return 'E'
		elif counter == 'E':
			return 'F'

def addRandomLetters(Board):
	for i in range(0,len(Board)):
		for j in range(0,len(Board[i])):
			if Board[i][j] == ' ':
				Board[i][j] = chr(random.randint(65, 90))
	return Board

def PrintBoard(Board):
	print('    0 1 2 3 4 5 6 7 8 9 A B C D E F')
	print('')
	counter = 0
	for i in Board:
		print(counter,end='   ')
		for j in range(MIN_BOARD,len(i)):
			print(i[j], end=' ')
		print('')
		counter = CounterCheck(counter)

def PlayWordsearch(Board,StartLocations,EndLocations,WordsUsed):
	WordsFound = 0
	while WordsFound != WORD_AMOUNT:
		WordFlag = False
		startY = input("What y co-ordinate has the start of a word? ")
		startX = input("What x co-ordinate has the start of a word? ")
		endY = input("What y co-ordinate has the end of a word? ")
		endX = input("What x co-ordinate has the end of a word? ")
		for x in range(0,len(StartLocations)):
			if startY == StartLocations[x][0] and startX == StartLocations[x][1]:
				if endY == EndLocations[x][0] and endX == EndLocations[x][1]:
					WordFlag = True
					break
		if WordFlag == True:
			print("Word Found:",WordsUsed[x]+"!")
			StartLocations[x][0] = 'G'
			StartLocations[x][1] = 'G'
			EndLocations[x][0] = 'G'
			EndLocations[x][1] = 'G'
			WordsFound += 1
		else:
			print("Sorry, no word found. Either you already found this word, or it's not one of the words in the wordsearch.")
	print("Congratulations! You found all the words!")

def main():
	Board = CreateBoard()
	Words = SetUpWords(Board)
	Board,StartLocations,EndLocations,WordsUsed = FinishCreation(Words,Board)
	Board = addRandomLetters(Board)
	PrintBoard(Board)
	print("")
	PlayWordsearch(Board,StartLocations,EndLocations,WordsUsed)

main()