Began working on the player application
[getaclue.git] / crosswordpuzzlecreator.py
index ae1f8ca..4ffffb3 100644 (file)
@@ -4,6 +4,10 @@
 
 # Cross puzzle creator class
 
+import sys
+import cPickle
+import readline
+
 import crosswordpuzzle
 
 class CrosswordPuzzleCreator:
@@ -15,6 +19,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;1;30m'
+       # white background red text
+       REDWHITE_BG = '\033[47;1;31m'
        # disable colors
        ENDCOL = '\033[0m'
 
@@ -23,6 +33,20 @@ class CrosswordPuzzleCreator:
                self.current_file = None
                self.puzzle = None
 
+       # save the puzzle
+       def save_puzzle (self):
+               if self.current_file:
+                       fpuzzle = open (self.current_file, "wb")
+                       cPickle.dump (self.puzzle, fpuzzle, cPickle.HIGHEST_PROTOCOL)
+                       sys.stdout.write (self.BLUE + "Puzzle saved to: " +
+                                               self.current_file + "\n" + self.ENDCOL)
+
+       # load the puzzle
+       def load_puzzle (self):
+               if self.current_file:
+                       fpuzzle = open (self.current_file, "rb")
+                       self.puzzle = cPickle.load (fpuzzle)
+
        # display the grid with words
        def print_puzzle (self, no_words=False):
                # if self.puzzle is not none
@@ -30,84 +54,327 @@ 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 <return> to continue" + self.ENDCOL)
+
+       # display existing clues
+       def on_display_clues (self):
+               try:
+                       aclues = self.puzzle.get_clues_across ()
+                       sys.stdout.write (self.BOLD + "\n------------\n")
+                       sys.stdout.write ("Across words\n")
+                       sys.stdout.write ("------------\n" + self.ENDCOL)
+
+                       for word, clue in aclues:
+                               sys.stdout.write (self.BOLD + word[0] + ": " + self.ENDCOL)
+                               if clue:
+                                       sys.stdout.write (self.BLUE + clue + "\n" + self.ENDCOL)
+                               else:
+                                       sys.stdout.write (self.BLUE + "(No clue yet)\n" + self.ENDCOL)
+               except crosswordpuzzle.NoWordsException:
+                       sys.stderr.write ("No words across\n")
+
+               try:
+                       dclues = self.puzzle.get_clues_down ()
+                       sys.stdout.write (self.BOLD + "\n----------\n")
+                       sys.stdout.write ("Down words\n")
+                       sys.stdout.write ("----------\n" + self.ENDCOL)
+
+                       for word, clue in dclues:
+                               sys.stdout.write (self.BOLD + word[0] + ": " + self.ENDCOL)
+                               if clue:
+                                       sys.stdout.write (self.BLUE + clue + "\n" + self.ENDCOL)
+                               else:
+                                       sys.stdout.write (self.BLUE + "(No clue yet)\n" + self.ENDCOL)
+               except crosswordpuzzle.NoWordsException:
+                       sys.stderr.write ("No words down\n")
+
+               raw_input (self.BRICKRED + "Press <return> to continue" + self.ENDCOL)
+
+       # set a clue to a word
+       def on_set_clue (self):
+               self.print_puzzle ()
+               # get the row and column
+               srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL)
+               scol = raw_input (self.BRICKRED + "At 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
+
+               try:
+                       # across word set the clue if found
+                       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)
+                       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")
+
+               try:
+                       # down word set the clue if found
+                       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)
+                       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 ()
 
-                               print ' ' + self.BLUE + str(row) + self.ENDCOL
-                       raw_input (self.GREY + "Press <return> to continue" + self.ENDCOL)
+               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
+               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)
+
+               # 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")
+
+       # 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
-               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
+               if self.current_file and self.puzzle:
+                       while True:
+                               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 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.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":
+                                       self.puzzle.unfreeze_grid ()
+                               elif ch == "8":
+                                       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
 
        # 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 ()
 
+       # when user chooses to load puzzle
+       def on_load_puzzle (self):
+               self.current_file = raw_input (self.BRICKRED + "Puzzle to load: "
+                                               + self.ENDCOL)
+               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
                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 ()
+                       if ch == '2':
+                               self.on_load_puzzle ()
                        if ch == 'x' or ch == 'X':
                                break