From: Harishankar Date: Sat, 4 Dec 2010 10:46:48 +0000 (+0530) Subject: Menus in progress X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=cd153df772f936f9aaed0f75d83f15c05a6ee9b6;p=getaclue.git Menus in progress Menu functionality for main menu and puzzle creator in progress --- diff --git a/crosswordpuzzle.py b/crosswordpuzzle.py index c1f822b..fb9ada5 100644 --- a/crosswordpuzzle.py +++ b/crosswordpuzzle.py @@ -156,40 +156,6 @@ class CrosswordPuzzle: self.data[row][col+i].char = word[i].upper () self.data[row][col+i].occupied_across = True - # display the grid with words - def print_grid (self, no_words=False): - # get row, col and print them (with grid number if set) - for col in range (self.cols): - # print the first row as column headers - print self.BLUE + ' ' + str (col) + self.ENDCOL, - print - - for row in range (self.rows): - for col in range (self.cols): - # print the data - # if the cell is numbered i.e. start of a word - if self.data[row][col].numbered != 0: - print self.BRICKRED + str(self.data[row][col].numbered) + self.ENDCOL, - # print a space - else: - print ' ', - # if the character is not a blank or a block - if self.data[row][col].char <> "." and self.data[row][col].char <> "#": - # if words are to be shown regardless of hidden/revealed state - if no_words is False: - print self.BOLD + self.data[row][col].char + self.ENDCOL, - else: - # display only revealed - if self.data[row][col].revealed is True: - print self.BOLD + self.data[row][col].char + self.ENDCOL, - # else print a block - else: - print self.GREY + '.' + self.ENDCOL, - else: - print self.GREY + self.data[row][col].char + self.ENDCOL, - - print ' ' + self.BLUE + str(row) + self.ENDCOL - # freeze the grid numbers etc. def freeze_grid (self): # numbering @@ -208,3 +174,13 @@ class CrosswordPuzzle: self.frozen_grid = True + # unfreeze the grid numbers etc. + def unfreeze_grid (self): + # run through the grid + for row in range (self.rows): + for col in range (self.cols): + self.data[row][col].numbered = 0 + if (self.data[row][col].occupied_across is False and + self.data[row][col].occupied_down is False): + self.data[row][col].char = '.' + diff --git a/crosswordpuzzlecreator.py b/crosswordpuzzlecreator.py index ae1f8ca..42d4fdd 100644 --- a/crosswordpuzzlecreator.py +++ b/crosswordpuzzlecreator.py @@ -3,6 +3,7 @@ # Licensed under the GNU GPL v3 # Cross puzzle creator class +import sys import crosswordpuzzle @@ -15,6 +16,12 @@ class CrosswordPuzzleCreator: BLUE = '\033[34m' # grey GREY = '\033[30m' + # Black background white text + BLACK_BG = '\033[40;1;37m' + # white background black text + WHITE_BG = '\033[47;0;30m' + # white background red text + REDWHITE_BG = '\033[47;0;31m' # disable colors ENDCOL = '\033[0m' @@ -30,67 +37,120 @@ class CrosswordPuzzleCreator: # get row, col and print them (with grid number if set) for col in range (self.puzzle.cols): # print the first row as column headers - print self.BLUE + ' ' + str (col) + self.ENDCOL, - print + sys.stdout.write (self.BLUE + ' %02d' % col + self.ENDCOL) + sys.stdout.write ("\n") for row in range (self.puzzle.rows): for col in range (self.puzzle.cols): # print the data # if the cell is numbered i.e. start of a word if self.puzzle.data[row][col].numbered != 0: - print self.BRICKRED + str(self.puzzle.data[row][col].numbered) + self.ENDCOL, - # print a space + sys.stdout.write (self.REDWHITE_BG + + '%02d ' % self.puzzle.data[row][col].numbered + self.ENDCOL) + # print spaces else: - print ' ', + if self.puzzle.data[row][col].char == "#": + sys.stdout.write (self.BLACK_BG + ' ' + self.ENDCOL) + else: + sys.stdout.write (self.WHITE_BG + ' ' + self.ENDCOL) # if the character is not a blank or a block - if self.puzzle.data[row][col].char <> "." and self.puzzle.data[row][col].char <> "#": + if (self.puzzle.data[row][col].char <> "." and + self.puzzle.data[row][col].char <> "#"): # if words are to be shown regardless of hidden/revealed state if no_words is False: - print self.BOLD + self.puzzle.data[row][col].char + self.ENDCOL, + sys.stdout.write (self.WHITE_BG + + self.puzzle.data[row][col].char + self.ENDCOL) else: # display only revealed if self.puzzle.data[row][col].revealed is True: - print self.BOLD + self.puzzle.data[row][col].char + self.ENDCOL, - # else print a block + sys.stdout.write (self.WHITE_BG + + self.puzzle.data[row][col].char + self.ENDCOL) + # else print a blank else: - print self.GREY + '.' + self.ENDCOL, - else: - print self.GREY + self.puzzle.data[row][col].char + self.ENDCOL, + sys.stdout.write (self.WHITE_BG + '.' + self.ENDCOL) + elif self.puzzle.data[row][col].char == '.': + sys.stdout.write (self.WHITE_BG + + self.puzzle.data[row][col].char + self.ENDCOL) + elif self.puzzle.data[row][col].char == '#': + sys.stdout.write (self.BLACK_BG + " " + self.ENDCOL) + + sys.stdout.write (' ' + self.BLUE + "%2d" % row + self.ENDCOL + "\n") + raw_input (self.BRICKRED + "Press to continue" + self.ENDCOL) + + # add a word to the puzzle + def add_word (self, across=True): + # first display the grid + self.print_puzzle () + # get the row and column + srow = raw_input (self.BRICKRED + "Start row: " + self.ENDCOL) + scol = raw_input (self.BRICKRED + "Start col: " + self.ENDCOL) + # try converting it to number + try: + row = int (srow) + col = int (scol) + except ValueError: + sys.stderr.write ("Invalid row or column\n") + return + # get the word + word = raw_input (self.BRICKRED + "Word: " + self.ENDCOL) - print ' ' + self.BLUE + str(row) + self.ENDCOL - raw_input (self.GREY + "Press to continue" + self.ENDCOL) + # try to add the word to the puzzle grid + try: + if across == True: + self.puzzle.set_word_across (row, col, word) + else: + self.puzzle.set_word_down (row, col, word) + except crosswordpuzzle.TooLongWordException: + sys.stderr.write ("Word is too long to fit in the grid! Aborting.\n") + except crosswordpuzzle.IntersectWordException: + sys.stderr.write ("Word intersects badly with another word!\n") + except crosswordpuzzle.FrozenGridException: + sys.stderr.write ("Word cannot be added to a frozen puzzle.\n") # Puzzle loop def do_puzzle_loop (self): # there is a current file while True: if self.current_file and self.puzzle: - print self.BOLD + "-----------------------------------" - print "Puzzle: " + self.current_file - print "-----------------------------------" + self.ENDCOL - print self.BLUE + "1. Display grid" - print "2. Add across word" - print "3. Add down word" - print "4. Freeze grid" - print "5. Unfreeze grid" - print "6. Save puzzle" - print "X. Exit to main menu" + self.ENDCOL + sys.stdout.write (self.BOLD + "\n-----------------------------------\n") + sys.stdout.write ("Puzzle: " + self.current_file + "\n") + sys.stdout.write ("-----------------------------------" + self.ENDCOL + "\n") + sys.stdout.write (self.BLUE + "1. Display grid\n") + sys.stdout.write ("2. Add across word\n") + sys.stdout.write ("3. Add down word\n") + sys.stdout.write ("4. Remove across word\n") + sys.stdout.write ("5. Remove down word\n") + sys.stdout.write ("6. Freeze grid\n") + sys.stdout.write ("7. Unfreeze grid\n") + sys.stdout.write ("8. Set a clue\n") + sys.stdout.write ("9. Remove clue\n") + sys.stdout.write ("S. Save puzzle\n") + sys.stdout.write ("X. Exit to main menu\n" + self.ENDCOL) ch = raw_input (self.BRICKRED + "Your choice: " + self.ENDCOL) if ch == "1": self.print_puzzle () + elif ch == "2": + self.add_word () + elif ch == "3": + self.add_word (False) + elif ch == "6": + self.puzzle.freeze_grid () + elif ch == "7": + self.puzzle.unfreeze_grid () elif ch == "X" or ch == "x": break # when user chooses new puzzle def on_new_puzzle (self): - self.current_file = raw_input ("New puzzle file name: ") - srows = raw_input ("Number of rows: ") - scols = raw_input ("Number of cols: ") + self.current_file = raw_input (self.BRICKRED + "New puzzle file name: " + + self.ENDCOL) + srows = raw_input (self.BRICKRED + "Number of rows: " + self.ENDCOL) + scols = raw_input (self.BRICKRED + "Number of cols: " + self.ENDCOL) try: rows = int (srows) cols = int (scols) except ValueError: - print "Invalid number of rows/columns" + sys.stderr.write ("Invalid number of rows/columns") return self.puzzle = crosswordpuzzle.CrosswordPuzzle (rows, cols) self.do_puzzle_loop () @@ -99,13 +159,12 @@ class CrosswordPuzzleCreator: def do_main_loop (self): # display the menu while True: - print - print self.BOLD + "-----------------------------------" - print "Get A Clue - Crossword Puzzle Maker" - print "-----------------------------------" + self.ENDCOL - print self.BLUE + "1. Start a new puzzle" - print "2. Open an existing puzzle" - print "X. Exit" + self.ENDCOL + sys.stdout.write (self.BOLD + "\n-----------------------------------\n") + sys.stdout.write ("Get A Clue - Crossword Puzzle Maker\n") + sys.stdout.write ("-----------------------------------\n" + self.ENDCOL) + sys.stdout.write (self.BLUE + "1. Start a new puzzle\n") + sys.stdout.write ("2. Open an existing puzzle\n") + sys.stdout.write ("X. Exit\n" + self.ENDCOL) ch = raw_input (self.BRICKRED + "Your choice: " + self.ENDCOL) if ch == '1': self.on_new_puzzle ()