eventhorizon02 wrote: ↑Mon Sep 21, 2020 6:17 pm
Hi,
I have written a C++ chess engine in the past as a learning experiment, the board was represented as a one dimensional array.
I am now attempting to write a very simple chess engine in Prolog for a class I'm taking in my old age.
It's going to be a super simple one and I'm still trying to figure out whats the best way to represent the board and the pieces.
There are no arrays in prolog, they can be somewhat simulated with lists.
How are you presenting your board, can you give some advise?
cheers
Hi, Just saw this. Sorry I've been away for a while. I use a modern version of Prolog "Visual Prolog" that you can download for education for free. Modern Prolog has come a long way since the Edenburge prolog your class might have you using. Your can write your own "array" classes in modern prolog, or use one from an existing library. Here are some of the constants and variables for my "Board" class. I use a customizable board size (in this example 8x8).
Code: Select all
constants
allow_no_piece : boolean = true.
validate_piece : boolean = false.
board_size : positive = 64.
row_count : positive = 8.
col_count : positive = 8.
Some class variables from my board.pro:
Code: Select all
facts
b_state : state := erroneous.
b : arrayM_inline{c_piece} := arrayM_inline{c_piece}::new(board_size). % Array here!
my_move : move := erroneous.
score_ : c_score := erroneous.
k_pos_w : c_pos := find_kings_(white) [constant].
k_pos_b : c_pos := find_kings_(black) [constant].
pos_bits_w : c_bb := get_pos_bits_w_() [constant].
pos_bits_b : c_bb := get_pos_bits_b_() [constant].
tlw : c_bb := 0.
tlb : c_bb := 0.
board_index : arrayM_inline{c_pos} := arrayM_inline{c_pos}::new(board_size). % Array here!
My Prolog engine uses lists everywhere, but for the board, I use a "modifiable" array from the library the Visual Prolog compiler provides. The code above where you see the comment % Array here! for "arrayM_inline" is the array class. The "M" stands for "modifiable", which as you know in Prolog you want to be careful with.
Now when I check the validity of moves on the board size, since we know the exact "distance" pieces can move when they make a "single" move (i.e. a single move for a bishop on my board for distance will be +-1 in a vertical position, and then +- 1 in horizontal position for a total of two. If the piece moves beyond the "distance" of 2 on an 8x8 board, we know that move is off the board. You have to take special consideration for the pawn (first possible move/captures), and the king (castling). And for example for an 8x8 size board this code, I use this code to calculate the distance.
Code: Select all
clauses
% i,i,o
distance(X, Y, D) :-
R1 = X div 8, C1 = X mod 8,
R2 = Y div 8, C2 = Y mod 8,
D = math::abs(R1-R2) + math::abs(C1-C2).
If the distance, does not equal the "fact" assertion of the move distance for a particular piece then it is invalid. The predicate that would call this is a "determ" predicate, so if the move is invalid, it is rejected.
Sorry I only just saw this post, I hope even though I suspect your class project might be restricted to Edenburge Prolog, that the above will help a tiny bit. If your are restricted to Edenburge, PM me and I can give you advice on using a "string" board for simplicity (which is precisely what my NADYA1.0 experiment used). Most Prolog implementations have very good string manipulation packages.
In case this is not too late, I'll check my PM's here on TalkChess more often to help you out if you still need any advice.
Cheers,
CHoibakk