ae1f8ca4014146f92443412217a88b651d500643
[getaclue.git] / crosswordpuzzlecreator.py
1 # Get A Clue (C) 2010 V. Harishankar
2 # Crossword puzzle maker program
3 # Licensed under the GNU GPL v3
4
5 # Cross puzzle creator class
6
7 import crosswordpuzzle
8
9 class CrosswordPuzzleCreator:
10 # ansi color codes for grid display
11 BRICKRED = '\033[31m'
12 # bold
13 BOLD = '\033[33m'
14 # blue
15 BLUE = '\033[34m'
16 # grey
17 GREY = '\033[30m'
18 # disable colors
19 ENDCOL = '\033[0m'
20
21 def __init__ (self):
22 self.do_main_loop ()
23 self.current_file = None
24 self.puzzle = None
25
26 # display the grid with words
27 def print_puzzle (self, no_words=False):
28 # if self.puzzle is not none
29 if self.puzzle:
30 # get row, col and print them (with grid number if set)
31 for col in range (self.puzzle.cols):
32 # print the first row as column headers
33 print self.BLUE + ' ' + str (col) + self.ENDCOL,
34 print
35
36 for row in range (self.puzzle.rows):
37 for col in range (self.puzzle.cols):
38 # print the data
39 # if the cell is numbered i.e. start of a word
40 if self.puzzle.data[row][col].numbered != 0:
41 print self.BRICKRED + str(self.puzzle.data[row][col].numbered) + self.ENDCOL,
42 # print a space
43 else:
44 print ' ',
45 # if the character is not a blank or a block
46 if self.puzzle.data[row][col].char <> "." and self.puzzle.data[row][col].char <> "#":
47 # if words are to be shown regardless of hidden/revealed state
48 if no_words is False:
49 print self.BOLD + self.puzzle.data[row][col].char + self.ENDCOL,
50 else:
51 # display only revealed
52 if self.puzzle.data[row][col].revealed is True:
53 print self.BOLD + self.puzzle.data[row][col].char + self.ENDCOL,
54 # else print a block
55 else:
56 print self.GREY + '.' + self.ENDCOL,
57 else:
58 print self.GREY + self.puzzle.data[row][col].char + self.ENDCOL,
59
60 print ' ' + self.BLUE + str(row) + self.ENDCOL
61 raw_input (self.GREY + "Press <return> to continue" + self.ENDCOL)
62
63 # Puzzle loop
64 def do_puzzle_loop (self):
65 # there is a current file
66 while True:
67 if self.current_file and self.puzzle:
68 print self.BOLD + "-----------------------------------"
69 print "Puzzle: " + self.current_file
70 print "-----------------------------------" + self.ENDCOL
71 print self.BLUE + "1. Display grid"
72 print "2. Add across word"
73 print "3. Add down word"
74 print "4. Freeze grid"
75 print "5. Unfreeze grid"
76 print "6. Save puzzle"
77 print "X. Exit to main menu" + self.ENDCOL
78 ch = raw_input (self.BRICKRED + "Your choice: " + self.ENDCOL)
79 if ch == "1":
80 self.print_puzzle ()
81 elif ch == "X" or ch == "x":
82 break
83
84 # when user chooses new puzzle
85 def on_new_puzzle (self):
86 self.current_file = raw_input ("New puzzle file name: ")
87 srows = raw_input ("Number of rows: ")
88 scols = raw_input ("Number of cols: ")
89 try:
90 rows = int (srows)
91 cols = int (scols)
92 except ValueError:
93 print "Invalid number of rows/columns"
94 return
95 self.puzzle = crosswordpuzzle.CrosswordPuzzle (rows, cols)
96 self.do_puzzle_loop ()
97
98 # Main application loop
99 def do_main_loop (self):
100 # display the menu
101 while True:
102 print
103 print self.BOLD + "-----------------------------------"
104 print "Get A Clue - Crossword Puzzle Maker"
105 print "-----------------------------------" + self.ENDCOL
106 print self.BLUE + "1. Start a new puzzle"
107 print "2. Open an existing puzzle"
108 print "X. Exit" + self.ENDCOL
109 ch = raw_input (self.BRICKRED + "Your choice: " + self.ENDCOL)
110 if ch == '1':
111 self.on_new_puzzle ()
112 if ch == 'x' or ch == 'X':
113 break