A bugfix for TSCP

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

A bugfix for TSCP

Post by sje »

A bugfix for TSCP

If you're using TSCP (Tom Kerrigan's Simple Chess Program) and XBoard, you might be interested in the following bugfix to source file main.c, routine parse_move():

Code: Select all

/* parse the move s (in coordinate notation) and return the move's
   index in gen_dat, or -1 if the move is illegal */

int parse_move(char *s)
{
	int from, to, i;

	/* make sure the string looks like a move */
	if &#40;s&#91;0&#93; < 'a' || s&#91;0&#93; > 'h' ||
			s&#91;1&#93; < '0' || s&#91;1&#93; > '9' ||
			s&#91;2&#93; < 'a' || s&#91;2&#93; > 'h' ||
			s&#91;3&#93; < '0' || s&#91;3&#93; > '9')
		return -1;

	from = s&#91;0&#93; - 'a';
	from += 8 * &#40;8 - &#40;s&#91;1&#93; - '0'));
	to = s&#91;2&#93; - 'a';
	to += 8 * &#40;8 - &#40;s&#91;3&#93; - '0'));

	for &#40;i = 0; i < first_move&#91;1&#93;; ++i&#41;
		if &#40;gen_dat&#91;i&#93;.m.b.from == from && gen_dat&#91;i&#93;.m.b.to == to&#41; &#123;

			/* if the move is a promotion, handle the promotion piece;
			   assume that the promotion moves occur consecutively in
			   gen_dat. */
			if &#40;gen_dat&#91;i&#93;.m.b.bits & MMpromote&#41;
				switch &#40;s&#91;4&#93;) &#123;
					case 'N'&#58;
					case 'n'&#58;
						return i;
					case 'B'&#58;
					case 'b'&#58;
						return i + 1;
					case 'R'&#58;
					case 'r'&#58;
						return i + 2;
					case 'Q'&#58;
					case 'q'&#58;
						return i + 3;
					default&#58;  /* assume it's a queen */
						return i + 3;
				&#125;
			return i;
		&#125;

	/* didn't find the move */
	return -1;
&#125;
Without a fix, tscp running under XBoard will think an underpromotion move which uses a lowercase piece letter is a queen promotion. This occurred in an XBoard game against Symbolic where Symbolic underpromoted a pawn to a rook and tscp thought it was a promotion to a queen. After this, tscp incorrectly claimed a stalemate, and was then forfeited by XBoard for making a false draw claim.

----

I've made a big bunch of changes in tscp to greatly reduce the use of naked literals. I'l probably make some more changes after further testing. If you're interested, send me a message in another week or two and I'll upload the revised source.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Additions to TSCP file defs.h

Post by sje »

Additions to TSCP file defs.h

This is the start; there will be more. The changes are propagated throughout the source to make the code more readable.

Code: Select all

/* Binary exponentiation macro */

#define BX&#40;n&#41; &#40;1 << &#40;n&#41;)

/* Avoiding naked literals */

#define SqLen        64
#define FileLen       8
#define MailboxLen  120
#define OffsetLen     8
#define ColorRCLen    2
#define PieceRCLen    6

Code: Select all

/* Move flag bits */

#define MBcapture   0
#define MBcastle    1
#define MBenpassant 2
#define MBdoubleadv 3
#define MBpawnmove  4
#define MBpromote   5

/* Move flag bit masks */

#define MMcapture   BX&#40;MBcapture&#41;
#define MMcastle    BX&#40;MBcastle&#41;
#define MMenpassant BX&#40;MBenpassant&#41;
#define MMdoubleadv BX&#40;MBdoubleadv&#41;
#define MMpawnmove  BX&#40;MBpawnmove&#41;
#define MMpromote   BX&#40;MBpromote&#41;

/* Move flag bit mask combinations */

#define MCpawncapt &#40;MMpawnmove | MMcapture&#41;
#define MCpawndadv &#40;MMpawnmove | MMdoubleadv&#41;
#define MCpawnep   &#40;MMpawnmove | MMenpassant | MMcapture&#41;

/* Castling flag bits */

#define CBwk 0
#define CBwq 1
#define CBbk 2
#define CBbq 3

/* Castling flag bit masks */

#define CMwk BX&#40;CBwk&#41;
#define CMwq BX&#40;CBwq&#41;
#define CMbk BX&#40;CBbk&#41;
#define CMbq BX&#40;CBbq&#41;

/* Castling flag bit mask combinations */

#define CCall &#40;CMwk | CMwq | CMbk | CMbq&#41;
#define CCiA1 &#40;CMwk | CMbk | CMbq&#41;
#define CCiE1 &#40;CMbk | CMbq&#41;
#define CCiH1 &#40;CMwq | CMbk | CMbq&#41;
#define CCiA8 &#40;CMwk | CMwq | CMbk&#41;
#define CCiE8 &#40;CMwk | CMwq&#41;
#define CCiH8 &#40;CMwk | CMwq | CMbq&#41;
mvk
Posts: 589
Joined: Tue Jun 04, 2013 10:15 pm

Re: Additions to TSCP file defs.h

Post by mvk »

sje wrote:Additions to TSCP file defs.h

This is the start; there will be more. The changes are propagated throughout the source to make the code more readable.

Code: Select all

#define CMbq BX&#40;CBbq&#41;
Readability is in the eye of the beholder. I have never found issues reading TSCP. Personally I find a couple of hardcoded numbers easier to read than vwllss bbrvtns.

On a more objective subject: Mind that TSCP is copyrighted by its author and explicitly not released under any open source license such as GPL-like or BSD-like. This means that you need permission from the author to publish modified versions or snippets of modified versions. Such big refactoring doesn't fall under the "fair use" exemptions copyright provides. Just a heads-up.
[Account deleted]
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Additions to TSCP file defs.h

Post by Rein Halbersma »

mvk wrote:
sje wrote:Additions to TSCP file defs.h

This is the start; there will be more. The changes are propagated throughout the source to make the code more readable.

Code: Select all

#define CMbq BX&#40;CBbq&#41;
Readability is in the eye of the beholder. I have never found issues reading TSCP. Personally I find a couple of hardcoded numbers easier to read than vwllss bbrvtns.

On a more objective subject: Mind that TSCP is copyrighted by its author and explicitly not released under any open source license such as GPL-like or BSD-like. This means that you need permission from the author to publish modified versions or snippets of modified versions. Such big refactoring doesn't fall under the "fair use" exemptions copyright provides. Just a heads-up.
I would expect that posting a few snippets (like the one above) *would* fall under the fair use exemption. Uploading the entire modified source distro, or creating a GitHub repo to that effect, most likely wouldn't.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Additions to TSCP file defs.h

Post by sje »

mvk wrote:Readability is in the eye of the beholder. I have never found issues reading TSCP. Personally I find a couple of hardcoded numbers easier to read than vwllss bbrvtns.

On a more objective subject: Mind that TSCP is copyrighted by its author and explicitly not released under any open source license such as GPL-like or BSD-like. This means that you need permission from the author to publish modified versions or snippets of modified versions. Such big refactoring doesn't fall under the "fair use" exemptions copyright provides. Just a heads-up.
Naked literals much other than 0 or 1 are always suspect.

Re: TSCP copyright. Of course permission for a customized version general release is needed, as is apparent to anyone who has read the documentation packaged with TSCP. That same documentation contains release notes and credits for others who have provided derivative works.
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Additions to TSCP file defs.h

Post by Dann Corbit »

I made a version once that had piece lists and other things to make it go faster and Tom asked me not to release it so I didn't.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Additions to TSCP file defs.h

Post by sje »

Dann Corbit wrote:I made a version once that had piece lists and other things to make it go faster and Tom asked me not to release it so I didn't.
That's quite interesting. By the way, is his email address still the same as in the source? I'll need it to ask him for permission to distribute an entire source package.

My edits are not for the purposes of making the program stronger or faster; there are already strong and fast programs with publicly available sources. My interests are only in fixing bugs and improving readability -- the second being most helpful with the first. Because others may also benefit from these efforts, it makes it easier for me to justify my time working on the program. Also, others looking at my changes might spot deficiencies in my code and so let me know how to improve it further.

However, if permission is not forthcoming, then that's okay because I respect an author's desire to protect their work even if the reasons aren't clear to me.
Dann Corbit
Posts: 12541
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Additions to TSCP file defs.h

Post by Dann Corbit »

sje wrote:
Dann Corbit wrote:I made a version once that had piece lists and other things to make it go faster and Tom asked me not to release it so I didn't.
That's quite interesting. By the way, is his email address still the same as in the source? I'll need it to ask him for permission to distribute an entire source package.

My edits are not for the purposes of making the program stronger or faster; there are already strong and fast programs with publicly available sources. My interests are only in fixing bugs and improving readability -- the second being most helpful with the first. Because others may also benefit from these efforts, it makes it easier for me to justify my time working on the program. Also, others looking at my changes might spot deficiencies in my code and so let me know how to improve it further.

However, if permission is not forthcoming, then that's okay because I respect an author's desire to protect their work even if the reasons aren't clear to me.
I don't know if his email has changed. There is a link here:
http://www.tckerrigan.com/Chess/TSCP/

The version I made had lots of complicated changes and made the package much larger.
His reasoning was that the spirit of TSCP was a simple program for people to learn chess from and the expansions made it into something else.