Chess player: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: <del>will</del>) |
(→{{header|Python}}: add a simple Python engine using python-chess) |
||
Line 954: | Line 954: | ||
==={{libheader|VPython}}=== |
==={{libheader|VPython}}=== |
||
There is a 3D-Chess-Board in the [http://vpython.org/contents/contributed/chessboard.py VPython contributed section]. |
There is a 3D-Chess-Board in the [http://vpython.org/contents/contributed/chessboard.py VPython contributed section]. |
||
==={{libheader|python-chess}}=== |
|||
A very simple chess engine (two-ply search plus mobility) using [https://github.com/niklasf/python-chess python-chess]. The computer plays Black. |
|||
The default Unicode board may look wonky and misaligned with certain terminal fonts. To use an ASCII board instead (like shown below), replace "print(board.unicode())" with "print(board)". |
|||
<lang python># Simple Python chess engine |
|||
# Computer plays Black |
|||
import sys, chess |
|||
from collections import Counter |
|||
board = chess.Board() |
|||
while not board.outcome(): |
|||
while True: |
|||
try: |
|||
move = input("Your move? ") |
|||
if move == "quit": sys.exit() |
|||
board.push_uci(move) |
|||
except ValueError: print("Sorry?") |
|||
else: break |
|||
moves = {} |
|||
for mymove in board.legal_moves: |
|||
board.push(mymove) |
|||
for yourmove in board.legal_moves: |
|||
board.push(yourmove) |
|||
v = Counter(board.fen().split()[0]) |
|||
p = (9 * (v['q']-v['Q']) + 5 * (v['r']-v['R']) + 3 * (v['b']-v['B']) |
|||
+ 3 * (v['n']-v['N']) + v['p'] - v['P']) |
|||
mobility = len(list(board.legal_moves)) |
|||
p += mobility / 1000 |
|||
#print(mymove, yourmove, p) |
|||
old = moves.get(mymove, 1e6) |
|||
if p < old: moves[mymove] = p |
|||
board.pop() |
|||
board.pop() |
|||
try: sel = sorted(moves.items(), key=lambda item: -item[1])[0][0] |
|||
except: break |
|||
print(sel) |
|||
board.push(sel) |
|||
print(board.unicode()) |
|||
print(f"Game finished, result is {board.result()}")</lang> |
|||
{{Output}} (in ASCII) |
|||
<pre>$ python3 simplechess.py |
|||
Your move? e2e4 |
|||
e7e6 |
|||
r n b q k b n r |
|||
p p p p . p p p |
|||
. . . . p . . . |
|||
. . . . . . . . |
|||
. . . . P . . . |
|||
. . . . . . . . |
|||
P P P P . P P P |
|||
R N B Q K B N R |
|||
Your move? g1f3 |
|||
d8f6 |
|||
r n b . k b n r |
|||
p p p p . p p p |
|||
. . . . p q . . |
|||
. . . . . . . . |
|||
. . . . P . . . |
|||
. . . . . N . . |
|||
P P P P . P P P |
|||
R N B Q K B . R |
|||
Your move?</pre> |
|||
Here is the PGN of a full game against Stockfish. Like many simple chess engines, it has an unfortunate tendency to rush out with its Queen early in the game, leading to its loss: |
|||
<pre> |
|||
[Event "Stockfish vs Python World Championship"] |
|||
[Site "Rosetta Code"] |
|||
[Date "2022.05.08"] |
|||
[Round "1"] |
|||
[White "Stockfish"] |
|||
[Black "Python Simple Chess"] |
|||
[Result "1-0"] |
|||
1.e4 e6 2.Nf3 Qf6 3.Nc3 Qf4 4.d4 Qg4 5.Be2 Qxg2 6.Rg1 Qh3 7.Bg5 Nc6 8.Rg3 |
|||
Qh5 9.Nh4 Qxg5 10.Rxg5 Bd6 11.Rxg7 Nf6 12.e5 Bxe5 13.dxe5 Nxe5 14.Qd4 Ke7 |
|||
15.Qxe5 Ne8 16.Qg5+ Kd6 17.O-O-O+ Kc6 18.Qb5# 1-0 |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |