small fixes

master
Tibor Bizjak 2019-09-06 19:43:35 +02:00
parent e68196be39
commit 8256ba1bf6
1 changed files with 16 additions and 18 deletions

View File

@ -11,14 +11,18 @@ ranks = "87654321"
files = "abcdefgh" files = "abcdefgh"
squares = cross(files, ranks) squares = cross(files, ranks)
pawn_ranks = "27" pawn_ranks = {"white" : '2',
home_ranks = "18" "black" : '7'
init_positions = {"pawn" : cross(files, pawn_ranks), }
"rook" : cross("ah", home_ranks), home_ranks = {"white" : '1',
"knight" : cross("bg", home_ranks), "black" : '8'
"bishop" : cross("cf", home_ranks), }
"queen" : cross("d", home_ranks), init_positions = {"pawn" : cross(files, pawn_ranks.values()),
"king" : cross("e", home_ranks) "rook" : cross("ah", home_ranks.values()),
"knight" : cross("bg", home_ranks.values()),
"bishop" : cross("cf", home_ranks.values()),
"queen" : cross("d", home_ranks.values()),
"king" : cross("e", home_ranks.values())
} }
AN_names = {'R' : "rook", AN_names = {'R' : "rook",
@ -27,12 +31,6 @@ AN_names = {'R' : "rook",
'Q' : "queen", 'Q' : "queen",
'K' : "king"} 'K' : "king"}
def get_rank(sq):
return int(sq[1])
def get_file(sq):
return sq[0]
def move(sq, v): def move(sq, v):
return chr(ord(sq[0]) + v[0]) + str(int(sq[1]) + v[1]) return chr(ord(sq[0]) + v[0]) + str(int(sq[1]) + v[1])
@ -208,9 +206,7 @@ class Game:
if frwd in squares and is_empty(frwd): if frwd in squares and is_empty(frwd):
r.append(frwd) r.append(frwd)
is_on_pawn_rank = color == "white" and get_rank(sq)==2 \ is_on_pawn_rank = pawn_ranks[color] == sq[1]
or \
color == "black" and get_rank(sq)==7
if is_on_pawn_rank and is_empty(jump): if is_on_pawn_rank and is_empty(jump):
r.append(jump) r.append(jump)
return r + [sq for sq in targets if can_eat(sq)] return r + [sq for sq in targets if can_eat(sq)]
@ -269,7 +265,7 @@ def test():
game = Game() game = Game()
assert len(squares) == 8**2 assert len(squares) == 8**2
assert sum(map(len, init_positions.values())) == 8*4 assert sum(map(len, init_positions.values())) == 8*4
moves = [("a2", "a3"), ("b1", "c3"), ("c3", "b5"), ("b8", "c6")] moves = [("a2", "a4"), ("b1", "c3"), ("c3", "b5"), ("b8", "c6")]
for m in moves: for m in moves:
game.move(*m) game.move(*m)
print (game) print (game)
@ -280,3 +276,5 @@ def test():
print(game.possible_moves("c3")) print(game.possible_moves("c3"))
print(game.is_attacked("black", "d8")) print(game.is_attacked("black", "d8"))
test()