Chess Introduction.
Copyright © 1997, Leonid Liberman, leonidd at NO SPAM sympatico dot ca
Edited by: Particle,
bsptree at NO SPAM geocities dot com

Part 1

My intention is to give, in very general terms, the base of chess game writting. I will try to be as little language specific as possible. Nevertherless, when I will talk about direct programming, all procedures will be given in Assembler, more specifically, in Microsoft Assembler 6.1. But everyone can ask his personal question and I will try to respond at my best. Even more that this, if it will become obvious that there are big demand to be "code specific" I will be glad to do so. Everybody is free and invited to entrer into topic, by contributing his ideas and by indicating some flaws in the exposed one. Only few suggestions from my side. If you have something to say about my English, you are invited to do so but, probably, it can remediate not that much. English is not my native tongue. But if you will find something wrong in my Assembler (wich is my mother language), or in general logic, please do so. This will be extremely useful.

All ideas that you will find here are 100% original. Each line of my chess game was done only by me. Even the Assembler I studied on my own. I never read any books about the theory of the chess programming, but it is my intention to do so in the future. This is why if somebody knows already the normal chess game structure, and could tell me how far my logic differ from usual one, please tell me. Even my intention, later, to read few books on the subject is motivated, partially, by natural curiosity to recognize where I am. Mainly, it is the intention to verify if somehow the old ideas are not better that the new one. And how, doing the same engine, in completely independent way, everybody is constrained to struck the same road. How, in other words, all our ideas can be different because they are produced by different individuals, or how they are juste the same because they treat the same subject.

If your read some good book of this nature, please send me the title.

Game:

The most normal, before you will even start to play chess game, is to have your chessboard with all the pieces on it. And it is logical for us to start the same way here.

All the description of the table can be easily deposed in some "reference table". 64 bytes are enough for description. 1 byte for each square.

In this byte you can have (I have said, you can have, since there are one billion ways to do the same) description of the piece, his color and say if the square is occupied or empty. I took 3 bits for description of the piece. 1 bit for color. 1 bit to indicate if the square is free or empty. Two bits are left free. Probably you will say: and how about the position of the square on the board? In reality, you don't need this. The position of the byte inside of your description table gives you the position itself.


This is passage only for direct programmer.

In example here we will indicate the position of the square H1 (below-right). In the table that goes below, squares are deposed starting from the upper-left (A8). Initial position of the table is OFFSET CARREAU (table name is CARREAU).

mov si,offset carreau - initiale position of the table 
si + 00111111b - position of the piece in the end of the table. 
00111111b = 63, or equal to 64 squares, since we count from 0.

Generaly, you need to look into the reference table (chess board) in order to find the piece and later to find her moves. At the beginnig, you will go to look through your table, one byte (one square) at the time. When you will find your piece, the position of your OFFSET will say you the position of your piece on the chessboard. It is that simple. Only after, when you will find the moves of your piece, you will need to save the position of two squares for each move. So give 1 byte for the position of the initial square and 1 byte for position of the end square.

Finaly, 4 bytes to describe each move.
1 byte - position of the initial square, 1 byte - content of initial square.
1 byte - position of end square, 1 byte - content of end square. 

In my game I took:

1) 000b - rook
2) 001b - knight 
3) 010b - bishop
4) 011b - queen
5) 100b - king
6) 101b - pawn

Color:

1) 0b - black
2) 1b - white

Occupation:

1) 0b - occupied
2) 1b - empty.

Bits in each byte:

0,1,2 - piece.
3 - color.
4 - occupation.
Bits 6 and 7 are empty (put to zero).

Now few direct examples:

Initiale position of chessboard.

Square A8. Black rook - 00000000b
Square B8. Black knight - 00000001b 
Square C6. Empty square - 00010000b 
Square D1. White queen - 00001011b
Square A2. White pawn - 00001101b

And now description of the whole chess board:

carreau 	db 00000000b,00000001b,00000010b,00000011b,00000100b,00000010b
		db 00000001b,00000000b
		db 00000101b,00000101b,00000101b,00000101b,00000101b,00000101b
		db 00000101b,00000101b
		db 00010000b,00010000b,00010000b,00010000b,00010000b,00010000b
		db 00010000b,00010000b
		db 00010000b,00010000b,00010000b,00010000b,00010000b,00010000b
		db 00010000b,00010000b
		db 00010000b,00010000b,00010000b,00010000b,00010000b,00010000b
		db 00010000b,00010000b
		db 00010000b,00010000b,00010000b,00010000b,00010000b,00010000b
		db 00010000b,00010000b
		db 00001101b,00001101b,00001101b,00001101b,00001101b,00001101b
		db 00001101b,00001101b
		db 00001000b,00001001b,00001010b,00001011b,00001100b,00001010b
		db 00001001b,00001000b

7/9/97
leonidd at NO SPAM sympatico dot ca Leonid.


Part 2.

When you have your "reference table" you can reproduce immidiately the whole chessboard on the screen. This is something that you will do each time when you will start the game. You must just go and read one byte, at the time, from your "reference table". Decode what it want to say (piece or empty square). If the square is empty, go and depose the "empty" square. But before deposing the "empty" square you must recognize, after the position of the byte in the table, what is the color of this square. My game do this just after the logic. After the same logic it recognize the color inside of the square when it must depose the piece. If it is the piece, logic will recognize, easily, what piece must be depose and what is the color of this piece.

Obviously, you must write your pieces and know where you must depose them on the screen. But if you did even once something graphic in your life, it will be very simple. Chessboard is compose from 64 equal squares. There are nothing more, on the board, that 6 different pieces to write.White and black you will obtain "free of charge" by simply saying to your procedure what color you like the most.

Actually, I did my chess graphic after writing my Clock. The procedures for executing the elaborate (images like knight) and solid picture (like empty square) was taken from my previous program. Creation of the whole board with the pieces, that could be moved freely, was done in one week. But to the picture itself I came with all kind of changes, at least, one hundred times later.

Probably, after writing very bref revision of the chess game structure, I will return and will produce two logics for solid and elaborated graphic. I will do this with few direct examples. I believe that examples bring some adorable magic with them. Very often, they can spell with touching innocence some obvious truth that was easily forgotten.

When your graphic is done (but it is not necessary to have it) you are ready for so many things to come. All your logic can be linked to your graphic. Each part of the logic, that you will develop, could be better revision through the picture. Each time that your "reference table" was changed, you depose it on the screen, in order to see what your game have in mind. Very often, like in real life, what you tryed to say is not what the other man understood. From my experience, I can say you that it take not that much time to write the game, as to clean it from all kind of nasty and prolific bugs.


For direct programmer.

You will see, in order only to have some direct touch with the code, two procedures. The first one, FICHAS, look through the "reference table" and take the coordinates in variable SUR and the description of the square in the variable DEMANDE. Each description of square is send into procedure PIECE wich will recognize the piece, or empty square, and will depose it on the screen. Procedure PIECE include short logic for recognizing the inner color of the square itself.

comment 	# Le 2 juin 1993, 23 mars 1994, 14/4/1994,12/5/1994, 20/5/1994,2/10/95 15/10/95
		Placer les pieces sur ecran. Reviser les coordonnees et placer les coordonnees
		correspondantes.
		#

fichas proc uses ds es fs gs esi edi eax ebx ecx edx
		;---------Placer les pieces sur l'echiquier.------------------------
	mov 	cx,64
	mov 	si,seg carreau
	mov 	ds,si
	mov 	si,offset carreau
	mov 	dx,si 				;Placer la position initiale de la table en DX.
lopara:
	mov ax,si
	sub ax,dx
	mov sur,al
	mov al,byte ptr ds:[si]
	mov demande,al
	call piece 				;Placer une piece ou un carre sur l'ecran.
	inc si
	loop lopara
		;------------------Toutes pieces sont placees--------------------
	bt control,8 			;Quand bit 8 = 0 les coordonnees normales.
	jc enverso 				;Placer les coordonnees renversees.
	call coornor 			;Autrement, placer les coordonnees normales.
	jmp duio ;Sortir
enverso: 					;Placer les coordonnees renversees.
	call coorren
duio:
	ret
fichas endp



comment 	# Le vingt deux d'avril 1993, 4 avril 1994, 11 avril 1994, 15/9/1994
		30/10/94,19/11/95
		#

piece proc uses ds es fs gs esi edi eax ebx edx ecx 
		;Voir quoi on doit placer a la position indiquee et placer
		;le carre, ou la piece, a cette place.
		;-------------------Placer un carre sur l'ecran avec son image----------
		;-----Chercher la couleur du carre---------------------- 
	mov di,0 ;Si DI = 0 le nombre des lignes est paire.
		;------------------------------------------------------
	cmp sur,0 ;Si c'est juste le debut de la table d'echec le carre est gris
	je colorgris
		;-----------------------------------------------------
	xor eax,eax 			;Trouver, au debut le nombre total des lignes.
	mov al,sur
	shr ax,3 				;Diviser AX sur 8 (8 carres dans chaque ligne)
	cmp ax,0 				;Sur la ligne 0 ?
	je ligne0 				;Oui. Aller sur ligne 0.
		;-- Autrement le nombre des lignes est > 0. Voir si ce nombre se divise 
		;-- entierement sur 2.
	xor dx,dx
	mov cx,2
	div cx
	cmp dx,0 				;La division dur 2 est complete ?
	je ligne0 				;Oui.
	mov di,1 				;Autrement, la division est incomplete. Le nombre des lignes est
		;impaire.
		;----------------------------------------------------------
ligne0: 					;Regarder si le nombre des carres dans une ligne se divise
	xor dx,dx 				;entierement sur 2 ou non.
	mov al,sur
	and ax,0000000000000111b 	;Laisser seulement le nombre des carres dans 
	cmp ax,0 ;une ligne.
	je nulito
	mov cx,2
	div cx
	cmp dx,0 				;Le nombre des carres dans une ligne se divise completement sur 2?
	je nulito 				;Oui. Le nombre est pair (ou = 0).
		;-- Autrement, le nombre des carres dans une ligne est impaire.
	cmp di,0 				;Si le nombre des lignes est imapair DI > 0.
	ja colorgris 				;Le nombre des lignes est impair.
	jmp colorverde
		;--------------------------------------------------
nulito: 					;Le nombre de carres dans une ligne est paire.
	cmp di,0
	je colorgris
		;---------------------------------------------------------------
colorverde:
	call couleurdeux 			;Placer la couleur vert vive pour le carre.
	jmp bahar
colorgris:
	call couleurun 			;Placer la couleur gris pour le carreau.
bahar:
		;---------------La recherche de la couleur du carre est terminee.-------------
		;---------------Recherche des coordonnees.------------------
		;---------------------Recherche de la position verticale--------------
	mov al,sur 				;Placer les coordonnees en AL.
	and al,00000111b 		;Regarder seulement la position horizontale.

	cmp al,0
	jne q0
	call unver
	jmp after
q0:
	cmp al,1
	jne q1
	call deuxver
	jmp after
q1:
	cmp al,2
	jne q2
	call troisver
	jmp after
q2:
	cmp al,3
	jne q3
	call quatrever
	jmp after
q3:
	cmp al,4
	jne q4
	call cinqver
	jmp after
q4:
	cmp al,5
	jne q5
	call sixver
	jmp after
q5:
	cmp al,6
	jne q6
	call septver
	jmp after
q6:
	call huitver
after:
		;-------------------Fin de la recherche de la position verticale.-----------

		;------------------Chercher la position horizontale------------------
	mov al,sur 				;Placer en al le byte contenant les coordonnees.
	and al,00111000b 		;Regarder seulement la partie qui donne la position verticale.

	cmp al,00000000b
	jne e0
	call unhor
	jmp afhor
e0:
	cmp al,00001000b
	jne e1
	call deuxhor
	jmp afhor
e1:
	cmp al,00010000b
	jne e2
	call troishor
	jmp afhor
e2:
	cmp al,00011000b
	jne e3
	call quatrehor
	jmp afhor
e3:
	cmp al,00100000b
	jne e4
	call cinqhor
	jmp afhor
e4:
	cmp al,00101000b
	jne e5
	call sixhor
	jmp afhor
e5:
	cmp al,00110000b
	jne e6
	call septhor
	jmp afhor
e6:
	call huithor
afhor:
		;--------------La recherche de la position horizontale est terminee--------
	mov al,demande
	bt ax,4 				;Voir si le carre contient une figure. Si 4 bit=1 carre est libre.
	jc libre 				;Si il n'y a pas de figure, aller placer seulement un carre.
	bt ax,3 				;Retrouver la couleur de la piece.
	jnc amerique 			;La couleur est 0 (noire).
	call coloruno 			;Placer la premiere possible couleur de la piece (initier cun).
	jmp adrrt
amerique:
	call colordos 			;Placer la deuxieme possible couleur de la piece (initier cdeux).
adrrt:
	and al,00000111b 		;Voir seulement la partie qui identifie la figure.

	cmp al,0 				;La tour ?
	jne g0 				;Non.
	call tourr 				;Autrement, c'est la tour.
	jmp affif 				;Et sortir de la recherche de la piece.
g0:
	cmp al,1 				;Cheval ?
	jne g1
	call chevall 
	jmp affif
g1:
	cmp al,2 ;Fou ?
	jne g2
	call officierr
	jmp affif
g2:
	cmp al,3 ;Reine ?
	jne g3 
	call reine
	jmp affif
g3:
	cmp al,4 ;Roi ?
	jne g4
	call roii 
	jmp affif
g4:
	call peonn
affif:
		;-----------La recherche et le placement de la figure est terminee.--------
		;---------------Placer un seul carre quand la figure n'existe pas.----------
	jmp aftodo
libre: 						;On n'a pas la figure. Appeler seulement le carre.
	call uncarre 
		;---------------Un seul carre sans figure est place.-----------------------
aftodo:
	ret
piece endp 					;La fin de la reconnaissance et de placement de la piece ou
						;d'un carre sur l'ecran.

8/9/97 Leonid.

Part 3.

Move your piece freely. The first joy in the game, that goes to be real, is to move your piece on the board, freely. Like in the game with the plastic set, you take one piece from the board and you see its square empty. Your piece goes into other square and you see this square with your piece in it. In your game it is the same. You will depose the empty square from where the piece was initally taken, on the screen, and you will ignite the square of deposition with the image of your piece. Each move, that you do, you must repeat twice, like the government of Canada. They say everything in English and in French. Please, start with English by announcing your move to your "reference table" and, later, repeat it in loud French by puting it on the screen.

I presume that the coordinates indicater can be done very easily by you. Initial coordinates you must say at the beginning of your game, after your free fantasy. When indicater reach some piece and program receive the sign to take it (usually by ENTER or BAR), you can, by changing the light of coordinates, shout your "Understood!". Also, when the piece was depose into its destination square, make your program to acknowledge this kindly by changing the light of coordinates.

Moves after the Rule of Law.

The next stage, at least after my experiece, is to make to move each piece on the board after the logic of the chess game. Before some piece will be taken, in order to depose it on a new location, you know what color have the right to move. The program goes, in advance, and find all the legal moves for the actif side. When the player says that he want to take such a piece, program goes into its chain of moves and see if some move start this way. If yes, ignition of the square will indicate that the piece is taken, otherwise program stays without any sign of excitement, like nothing happened. When the player reach the square of deposition and say that he want to depose the piece, once again, program will go and will see if the move, that already started, end like this. If yes, move is executed and the sign of approval goes on the screen.

In bref, all moves can be found like this:

1) You find in the "reference table" all the pieces of the actif side and depose them in same chain. In this chain you save the description of the piece, one byte from the "reference table" and its position after the offset of it in the same table. Description for each piece take 2 bytes. 1 byte for piece and 1 byte for coordonates. Number of all the pieces in chain goes in special variable.

2) After, you take each piece from the chain of pieces and recognize it. Once the piece is known, you can call the procedure in order to find its moves. All the moves, for the same color, goes into the same chain. This is the chain where program will consult to see if given move is legal or not.

Now you can even calculate how big the chain of moves can be. If each move take 4 bytes, and the highest number that you can have, sometime, can go hardly beyond 100, so give place for 200, maybe. In total, 200 moves * 4 bytes = 800 bytes. Probably it is exageration, but you never knows. When I tryed to solve some fantasy, but legal, inevitable forced mate position, few, between the best chess games, just came down (like Wchess, Mchess, Fritz) the others (like Genius and Chessmaster) quietly withstood the trial. Probably those that failed had insufficient chain space.

One small remark about the logic that find the moves of each piece. You must develop the logic just for 5 pieces unstead of 6. The exception is the queen. This lady make easy the life of programer by the double nature of her personality. If you will only put on her square two different pieces, you will find all the moves of this sweet woman. The pieces are bishop and rook. And I like her not only as programer, I confess you. She is really the most agile and expidient piece on the chessboard. The woman that can be instantly everywhere in the same time. Some amancipated brilliant woman that, in dispite of her undeniable superior quality, is ready to be sacrificed each time when somebody goes to kick the ass of her Master, His Majesty of King. And when I think that this woman existed probably, as very real one, some 25 centuries ago in India, I see already somebody else on her place. Beautiful, gorgeous woman in sari that speek impecable Hindi. The queen that will never treason you! She will not say, like it could be with somebody else, that she refuse to pay your rent bill when you lost your job. Oh, sorry! But this is an another story to tell...


For direct programer.

Here you can see the procedure FEUNEGRO that find the moves for black pieces. Macro TPIECE find all the black pieces and save them in the chain CADENA0. In the procedure FEUNEGRO each piece, from the chain of black pieces, is taken and recognized. After recognition, specialized procedure looks for the moves for each piece. In order to give you the idea how it is done, procedure TENTOUR is presented that search the moves of black rook and save them. For other pieces logic is very similer.

comment 	# 28/1/95,30/1/95,10/2/95,26/1/96,25/8/96
		Retrouve dans l'echiquier general (CARREAU) toutes les pieces de la couleur
		0 (noire), a l'exception de roi noir. Les pieces trouvees vont en CADENA0
		et le nombre des pas en NPIECE0. Le carre de roi va en GS
		#

tpiece macro 
	mov 	npiece0,0
	mov 	ax,seg carreau
	mov 	ds,ax
	mov 	es,ax
	mov 	di,offset cadena0

	mov si,offset carreau 		;L'adresse de la table de la description generale est en
						;DS:DI
	mov ah,0 				;Indiquer les coordonnees d'un dernier carre.
		;---------------------------------------------------------------
rrrepete: 
	mov al,byte ptr ds:[si] 
	cmp al,empty 			;Le carre vide ?
	je retour 				;Oui. Aller voir le prochain carre.
	bt ax,3 				;Le carre est occupe. Si piece blanche bit 3 = 1.
	jnc blando 				;Oui. La piece noire.
		;---------- Autrement, le carre ne tient pas la piece noire.
retour:
	inc ah
	cmp ah,64 				;Tous les carres sont deja revises ?
	jge vvsortie 			;Oui.
	inc si 					;Aller a la position d'un prochain carre.
	jmp rrrepete

blando: 				;Augmenter le nombre des pieces deja retrouvees dans la table
	cmp al,00000100b 	;CARREAU. Le roi noir ?
	jne aa1 			;Non.
	mov gs,ax 			;Sauver la description du carre qui tient le roi en GS.
	jmp retour

aa1:
	mov word ptr ds:[di],ax 	;Sauver la description du carre qui tient la piece dans
	inc npiece0 		;la chaine de pieces noires. Augmenter le nombre des pieces
	add di,2 			;deja sauvees dans sa chaine. Aller a la nouvelle position de 
	jmp retour			 ;sauvetage. Aller voir le prochain carre.

vvsortie:
endm


	comment # 3 mars 1994, 4/3/1994, 9/3/1994, 18/3/1994,2/10/1994,3/10/1994
	31/10/94,8/11/94,22/11/94,27/12/94,12/2/95,22/6/95,18/11/95,9/2/96,13/2/96
	17/2/96,18/2/96,20/2/96,29/2/96,2/2/96,9/3/96,31/3/96,14/4/96,28/4/96,1/5/96
	5/5/96,24/5/96,1/6/96,17/7/96,18/8/96,24/8/96,2/9/96,10/9/96

	Dans cette procedure le COMPUTER doit TROUVER tous ses pas qu'il est capable
	a joue AVEC NOIRS (couleur 0). Le cas quand l'ordinateur peut exposer son roi
	sous le feu d'ennemi, en faisant son pas, n'est pas considere.
	#

feunegro proc uses fs gs esi edi eax ebx ecx edx 
	local atantos:word, aindique:word

	tpiece 	;Demander a retrouver toutes les pieces noires dans la 
			;table CARREAU. Les pieces trouvees, a l'exception de roi, vont
			;dans la chaine CADENA0 et les nombre des pieces en NPIECE0.
			;La desription de carre ou le roi noir se trouve est sauvee en 
			;GS.

	xor edx,edx 	;Le registre DX sera utiliser dans les macros qui cherchent les
				;pas de toutes les pieces, pour le macro FENCEDX. DH sera une
				;constante, a la longueur de toutes les recherches des pas, quand
				;DL va apporter les coordonnees a le macro FENCEDX.

	mov numpas,0 	;Initier le nombre total des pas que les pieces noires sont 
				;capables a executer a 0. NUMPAS indique le nombre des pas
				;sauve dans la chaine TOUSPAS.


	cmp npiece0,0 	;Un seul roi a ete trouve ?
	jle fuerito 		;Oui.

		;-- Aller chercher les autres pieces dans la chaine CADENA0 pour trouver les
		;-- pas de ces pieces.
	mov ax,npiece0 	;Placer le nombre des pieces qui se trouve dans la chaine 
	mov atantos,ax 	;des pieces de la couleur zero en variable de parcours de 
				;la chaine "CADENA0" en "TANTOS".
	mov si,offset cadena0

autrapiesa: 		;Retrouver chaque piece de la chaine "cadena0" et voir quels
	dec atantos 	;pas elles sont capables a executer.
	mov aindique,si
	mov ax,word ptr ds:[si] 	;Placer le mot qui decrit cette piece en ax.
	mov fs,ax 				;Sauver la description de la piece dans FS.

		;------------------------------------------------------------
	cmp al,00000000b 		;Es-ce une tour?
	jne bb0

	call tourn
	jmp diner
	;-----------------------------------------------------------

	;---------------------------------------------------------------
bb0:
	cmp al,00000001b 		;Es-ce un cheval ?
	jne bb1

	call chevaln
	jmp diner
		;-------------------------------------------------------

		;----------------------------------------------------------
bb1:
	cmp al,00000010b 		;Fou?
	jne bb2

	call foun
	jmp diner
		;-------------------------------------------------------------

		;---------------------------------------------------------
bb2:
	cmp al,00000011b 		;Reine?
	jne bb3

	call tourn
	call foun
	jmp diner
		;---------------------------------------------------------


		;--------------------------------------------------------
bb3:
	cmp al,00000101b 		;Le pion ?
	jne diner				 ;Non.

	call pionn
;--------------------------------------------------------


diner:
	mov si,aindique
	add si,2

	cmp atantos,0 			;Voir si on a encore une piece a etudier.
	jg autrapiesa 			;La chaine n'est pas encore vide. Aller etudier la prochaine piece.

fuerito: 					;Pas plus des pieces a etudier, sortir.

	call tenroi 				;Trouver le pas d'un roi noir.

sortie:
ret 
feunegro endp




	comment # 3/10/1994,31/10/94,7/11/94,26/11/94,9/1/96,10/1/96,6/2/96
	Chercher les pas de la tour noire. La position initiale de la tour arrive en
	FS.
	#
tentour proc
	mov ax,fs 			;Placer la description du carre de sortie en ax
			;-- AH va conserver les coordonnees a la longueur de ce macro. AL va servir
			;-- comme variable dans chaque partie de ce macro.

	mov dl,ah 		;Placer les coordonnees en DL et aussi en 
	mov al,ah 		;AL.
				;************* La tour va a droite.
uuu:
	and al,00000111b 	;Regarder seulement la position horizontale.
	cmp al,00000110b 	;On est deja a la position exetreme droite?
	ja bb1 			;Oui. Aller voir le pas a gauche.
		;-------------- On peut aller un carre a droite. Regarder son contenu.
	inc dl 			;Aller un carre a droite.
	fencedx 			;La procedure "FENCE" recoit les coordonnees en DX en redonnant
	mov ch,cl 			;Voir si le carre tient la piece noire.
	and ch,00011000b
	cmp ch,00000000b
	je bb1 		;La piece est noire. Le pas plus a droite est impossible.
		;----------------------------------------------------------------------------
	call verroi 			;Autrement, le carre est ou vide, ou tient la piece blanche.
	cmp golpe,0 		;Regarder si le roi noir sera sous le feu, si la tour entre
	jg dbb1 			;dans ce carre. Oui. Le roi noir sera sous le feu.
		;----------------------------------------------------------------------------
	danstouspas 		;Autrement, sauver ce pas.
dbb1:
		;----------------------------------------------------------------------------
	cmp cl,empty 		;Regarder, si le carre d'entre a ete libre.
	jne bb1 			;Non. Il contenait la piece blanche. La tour ne peut aller
					;plus a droite.
		;----------------------------------------------------------------------------
	mov al,dl 			;Autrement, aller plus a droite.
	jmp uuu
	;******************************************************************

	;*************** La tour va a gauche
bb1:
	mov dl,ah
	mov al,ah
oo:
	and al,00000111b 	;Laisser seulement la position horizontale.
	cmp al,00000001b
	jb bb2 			;On ne peut plus aller a gauche. sortir.
		;----- On peut aller plus a gauche. voir le contenu de ce carre.
	dec dl 
	fencedx
	mov ch,cl
	and ch,00011000b
	cmp ch,00000000b 	;La piece noire ?
	je bb2 ;Oui.
		;-------------------------------------------------------------
	call verroi 			;Le roi sera sous le feu dans ce carre ?
	cmp golpe,0
	jg dbb2 ;Oui.
		;------------------------------------------------------------
	danstouspas 		;Autrement, sauver ce pas.
dbb2:
		;-------------------------------------
	cmp cl,empty 		;Le carre d'entre est vide ?
	jne bb2 			;Non.
		;------------------------------------
	mov al,dl 			;Aller plus a droite.
	jmp oo
		;**********************************************
	
		;********* La tour va en bas
bb2:
	mov dl,ah
po:
	cmp dl,00110111b 	;On peut aller encore en bas?
	ja bb3			 ;Non.
		;----------------------
	add dl,00001000b
	fencedx
	mov ch,cl
	and ch,00011000b
	cmp ch,00000000b 	;La piece noire ?
	je bb3 			;Oui.
		;-------------------------------------------------
	call verroi 			;Le roi sera sous le feu, dans ce carre ?
	cmp golpe,0
	jg dbb3			 ;Oui.
		;---------------------------------------------------
	danstouspas 		;Autrement, sauver ce pas.
dbb3:
		;---------------------------------------------------
	cmp cl,empty 		;Le carre d'entre est libre ?
	je po 				;Oui. Continuer. 
		;--------------------------------------
		;Autrement, le carre est occupe.
		;******************************************************

		;*********** La tour va en haut.
bb3:
	mov dl,ah
aa4:
	cmp dl,00001000b
	jb bb4
		;--------------------------
	sub dl,00001000b
	fencedx
	mov ch,cl
	and ch,00011000b
	cmp ch,00000000b		 ;La piece noire ?
	je bb4 ;Oui.
		;----------------------------------
	call verroi 		;Le roi noir sera sous le feu, dans ce carre ?
	cmp golpe,0
	jg dbb4 		;Oui.
		;----------------------------------
	danstouspas 	;Autrement, sauver ce pas.
dbb4:
		;---------------------------
	cmp cl,empty 		;Le carre d'entre est libre ?
	je aa4 			;Oui. Continuer.
		;-------------------------
		;--- Autrement, le carre est occupe.
bb4:

ret
tentour endp ;La fin de la recherche des pas de la tour noire.

9/9/97 Leonid.

Part 4

Look for mate.

Now we came to two final chapters. You probably will be surprised that it came so quicky. En reality, the main structure, behind the chess game, is more that simple. All the complications are in one billion tiny ditails that wraps its body. But since we want to see only the main structure, this is why we go at such a lightning speed.

In my game, after the moves finding, existe two logics:
1) Logic for solving the mate.
2) Positional logic.

First logic is completely mature, the second one (Position Logic) still must be finalized, but her structure is already clear.

Mate Solving Logic - this is the logic which is activated at the begining of each move finding, when computer must do the move. Presumably, what the human want, when it is his turn to move, is to mate his adversary immidiately. When he can't do this, he try to grab something or goes into to the posturing. To satisfy first natural desire of human, but when the computer have it, Mate Solving Logic was created.

Immidiate Mate - when the pieces have no legal moves and their king is under the fire.

Immidiate mate is nothing more that the mate in one move.

And now, mate in few moves ahead.

The side, that goes to mate, execute each move and look at the response. When after each answer, available from opposite side, it still can mate his adversery, the mate is found "x" moves ahead.

Once again all this, but now I will turn more slowly my tongue in my mouth.

You want to mate your enemy, looking 5 moves ahead. Your pieces are white. You make one white move at once. You find all the black responses to your move. If after all the black responses you can mate 4 moves ahead, mate in 5 moves ahead is found.

The logic, that was described, is the total mate finding. Here all the moves are regarded. The other way, to find the mate, is through quick or partial way. The partial one is where you regard only promising moves, neglecting the others. This give very high speed of research, but many mate solutions will be invisible.

In reality, total and partial mate search can be done in many, many ways. But the greatest diversity offer partial mate solving logic. I developed, around, 30 of them. But in my game was left only 5, in total, for simplicity.


Only for direct programer.

You can see here procedure where the whites search mate 5 moves ahead. Procedure was simplified, in order to keep it as clear as possible. Before calling this procedure all white moves was found. The response is send in MATAUX0. If mate was found MATAUX0 is greater that 0, otherwise equal to 0.

	comment # 20/9/1994,6/10/1994,4/12/94,21/2/95,27/9/95,6/10/95,7/10/95,28/10/95
	3/11/95,25/11/95,28/11/95,11/12/95,28/12/95,26/1/96,18/2/96,31/3/96,11/5/96
	12/5/96,23/5/96,24/6/96,3/12/96,25/1/97,19/3/97
	#
whitejoue5pas proc uses esi edi eax ebx ecx edx
	mov mataux0,0 			;Initiate the move that lead to mate.

	cmp unnumpas,0 			;See, by precaution, if whites have some move.
	jle quitter 				;No. Quit the procedure. 
		;******************************************************
	call carreaustok5 			;Save the content of "reference table" in ...
	call copyclaire5 			;Save all the white moves in chain ... and number in...
		;*********************************************************
	mov cx,nsauveb5 		;Number of white moves to see.
	mov si,offset sauveb5 		;Go to the beginning of the chain of white moves. 
larevision:
		;========================================================
	mov eax,dword ptr ds:[si] 	;Place white move in EAX.
	mov ebx,eax 			;Take copy.
	call abplacecarreau 		;Depose the move into the "description table".
		;--------------------------------------------------------------------------
	call feunegro 			;Find all black moves.
	cmp numpas,0			 ;Some moves was found?
	jg legal 				;Yes. Go and see more closely this position.

		;--- Black have 0 moves. See if this is already mate.
	cmp pah,0 				;If black king is under the fire PAH > 0.
	jle beau 				;It is draw. Go to see the next white move. 

	mov mataux0,ebx 		;Mate. Indicate the move that leads to the mate.
	jmp mais				 ;And quit the procedure.
		;------------------------------------------------------------------------
	
legal:
		;===========================================================
	call carreaustof5			 ;Save the position of "reference table" where alredy white
						;move was executed.
	call copyfonce5 			;Save all black moves.
	mov di,offset sauven5 		;Go to the beginning of the black chain of moves.
	mov dx,nsauven5 		;Indicate the number of black moves to see.
dada:
	mov eax,dword ptr ds:[di] 	;Depose black move in EAX.
	call anplacecarreau 		;Depose black move into the "description table".

	call feublanco 			;Find all white moves.
	call whitejoue4pas 		;Search for mate 4 moves ahead.
	cmp mataux0,0			 ;See if mate was found.
	jbe beau 				;No mate.

		;--- Otherwise mate was found
	dec dx 				;One black move was revisioned.
	jz succes 				;No more black moves. Mate is found.

		;-- Still some black move to see.
	add di,4
	call stofcarreau5			 ;Renew the "refence table".
	jmp dada 				;Go to see the next black move.
	;======================================================

succes: 					;One white move that lead to mate was found.
	mov mataux0,ebx			 ;Indicate your happy move.
	jmp mais 				;Quit the procedure.

beau:
	dec cx 				;Go to see the next white move.
	jz sadil				 ;No more white moves. Mate is impossible.

		;--- Otherwise, go to see the next white move.
	add si,4 			;Go to the position of the next move. Each move 4 bytes.
	call stokcarreau5 		;Retake the initial position of "reference table".
	jmp larevision		 ;Go to see the next white move.


sadil: 
	mov mataux0,0 		;If, finaly, MATAUX0 = 0, mate in ... was not found

mais:
		;*****************************************************
	call renewclaire5 ;Retake initial chain of all white moves.
	call stokcarreau5 ;Come to the inital position in "reference table".
		;*****************************************************
quitter:
ret
whitejoue5pas endp

10/9/97 Leonid.

Part 5.

Positional Logic.

The core of the Positional Logic is the logic of material exchange between the two sides. Its aim is to find the best bargain possible for the program. The same logic can solve the Mate and Draw, it is true, but his expediency is not the same as when it is done by Mate Solving Logic. Because when you look for the Mate, you run practically for the skin of the King and you don't care about some small fish. The Positional Logic, in other words, can give you some material satisfaction when the King, that you like capture, is somewhere in hiding.

For more clarity, I will repeat the same differently. You look for immediate material gain. You see on the board what piece you can capture, and you try to take the piece of the best value. This is how the Positonal Logic works just one move ahead. And now few moves ahead. You take the piece that attract you the most but, disgracefully, you must take in consideration that the other guy will steal something from you in his turn.

Before starting the Positional Logic, you must have your "list of value", or the value that you attach to each of 6 pieces. As the starting point you can use this:

Pawn - 2000 
Knight - 4800 
Bishop - 4825 
Rook - 9100
Queen - 18000 
King - 80000

From this table few short conclusions can be made. The first one - mate is found when you leave the "bargain table" with king price in your pocket. The second one - by even slightly changing value of one piece, in the "list of price", you will automatically change the style of your game.

Below, you will find simplified procedure for Positional Logic. In dispite that it is done in Assembler, explanation in English must keep it clear for the people that use different language.


For direct programer.

Logic looks for the best exchange value, for blacks, in 3 moves. Each black move is executed and the value that it obtain is deposed in EBX. After this move, whites look for their best value to take 2 moves ahead. Value taken by the black piece, less the value that the whites can take, give the final result. After each black move, the value of the last move is compared with the best previous move. The best value, and the move that lead to it, will be left as final result.

	comment # 20/3/97,30/3/97,31/3/97,6/4/97,12/5/97,24/5/97,14/6/97,11/7/97
	20/7/97,22/7/97,3/8/97,14/8/97,15/8/97
	#
noir3 proc uses esi edi eax ebx ecx edx
	xor eax,eax 			;Put in EAX 0.
	sub eax,valueofking 		;Move in EAX the negative value of king.
	mov prise3,eax			;Initialize the taking to the negative value of king.
	mov paso3,0 			;Initialize the move. If blacks are mated, PASO3 will stay = 0.

	call feunegro 			;Find all black moves. 
	cmp numpas,0 			;By precaution, see if blacks have one move.
	jg mars 				;Yes. Go farther into procedure.

		;---- Otherwise, blacks have no moves.
	cmp pah,0 				;The black king is under the fire?
	jg quitter 				;Yes, It is mate.

	mov prise3,0 			;Otherwise, it is draw.
	jmp quitter 				;Quit the procedure.
		;----------------------------------------------------------------------

mars:
		;*******************************************************
	call carreaustok3 			;Save description table in ...
	call copyfonce3 			;Save chain of black moves in...
		;*******************************************************
	mov cx,nsauven3 		;Indicate the number of black moves to see.
	mov si,offset sauven3 		;Find the begining of the chain of black moves.

larevision:
		;=======================================================
	mov eax,dword ptr ds:[si] 	;Place black move in EAX.
	call vvnplacecarreau 		;Depose this move in description table and find the value
						;of this move. Value comes in EBX.
		;-----------------------------------------------------------------------
	call blanc2 			;Find the best value for the whites in 2 moves ahead.
	sub ebx,prise2 		;Find the value that the black move finaly give.

	cmp ebx,prise3 		;This time black take more?
	jle macha 			;No. Go to see the next black move.

	mov prise3,ebx 		;Otherwise, save the best value found.
	mov paso3,eax 		;Save the best move that lead to the best value.

	cmp ebx,valueofking 	;See if this value is already the best value possible.
	jge mais 			;Yes, the best value (the value of mate) is found.

macha:
	dec cx 			;One black move was seen.
	jz mais 			;No more black moves.

		;--- Otherwise, still one move more to see.
	add si,4 			;Go to the position of next move. Each move 4 bytes.
	call stokcarreau3 		;Renew the reference table.
	jmp larevision 		;Go to see the next black move.

mais:
		;*****************************************************
	call stokcarreau3 		;Retake inital position of reference table.
		;*****************************************************
quitter:
ret
noir3 endp

And now, all the best for you if you started the next chess game!

11/9/97 Leonid.

Any comments are welcome...

Chess Introduction.
Copyright © 1997, Leonid Liberman, leonidd at NO SPAM sympatico dot ca
Edited by: Particle,
bsptree at NO SPAM geocities dot com