intermmediate
This commit is contained in:
parent
022cb43c92
commit
720bb5b566
75
chess.py
75
chess.py
@ -11,6 +11,9 @@ ranks = 'RNBQKBNR'
|
|||||||
def next_turn(turn):
|
def next_turn(turn):
|
||||||
return 'B' if turn == 'W' else 'W'
|
return 'B' if turn == 'W' else 'W'
|
||||||
|
|
||||||
|
def get_turn_pieces(turn, whites, blacks):
|
||||||
|
return (whites, blacks) if turn == 'W' else (blacks, whites)
|
||||||
|
|
||||||
def create_board():
|
def create_board():
|
||||||
whites = {}
|
whites = {}
|
||||||
whites.update({vec2(c, 0): rank for (c, rank) in zip(range(8), ranks)})
|
whites.update({vec2(c, 0): rank for (c, rank) in zip(range(8), ranks)})
|
||||||
@ -40,14 +43,19 @@ def get_human_move(turn):
|
|||||||
s = input(turn + ' to move: ')
|
s = input(turn + ' to move: ')
|
||||||
return tuple(map(vec2_from_text, s.split()))
|
return tuple(map(vec2_from_text, s.split()))
|
||||||
|
|
||||||
|
def get_start_move(start_move):
|
||||||
|
print(start_move)
|
||||||
|
return tuple(map(vec2_from_text, start_move.split()))
|
||||||
|
|
||||||
|
|
||||||
def get_computer_move(turn, whites, blacks):
|
def get_computer_move(turn, whites, blacks):
|
||||||
return random.choice(get_valid_moves(turn, whites, blacks))
|
return random.choice(get_valid_moves(turn, whites, blacks))
|
||||||
|
|
||||||
def get_valid_dsts(src, whites, blacks):
|
def get_valid_dsts(src, turn, whites, blacks):
|
||||||
# todo: castling
|
# todo: castling
|
||||||
# todo: en passant
|
# todo: en passant
|
||||||
is_white_turn = src in whites
|
# todo: promotion
|
||||||
self_pieces, oppo_pieces = (whites, blacks) if is_white_turn else (blacks, whites)
|
self_pieces, oppo_pieces = get_turn_pieces(turn, whites, blacks)
|
||||||
|
|
||||||
piece = self_pieces[src]
|
piece = self_pieces[src]
|
||||||
valid_dsts = set()
|
valid_dsts = set()
|
||||||
@ -69,8 +77,8 @@ def get_valid_dsts(src, whites, blacks):
|
|||||||
delta += dir
|
delta += dir
|
||||||
|
|
||||||
if piece == 'P': # Pawn
|
if piece == 'P': # Pawn
|
||||||
dy = 1 if is_white_turn else -1
|
dy = 1 if turn == 'W' else -1
|
||||||
sy = 1 if is_white_turn else 6
|
sy = 1 if turn == 'W' else 6
|
||||||
dst_fw1 = src + vec2(0, dy)
|
dst_fw1 = src + vec2(0, dy)
|
||||||
dst_fw2 = src + vec2(0, dy * 2)
|
dst_fw2 = src + vec2(0, dy * 2)
|
||||||
dst_tkl = src + vec2(-1, dy)
|
dst_tkl = src + vec2(-1, dy)
|
||||||
@ -99,10 +107,10 @@ def get_valid_dsts(src, whites, blacks):
|
|||||||
|
|
||||||
def get_valid_moves(turn, whites, blacks):
|
def get_valid_moves(turn, whites, blacks):
|
||||||
self_pieces = whites if turn == 'W' else blacks
|
self_pieces = whites if turn == 'W' else blacks
|
||||||
return [(src, dst) for src in self_pieces for dst in get_valid_dsts(src, whites, blacks)]
|
return [(src, dst) for src in self_pieces for dst in get_valid_dsts(src, turn, whites, blacks)]
|
||||||
|
|
||||||
def next_board(src, dst, whites, blacks):
|
def next_board(src, dst, turn, whites, blacks):
|
||||||
self_pieces, oppo_pieces = (whites, blacks) if src in whites else (blacks, whites)
|
self_pieces, oppo_pieces = get_turn_pieces(turn, whites, blacks)
|
||||||
self_pieces[dst] = self_pieces[src] # Move src -> dst
|
self_pieces[dst] = self_pieces[src] # Move src -> dst
|
||||||
del self_pieces[src] # Remove src
|
del self_pieces[src] # Remove src
|
||||||
if dst in oppo_pieces: # Remove dst if it was an opponent
|
if dst in oppo_pieces: # Remove dst if it was an opponent
|
||||||
@ -111,14 +119,14 @@ def next_board(src, dst, whites, blacks):
|
|||||||
|
|
||||||
# Valid moves filtered such that the opponent cannot take the current player's king
|
# Valid moves filtered such that the opponent cannot take the current player's king
|
||||||
def get_legal_moves(turn, whites, blacks):
|
def get_legal_moves(turn, whites, blacks):
|
||||||
self_pieces, oppo_pieces = (whites, blacks) if turn == 'W' else (blacks, whites)
|
self_pieces, oppo_pieces = get_turn_pieces(turn, whites, blacks)
|
||||||
valid_moves = get_valid_moves(turn, whites, blacks)
|
valid_moves = get_valid_moves(turn, whites, blacks)
|
||||||
legal_moves = []
|
legal_moves = []
|
||||||
for src, dst in valid_moves:
|
for src, dst in valid_moves:
|
||||||
n_whites, n_blacks = next_board(src, dst, whites.copy(), blacks.copy())
|
n_whites, n_blacks = next_board(src, dst, turn, whites.copy(), blacks.copy())
|
||||||
n_turn = next_turn(turn)
|
n_turn = next_turn(turn)
|
||||||
n_self_pieces, n_oppo_pieces = (n_whites, n_blacks) if n_turn == 'W' else (n_blacks, n_whites)
|
n_self_pieces, n_oppo_pieces = get_turn_pieces(n_turn, n_whites, n_blacks)
|
||||||
n_valid_moves = get_valid_moves(n_turn, whites, blacks)
|
n_valid_moves = get_valid_moves(n_turn, n_whites, n_blacks)
|
||||||
legal = True
|
legal = True
|
||||||
for n_src, n_dst in n_valid_moves:
|
for n_src, n_dst in n_valid_moves:
|
||||||
if n_dst in n_oppo_pieces and n_oppo_pieces[n_dst] == 'K':
|
if n_dst in n_oppo_pieces and n_oppo_pieces[n_dst] == 'K':
|
||||||
@ -129,7 +137,7 @@ def get_legal_moves(turn, whites, blacks):
|
|||||||
return legal_moves
|
return legal_moves
|
||||||
|
|
||||||
def is_in_check(turn, whites, blacks):
|
def is_in_check(turn, whites, blacks):
|
||||||
self_pieces, oppo_pieces = (whites, blacks) if turn == 'W' else (blacks, whites)
|
self_pieces, oppo_pieces = get_turn_pieces(turn, whites, blacks)
|
||||||
n_turn = next_turn(turn)
|
n_turn = next_turn(turn)
|
||||||
threat_moves = get_valid_moves(n_turn, whites, blacks)
|
threat_moves = get_valid_moves(n_turn, whites, blacks)
|
||||||
for src, dst in threat_moves:
|
for src, dst in threat_moves:
|
||||||
@ -139,28 +147,43 @@ def is_in_check(turn, whites, blacks):
|
|||||||
|
|
||||||
turn = 'W'
|
turn = 'W'
|
||||||
whites, blacks = create_board()
|
whites, blacks = create_board()
|
||||||
|
start_moves = [
|
||||||
|
'D2 D4',
|
||||||
|
'E7 E5',
|
||||||
|
'D4 E5',
|
||||||
|
'D8 E7',
|
||||||
|
'E5 E6',
|
||||||
|
'E7 E6',
|
||||||
|
'E2 E4',
|
||||||
|
'F7 F5',
|
||||||
|
#'E4 F5' # Should not be legal
|
||||||
|
]
|
||||||
while True:
|
while True:
|
||||||
print(turn, 'to move')
|
print(turn, 'to move')
|
||||||
print_board(whites, blacks)
|
print_board(whites, blacks)
|
||||||
legal_moves = get_legal_moves(turn, whites, blacks)
|
legal_moves = get_legal_moves(turn, whites, blacks)
|
||||||
print(len(legal_moves), 'valid moves')
|
print(len(legal_moves), 'legal moves')
|
||||||
for src, dst in legal_moves:
|
self_pieces, oppo_pieces = get_turn_pieces(turn, whites, blacks)
|
||||||
print(src, '->', dst)
|
|
||||||
if len(legal_moves) == 0:
|
if len(legal_moves) == 0:
|
||||||
print('game is over')
|
print('game is over')
|
||||||
break
|
break
|
||||||
# if turn == 'W':
|
# if turn == 'W':
|
||||||
while True:
|
if len(start_moves):
|
||||||
try:
|
src, dst = get_start_move(start_moves.pop(0))
|
||||||
src, dst = get_human_move(turn)
|
else:
|
||||||
if not (src, dst) in legal_moves:
|
for src, dst in legal_moves:
|
||||||
print('invalid move')
|
print(f"{self_pieces[src]} {src}{'x' if dst in oppo_pieces else ' '}{dst}")
|
||||||
else:
|
while True:
|
||||||
break
|
try:
|
||||||
except ValueError:
|
src, dst = get_human_move(turn)
|
||||||
print('invalid input')
|
if not (src, dst) in legal_moves:
|
||||||
|
print('not a legal move')
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
except ValueError:
|
||||||
|
print('invalid input')
|
||||||
# else:
|
# else:
|
||||||
# src, dst = get_computer_move(turn, whites, blacks)
|
# src, dst = get_computer_move(turn, whites, blacks)
|
||||||
|
|
||||||
whites, blacks = next_board(src, dst, whites, blacks)
|
whites, blacks = next_board(src, dst, turn, whites, blacks)
|
||||||
turn = next_turn(turn)
|
turn = next_turn(turn)
|
||||||
|
Loading…
Reference in New Issue
Block a user