Last active
June 4, 2023 07:12
-
-
Save naiemk/609945c37ee4729e819f318df522a4e2 to your computer and use it in GitHub Desktop.
Silly Chess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from subprocess import call | |
from time import sleep | |
import readchar | |
BOARD_L1=[ | |
'□■'*4, | |
'■□'*4, | |
]*4 | |
BOARD_L2=[ | |
'♜♞♝♛♚♝♞♜', | |
"\u265f"*8, | |
' '*8, | |
' '*8, | |
' '*8, | |
' '*8, | |
'♙'*8, | |
'♖♘♗♕♔♗♘♖', | |
] | |
BOARD_L2=[list(l) for l in BOARD_L2] | |
POS=[0,0] | |
HOLD=[9,9] | |
def print_board(current, hold): | |
call('clear') | |
(ci, cj) = current | |
(hi, hj) = hold | |
print(' 1 2 3 4 5 6 7 8') | |
for i in range(0, 8): | |
sys.stdout.write(chr(ord('a')+i)) | |
sys.stdout.write(' ') | |
for j in range(0, 8): | |
l2=BOARD_L2[i][j] | |
sys.stdout.write(l2 if l2 != ' ' else BOARD_L1[i][j]) | |
sys.stdout.write('↩' if ci==i and cj==j else ('*' if hi==i and hj==j else ' ')) | |
sys.stdout.write("\n") | |
print("\nUse arrows to move. Enter to select, <ctrl-D> to exit") | |
def move(i1, j1, i2, j2): | |
a, b=BOARD_L2[i1][j1], BOARD_L2[i2][j2] | |
BOARD_L2[i1][j1], BOARD_L2[i2][j2]=b,a | |
def on_key_release(key): | |
if key == '\x1b[C': | |
POS[1]+=1 | |
elif key == '\x1b[D': | |
POS[1]-=1 | |
elif key == '\x1b[A': | |
POS[0]-=1 | |
elif key == '\x1b[B': | |
POS[0]+=1 | |
elif key == '\n': | |
if HOLD[0]==9: | |
HOLD[0]=POS[0] | |
HOLD[1]=POS[1] | |
else: | |
return True | |
elif key == '\x04': | |
exit() | |
return False | |
def loop(): | |
while True: | |
print_board(POS,HOLD) | |
ch = readchar.readkey() | |
swap = on_key_release(ch) | |
if swap: | |
_from=HOLD | |
_to=POS | |
# TODO: validate the move... | |
move(_from[0],_from[1], _to[0],_to[1]) | |
HOLD[0]=9 | |
HOLD[1]=9 | |
sleep(0.3) | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment