Jump to content

Chess player: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 3 users not shown)
Line 14:
 
or use those components as part of a complete program, demonstrating your language's support for modularity.
 
 
=={{header|BASIC}}==
{{works with|QBasic}}
Line 21 ⟶ 19:
 
Encontrado en: http://www.petesqbsite.com/sections/express/issue23/Tut_QB_Chess.txt
<langsyntaxhighlight lang="qbasic">DEFINT A-Z
 
DECLARE SUB SQUARE (A, B, C)
Line 496 ⟶ 494:
PRINT MT$
COLOR 7, 0
END SUB</langsyntaxhighlight>
 
 
=={{header|Go}}==
There are a number of open source Chess programs written in Go on Github.
 
Rather than spend a lot of time trying to write my own (likely mediocre) program, I thought I'd simply post a link to [https://github.com/notnil/chess notnil/chess] which explains its various capabilities quite well. However, you need to look at the code itself to see that it can cope with all types of move including castling, ''en passant'' capture and promotion to a piece of the player's choice.
 
=={{header|Perl}}==
Primarily written to see if I could find all moves with one regex. The answer was "mostly", the main problem being
some moves require history (the current state of the board is not sufficient for castling and en passant).
I also wanted to try different methods of making moves. It does not play well, but then neither do I.
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 939 ⟶ 934:
then left click on the square the opponent's Pawn skipped over.
END
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Version 0.8.1+ contains demo\rosetta\chess.exw, a slightly cleaned-up copy of a 20-year old translation of TSCP.<br>
It isn't particularly good (though perhaps a reasonable starting point for something better), at over 1,600 lines it does not really bear any useful comparison to the lisp version, and is simply not worth posting on this site, especially in light of potential copyright issues.
 
=={{header|PicoLisp}}==
{{:Chess player/PicoLisp}}
 
=={{header|Python}}==
==={{libheader|pygame}}===
Line 959 ⟶ 951:
 
The default Unicode board may look wonky and misaligned with certain terminal fonts. To use an ASCII board instead (like in the output shown below), set UNICODE = False. If your terminal uses dark mode, set DARKMODE = True.
 
Increasing RANDFAC, e.g. to 10 or even 100, creates more variety in computer moves, so that there is less repetition in games and openings.
 
The computer may say some things that allude to the chess game in ''2001: A Space Odyssey'', by the way.
<langsyntaxhighlight lang="python"># Simple Python chess engine
# Computer plays Black
 
import sys, random, chess
from collections import Counter
 
UNICODE = True # Print board with Unicode symbols?
DARKMODE = False # Invert symbol colors?
RANDFAC = 1 # Randomness factor
 
board = chess.Board()
Line 1,012 ⟶ 1,007:
for yourmove in board.legal_moves:
board.push(yourmove)
if board.result() == "1-0": # Has White won? If so, avoid move.
board.pop()
moves[mymove] = -1000
break
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)) + RANDFAC * random.random()
p += mobility / 1000
#print(mymove, yourmove, p)
Line 1,029 ⟶ 1,028:
 
print(f"Game finished, result is {board.result()}")
hal9000()</langsyntaxhighlight>
{{Output}} (in ASCII)
<pre>$ python3 simplechess.py
Line 1,093 ⟶ 1,092:
18.Ke2 Bxa1 19.Kd2 Qxf1 20.Kc2 Qxf2+ 21.Kb1 Qb2# 0-1
</pre>
 
=={{header|Wren}}==
{{trans|BASIC}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "./traititerate" for Stepped
import "./fmt" for Fmt
import "./ioutil" for Input, Output
import "./str" for Str
 
var Board = List.filled(8, null)
Line 1,662 ⟶ 1,660:
}
 
Chess.start()</langsyntaxhighlight>
9,476

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.