8
8
import curses
9
9
from curses import wrapper
10
10
11
+ KOMI = 6.5
12
+ SIZE = 19
13
+
11
14
BLACK = - 1
12
15
EMPTY = 0
13
16
WHITE = 1
14
- SIZE = 19
15
17
MARGIN_X = 3
16
18
MARGIN_Y = 1
17
19
MESSAGE = (SIZE + 2 , 2 )
18
20
EMPTY = '·'
19
21
STONE = '●'
20
22
SPACE = ' '
21
-
22
23
VERSION = "0.4.2"
23
24
24
25
"""note that implementing curses will have breaking changes on API
@@ -98,7 +99,6 @@ def logo():
98
99
99
100
100
101
def curses_setup (stdscr ):
101
- """Sets up the screen and paints the pond blue"""
102
102
stdscr .clear () # Clear the screen
103
103
curses .noecho () # Turn off echoing of keys
104
104
curses .cbreak () # Turn off normal tty line buffering
@@ -339,7 +339,6 @@ def clear_message(stdscr):
339
339
340
340
341
341
def play (stdscr ):
342
- curses_setup (stdscr )
343
342
moves = [] # moves (y,x), for undo. None if pass
344
343
captures = [] # list of stones removed, per move
345
344
n_toggle = 0
@@ -357,7 +356,6 @@ def play(stdscr):
357
356
if c == 10 or c == ord (" " ):
358
357
if stones .legal_placement (y , x , color , captures ):
359
358
place_piece (y , x , color , stones , moves , captures , stdscr )
360
- # moves.append((y, x))
361
359
color *= - 1
362
360
update_to_play = True
363
361
else :
@@ -376,9 +374,7 @@ def play(stdscr):
376
374
if stones .legal_placement (y , x , color , captures ):
377
375
place_piece (y , x , color , stones ,
378
376
moves , captures , stdscr )
379
- # moves.append((y, x))
380
377
color *= - 1
381
- update_to_play = True
382
378
else :
383
379
error_out (stdscr , "Not a valid move" )
384
380
break
@@ -412,7 +408,6 @@ def play(stdscr):
412
408
n_toggle = 0
413
409
else :
414
410
i = 1
415
- print (moves )
416
411
for move in moves :
417
412
if move in stones .black :
418
413
stdscr .addstr (move [0 ], move [1 ], str (i ),
@@ -469,10 +464,130 @@ def play(stdscr):
469
464
stdscr .refresh ()
470
465
471
466
467
+ def menu (stdscr ):
468
+ global SIZE
469
+ global KOMI
470
+ global MESSAGE
471
+ curses .curs_set (0 ) # Hide the cursor
472
+ stdscr .clear ()
473
+
474
+ # Define menu items and their associated values
475
+ menu_items = [
476
+ "Start" ,
477
+ "Size" ,
478
+ "Komi" ,
479
+ "Players" ,
480
+ "Help" ,
481
+ "Exit"
482
+ ]
483
+
484
+ screen_height , screen_width = stdscr .getmaxyx ()
485
+ center_y = screen_height // 2
486
+ center_x = screen_width // 2
487
+ square_height = 9
488
+ square_width = square_height * 2
489
+ square_y = center_y - (square_height // 2 )
490
+ square_x = center_x - (square_width // 2 )
491
+
492
+ for y in range (square_y , square_y + square_height ):
493
+ for x in range (square_x , square_x + square_width , 2 ):
494
+ stdscr .addstr (y , x , EMPTY + SPACE , curses .color_pair (1 ))
495
+ stdscr .addstr (square_y + 1 , square_x + 10 , "dango" , curses .color_pair (1 ) | curses .A_BOLD )
496
+ stdscr .addstr (square_y + 2 , square_x + 10 , STONE , curses .color_pair (7 ))
497
+ stdscr .addstr (square_y + 3 , square_x + 12 , STONE , curses .color_pair (2 ))
498
+ stdscr .addstr (square_y + 4 , square_x + 14 , STONE , curses .color_pair (8 ))
499
+
500
+ menu_y = center_y - (len (menu_items ) // 2 )
501
+ for idx , item in enumerate (menu_items ):
502
+ x = center_x + (square_width // 2 ) + 4
503
+ y = menu_y + idx
504
+ stdscr .addstr (y , x , f" { item } " )
505
+
506
+ menu_len = len (menu_items )
507
+ max_len = len (max (menu_items , key = len ))
508
+ selected_item = 0
509
+ selected_value = None
510
+ tally = 0
511
+
512
+ while True :
513
+ if tally == 3 : # activate usage threshold
514
+ stdscr .addstr (square_y + 10 , square_x , "Use up-down to select item," , curses .color_pair (0 ))
515
+ stdscr .addstr (square_y + 11 , square_x , "left-right to change value, " , curses .color_pair (0 ))
516
+ stdscr .addstr (square_y + 12 , square_x , "and space or enter to continue." , curses .color_pair (0 ))
517
+ tally = 0
518
+
519
+ stdscr .addstr (center_y + (len (menu_items ) // 2 ) + 2 , 0 , " " * screen_width )
520
+ menu_values = [None , SIZE , KOMI , None , None , None ] # refresh SIZE, KOMI
521
+
522
+ for idx , item in enumerate (menu_items ):
523
+ x = center_x + (square_width // 2 ) + 4
524
+ y = menu_y + idx
525
+ if idx == selected_item :
526
+ if selected_item == 1 or selected_item == 2 : # SIZE or KOMI
527
+ stdscr .addstr (y , x , " " * 5 )
528
+ stdscr .addstr (y , x , f" { menu_values [selected_item ]} " , curses .A_BLINK | curses .A_BOLD )
529
+ stdscr .addstr (y , x - 2 , ">" )
530
+ stdscr .addstr (y , x + len (str (menu_values [selected_item ])) + 3 , "<" )
531
+ else :
532
+ # stdscr.addstr(y, x, f" {item}")
533
+ stdscr .addstr (y , x - 2 , ">" )
534
+ stdscr .addstr (y , x , f" { item } " , curses .color_pair (0 ) | curses .A_BOLD )
535
+ stdscr .addstr (y , x + len (item ) + 3 , "<" )
536
+ else :
537
+ stdscr .addstr (y , x , f" { item } " )
538
+ stdscr .addstr (y , x - 2 , " " )
539
+ stdscr .addstr (y , x + len (item ) + 3 , " " )
540
+
541
+ stdscr .refresh ()
542
+ c = stdscr .getch ()
543
+ if c == curses .KEY_UP :
544
+ selected_item = (selected_item - 1 ) % menu_len
545
+ selected_value = menu_values [selected_item ]
546
+ elif c == curses .KEY_DOWN :
547
+ selected_item = (selected_item + 1 ) % menu_len
548
+ selected_value = menu_values [selected_item ]
549
+ elif c == curses .KEY_LEFT :
550
+ if selected_item == 1 :
551
+ SIZE = max (1 , SIZE - 1 )
552
+ if selected_item == 2 :
553
+ KOMI = max (0 , KOMI - 1 )
554
+ elif c == curses .KEY_RIGHT :
555
+ if selected_item == 1 :
556
+ SIZE += 1
557
+ if selected_item == 2 :
558
+ KOMI += 0.5
559
+ elif c == 10 or c == ord (" " ):
560
+ for y in range (square_y + 10 , square_y + 13 ): # clear help/usage
561
+ stdscr .move (y , square_x )
562
+ stdscr .clrtoeol ()
563
+ if selected_item == 0 :
564
+ MESSAGE = (SIZE + 2 , 2 )
565
+ break
566
+ elif selected_item == menu_len - 1 :
567
+ exit ()
568
+ elif selected_item == 3 :
569
+ stdscr .addstr (square_y + 10 , square_x , "Only two-player is currently enabled." , curses .color_pair (0 ))
570
+ elif selected_item == 4 :
571
+ stdscr .addstr (square_y + 10 , square_x , "Run 'dango -h' for help." , curses .color_pair (0 ))
572
+ elif c == ord ('q' ):
573
+ exit ()
574
+ else :
575
+ tally += 1
576
+
577
+
578
+ def main (stdscr ):
579
+ curses_setup (stdscr )
580
+ menu (stdscr )
581
+ stdscr .clear ()
582
+ play (stdscr )
583
+
584
+
472
585
if __name__ == "__main__" :
473
586
try :
474
- wrapper (play )
587
+ wrapper (main )
475
588
except KeyboardInterrupt :
476
589
print ('Thank you for the game.' )
590
+ except curses .error :
591
+ print ("Terminal not large enough, resize and try again." )
477
592
# except:
478
593
# print("Something went wrong. Let us know, we'll try to fix it!")
0 commit comments