-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtorres.py
More file actions
executable file
·119 lines (102 loc) · 3.43 KB
/
Copy pathtorres.py
File metadata and controls
executable file
·119 lines (102 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
# El Ajedrecista by Torres-Quevedo (1912)
import chess as c
import sys, math, time
from random import random, choice
b = c.Board("K7/1R6/8/8/8/8/8/7k b - - 0 1")
# pick a random position for the Black King on the first six ranks
b.remove_piece_at(7)
b.set_piece_at(choice(range(6 * 8)), c.Piece(c.KING, c.BLACK))
rookdir = 1
COMPC = c.WHITE
PLAYC = c.BLACK
def wmove(x1, y1, x2, y2, reason):
"Get White move"
uci = "%s%s%s%s" % (
c.FILE_NAMES[x1],
c.RANK_NAMES[y1],
c.FILE_NAMES[x2],
c.RANK_NAMES[y2],
)
print("# ", reason)
return 0, [uci]
def getmove(b, silent=False, usebook=False):
"Get move for board"
global rookdir
# play random legal moves as Black to allow self play
if b.turn == c.BLACK:
bmove = choice(list(b.legal_moves))
return 0, [str(bmove)]
for i in b.piece_map().keys():
m = b.piece_at(i)
if m.piece_type == c.KING and m.color == c.WHITE:
wk = i
wkx, wky = c.square_file(i), c.square_rank(i)
if m.piece_type == c.KING and m.color == c.BLACK:
bk = i
bkx, bky = c.square_file(i), c.square_rank(i)
if m.piece_type == c.ROOK and m.color == c.WHITE:
wr = i
wrx, wry = c.square_file(i), c.square_rank(i)
if wr in b.attacks(bk):
if wrx < 4:
return wmove(wrx, wry, 7, wry, "move Rook right")
else:
return wmove(wrx, wry, 0, wry, "move Rook left")
else:
if wry - bky > 1:
return wmove(wrx, wry, wrx, wry - 1, "move Rook down")
else:
vd = wky - bky
hd = abs(wkx - bkx)
if vd > 2:
return wmove(wkx, wky, wkx, wky - 1, "move King down")
else:
if hd == 0:
return wmove(wrx, wry, wrx, wry - 1, "move Rook down")
elif hd % 2 == 0:
if wkx < bkx:
return wmove(wkx, wky, wkx + 1, wky, "move King towards King")
else:
return wmove(wkx, wky, wkx - 1, wky, "move King towards King")
else:
if wrx == 0:
rookdir = 1
if wrx == 7:
rookdir = -1
return wmove(wrx, wry, wrx + rookdir, wry, "move Rook horizontally")
if __name__ == "__main__":
while True: # game loop
while True:
print(b.unicode())
if sys.version < "3":
move = raw_input("Your move? ")
else:
move = input("Your move? ")
try:
try:
b.push_san(move)
except ValueError:
b.push_uci(move)
except:
print("Sorry? Try again. (Or type Control-C to quit.)")
else:
break
if b.result() != "*":
print("Game result:", b.result())
break
tt = time.time()
t, m = getmove(b)
print(
"My move: %u. %s ( calculation time spent: %u m %u s )"
% (
b.fullmove_number,
m[0],
(time.time() - tt) // 60,
(time.time() - tt) % 60,
)
)
b.push(c.Move.from_uci(m[0]))
if b.result() != "*":
print("Game result:", b.result())
break