app.html
001| '''Chessboard simulation and move parser.
001|
001| Commands:
001| -3 do_chess_3 - make an image of the board
001| -2 do_chess_2 - move pieces with ChessMove
001|
001| c2mp do_test_movepiece - test Chessboard.move_piece method
001| move do_test_chessmove - test ChessMove class
001| old1 do_chess_1 - display board (version 1)
001| test do_test - test
001|
001| queens do_eight_queens_images - make images of some Eight Queens solutions
001|
001| '''
002|
003| from sys import argv, stdout, exc_info
004| from traceback import extract_tb
005| from os import path
006| from PIL import Image, ImageDraw, ImageFont
007| from logger import logger,info,debug,trace, LogFilenameTimestamp
008| from goodies import KWARG, typename
009| from chessboard import Chessboard, ChessSet, ChessMove
010|
011| BasePath = r'C:\CJS\prj\Python\app\chess'
012| ImgsPath = r'C:\CJS\prj\Python\app\img'
013| Log = logger('Chess')
014|
015| CMND = '-3'
016| ARGS = {
017|
018| 'fpath': BasePath,
019|
020| }
021| Out = []
022|
023|
024|
025|
026| ExampleBoard = [
027| [ (1, 6), (1, 4), (1, 2), (1, 0), (1, 1), (1, 3), (1, 5), (1, 7) ],
028| [ (1,14), (1,12), (1,10), (1, 8), (1, 9), (1,11), (1,13), (1,15) ],
029| [ (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0) ],
030| [ (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0) ],
031| [ (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0) ],
032| [ (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0) ],
033| [ (3,30), (3,28), (3,26), (3,24), (3,25), (3,27), (3,29), (3,31) ],
034| [ (3,22), (3,20), (3,18), (3,16), (3,17), (3,19), (3,21), (3,23) ]
035| ]
036|
037|
038| ExamplePieces = [
039| (1,(0,3)), (1,(0,4)), (1,(0,2)), (1,(0,5)), (1,(0,1)), (1,(0,6)), (1,(0,0)), (1,(0,7)),
040| (1,(1,3)), (1,(1,4)), (1,(1,2)), (1,(1,5)), (1,(1,1)), (1,(1,6)), (1,(1,0)), (1,(1,7)),
041| (3,(7,3)), (3,(7,4)), (3,(7,2)), (3,(7,5)), (3,(7,1)), (3,(7,6)), (3,(7,0)), (3,(7,7)),
042| (3,(6,3)), (3,(6,4)), (3,(6,2)), (3,(6,5)), (3,(6,1)), (3,(6,6)), (3,(6,0)), (3,(6,7))
043| ]
044|
045|
046|
047| def do_chess_3 (**kwargs):
048| """CMND: Chess 3."""
049| fpath = KWARG('fpath', BasePath, str, kwargs)
050| fname = KWARG('fname', 'chess3.png', str, kwargs)
051| move1 = KWARG('m1', 'P-K4', str, kwargs)
052| move2 = KWARG('m2', 'P-K4', str, kwargs)
053| move3 = KWARG('m3', 'p-q4', str, kwargs)
054| move4 = KWARG('m4', 'p-q3', str, kwargs)
055| move5 = KWARG('m5', 'KB-B4', str, kwargs)
056| move6 = KWARG('m6', 'N-QR3', str, kwargs)
057| Log.info('chess-3:: %s' % str(list(kwargs.keys())))
058| Log.debug('fpath="%s"' % fpath)
059| Log.debug('fname="%s"' % fname)
060| Log.debug('move-1="%s"' % move1)
061| Log.debug('move-2="%s"' % move2)
062| Log.debug('move-3="%s"' % move3)
063| Log.debug('move-4="%s"' % move4)
064| Log.debug('move-5="%s"' % move5)
065| Log.debug('move-6="%s"' % move6)
066| Log.info()
067| from chessboard import BLACK, QUEEN, ChessPieceName
068|
069| brd = Chessboard()
070| Out.append(brd)
071|
072|
073| brd.parse(move1); brd.next_turn()
074| brd.parse(move2); brd.next_turn()
075| brd.parse(move3); brd.next_turn()
076| brd.parse(move4); brd.next_turn()
077| brd.parse(move5); brd.next_turn()
078| brd.parse(move6); brd.next_turn()
079|
080| cset = ChessSet()
081|
082| cset.pieces = [
083| [ChessPieceName[ix], cset.images[cset.ImgIndex[ix]], pc[1], True]
084| for ix,pc in enumerate(brd.pieces)
085| if pc[0]
086| ]
087|
088| cset.save_image(fname)
089|
090| Log.info()
091| return 'Done'
092|
093|
094| def do_chess_2 (**kwargs):
095| """CMND: Chess 2."""
096| fpath = KWARG('fpath', BasePath, str, kwargs)
097| fname = KWARG('fname', 'chess2.out', str, kwargs)
098| move1 = KWARG('m1', 'P-K4', str, kwargs)
099| move2 = KWARG('m2', 'P-K4', str, kwargs)
100| move3 = KWARG('m3', 'p-q4', str, kwargs)
101| move4 = KWARG('m4', 'p-q3', str, kwargs)
102| move5 = KWARG('m5', 'KB-B4', str, kwargs)
103| move6 = KWARG('m6', 'N-QR3', str, kwargs)
104| Log.info('chess-2:: %s' % str(list(kwargs.keys())))
105| Log.debug('fpath="%s"' % fpath)
106| Log.debug('fname="%s"' % fname)
107| Log.debug('move-1="%s"' % move1)
108| Log.debug('move-2="%s"' % move2)
109| Log.debug('move-3="%s"' % move3)
110| Log.debug('move-4="%s"' % move4)
111| Log.debug('move-5="%s"' % move5)
112| Log.debug('move-6="%s"' % move6)
113| Log.info()
114| brd = Chessboard()
115|
116|
117| Log.flush()
118| brd.list_board(Log.fp)
119| Log.flush()
120| Log.info()
121|
122|
123| brd.parse(move1); brd.next_turn()
124| brd.parse(move2); brd.next_turn()
125| brd.parse(move3); brd.next_turn()
126| brd.parse(move4); brd.next_turn()
127| brd.parse(move5); brd.next_turn()
128| brd.parse(move6); brd.next_turn()
129|
130|
131| Log.info()
132| Log.flush()
133| brd.list_board(Log.fp)
134| Log.flush()
135| Log.info()
136|
137| Log.info()
138| return 'Done'
139|
140|
141| def do_chess_1 (**kwargs):
142| """CMND: Chess 1. (2016?)"""
143| fpath = KWARG('fpath', BasePath, str, kwargs)
144| fname = KWARG('fname', 'chess1.out', str, kwargs)
145| Log.info('chess-1:: %s' % str(list(kwargs.keys())))
146| Log.debug('fpath="%s"' % fpath)
147| Log.debug('fname="%s"' % fname)
148| Log.info()
149| from chessboard1 import Chess1Board
150|
151| brd = Chess1Board()
152|
153| Log.flush()
154| brd.list_board(Log.fp)
155| Log.flush()
156|
157| Log.info()
158| return 'Done'
159|
160|
161| def do_test_movepiece (**kwargs):
162| """CMND: Exercise the Chessboard.move_piece method."""
163| fpath = KWARG('fpath', BasePath, str, kwargs)
164| fname = KWARG('fname', 'chess2.out', str, kwargs)
165| Log.info('test-move-piece:: %s' % str(list(kwargs.keys())))
166| Log.debug('fpath="%s"' % fpath)
167| Log.debug('fname="%s"' % fname)
168| Log.info()
169| brd = Chessboard()
170|
171|
172| Log.flush()
173| brd.list_board(Log.fp)
174| Log.flush()
175| Log.info()
176|
177|
178| brd.move_piece((1,3), (3,3))
179| brd.move_piece((6,3), (4,3))
180| brd.move_piece((1,4), (3,4))
181|
182|
183| Log.info()
184| Log.flush()
185| brd.list_board(Log.fp)
186| Log.flush()
187| Log.info()
188|
189| Log.info()
190| return 'Done'
191|
192|
193| def do_test_chessmove (**kwargs):
194| """CMND: Exercise the ChessMove class."""
195| fpath = KWARG('fpath', BasePath, str, kwargs)
196| fname = KWARG('fname', 'chess2.out', str, kwargs)
197| move1 = KWARG('m1', 'P-K3', str, kwargs)
198| move2 = KWARG('m2', 'QP-Q4', str, kwargs)
199| move3 = KWARG('m3', 'KBP-B3', str, kwargs)
200| move4 = KWARG('m4', 'BP-N6', str, kwargs)
201| move5 = KWARG('m5', 'P x P', str, kwargs)
202| move6 = KWARG('m6', 'N-QR3', str, kwargs)
203| Log.info('test-chessmove:: %s' % str(list(kwargs.keys())))
204| Log.debug('fpath="%s"' % fpath)
205| Log.debug('fname="%s"' % fname)
206| Log.debug('move-1="%s"' % move1)
207| Log.debug('move-2="%s"' % move2)
208| Log.debug('move-3="%s"' % move3)
209| Log.debug('move-4="%s"' % move4)
210| Log.debug('move-5="%s"' % move5)
211| Log.debug('move-6="%s"' % move6)
212| Log.info()
213| mv = ChessMove()
214|
215| def parse_move (move_text):
216| mv.parse(move_text)
217| Log.info('> "%s"' % mv.instr)
218| Log.info('= %s' % mv)
219| Log.info()
220|
221| parse_move('')
222| parse_move(move1)
223| parse_move(move2)
224| parse_move(move3)
225| parse_move(move4)
226| parse_move(move5)
227| parse_move(move6)
228|
229| Log.info()
230| return 'Done'
231|
232|
233| def do_eight_queens_images (**kwargs):
234| """CMND: Create images of some Eight Queens solutions."""
235| fpath = KWARG('fpath', BasePath, str, kwargs)
236| fname = KWARG('fname', 'queens%02d.png', str, kwargs)
237| Log.info('8queens-images:: %s' % str(list(kwargs.keys())))
238| Log.debug('fpath="%s"' % fpath)
239| Log.debug('fname="%s"' % fname)
240| Log.info()
241| from chessboard import BLACK, QUEEN, ChessPieceName
242| solutions = [
243| [1,5,8,6,3,7,2,4],
244| [1,6,8,3,7,4,2,5],
245| [2,7,3,6,8,5,1,4],
246| [3,5,7,1,4,2,8,6],
247| [4,8,5,3,1,7,2,6],
248| ]
249| cset = ChessSet()
250| px = BLACK+QUEEN
251| gx = cset.ImgIndex[-1]
252|
253| for ix in range(len(solutions)):
254|
255| cset.pieces = [
256| [ChessPieceName[px], cset.images[gx], (row-1,col), True]
257| for col,row in enumerate(solutions[ix])
258| ]
259|
260| fn = path.join(fpath, 'queens%02d.png' % (ix+1))
261| cset.save_image(fn)
262| Log.info('wrote: %s' % fn)
263|
264| Log.info()
265| Log.info()
266| return 'Done'
267|
268|
269| def do_test (**kwargs):
270| """CMND: Test."""
271| fpath = KWARG('fpath', BasePath, str, kwargs)
272| fname = KWARG('fname', 'chessboard.png', str, kwargs)
273| Log.info('test:: %s' % str(list(kwargs.keys())))
274| Log.debug('fpath="%s"' % fpath)
275| Log.debug('fname="%s"' % fname)
276| Log.info()
277|
278| cset = ChessSet()
279| cset.save_image(fname)
280|
281| Log.info()
282| return 'Done'
283|
284| def dispatch (cmd, **kwargs):
285| """Dispatch to a module function based on CMD."""
286| Log.info('CMD: %s' % cmd)
287| for kw in kwargs:
288| Log.debug('argument[%s]: %s' % (kw,kwargs[kw]))
289| Log.info()
290| if cmd == '-3': return do_chess_3(**kwargs)
291| if cmd == '-2': return do_chess_2(**kwargs)
292| if cmd == 'test': return do_test(**kwargs)
293| if cmd == 'move': return do_test_chessmove(**kwargs)
294| if cmd == 'c2mp': return do_test_movepiece(**kwargs)
295| if cmd == 'old1': return do_chess_1(**kwargs)
296| if cmd == 'queens': return do_eight_queens_images(**kwargs)
297| return 'Nothing to do!'
298|
299| if __name__ == '__main__':
300| print('autorun: %s' % argv[0])
301|
302| Log.start(path.join(BasePath,'chess.log'))
303| Log.level(trace())
304| Log.info(argv[0])
305| Log.info()
306| cmd = argv[1] if 1 < len(argv) else CMND
307| kwargs = ARGS
308| for a in argv[2:]:
309| nv = a.split('=', 1)
310| kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
311| Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
312| try:
313| print('CMD: %s' % cmd)
314| print()
315| obj = dispatch(cmd, **kwargs)
316| print()
317| print(obj)
318| Log.info()
319| Log.info(obj)
320| except:
321| etype, evalue, tb = exc_info()
322| ts = extract_tb(tb)
323| Log.error()
324| Log.error('%s: %s' % (etype.__name__,str(evalue)))
325| Log.error()
326| for t in ts[-5:]:
327| Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
328| Log.error(' %s' % t[3])
329| Log.error()
330| raise
331| finally:
332| Log.end()
333|
334| '''eof'''
335|