Đến nội dung

Hình ảnh

Hỏi tìm

- - - - -

  • Please log in to reply
Chủ đề này có 4 trả lời

#1
FOOL90

FOOL90

    Thiếu úy

  • Thành viên
  • 628 Bài viết
Có bạn nào có thuật toán lập trình cờ vua ,hoặc CAro (bản tiếng việt) thì share cho mình !
THanks !
thank
than
tha
th
t
var i:integer;
begin
for i :=1 to 2007 do writeln('Thanks');
readln;
end.
Take it easy

#2
Magus

Magus

    Trung tá

  • Hiệp sỹ
  • 2781 Bài viết
Xài tạm cái này nhé :( chưa tìm thấy :varepsilon

http://www.vnschool....N...cle&sid=651
http://gamedev.vn/fo...php?showforum=6

Bài viết đã được chỉnh sửa nội dung bởi Magus: 10-01-2007 - 18:55

<div align="center"><img src="http://img221.images...4795706ld2.jpg" border="0" class="linked-image" /><br />

<!--fonto:Verdana--><span style="font-family:Verdana"><!--/fonto--><a href="http://diendantoanho...0&#entry168717" target="_blank">Hướng dẫn gõ công thức toán lên diễn đàn cho người mới</a><!--fontc--></span><!--/fontc--></div>

<br /><div align="center"><!--fonto:Verdana--><span style="font-family:Verdana"><!--/fonto--><a href="http://diendantoanho...howtopic=38505" target="_blank">Cách gõ công thức toán mới</a><br /><a href="http://diendantoanho...id=1&Itemid=18" target="_blank"><!--coloro:#008000--><span style="color:#008000"><!--/coloro--><b>Bạn có muốn gửi bài viết của mình lên trang chủ không?</b><!--colorc--></span><!--/colorc--></a><!--fontc--></span><!--/fontc--></div><br /><div align="center"><!--fonto:Courier New--><span style="font-family:Courier New"><!--/fonto--><!--sizeo:2--><span style="font-size:10pt;line-height:100%"><!--/sizeo-->em=Console.ReadLine();Console.Write("Anh yêu {0}",em);<!--sizec--></span><!--/sizec--><!--fontc--></span><!--/fontc--></div>

#3
vns_master88

vns_master88

    Naruto

  • Thành viên
  • 507 Bài viết
Pascal hở.
{$A+,B-,D-,N-,E-,F-,G+,I-,L-,R-,S-,V-,M 65520,0,655360}

&#40;* this previous line are optimizing compiler directives *&#41;

&#40;*******************************************************************&#41;
&#40;*																 *&#41;
&#40;*  MATER&#58; Mate searching program - &#40;c&#41; Valentin Albillo 1998	  *&#41;
&#40;*																 *&#41;
&#40;*	  This program or parts thereof can be used for any purpose  *&#41;
&#40;*  whatsoever as long as proper credit is given to the copyright  *&#41;
&#40;*  holder. Absolutely no guarantees given, no liabilities of any  *&#41;
&#40;*  kind accepted. Use at your own risk.  Your using this code in  *&#41;
&#40;*  all or in part does indicate your acceptance of these terms.   *&#41;
&#40;*																 *&#41;
&#40;*******************************************************************&#41;

program mater;

Uses Crt, Dos;

type strg80 = string&#91;80&#93;; strg2 = string&#91;2&#93;; strg1 = string&#91;1&#93;;
	 strg20 = string&#91;20&#93;; strg3 = string&#91;3&#93;;

const maxmov = 200; maxpcs =  16; NA = true; FullLegal = true;
			 
	  &#40;* definition of pieces and other constants *&#41;
	   
	  WhitePawn   =  1; BlackPawn   = -1; Pawn   = 1; Blank =  0;
	  WhiteKnight =  2; BlackKnight = -2; Knight = 2; Out   =  7;
	  WhiteBishop =  3; BlackBishop = -3; Bishop = 3; White =  1;
	  WhiteRook   =  4; BlackRook   = -4; Rook   = 4; Black = -1;
	  WhiteQueen  =  5; BlackQueen  = -5; Queen  = 5; None  =  0;
	  WhiteKing   =  6; BlackKing   = -6; King   = 6; 
	  Top		 = 22; Bot		 = 99;

	  &#40;* classes of every possible move&#58; captures, castling, en passant *&#41;

	  tCAP = -1;	   tANY =  1;
	  tPKN =  Knight;  tPBI =  Bishop;  tPRK =  Rook;  tPQN =  Queen;
	  tPAS =  8;	   tOO  =  6;	   tOOO =  7;	
	  vOO   = 50;	 vOOO  =  30;

type
	  Squares = set of 1..120;

	  tArrayBoolean = array&#91;Black..White&#93; of boolean;
	  tArraySquares = array&#91;Black..White&#93; of Squares;

	  &#40;* type for Position variables&#58; squares plus status *&#41;

	  tPosit = record	   Board&#58; array&#91;1..120&#93; of integer;
					   KingCastle&#58; tArrayBoolean;
				  QueenRookCastle&#58; tArrayBoolean;
				   KingRookCastle&#58; tArrayBoolean;
				  EnPassantSquare&#58; integer;
			   end;

	   &#40;* type for Move variables&#58; origin, destination, class, value *&#41;

	   tMove = record 
					 SqFrom&#58; integer;
					   SqTo&#58; integer;
				  MoveClass&#58; integer;
					MoveVal&#58; integer;
			   end;

	  tMoves = array&#91;1..maxmov&#93; of tMove;

	  tPieces = array&#91;1..maxpcs&#93; of integer;

	  tsPieces = array&#91;Black..White&#93; of record  
											pk&#58; integer;
										  nfig&#58; integer;
										  Posi&#58; tPieces;
										end;

const 
	  &#40;* values of the different pieces, normal or promoted *&#41;

	  vBLNK = 0;
	  vPW = 100; vKN  = 300; vpKN = 200; vBI  = 300; vpBI = 200;
	  vRK = 500; vpRK = 400; vQN  = 900; vpQN = 800; vKI  = 9999;

	  name&#58; array&#91;Pawn..King&#93; of strg1 = &#40;&#39;&#39;,&#39;N&#39;,&#39;B&#39;,&#39;R&#39;,&#39;Q&#39;,&#39;K&#39;&#41;;

	  value&#58; array&#91;BlackKing..WhiteKing&#93; of integer 
			 = &#40;vKI,vQN,vRK,vBI,vKN,vPW,vBLNK,vPW,vKN,vBI,vRK,vQN,vKI&#41;;

	  &#40;* printed representations of the pieces, empty squares, out board *&#41;

	  Shape&#58; array&#91;BlackKing..Out&#93; of char 
			 = &#40;&#39;k&#39;,&#39;q&#39;,&#39;r&#39;,&#39;b&#39;,&#39;n&#39;,&#39;p&#39;,#250,&#39;P&#39;,&#39;N&#39;,&#39;B&#39;,&#39;R&#39;,&#39;Q&#39;,&#39;K&#39;,&#39;#&#39;&#41;;

	  vDIRPWN = 10;

	  &#40;* direction offsets available when moving each piece *&#41;

		DirPawn&#58; array&#91;1..3&#93; of integer = &#40; 10,   9,  11&#41;;
	  DirKnight&#58; array&#91;1..8&#93; of integer = &#40;-21, -19, -12, -8,  8, 12, 19, 21&#41;;
	  DirBishop&#58; array&#91;1..4&#93; of integer = &#40;-11,  -9,   9, 11&#41;;
		DirRook&#58; array&#91;1..4&#93; of integer = &#40;-10,  -1,   1, 10&#41;;
	   DirQueen&#58; array&#91;1..8&#93; of integer = &#40;-11, -10,  -9, -1,  1,  9, 10, 11&#41;;
		DirKing&#58; array&#91;1..8&#93; of integer = &#40;-11, -10,  -9, -1,  1,  9, 10, 11&#41;;
	  
	  &#40;* promotion squares, first rows, en passant *&#41;

	  sqpromo&#58; tArraySquares = &#40;&#91;Bot-7..Bot&#93;,&#91;&#93;,&#91;Top..Top+7&#93;&#41;;
	  sqprime&#58; tArraySquares = &#40;&#91;32..39&#93; ,&#91;&#93;,&#91;82..89&#93;&#41;;
	  sqpassc&#58; tArraySquares = &#40;&#91;72..79&#93; ,&#91;&#93;,&#91;42..49&#93;&#41;;

	  sqRooksq&#58; array&#91;Black..White&#93; of integer = &#40;Top,   None, Bot-7&#41;;
	  sqRooksk&#58; array&#91;Black..White&#93; of integer = &#40;Top+7, None, Bot&#41;;

	  &#40;* empty board Position, including borders and status *&#41;

	  ZeroPosit&#58; tPosit = &#40;Board&#58;

	  &#40; Out, Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,
		Out, Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Out,
		Out, Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,
		Out, Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out,   Out&#41;;

					  KingCastle&#58; &#40;false,NA,false&#41;;
				 QueenRookCastle&#58; &#40;false,NA,false&#41;;
				  KingRookCastle&#58; &#40;false,NA,false&#41;;
				 EnPassantSquare&#58; None&#41;;

&#40;***************** assorted main Program variables ********************&#41;

var		f,flag,kpallmov&#58; boolean;
					 Posit&#58; tPosit;
  i,j,k,l,m,n,p,q,dum,Turn&#58; integer;
					  xmov&#58; tMove;
					 a,b,c&#58; char;
					 t1,t2&#58; real;
					 Nodes&#58; longint;
			  kpmaxmov,cod&#58; integer;
					Device&#58; Text;

&#40;******************** procedures and functions ************************&#41;

&#40;* converts a string up to 80 characters to uppercase *&#41;

FUNCTION upc&#40;s&#58; strg80&#41;&#58; strg80;
  var i&#58; integer;
  begin 
	 for i&#58;=1 to length&#40;s&#41; do s&#91;i&#93;&#58;=upcase&#40;s&#91;i&#93;&#41;; upc&#58;=s; 
  end;

&#40;* returns the actual time as a number of seconds plus hundreths *&#41;

FUNCTION time&#58; real;
  var h,m,s,c&#58; word;
  begin
	 gettime&#40;h,m,s,c&#41;;
	 time&#58;=3600.0*h+60.0*m+s+c/100.0;
  end;

&#40;* 
   converts internal square references to algebraic notation 
   for printing or displaying moves 
*&#41;

FUNCTION sq2al&#40;n&#58; integer&#41;&#58; strg2;
  begin 
	sq2al&#58;=chr&#40;n mod 10+63+32&#41;+chr&#40;58-n div 10&#41;; 
  end;

&#40;* prints the position&#58; board, castling rights, en passant square *&#41;

PROCEDURE PrintBoard;
  var i&#58; integer;
  begin
	writeln&#40;Device, &#39;	 a  b  c  d  e  f  g  h&#39;&#41;; 
	write&#40;Device, &#39; &#39;&#41;;

	with Posit do
	  begin
		for i&#58;=11 to 110 do
		  begin

			write&#40;Device, &#39; &#39; + Shape&#91;Board&#91;i&#93;&#93; + &#39; &#39;&#41;;

		case i of
		  20&#58; if KingCastle&#91;White&#93; and QueenRookCastle&#91;White&#93;
				 then write&#40;Device, &#39;  White can castle long&#39;&#41;
				 else write&#40;Device, &#39;  White can&#39;&#39;t castle long&#39;&#41;;

		  30&#58; if KingCastle&#91;White&#93; and KingRookCastle&#91;White&#93;
				 then write&#40;Device, &#39;  White can castle short&#39;&#41;
				 else write&#40;Device, &#39;  White can&#39;&#39;t castle short&#39;&#41;;

		  40&#58; if KingCastle&#91;Black&#93; and QueenRookCastle&#91;Black&#93;
				 then write&#40;Device, &#39;  Black can castle long&#39;&#41;
				 else write&#40;Device, &#39;  Black can&#39;&#39;t castle long&#39;&#41;;

		  50&#58; if KingCastle&#91;Black&#93; and KingRookCastle&#91;Black&#93;
				 then write&#40;Device, &#39;  Black can castle short&#39;&#41;
				 else write&#40;Device, &#39;  Black can&#39;&#39;t castle short&#39;&#41;;

		  60&#58; if EnPassantSquare<>None
				 then write&#40;Device, &#39;  En passant square&#58; &#39;,EnPassantSquare&#41;
				 else write&#40;Device, &#39;  No en passant square&#39;&#41;;

			end;

			if i mod 10 = 0 then
			  begin
				writeln&#40;Device&#41;;

				case i of
				  20, 30, 40, 50, 
				  60, 70, 80, 90&#58; write&#40;Device, 10 - i div 10&#41;; 
							 100&#58; write&#40;Device, &#39; &#39;&#41;;
				end;

			  end;

		  end;
		writeln&#40;Device&#41;;
	  end;
  end;

&#40;* 
	converts a string representing a FEN position to an internal 
	representation. Returns true if the FEN notation is correct,
	false otherwise.

	Illegalities checked include&#58; more than one king per side 
	or none, more than 8 pawns per side, more than 16 pieces
	per side, more or less than 8 rows defined, more or less than
	8 squares per row defined, pieces other than p,n,b,r,q,k.

	The castling rights are automatically assigned depending on
	rooks and king positions. No en passant square is considered.

*&#41;

FUNCTION FEN2Posit&#40;fen&#58; strg80&#41;&#58; boolean;

  const kMAXFIL = 8; kMAXPIECES = 16; kKINGS = 1; kMAXPAWNS = 8;

  label sig, fenerr;

  var c&#58; char;
	  p,i,q,n,nwk,nbk,nwp,nbp,nb,nw,nfil&#58; integer;

  begin

	with Posit do
	  begin
		p&#58;=1; i&#58;=pred&#40;Top&#41;; q&#58;=0; nwk&#58;=0; nbk&#58;=0; 
		nwp&#58;=0; nbp&#58;=0; nb&#58;=0; nw&#58;=0; nfil&#58;=0;
sig&#58;
		if p>length&#40;fen&#41; then goto fenerr;
		c&#58;=fen&#91;p&#93;; 

		case upcase&#40;c&#41; of
		  &#39;P&#39;,&#39;N&#39;,&#39;B&#39;,&#39;R&#39;,&#39;Q&#39;,&#39;K&#39;&#58;
			begin
			  if q=kMAXFIL then goto fenerr;
			  inc&#40;i&#41;;

			  case c of
				 &#39;p&#39;&#58; begin Board&#91;i&#93;&#58;=BlackPawn;   inc&#40;nb&#41;; inc&#40;nbp&#41;; end;
				 &#39;n&#39;&#58; begin Board&#91;i&#93;&#58;=BlackKnight; inc&#40;nb&#41;; end;
				 &#39;b&#39;&#58; begin Board&#91;i&#93;&#58;=BlackBishop; inc&#40;nb&#41;; end;
				 &#39;r&#39;&#58; begin Board&#91;i&#93;&#58;=BlackRook;   inc&#40;nb&#41;; end;
				 &#39;q&#39;&#58; begin Board&#91;i&#93;&#58;=BlackQueen;  inc&#40;nb&#41;; end;
				 &#39;k&#39;&#58; begin Board&#91;i&#93;&#58;=BlackKing;   inc&#40;nb&#41;; inc&#40;nbk&#41;; end;
				 &#39;P&#39;&#58; begin Board&#91;i&#93;&#58;=WhitePawn;   inc&#40;nw&#41;; inc&#40;nwp&#41;; end;
				 &#39;N&#39;&#58; begin Board&#91;i&#93;&#58;=WhiteKnight; inc&#40;nw&#41;; end;
				 &#39;B&#39;&#58; begin Board&#91;i&#93;&#58;=WhiteBishop; inc&#40;nw&#41;; end;
				 &#39;R&#39;&#58; begin Board&#91;i&#93;&#58;=WhiteRook;   inc&#40;nw&#41;; end;
				 &#39;Q&#39;&#58; begin Board&#91;i&#93;&#58;=WhiteQueen;  inc&#40;nw&#41;; end;
				 &#39;K&#39;&#58; begin Board&#91;i&#93;&#58;=WhiteKing;   inc&#40;nw&#41;; inc&#40;nwk&#41;; end;
			  end;

			  inc&#40;q&#41;; inc&#40;p&#41;; goto sig;
			end;

		  &#39;1&#39;..&#39;8&#39;&#58;
			begin
			  n&#58;=ord&#40;c&#41;-ord&#40;&#39;0&#39;&#41;; inc&#40;q,n&#41;; 
			  if q>kMAXFIL then goto fenerr;
			  for j&#58;=1 to n do Board&#91;i+j&#93;&#58;=Blank; 
			  inc&#40;i,j&#41;; inc&#40;p&#41;; goto sig;
			end;

		  &#39;/&#39;&#58;
			begin
			  if q<>kMAXFIL then goto fenerr;
			  inc&#40;i,2&#41;; q&#58;=0; inc&#40;p&#41;; inc&#40;nfil&#41;;
			  if nfil<kMAXFIL then goto sig;
			  if p<=length&#40;fen&#41; then goto fenerr;
			end;

		  else
			begin
			  goto fenerr;
			end;

		end;

		&#40;* error if there&#39;s an illegal number of kings, pawns, or pieces *&#41;

		if &#40;nbk<>kKINGS&#41;   or &#40;nwk<>kKINGS&#41;   then goto fenerr;
		if &#40;nbp>kMAXPAWNS&#41; or &#40;nwp>kMAXPAWNS&#41; then goto fenerr;
		if &#40;nb>kMAXPIECES&#41; or &#40;nw>kMAXPIECES&#41; then goto fenerr;

		&#40;* 
		   automatically attempt to assign castling rights based upon
		   the positions of kings and rooks 
		*&#41;

		if Board&#91;26&#93;=BlackKing then KingCastle&#91;Black&#93;&#58;=true
							   else KingCastle&#91;Black&#93;&#58;=false;

		if Board&#91;Top&#93;=BlackRook then QueenRookCastle&#91;Black&#93;&#58;=true 
							   else QueenRookCastle&#91;Black&#93;&#58;=false;

		if Board&#91;29&#93;=BlackRook then KingRookCastle&#91;Black&#93;&#58;=true 
							   else KingRookCastle&#91;Black&#93;&#58;=false;

		if Board&#91;96&#93;=WhiteKing then KingCastle&#91;White&#93;&#58;=true  
							   else KingCastle&#91;White&#93;&#58;=false;

		if Board&#91;92&#93;=WhiteRook then QueenRookCastle&#91;White&#93;&#58;=true 
							   else QueenRookCastle&#91;White&#93;&#58;=false;

		if Board&#91;Bot&#93;=WhiteRook then KingRookCastle&#91;White&#93;&#58;=true 
								else KingRookCastle&#91;White&#93;&#58;=false;

		&#40;*  
		   in this version, no en passant square is assigned for a 
		   given position
		*&#41;

		EnPassantSquare&#58;=None;

	  end;

	  FEN2Posit&#58;=true;
	  exit;

fenerr&#58;

	FEN2Posit&#58;=false;

  end;

&#40;* 
   returns a string representing the class of a move&#58; promotion and to
   which piece, castling short, castling long, en passant capture,
   none of these, unknown
*&#41; 

FUNCTION cl2&#40;m&#58; integer&#41;&#58; strg3;
  var s&#58; strg3;
  begin

	case abs&#40;m&#41; of
	  tANY&#58; s&#58;=&#39;&#39;;	 &#40;* nothing special *&#41;
	  tPKN&#58; s&#58;=&#39;=N&#39;;   &#40;* subpromotion to a knight *&#41;
	  tPBI&#58; s&#58;=&#39;=B&#39;;   &#40;* subpromotion to a bishop *&#41;
	  tPRK&#58; s&#58;=&#39;=R&#39;;   &#40;* subpromotion to a rook *&#41;
	  tPQN&#58; s&#58;=&#39;=Q&#39;;   &#40;* promotion to a queen *&#41;
	   tOO&#58; s&#58;=&#39; OO&#39;;  &#40;* castling short *&#41;
	  tOOO&#58; s&#58;=&#39; OOO&#39;; &#40;* castling long *&#41;
	  tPAS&#58; s&#58;=&#39; e.p&#39;; &#40;* en passant capture *&#41;
	   else s&#58;=&#39; ???&#39;; &#40;* unknown; should never happen *&#41; 
	end;

	cl2&#58;=s;
  end;

&#40;* returns a string representing textually a move &#40;i.e&#58; b7xc8=N+&#41;  *&#41;

FUNCTION PrintMove&#40;var m&#58; tMove&#41;&#58; strg20;
  begin
	with m do
	  if &#40;SqFrom=0&#41; and &#40;SqTo=0&#41; then &#40;* should never happen *&#41;
		 PrintMove&#58;=&#39;&#40;no move&#41;&#39;
	  else
		 if MoveClass<0 then &#40;* it&#39;s a capture *&#41;

		   PrintMove&#58;=name&#91;Posit.Board&#91;SqFrom&#93;&#93; + sq2al&#40;SqFrom&#41; + &#39;x&#39;
					  + sq2al&#40;SqTo&#41; + cl2&#40;MoveClass&#41;

		 else  &#40;* it&#39;s not a capture *&#41;

		   PrintMove&#58;=name&#91;Posit.Board&#91;SqFrom&#93;&#93; + sq2al&#40;SqFrom&#41; + &#39;-&#39;
					  + sq2al&#40;SqTo&#41; + cl2&#40;MoveClass&#41;;

  end;

&#40;*  
   fills a record with the number, types, and positions of all the
   pieces for a given side, special provision for the king
*&#41;

PROCEDURE PosPieces&#40;var sPieces&#58; tsPieces&#41;;
  var i,c&#58; integer;
  begin
	fillchar&#40;sPieces,sizeof&#40;sPieces&#41;,0&#41;;

	with Posit do for i&#58;=Top to Bot do  &#40;* scan the whole board *&#41;
	  begin
		c&#58;=Board&#91;i&#93;;
		if c<>Blank then if c<>Out then
		  if c<0 then &#40;* it&#39;s a Black piece *&#41;
			with sPieces&#91;Black&#93; do
			  begin 
				inc&#40;nfig&#41;; Posi&#91;nfig&#93;&#58;=i; if c=BlackKing then pk&#58;=i;
			  end
		  else &#40;* it&#39;s a White piece *&#41;
			with sPieces&#91;White&#93; do
			  begin  
				inc&#40;nfig&#41;; Posi&#91;nfig&#93;&#58;=i; if c=WhiteKing then pk&#58;=i; 
			  end;
	  end;
  end;

&#40;* returns true if a given side&#39;s king is in check, false otherwise *&#41;

FUNCTION InCheck&#40;Color,pkm,pke&#58; integer&#41;&#58; boolean;
  var i,fig,cfig,cbi,crk,cqn,sq,c,d,ncolo&#58; integer;
  begin

	InCheck&#58;=true; &#40;* we assume beforehand it is in check *&#41;

	&#40;* first we test if it is near the enemy king *&#41;

	case abs&#40;pkm-pke&#41; of 
		 1,9..11&#58; exit; 
	end;

	ncolo&#58;=-Color; cbi&#58;=ncolo*Bishop; crk&#58;=ncolo*Rook; cqn&#58;=ncolo*Queen;

	with Posit do
	  begin

		for i&#58;=1 to 4 do
		  begin

			&#40;* test if it is under the attack of a Bishop or a Queen *&#41;

			d&#58;=DirBishop&#91;i&#93;; sq&#58;=pkm;

			repeat 
			   inc&#40;sq,d&#41;; c&#58;=Board&#91;sq&#93;; 
			until c<>Blank;

			if c=cbi then exit else if c=cqn then exit;

			&#40;* test if it is under the attack of a Rook or a Queen *&#41;

			d&#58;=DirRook&#91;i&#93;; sq&#58;=pkm;

			repeat 
			   inc&#40;sq,d&#41;; c&#58;=Board&#91;sq&#93;; 
			until c<>Blank;

			if c=crk then exit else if c=cqn then exit;
		  end;

		&#40;* test if it is under the attack of a Knight *&#41;

		cfig&#58;=ncolo*Knight;
		for i&#58;=1 to 8 do if Board&#91;pkm+DirKnight&#91;i&#93;&#93;=cfig then exit;

		&#40;* test if it is under the attack of a Pawn *&#41;

		cfig&#58;=ncolo*Pawn;
		for i&#58;=2 to 3 do if Board&#91;pkm+ncolo*DirPawn&#91;i&#93;&#93;=cfig then exit;

	  end;

	&#40;* the King is not in check *&#41;

	InCheck&#58;=false;

  end;

&#40;* 
   generates moves for a given side. Options permit generation of all
   moves or a single move, full legal moves or pseudo-legal &#40;own king may
   be left under check&#41;, etc.

   All chess rules are implemented, including all five rules for legal
   castling, promotions, underpromotions, and en passant captures 
*&#41; 

PROCEDURE GenerateMoves&#40;Color&#58; integer;
						   sq&#58; integer;
				   var Movesf&#58; tMoves;
				  var nMovesf&#58; integer;
					  pkm,pke&#58; integer;
						legal&#58; boolean;
					   single&#58; boolean;
					var Found&#58; boolean&#41;;

  var sq2,c,fig,cfig,
		   i,d,ncolo&#58; integer;
			  Posit2&#58; tPosit;
			   v,cap&#58; integer;

  &#40;* tests the pseudo-legal move for full legality, and records it *&#41;

  PROCEDURE TestRecordMove&#40;cfig,clas,vlm&#58; integer&#41;;
	begin
	  if legal then
		begin
		  Posit2&#58;=Posit;

		  with Posit do
			begin

			  &#40;* we try tentatively the pseudo-legal move *&#41;

			  Board&#91;sq2&#93;&#58;=cfig; Board&#91;sq&#93;&#58;=Blank;
			  if clas=-tPAS then Board&#91;sq2+vDIRPWN*Color&#93;&#58;=Blank;

			  &#40;* if then we are left in check, it&#39;s not legal *&#41;

			  if InCheck&#40;Color,pkm,pke&#41; then
				begin 
				  Posit&#58;=Posit2; exit; 
				end;

			  if single then 
				begin 
				  Found&#58;=true; Posit&#58;=Posit2; exit; 
				end;

			end;
		  Posit&#58;=Posit2;
		end;

	  &#40;* 
		 either it was full legal, or we specified pseudo-legal moves.
		 Now we proceed to record the move in the array
	  *&#41;

	  inc&#40;nMovesf&#41;;

	  with Movesf&#91;nMovesf&#93; do
		begin 
		  SqFrom&#58;=sq; SqTo&#58;=sq2; MoveClass&#58;=clas; MoveVal&#58;=vlm; 
		end;

	end;

  &#40;* 
	 tests the pseudo-legal pawn move for full legality and records it.
	 Takes proper care of promotions and subpromotions.
  *&#41;

  PROCEDURE TestRecordPawn;
	begin
	  v&#58;=value&#91;abs&#40;c&#41;&#93;; 
	  if v=0 then cap&#58;=tANY else cap&#58;=tCAP;

	  &#40;* if the pawn promotes, test the 4 possible &#40;sub&#41;promotions *&#41;

	  if sq2 in sqpromo&#91;Color&#93; then
		begin
		  TestRecordMove&#40;Color*Queen ,tPQN*cap,v+vQN&#41;; if Found then exit;
		  TestRecordMove&#40;Color*Rook  ,tPRK*cap,v+vRK&#41;; if Found then exit;
		  TestRecordMove&#40;Color*Bishop,tPBI*cap,v+vBI&#41;; if Found then exit;
		  TestRecordMove&#40;Color*Knight,tPKN*cap,v+vKN&#41;; if Found then exit;
		end
	  else 
		begin 
		  TestRecordMove&#40;Pawn,cap,v&#41;; if Found then exit; 
		end;
	end;

&#40;* tests if castling &#40;long or short&#41; is legal *&#41;

  PROCEDURE TestCastling;
	var i&#58; integer;

&#40;*
	Legal castling requirements&#58;

	 1&#41; the King hasn&#39;t moved
	 2&#41; the appropriate Rook hasn&#39;t moved
	 3&#41; the squares between the King and the Rook are empty
	 3&#41; the King isn&#39;t in check at the moment
	 4&#41; the square over which the King must pass isn&#39;t under attack
	 5&#41; the square where the King will be placed isn&#39;t under attack

*&#41;
	label sig;
	begin
	  with Posit do
		begin

		  if not KingCastle&#91;Color&#93; then exit; &#40;* the King has moved *&#41;
		  pkm&#58;=sq;

		  &#40;* test castle short *&#41;

		  if KingRookCastle&#91;Color&#93; then
			begin 
			  for i&#58;=succ&#40;pkm&#41; to pkm+2 do if Board&#91;i&#93;<>Blank then goto sig;
			  if InCheck&#40;Color,pkm,pke&#41; then exit;

			  for i&#58;=succ&#40;pkm&#41; to pkm+2 do
				if InCheck&#40;Color,i,pke&#41; then goto sig;

			  if single then begin Found&#58;=true; exit; end;
			  inc&#40;nMovesf&#41;;

			  &#40;* ok, record short castling *&#41;

			  with Movesf&#91;nMovesf&#93; do
				begin 
				  SqFrom&#58;=sq; SqTo&#58;=pkm+2; MoveClass&#58;=tOO; MoveVal&#58;=vOO;
				end;

			end; 

		  &#40;* test castle long *&#41;

sig&#58;	  if QueenRookCastle&#91;Color&#93; then
			begin 
			  for i&#58;=pkm-3 to pred&#40;pkm&#41; do if Board&#91;i&#93;<>Blank then exit;
			  if InCheck&#40;Color,pkm,pke&#41; then exit;

			  for i&#58;=pkm-2 to pred&#40;pkm&#41; do
				if InCheck&#40;Color,i,pke&#41; then exit;

			  if single then begin Found&#58;=true; exit; end;

			  &#40;* ok, record long castling *&#41;

			  inc&#40;nMovesf&#41;;

			  with Movesf&#91;nMovesf&#93; do
				begin 
				  SqFrom&#58;=sq; SqTo&#58;=pkm-2; MoveClass&#58;=tOOO; MoveVal&#58;=vOOO; 
				end;

			end; 
		end;
	end; &#40;* TestCastling *&#41;


  begin &#40;* GenerateMoves *&#41;
	Found&#58;=false; inc&#40;Nodes&#41;;

	with Posit do
	  begin
		cfig&#58;=Board&#91;sq&#93;; ncolo&#58;=-Color; nMovesf&#58;=None;

		case abs&#40;cfig&#41; of
		 Pawn&#58; begin 
				 d&#58;=ncolo*vDIRPWN; sq2&#58;=sq+d; c&#58;=Board&#91;sq2&#93;;

				 if c=Blank then &#40;* the pawn advances *&#41;
				   begin
					 TestRecordPawn; if Found then exit;

					 &#40;* 
						if the pawn is on his original square, it can
						advance one or two squares on its move 
					 *&#41;

					 if sq in sqprime&#91;Color&#93; then
					   begin
						 inc&#40;sq2,d&#41;; c&#58;=Board&#91;sq2&#93;;
						 if c=Blank then
						   begin 
							 TestRecordPawn; if Found then exit; 
						   end;
					   end;
				   end;

				 &#40;* now we test if the pawn can capture *&#41;

				 for i&#58;=2 to 3 do
				   begin
					 sq2&#58;=sq+ncolo*DirPawn&#91;i&#93;;

					 &#40;* check for an en passant capture *&#41;

					 if sq2=EnPassantSquare then
					   begin
						 if sq2 in sqpassc&#91;Color&#93; then
						   begin
							 TestRecordMove&#40;Pawn,-tPAS,vPW&#41;;
							 if Found then exit;
						   end;
					   end
					 else
					   begin
						 c&#58;=Board&#91;sq2&#93;;
						 if c<>Blank then
						   if c<>Out then
							 if c div ncolo>0 then &#40;* the pawn captures *&#41;
							   begin 
								 TestRecordPawn; if Found then exit; 
							   end;
					   end;
				   end;
			   end; 

	   Knight&#58; for i&#58;=1 to 8 do &#40;* eight possible moves for the knight *&#41;
				 begin
				   sq2&#58;=sq+DirKnight&#91;i&#93;; c&#58;=Board&#91;sq2&#93;;
				   if c<>Out then
					 if c div Color<=0 then
					   begin
						 v&#58;=value&#91;abs&#40;c&#41;&#93;;
						 if v=0 then cap&#58;=tANY else cap&#58;=tCAP;
						 TestRecordMove&#40;cfig,cap,v&#41;; if Found then exit;
					   end;
				 end;

	   Bishop&#58; for i&#58;=1 to 4 do &#40;* four directions for the Bishop *&#41;
				 begin
				   sq2&#58;=sq;
				   repeat
					 inc&#40;sq2,DirBishop&#91;i&#93;&#41;; c&#58;=Board&#91;sq2&#93;;
					 if c<>Out then
					   if c div Color<=0 then
						 begin
						   v&#58;=value&#91;abs&#40;c&#41;&#93;;
						   if v=0 then cap&#58;=tANY else cap&#58;=tCAP;
						   TestRecordMove&#40;cfig,cap,v&#41;; if Found then exit;
						 end;
				   until c<>Blank; &#40;* repeat until blocked *&#41;
				 end;

		 Rook&#58; for i&#58;=1 to 4 do &#40;* four directions for the Rook *&#41;
				 begin
				   sq2&#58;=sq;
				   repeat
					 inc&#40;sq2,DirRook&#91;i&#93;&#41;; c&#58;=Board&#91;sq2&#93;;
					 if c<>Out then
					   if c div Color<=0 then
						 begin
						   v&#58;=value&#91;abs&#40;c&#41;&#93;;
						   if v=0 then cap&#58;=tANY else cap&#58;=tCAP;
						   TestRecordMove&#40;cfig,cap,v&#41;; if Found then exit;
						 end;
				   until c<>Blank; &#40;* repeat until blocked *&#41;
				 end;

		Queen&#58; for i&#58;=1 to 8 do &#40;* eight directions for the queen *&#41;
				 begin
				   sq2&#58;=sq;
				   repeat
					 inc&#40;sq2,DirQueen&#91;i&#93;&#41;; c&#58;=Board&#91;sq2&#93;;
					 if c<>Out then
					   if c div Color<=0 then
						 begin
						   v&#58;=value&#91;abs&#40;c&#41;&#93;;
						   if v=0 then cap&#58;=tANY else cap&#58;=tCAP;
						   TestRecordMove&#40;cfig,cap,v&#41;; if Found then exit;
						 end;
				   until c<>Blank; &#40;* repeat until blocked *&#41;
				 end;

		 King&#58; begin
				 for i&#58;=1 to 8 do &#40;* eight directions for the King *&#41;
				   begin
					 sq2&#58;=sq+DirKing&#91;i&#93;; c&#58;=Board&#91;sq2&#93;; pkm&#58;=sq2;
					 if c<>Out then
					   if c div Color<=0 then
						 begin
						   v&#58;=value&#91;abs&#40;c&#41;&#93;;
						   if v=0 then cap&#58;=tANY else cap&#58;=tCAP;
						   TestRecordMove&#40;cfig,cap,v&#41;; if Found then exit;
						 end;
				   end;

				 &#40;* besides, test if the King can castle *&#41;

				 TestCastling; if Found then exit; 

			   end;
		end; &#40;* case *&#41;

	  end; &#40;* with Posit *&#41;

  end; &#40;* GenerateMoves *&#41;

&#40;*
  returns true if a given side has at least one legal move in a given
  position, false otherwise. To speed the search in near-mate positions,
  it considers King&#39;s moves first.
*&#41;

FUNCTION AnyMovSide&#40;	  Color&#58; integer;
					var sPieces&#58; tsPieces; 
						pkm,pke&#58; integer&#41;&#58; boolean;

  var i,nMovesf&#58; integer;
		 Movesf&#58; tMoves;
		  Found&#58; boolean;

  begin
	with sPieces&#91;Color&#93; do
	  begin

		&#40;* first, generate at least one move for the king *&#41;

		GenerateMoves&#40;Color,pkm,Movesf,nMovesf,pkm,pke,FullLegal,true,Found&#41;;

		&#40;* the king has at least one legal move. Exit *&#41;

		if Found then begin AnyMovSide&#58;=true; exit; end;

		&#40;* 
		   the King has no legal moves. Generate at least one move
		   for all pieces but the King 
		*&#41;

		for i&#58;=1 to nfig do if Posi&#91;i&#93;<>pkm then
		  begin
			GenerateMoves&#40;Color,Posi&#91;i&#93;,Movesf,nMovesf,pkm,pke,
						  FullLegal,true,Found&#41;;

			&#40;* there&#39;s at least one legal move available. Exit *&#41;

			if Found then begin AnyMovSide&#58;=true; exit; end;
		  end;

	  end;

	&#40;* no legal moves available. Either checkmated or stalemated *&#41;

	AnyMovSide&#58;=false;

  end;

&#40;*
  actually makes a given move in a given position, updating the board
  and all status&#58; castling rights, en passant square, and promotions
*&#41;

PROCEDURE PerformMove&#40;var xmov&#58; tMove; Color&#58; integer; var pk&#58; integer&#41;;
  var c,ncolo&#58; integer;
  begin
	with xmov, Posit do
	  begin

		&#40;* update the board *&#41;

		c&#58;=Board&#91;SqFrom&#93;; ncolo&#58;=-Color;
		Board&#91;SqFrom&#93;&#58;=Blank; Board&#91;SqTo&#93;&#58;=c; EnPassantSquare&#58;=None;

		case abs&#40;c&#41; of
		  Pawn&#58; begin

				  &#40;* check if an en passant capture is now possible *&#41;

				  if abs&#40;SqFrom-SqTo&#41;=20 then 
					EnPassantSquare&#58;=&#40;SqFrom+SqTo&#41; div 2;

				  &#40;*  
					 if it was a promotion, replace the pawn with the
					 promoted piece
				  *&#41;

				  case abs&#40;MoveClass&#41; of
					tPKN,tPBI,
					tPRK,tPQN&#58; Board&#91;SqTo&#93;&#58;=Color*abs&#40;MoveClass&#41;;
						 tPAS&#58; Board&#91;SqTo+vDIRPWN*Color&#93;&#58;=Blank;
				  end;

				end;

		  King&#58; begin
				  pk&#58;=SqTo;

				  &#40;* the king has moved. Remove castling rights *&#41;

				  if KingCastle&#91;Color&#93; then
					begin
					  KingCastle&#91;Color&#93;&#58;=false;
					  QueenRookCastle&#91;Color&#93;&#58;=false; 
					  KingRookCastle&#91;Color&#93;&#58;=false;
					end;

				  &#40;* if it has castled, move also the Rook *&#41;

				  case MoveClass of
					tOO&#58; begin
						   Board&#91;pred&#40;SqTo&#41;&#93;&#58;=Color*Rook; 
						   Board&#91;SqFrom+3&#93;&#58;=Blank;
						 end;
				   tOOO&#58; begin
						   Board&#91;succ&#40;SqTo&#41;&#93;&#58;=Color*Rook; 
						   Board&#91;SqFrom-4&#93;&#58;=Blank;
						 end;
				  end;

				end;

		  Rook&#58; 
				&#40;* the Rook has moved. Remove its castling right *&#41;

				if SqFrom=sqRooksq&#91;Color&#93; then 
				   QueenRookCastle&#91;Color&#93;&#58;=false 
				else
				  if SqFrom=sqRooksk&#91;Color&#93; then 
					 KingRookCastle&#91;Color&#93;&#58;=false;
		end;

		&#40;* 
		   some piece has moved to the Rook&#39;s original position. Remove
		   its castling rights
		*&#41;  

		if SqTo=sqRooksq&#91;ncolo&#93; then QueenRookCastle&#91;ncolo&#93;&#58;=false else
		if SqTo=sqRooksk&#91;ncolo&#93; then KingRookCastle&#91;ncolo&#93;&#58;=false;

	  end;
  end;

&#40;*
   accepts as parameters the position, the side to move, and a maximum
   number of movements to consider, and searches for a move that gives mate
   in that number of moves or less. If there&#39;s such a move, it returns
   true, else it returns false. 

   Depending on a parameter it can search among all legal moves for the
   mating side, or only checks.
*&#41;

  FUNCTION FindMate&#40;Color&#58; integer;
					 prof&#58; integer;
					 maxm&#58; integer;
				 var jmov&#58; tMove;
				  onlychk&#58; boolean&#41;&#58; boolean;

  label nxt,mat;

		var x_nMoves,y_nMoves,pkm,
				 pke,cfig,ncolo,i,
					   j,k,k2,dum&#58; integer;
						  sf1,sf2&#58; tsPieces;
							 xmov&#58; tMove;
					xMoves,yMoves&#58; tMoves;
					Posit2,Posit3&#58; tPosit;
	  prof1,profm,Found,Stalemate&#58; boolean;

	begin
	  prof1&#58;=prof=1; profm&#58;=prof=maxm; ncolo&#58;=-Color; cfig&#58;=King*Color;

	  &#40;* 
		 find out the number, positions, and types of every piece and
		 specially, the kings
	  *&#41; 

	  PosPieces&#40;sf1&#41;; pkm&#58;=sf1&#91;Color&#93;.pk; pke&#58;=sf1&#91;ncolo&#93;.pk;
	  Posit2&#58;=Posit;

	  with sf1&#91;Color&#93; do
		for k&#58;=1 to nfig do
		  begin

			&#40;*  
			   generate moves for each piece individually, full legal
			   at all depth except at the maximum depth, where only
			   pseudo-legal moves are generated, to save time 
			*&#41;

			GenerateMoves&#40;Color,Posi&#91;k&#93;,xMoves,x_nMoves,pkm,pke,
						  not profm, false, Found&#41;;

			&#40;* 
			   at the maximum depth, allow for the user to interrupt
			   the calculation by pressing any key 
			*&#41;

			if profm then
			  if keypressed then
				begin
				  writeln&#40;Device&#41;; writeln&#40;Device&#41;; 
				  writeln&#40;Device, &#39;HALTED BY USER&#39;&#41;;
				  close&#40;Device&#41;;
				  halt&#40;9999&#41;;
				end;

			&#40;* now perform each move one by one *&#41;

			for i&#58;=1 to x_nMoves do
			  begin
				xmov&#58;=xMoves&#91;i&#93;;

				&#40;* only at minimum depth, show progress on the screen *&#41;

				if prof1 then write&#40;Device, &#39;.&#39;&#41;;

				&#40;* perform the move *&#41;

				PerformMove&#40;xmov,Color,pkm&#41;;

				if profm then

				  &#40;* 
					 at maximum depth, our move is only pseudo-legal;
					 to save time, we&#39;ll test it for full legality 
					 only if it gives check to the enemy king. If it
					 does not give check, it can&#39;t possibly be a
					 checkmate, so it will be discarded and so there&#39;s no
					 need to waste time in making sure it&#39;s full legal
				  *&#41; 

				  if InCheck&#40;ncolo,pke,pkm&#41; then
					begin

					  &#40;* 
						 it indeed gives check, so, is it fully legal ? 
						 if not, discard it and go to next move
					  *&#41;

					  if InCheck&#40;Color,pkm,pke&#41; then goto nxt;

					  &#40;* 
						 it was fully legal. If it was also a capture,
						 we need to obtain a list of pieces 
					  *&#41;

					  if xmov.MoveClass<0 then PosPieces&#40;sf2&#41; else sf2&#58;=sf1;

					  &#40;* 
						 now we test if the enemy side &#40;which is under
						 check&#41; has any legal move. If not, it&#39;s a mate
					  *&#41; 

					  if AnyMovSide&#40;ncolo,sf2,pke,pkm&#41; then goto nxt;

					  goto mat;

					end
				  else goto nxt;

				&#40;* 
				   if we are looking for mates in which all the mating
				   side moves are checks, we skip moves which do not
				   give check
				*&#41;

				if onlychk then
				  if not InCheck&#40;ncolo,pke,pkm&#41; then goto nxt;

				&#40;* we assume the enemy may be stalemated *&#41;

				Stalemate&#58;=true;
				if xmov.MoveClass<0 then PosPieces&#40;sf2&#41; else sf2&#58;=sf1;

				with sf2&#91;ncolo&#93; do
				  for k2&#58;=1 to nfig do
					begin

					  &#40;* now we generate moves for the enemy&#39;s answer *&#41;

					  GenerateMoves&#40;ncolo,Posi&#91;k2&#93;,yMoves,y_nMoves,pke,pkm,
									FullLegal,false,Found&#41;;

					  &#40;* if there&#39;s at least one, no stalemate *&#41;

					  if y_nMoves<>None then
						begin
						  Stalemate&#58;=false; Posit3&#58;=Posit;

						  for j&#58;=1 to y_nMoves do
							begin

							  &#40;* perform each enemy answer on the board *&#41;

							  PerformMove&#40;yMoves&#91;j&#93;,ncolo,dum&#41;;

							  &#40;*  
								 and recursively, search for a mate in
								 this new position 
							  *&#41;

							  if not FindMate&#40;Color,succ&#40;prof&#41;,maxm,
											   jmov,onlychk&#41; then goto nxt;
							  Posit&#58;=Posit3;
							end;

						end;
					end;

				&#40;* if we specified only checks, we&#39;ve found a mate *&#41;

				if onlychk then goto mat;

				&#40;* 
				   test if the enemy is in check. If so, we&#39;ve found
				   a mate. Else, it&#39;s stalemated, continue the search 
				*&#41;

				if Stalemate then
				  if InCheck&#40;ncolo,pke,pkm&#41; then goto mat else goto nxt;

mat&#58;			&#40;* a mate was found; set flags and exit *&#41;

				if prof1 then jmov&#58;=xmov; 
				FindMate&#58;=true; Posit&#58;=Posit2; 
				exit;

nxt&#58;			&#40;* next move; restore the position and iterate again *&#41;

				Posit&#58;=Posit2; pkm&#58;=pk;

			  end;
		  end;

		&#40;* no mate has been found *&#41;

		FindMate&#58;=false;

	  end;

&#40;* 
   iteratively calls FindMate for increasing number of moves, and
   prints the time used each time it fails to find a mate
*&#41;

 FUNCTION SearchMate&#40;Color&#58; integer;
					  minm&#58; integer;
					  maxm&#58; integer;
				  var nmov&#58; integer;
				  var xmov&#58; tMove;
					 check&#58; boolean&#41;&#58; boolean;

  var i&#58; integer;
  begin
	SearchMate&#58;=false;

	for i&#58;=minm to maxm do
	  begin
		write&#40;Device, &#39;Mate in &#39;, i&#58;2&#41;;

		if FindMate&#40;Color,1,i,xmov,check&#41; then
		  begin
			SearchMate&#58;=true; nmov&#58;=i; exit;
		  end;

		&#40;* show the time for each unsuccessful iteration *&#41;

		writeln&#40;Device, time-t1&#58;0&#58;2&#41;;
	  end;
  end;

&#40;* prints some messages, calls SearchMate, and prints the result *&#41;

  PROCEDURE SolveMate;
	var	nmov&#58; integer;
		ExitVal&#58; integer;

	begin

	  n&#58;=kpmaxmov;
	  writeln&#40;Device, &#39;Max. no. of Moves to mate&#58;  &#39;,kpmaxmov&#41;;

	  if kpAllmov then flag&#58;=false else flag&#58;=true;

	  if flag then write&#40;Device, &#39;Searching only checks for&#58;  &#39;&#41;
			  else write&#40;Device, &#39;Searching *all Moves* for&#58;  &#39;&#41;;

	  if Turn=White then writeln&#40;Device, &#39;White&#39;&#41; 
					else writeln&#40;Device, &#39;Black&#39;&#41;;

	  writeln&#40;Device&#41;;

	  &#40;* go find the mate *&#41;

	  Nodes&#58;=None;
	  t1&#58;=time;
	  f&#58;=SearchMate&#40;Turn,1,n,nmov,xmov,flag&#41;;
	  t2&#58;=time;

	  &#40;* tell the result of the search *&#41;

	  if f then  &#40;* mate found *&#41;
		begin
		  writeln&#40;Device&#41;;
		  writeln&#40;Device, &#39;Mate in &#39;,nmov,&#39; with &#39;,PrintMove&#40;xmov&#41;
				  ,&#39;, t=&#39;,t2-t1&#58;0&#58;2,&#39;, Nodes=&#39;,Nodes&#41;;
		  ExitVal&#58;=nmov;  &#40;* ERRORLEVEL = nmov *&#41;
		end
	  else   &#40;* mate not found *&#41;
		begin
		  writeln&#40;Device&#41;;
		  writeln&#40;Device, &#39;No mate in &#39;,n,&#39;, t=&#39;,t2-t1&#58;0&#58;2,&#39;, Nodes=&#39;,Nodes&#41;;
		  ExitVal&#58;=0; &#40;* ERRORLEVEL = 0 *&#41;
		end;

	  close&#40;Device&#41;;
	  halt&#40;ExitVal&#41;;  

	end;

&#40;******************** Main program **********************************&#41;

&#40;* 
   prints the program&#39;s identification and copyright, 
   accepts and checks all command line parameters, prints error 
   messages for any input errors, and calls SolveMate
*&#41;

label entrynok;

begin

	&#40;* 
	   the following line allows for redirection, sending all output
	   to the standard output&#58; the screen or redirected to a file,
	   the printer, etc 
	*&#41; 

	assign&#40;Device, &#39;&#39;&#41;; rewrite&#40;Device&#41;; 

	writeln&#40;Device&#41;;

	&#40;* show name, version, author, and copyright *&#41;

	writeln&#40;Device,
			 &#39;MATER&#58; Mate searching program v1.1. &#40;c&#41; Valentin Albillo 1998&#39;&#41;;

	writeln&#40;Device&#41;;

	&#40;* check command line parameters *&#41;

	if paramcount<>4 then
	  begin
		writeln&#40;Device&#41;;
		writeln&#40;Device, &#39;INPUT ERROR&#58; wrong number of parameters&#39;&#41;;

entrynok&#58;

   writeln&#40;Device&#41;;
   writeln&#40;Device,
		   &#39; Sintax&#58; MATER &#91;FEN posit&#93; &#91;max.mov &#40;1..64&#41;&#93; &#91;all/checks &#40;a/c&#41;&#93;&#39;&#41;;

   writeln&#40;Device, &#39;Example&#58; mater b7/PP6/8/8/7K/6B1/6N1/4R1bk/ w  5  a&#39;&#41;;
   writeln&#40;Device&#41;;
   writeln&#40;Device, &#39;   Note&#58; The output can be redirected&#58; MATER ...  > mat.txt&#39;&#41;;
   writeln&#40;Device, &#39;   Exit&#58; - mate in n found&#58; ERRORLEVEL = n&#39;&#41;;
   writeln&#40;Device, &#39;		 - mate  not found&#58; ERRORLEVEL = 0&#39;&#41;;
   writeln&#40;Device, &#39;		 - halted  by user&#58; ERRORLEVEL = 9999&#39;&#41;;
   writeln&#40;Device&#41;;

		close&#40;Device&#41;;
		halt;

	  end;

	val&#40;paramstr&#40;3&#41;,kpmaxmov,cod&#41;;

	if &#40;cod<>0&#41; then
	  begin
		writeln&#40;Device&#41;;
		writeln&#40;Device, &#39;INPUT ERROR&#58; max.mov should be an integer&#39;&#41;;
		goto entrynok;
	  end;

	if &#40;kpmaxmov<1&#41; or &#40;kpmaxmov>64&#41; then
	  begin
		writeln&#40;Device&#41;;
		writeln&#40;Device, &#39;INPUT ERROR&#58; max.mov should be between 1 and 64&#39;&#41;;
		goto entrynok;
	  end;

	if upc&#40;paramstr&#40;4&#41;&#41;=&#39;A&#39; then kpallmov&#58;=true else
	  if upc&#40;paramstr&#40;4&#41;&#41;=&#39;C&#39; then kpallmov&#58;=false else
		begin
		  writeln&#40;Device&#41;;

		  writeln&#40;Device,
			  &#39;INPUT ERROR&#58; Must specify A &#40;all Moves&#41; or C &#40;checks only&#41;&#39;&#41;;

		  goto entrynok;
		end;
			 
   if upc&#40;paramstr&#40;2&#41;&#41;=&#39;W&#39; then Turn&#58;=White else
	 if upc&#40;paramstr&#40;2&#41;&#41;=&#39;B&#39; then Turn&#58;=Black else
		begin
		   writeln&#40;Device&#41;;

		   writeln&#40;Device,
			  &#39;INPUT ERROR&#58; Must specify who moves&#58; B &#40;Black&#41; or W &#40;White&#41;&#39;&#41;;

		   goto entrynok;
		 end;

	&#40;* reset the position and try to parse the FEN notation *&#41;

	Posit&#58;=ZeroPosit;

	if not FEN2Posit&#40;paramstr&#40;1&#41;&#41; then
	  begin
		 writeln&#40;Device&#41;;
		 writeln&#40;Device, &#39;INPUT ERROR&#58; Illegal FEN position&#39;&#41;;
		 goto entrynok;
	  end;

	&#40;* print the board and go search the mate *&#41;

	PrintBoard;
	writeln&#40;Device, &#39;FEN&#58; &#39;,paramstr&#40;1&#41;,&#39; &#39;,paramstr&#40;2&#41;&#41;; writeln;
	SolveMate;

	&#40;* that&#39;s all folks *&#41;

end.

vns chưa compile thử :(
.

#4
quanghoa

quanghoa

    Sĩ quan

  • Thành viên
  • 364 Bài viết
Không chạy được
Hình đã gửiHình đã gửiHình đã gửiHình đã gửiHình đã gửiHình đã gửiHình đã gửiHình đã gửiHình đã gửiHình đã gửi

#5
TheIncredibleMachine

TheIncredibleMachine

    Binh nhất

  • Thành viên
  • 42 Bài viết
Thuật toán cho những trò chơi chiến thuật theo từng bước này là Alpha-Beta, bạn có thể Google, có rất nhiều tài liệu nói về cái này.
Diễn đàn thảo luận giải thuật và lập trình: http://www.ioicamp.net/forums/
Các online judge hay: Sphere Online Judge - SPOJ Vietnam - TopCoder




0 người đang xem chủ đề

0 thành viên, 0 khách, 0 thành viên ẩn danh