More chess stuff

This commit is contained in:
MikeTheWatchGuy 2018-10-11 13:38:19 -04:00
parent eba9ea62bb
commit b6fdd93850
1 changed files with 80 additions and 31 deletions

View File

@ -4,6 +4,8 @@ import io
from PIL import Image, ImageDraw, ImageTk, ImageFont from PIL import Image, ImageDraw, ImageTk, ImageFont
import sys import sys
from random import randint as rand from random import randint as rand
import chess
import chess.pgn
button_names = ('close', 'cookbook', 'cpu', 'github', 'pysimplegui', 'run', 'storage', 'timer') button_names = ('close', 'cookbook', 'cpu', 'github', 'pysimplegui', 'run', 'storage', 'timer')
@ -34,15 +36,6 @@ initial_board = [[ROOKB, KNIGHTB, BISHOPB, KINGB, QUEENB, BISHOPB, KNIGHTB, ROO
[PAWNW for _ in range(8)], [PAWNW for _ in range(8)],
[ROOKW, KNIGHTW, BISHOPW, KINGW, QUEENW, BISHOPW, KNIGHTW, ROOKW]] [ROOKW, KNIGHTW, BISHOPW, KINGW, QUEENW, BISHOPW, KNIGHTW, ROOKW]]
def get_image_bytes(image64):
image_file = io.BytesIO(base64.b64decode(image64))
img = Image.open(image_file)
bio = io.BytesIO()
img.save(bio, format='PNG')
imgbytes = bio.getvalue()
return imgbytes
def DrawBoard():
blank = os.path.join(CHESS_PATH, 'blank.png') blank = os.path.join(CHESS_PATH, 'blank.png')
bishopB = os.path.join(CHESS_PATH, 'bishopb.png') bishopB = os.path.join(CHESS_PATH, 'bishopb.png')
bishopW = os.path.join(CHESS_PATH, 'bishopw.png') bishopW = os.path.join(CHESS_PATH, 'bishopw.png')
@ -57,15 +50,48 @@ def DrawBoard():
kingB = os.path.join(CHESS_PATH, 'kingb.png') kingB = os.path.join(CHESS_PATH, 'kingb.png')
kingW = os.path.join(CHESS_PATH, 'kingw.png') kingW = os.path.join(CHESS_PATH, 'kingw.png')
images = { BISHOPB:bishopB, BISHOPW:bishopW, PAWNB:pawnB, PAWNW:pawnW, KNIGHTB : knightB, KNIGHTW: knightW, ROOKB:rookB, ROOKW:rookW, KINGB:kingB, KINGW:kingW, QUEENB:queenB, QUEENW:queenW, BLANK:blank}
images = {BISHOPB: bishopB, BISHOPW: bishopW, PAWNB: pawnB, PAWNW: pawnW, KNIGHTB: knightB, KNIGHTW: knightW,
ROOKB: rookB, ROOKW: rookW, KINGB: kingB, KINGW: kingW, QUEENB: queenB, QUEENW: queenW, BLANK: blank}
def get_a_move():
board = chess.Board()
pgn = open('C:/Python/PycharmProjects/GooeyGUI/Chess/game.pgn')
first_game = chess.pgn.read_game(pgn)
for move in first_game.main_line():
print(move)
yield move
def render_square(image, key, location):
if (location[0] + location[1]) % 2:
color = '#B58863'
else:
color = '#F0D9B5'
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', color), pad=(0, 0), key=key)
def redraw_board(window, board):
for i in range(8):
for j in range(8):
if (i+j) % 2:
color = '#B58863'
else:
color = '#F0D9B5'
piece_image = images[board[i][j]]
elem = window.FindElement(key=(i,j))
elem.Update(button_color = ('white', color),
image_filename=piece_image,)
def update_board(board):
# board[5][5] = KINGB
return board
def DrawBoard():
sg.SetOptions(auto_size_buttons=True, margins=(0,0), button_color=sg.COLOR_SYSTEM_DEFAULT) sg.SetOptions(auto_size_buttons=True, margins=(0,0), button_color=sg.COLOR_SYSTEM_DEFAULT)
def render_tan(image):
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', '#B58863'), pad=(0, 0), key='_close_')
def render_brn(image):
return sg.RButton('', image_filename=image, size=(1, 1), button_color=('white', '#F0D9B5'), pad=(0, 0), key='_close_')
brn = sg.RButton('',image_filename=blank, size=(1,1), button_color=('white','#B58863') ,pad=(0,0), key='_close_') brn = sg.RButton('',image_filename=blank, size=(1,1), button_color=('white','#B58863') ,pad=(0,0), key='_close_')
tan = sg.RButton('', image_filename=blank, size=(4,3), button_color=('white','#F0D9B5') ,pad=(0,0)) tan = sg.RButton('', image_filename=blank, size=(4,3), button_color=('white','#F0D9B5') ,pad=(0,0))
@ -77,12 +103,16 @@ def DrawBoard():
row = [sg.T(str(8-i)+' ', font='Any 13')] row = [sg.T(str(8-i)+' ', font='Any 13')]
for j in range(8): for j in range(8):
piece_image = images[board[i][j]] piece_image = images[board[i][j]]
if (i+j) % 2: row.append(render_square(piece_image, key=(i,j), location=(i,j)))
row.append(render_tan(piece_image)) # if (i+j) % 2:
else: # row.append(render_tan(piece_image, key=(i,j)))
row.append(render_brn(piece_image)) # else:
# row.append(render_brn(piece_image, key=(i,j)))
row.append(sg.T(str(8-i)+' ', font='Any 13'))
board_layout.append(row) board_layout.append(row)
board_layout.append([sg.T(' ')] + [sg.T(f'{a}', pad=((23,27),0), font='Any 13') for a in 'abcdefgh'])
controls_layout = [[sg.Text('Performance Parameters', font='Any 20')]] controls_layout = [[sg.Text('Performance Parameters', font='Any 20')]]
statistics_layout = [[sg.Text('Statistics', font=('Any 20'))]] statistics_layout = [[sg.Text('Statistics', font=('Any 20'))]]
@ -96,10 +126,29 @@ def DrawBoard():
keep_on_top=True, keep_on_top=True,
icon='kingb.ico').Layout(layout) icon='kingb.ico').Layout(layout)
pgn = open('C:/Python/PycharmProjects/GooeyGUI/Chess/game.pgn')
first_game = chess.pgn.read_game(pgn)
moves = [move for move in first_game.main_line()]
# ---===--- Loop taking in user input --- # # ---===--- Loop taking in user input --- #
i = 0
while True: while True:
board = update_board(board)
button, value = window.Read() button, value = window.Read()
if button is None: if button is None:
break break
move = moves[i]
move_from = move.from_square
move_to = move.to_square
print(move_from, move_to)
row = move_from//8
col = move_from%8
print(row, col)
piece = board[row][col]
board[row][col] = BLANK
row = move_to//8
col = move_to % 8
board[row][col] = piece
print(piece)
redraw_board(window, board)
i += 1
DrawBoard() DrawBoard()