def update(self): currentPosition = (self.cursor.x, self.cursor.y) moveConsoleCursor(self.cursor.lastX * 2, self.cursor.lastY) if self.isMarking or (self.cursor.lastX, self.cursor.lastY) in self.permanentMark: print(Fore.BLACK + Back.WHITE + self.board[self.cursor.lastY][self.cursor.lastX], end=" ") else: print(self.board[self.cursor.lastY][self.cursor.lastX], end=" ") moveConsoleCursor(self.cursor.x * 2, self.cursor.y) if self.isMarking or currentPosition in self.permanentMark: print(Fore.RED + Back.WHITE + self.board[self.cursor.y][self.cursor.x], end=" ") else: print(Fore.RED + self.board[self.cursor.y][self.cursor.x], end=" ") moveConsoleCursor(self.cursor.x * 2, self.cursor.y) if self.isMarking: if calculateVectorLength(self.currentMark[0], currentPosition) < len(self.currentMark): unmarkedPosition = self.currentMark.pop(-1) moveConsoleCursor(unmarkedPosition[0] * 2, unmarkedPosition[1]) print(self.board[unmarkedPosition[1]][unmarkedPosition[0]], end=" ") else: self.currentMark.append(currentPosition) ########## Debuggin stuff, delete later ###################### moveConsoleCursor(0, self.height + 4 + len(self.hiddenWords)) print(self.currentMark) print(self.isMarking)
def showWords(self): moveConsoleCursor(0, self.height) print("==============================") print("Words to search:") for word in self.hiddenWords: print("- " + word.word) #+ str(word.spaces)) print("==============================")
def showYouWin(self): startOfPopup = (self.height // 2) - 2 moveConsoleCursor(0, startOfPopup) print("╔" + ("═" * (self.width * 2 - 3)) + "╗") print("║" + (" " * (self.width * 2 - 3)) + "║") print("║" + "You Win!".center(self.width * 2 - 3, " ") + "║") print("║" + "Press Enter".center(self.width * 2 - 3, " ") + "║") print("║" + (" " * (self.width * 2 - 3)) + "║") print("╚" + ("═" * (self.width * 2 - 3)) + "╝")
def keypress(event): global OPTION global IN_MENU global board if event.event_type == "up": return if IN_MENU: if event.scan_code == 72 and OPTION > 1: moveConsoleCursor(0,OPTION) print(" ",end="") OPTION -= 1 moveConsoleCursor(0,OPTION) print(">",end="") elif event.scan_code == 80 and OPTION < 4: moveConsoleCursor(0,OPTION) print(" ",end="") OPTION += 1 moveConsoleCursor(0,OPTION) print(">",end="") elif event.scan_code == 28: if OPTION == 1: board = Board(10,10) n_words = 5 elif OPTION == 2: board = Board(15,15) n_words = 12 elif OPTION == 3: board = Board(20,20) n_words = 25 elif OPTION == 4: board = Board(30,30) n_words = 40 else: raise ValueError("Option variable is out of bounds: "+str(OPTION)) words_to_search = sample(words, n_words) for word in words_to_search: board.addWord(word) IN_MENU = False board.startGame() else: if board.gameIsWon(): if event.scan_code == 28: IN_MENU = True OPTION = 1 promtStartMenu() return else: if board.moveCursor(event.scan_code): board.update() elif event.scan_code == 28: board.toggleMarking()
def toggleMarking(self): if self.isMarking: currentMarkReversed = reversed(self.currentMark) foundOne = False for i, word in enumerate(self.hiddenWords): if word.spaces == self.currentMark or word.spaces == currentMarkReversed: foundOne = True word.found = True self.wordsFound += 1 moveConsoleCursor(0, self.height + 2 + i) print(Fore.GREEN + "- " + word.word) self.permanentMark.extend(self.currentMark) if self.gameIsWon(): self.showYouWin() break if not foundOne: lastMark = self.currentMark.pop(-1) for markedSpace in self.currentMark: moveConsoleCursor(markedSpace[0] * 2, markedSpace[1]) print(self.board[markedSpace[1]][markedSpace[0]], end=" ") moveConsoleCursor(lastMark[0] * 2, lastMark[1]) print(Fore.RED + self.board[lastMark[1]][lastMark[0]], end=" ") self.isMarking = False self.currentMark = [] else: self.isMarking = True currentPosition = (self.cursor.x, self.cursor.y) self.currentMark = [currentPosition] moveConsoleCursor(self.cursor.x * 2, self.cursor.y) print(Fore.RED + Back.WHITE + self.board[self.cursor.y][self.cursor.x], end=" ")