X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=crosswordpuzzlecreator.py;h=4ffffb345a1d260711870eeb0506be4fe9893158;hp=710de09fb26c771efc87e8bd96b5aff8732c6bb2;hb=8178235fb21779eed3615e502f2c4fdebd42150e;hpb=2f775ee1c4d2c260221155494b2c7856bc38dafd diff --git a/crosswordpuzzlecreator.py b/crosswordpuzzlecreator.py index 710de09..4ffffb3 100644 --- a/crosswordpuzzlecreator.py +++ b/crosswordpuzzlecreator.py @@ -3,6 +3,7 @@ # Licensed under the GNU GPL v3 # Cross puzzle creator class + import sys import cPickle import readline @@ -146,9 +147,8 @@ class CrosswordPuzzleCreator: aword, arow, acol, alen = self.puzzle.get_word_across (row, col) sys.stdout.write (self.BLUE + "Across word at position: " + aword + "\n" + self.ENDCOL) clue = raw_input (self.BRICKRED + "Clue for across word: " + self.ENDCOL) - if clue: - self.puzzle.data[arow][acol].clue_across = clue - sys.stdout.write (self.BLUE + "Set clue: \n" + self.puzzle.data[arow][acol].clue_across) + self.puzzle.data[arow][acol].clue_across = clue + sys.stdout.write (self.BLUE + "Set the clue: \n" + self.puzzle.data[arow][acol].clue_across) except crosswordpuzzle.NoWordException: sys.stderr.write ("No across word found at that position\n") @@ -157,12 +157,53 @@ class CrosswordPuzzleCreator: dword, drow, dcol, dlen = self.puzzle.get_word_down (row, col) sys.stdout.write (self.BLUE + "Down word at position: " + dword + "\n" + self.ENDCOL) clue = raw_input (self.BRICKRED + "Clue for down word: " + self.ENDCOL) - if clue: - self.puzzle.data[drow][dcol].clue_down = clue - sys.stdout.write (self.BLUE + "Set clue: \n" + self.puzzle.data[drow][dcol].clue_down) + self.puzzle.data[drow][dcol].clue_down = clue + sys.stdout.write (self.BLUE + "Set the clue: \n" + self.puzzle.data[drow][dcol].clue_down) except crosswordpuzzle.NoWordException: sys.stderr.write ("No down word found at that position\n") + # remove a down word + def on_remove_down (self): + self.print_puzzle () + + srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL) + scol = raw_input (self.BRICKRED + "At col: " + self.ENDCOL) + try: + row = int (srow) + col = int (scol) + except ValueError: + sys.stderr.write ("Invalid row or column\n") + return + + try: + self.puzzle.remove_word_down (row, col) + sys.stdout.write (self.BLUE + "Down word removed\n" + self.ENDCOL) + except crosswordpuzzle.FrozenGridException: + sys.stderr.write ("Word cannot be removed from a frozen puzzle\n") + except crosswordpuzzle.NoWordException: + sys.stderr.write ("No down word found at that position\n") + + # remove an across word + def on_remove_across (self): + self.print_puzzle () + + srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL) + scol = raw_input (self.BRICKRED + "At col: " + self.ENDCOL) + try: + row = int (srow) + col = int (scol) + except ValueError: + sys.stderr.write ("Invalid row or column\n") + return + + try: + self.puzzle.remove_word_across (row, col) + sys.stdout.write (self.BLUE + "Across word removed\n" + self.ENDCOL) + except crosswordpuzzle.FrozenGridException: + sys.stderr.write ("Word cannot be removed from a frozen puzzle\n") + except crosswordpuzzle.NoWordException: + sys.stderr.write ("No across word found at that position\n") + # add a word to the puzzle def on_add_word (self, across=True): # first display the grid @@ -193,6 +234,26 @@ class CrosswordPuzzleCreator: except crosswordpuzzle.FrozenGridException: sys.stderr.write ("Word cannot be added to a frozen puzzle.\n") + # Export to image/HTML + def on_export_image (self, solution=True): + try: + sys.stdout.write (self.BLUE + "Exporting puzzle to image/HTML\n") + pngfile = raw_input (self.BRICKRED + "Filename (PNG): " + self.ENDCOL) + if solution is False: + htmlfile = raw_input (self.BRICKRED + "Filename (HTML): " + + self.ENDCOL) + puztitle = raw_input (self.BRICKRED + "Title of puzzle: " + + self.ENDCOL) + self.puzzle.export_image (pngfile, htmlfile, puztitle, solution) + else: + self.puzzle.export_image (pngfile) + + sys.stdout.write (self.BLUE + "Successfully exported!") + except crosswordpuzzle.FrozenGridException: + sys.stderr.write ("Cannot export as grid is not frozen/finalized\n") + except crosswordpuzzle.NoWordsException: + sys.stderr.write ("No words to export!\n") + # Export to across lite def on_export_acrosslite (self): try: @@ -231,8 +292,11 @@ class CrosswordPuzzleCreator: sys.stdout.write ("7. Unfreeze grid\n") sys.stdout.write ("8. Set clue for word\n") sys.stdout.write ("9. Display clues\n") + sys.stdout.write ("R. Reset grid\n") sys.stdout.write ("S. Save puzzle\n") sys.stdout.write ("E. Export to AcrossLite(TM) format\n") + sys.stdout.write ("H. Export puzzle as image/HTML\n") + sys.stdout.write ("I. Export solution as image\n") sys.stdout.write ("X. Exit to main menu\n" + self.ENDCOL) ch = raw_input (self.BRICKRED + "Your choice: " + self.ENDCOL) if ch == "1": @@ -241,6 +305,10 @@ class CrosswordPuzzleCreator: self.on_add_word () elif ch == "3": self.on_add_word (False) + elif ch == "4": + self.on_remove_across () + elif ch == "5": + self.on_remove_down () elif ch == "6": self.puzzle.freeze_grid () elif ch == "7": @@ -249,10 +317,16 @@ class CrosswordPuzzleCreator: self.on_set_clue () elif ch == "9": self.on_display_clues () + elif ch == "R" or ch == "r": + self.on_reset_grid () elif ch == "S" or ch == "s": self.save_puzzle () elif ch == "E" or ch == "e": self.on_export_acrosslite () + elif ch == "H" or ch == "h": + self.on_export_image (False) + elif ch == "I" or ch == "i": + self.on_export_image () elif ch == "X" or ch == "x": break @@ -278,6 +352,15 @@ class CrosswordPuzzleCreator: self.load_puzzle () self.do_puzzle_loop () + # when user chooses to reset grid + def on_reset_grid (self): + ans = raw_input (self.BRICKRED + + "This will clear the entire grid! Are you sure (Y/N)? " + self.ENDCOL) + if ans == "y" or ans == "Y": + self.puzzle.reset_grid () + sys.stdout.write (self.BLUE + "Grid has been cleared of all data!" + + self.ENDCOL + "\n") + # Main application loop def do_main_loop (self): # display the menu