Reset grid functionality implemented
[getaclue.git] / crosswordpuzzlecreator.py
index cdd233f..1e62315 100644 (file)
@@ -94,7 +94,7 @@ class CrosswordPuzzleCreator:
                        raw_input (self.BRICKRED + "Press <return> to continue" + self.ENDCOL)
 
        # display existing clues
-       def display_clues (self):
+       def on_display_clues (self):
                try:
                        aclues = self.puzzle.get_clues_across ()
                        sys.stdout.write (self.BOLD + "\n------------\n")
@@ -128,7 +128,7 @@ class CrosswordPuzzleCreator:
                raw_input (self.BRICKRED + "Press <return> to continue" + self.ENDCOL)
 
        # set a clue to a word
-       def set_clue (self):
+       def on_set_clue (self):
                self.print_puzzle ()
                # get the row and column
                srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL)
@@ -146,9 +146,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,14 +156,13 @@ 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")
 
        # add a word to the puzzle
-       def add_word (self, across=True):
+       def on_add_word (self, across=True):
                # first display the grid
                self.print_puzzle ()
                # get the row and column
@@ -193,6 +191,47 @@ 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:
+                       sys.stdout.write (self.BLUE + "Exporting to AcrossLite(tm) Format\n" +
+                                               self.ENDCOL)
+                       title = raw_input (self.BRICKRED + "Puzzle title: " + self.ENDCOL)
+                       name = raw_input (self.BRICKRED + "Author name: " + self.ENDCOL)
+                       copyright = raw_input (self.BRICKRED + "Copyright: " + self.ENDCOL)
+                       exportfile = raw_input (self.BRICKRED + "Export to file: " + self.ENDCOL)
+
+                       acrosslite_str = self.puzzle.export_acrosslite (title, name, copyright)
+                       fexport = open (exportfile, "w")
+                       fexport.write (acrosslite_str)
+                       fexport.close ()
+                       sys.stdout.write (self.BLUE + "Exported AcrossLite(tm) File: " +
+                               exportfile + "\n" + self.ENDCOL)
+               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")
+
        # Puzzle loop
        def do_puzzle_loop (self):
                # there is a current file
@@ -210,25 +249,37 @@ 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":
                                        self.print_puzzle ()
                                elif ch == "2":
-                                       self.add_word ()
+                                       self.on_add_word ()
                                elif ch == "3":
-                                       self.add_word (False)
+                                       self.on_add_word (False)
                                elif ch == "6":
                                        self.puzzle.freeze_grid ()
                                elif ch == "7":
                                        self.puzzle.unfreeze_grid ()
                                elif ch == "8":
-                                       self.set_clue ()
+                                       self.on_set_clue ()
                                elif ch == "9":
-                                       self.display_clues ()
+                                       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
 
@@ -254,6 +305,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