picoEngine is such a rook-ie!

picoEngineRook

Now the rooks can move too!

While it is only a small improvement, the rooks can now also be moved by picoEngine, which is a great step forward. Overall, that leaves 2 of 6 pieces complete for motion, or about 33% complete. Technically, when that is done, the engine will be completely playable, however, much work will be needed to make it “intelligent”.

While there are probably better ways to do this, I followed a pretty simple pattern for making the rook moves:

// Up moves
bool notI = true;
int j = 1;
int vert = 8;
int k = i;
if (i < 56) {
k = i + (vert * j);
}
while (theBoard[k] == ‘-‘ && notI) {
theseMoves.push_back(k);
vert += 8;
if (k < 56) {
k = i + (vert * j);
} else {
notI = false;
}
} // While it’s empty.
if (isupper(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.

// Down moves
notI = true;
j = -1;
vert = 8;
k = i;
if (i > 7) {
k = i + (vert * j);
}
while (theBoard[k] == ‘-‘ && notI) {
theseMoves.push_back(k);
vert += 8;
if (k >7) {
k = i + (vert * j);
} else {
notI = false;
}
} // While it’s empty.
if (isupper(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.

That is for “up” moves, and the pattern repeats for down, left, and right. As always, you can check out the full commit on my GitLab.

Linux – keep it simple.

Make a move picoEngine….

picoEngineMoves

For the first time ever, picoEngine made it’s very own move.

While no where near as important as my children taking their first steps, it was really a monumental moment for me when picoEngine made it’s very first move using UCI commands while loaded in Arena. That’s right, after several hours of tinkering, I loaded the picoEngine into the chess application called Arena, and let it make it’s very first move!

Of course, at this point, the only thing it can move is the knight, since that’s all I’ve programmed it for, but still, it was a monumental step in the direction of C++ chess engine programming for me!

While I’ve made two chess engines in Java, for various Android apps, this is different. Not only am I programming in C++, but my goal is to use the Universal Chess Interface so my chess engine can be loaded into any chess program. That is making things a bit tough. But now that I have successfully made a move, hopefully it will be smooth sailing from here on out.

string Moves::bestMove(string boardPositions, bool whoseTurn, bool styleRandom){
string candidateList = Moves::available(boardPositions, whoseTurn);
string chosenMove = “”;
if (candidateList.size() > 0){
//if (styleRandom) {
srand((int)time(0) + candidateList.size());
int r = (rand() % (candidateList.size()/5));
for (int a = 0; a < 4; a++) {
chosenMove += candidateList[(5*r)+a];
}
//} else {

//}
}
return chosenMove;
}

The check for available moves and king safety are things we already covered, so essentially, I just needed to make it choose the best move of the ones available. In the future, this will hopefully be a weighted decision, but for now, I’m just passing a random choice of the available moves in the list, which, of course, only features the knights at present.

As always, you can check out the full commit on my GitLab.

Linux – keep it simple.

picoEngine meets Bob Seger….

nightmoves

Okay, so not really. Bob Seger once had a hit called “Night moves” which is what I just added to my picoEngine: knight moves. As always, you can check through the whole commit on my GitLab. Once again, I borrowed from my Java engine, justchess engine but had to do quite a bit of work to convert this method into C++. For instance, in Java, I make an array, but in C++ I had to set up a vector.

Overall, the code is cleaner and more compact for C++ than it was for Java. I’m not sure if it is because I am getting better, or just the language differences, but I’ll take it! I’m still a bit fuzzy about comparing signed and unsigned commits, so I have to keep editing my loops like so:

for(int l=0; (unsigned)l<theseMoves.size();l++) {

Making sure to put the parenthesis and unsigned before the integer. But, it seems to work, so I’ll go with that for now, perhaps later I can study up on it a bit more and see why this is needed.

Linux – keep it simple.

Is the king safe, picoEngine?

kingsafe

One of the really important rules of playing chess, in fact, the rule upon which the entire game hinges, is king safety. You can’t make a move that puts your king in an unsafe position. This position of unsafe-ness is referred to as ‘check’. If you can’t find a way to get out of the position of being in ‘check’, then you are in ‘checkmate’.

Of course, if you are not in ‘check’ but any move would make you so, it is called a ‘stalemate’.

Thus, you can see the absolute need for a method or function to assess if the king is in check or not. So, I borrowed a method from my previous work in Java, the justchess engine. Of course it had to be edited for C++, but since both languages are object oriented programming languages, then it wasn’t too difficult.

You can check the commit at my GitLab for the full scoop, but essentially, my method assumes that the king is safe unless proven otherwise. This saves time, because at any point, if the king is proven unsafe, or in check, it will stop recursing through the board and other pieces and return a status of false: the king is not safe.

Linux – keep it simple.

Once again, and from the top, picoEngine.

Last time I mentioned how the UCI (Universal Chess Interface) required two types of input, the FEN input, and a move by move input style. The second is rather interesting, especially for the internal chess board I have set up, because the input uses standard notation with alphanumeric characters.

startpos

Here you can see my code in action, moving the pieces of the board.

Once I figured out how to convert char (actors) to int (egers) than all was well, but doing that is harder than it seems. Especially coming from Java to C++. I would think that there was a more “intuitive” way to do this, but alas, I could not find one.

Supposedly, you could simply use it like so:

char aChar = ‘1’;

char bChar = ‘a’;

int c = aChar;

int d = bChar;

But I get strange numbers from that. Instead, I needed to do this:

char aChar = ‘1’;

char bChar = ‘d’;

int c = aChar – ‘0’;

int d = bChar – ‘a’;

To convert them to useful numbers. Now it would return 4 for d, and 1 for 1. Great huh? Then I just used some simple math:

int first =63 – ((sepVect[k].at(0) – ‘a’ + 1) + (((sepVect[k].at(1) – ‘1’) * 8) – 1));

int second =63 – ((sepVect[k].at(2) – ‘a’ + 1) + (((sepVect[k].at(3) – ‘1’) * 8) – 1));

And converted them to space numbers for my board! It was pretty easy after that, to just move the piece from the first spot to the second spot and leave a blank space behind. Be sure to check out the full code on my GitLab repository!

Linux – keep it simple.

picoEngine Meets FEN

print

One of the more interesting parts to programming chess engines, is using the UCI, Universal Chess Interface. In that interface standard are very specific settings. One of those is that engines should accept input as either move by move, or through FEN.

The FEN part was actually quite easy. Just breaking down characters of the line and figuring out what goes where. The toughest part was understanding vectors so I could do what I wanted to do. Here’s a glimps of the code:

vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;

while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}

return internal;
}
………………………………………..Edited for space…………………………………
void inputPosition(string posString)
{
vector<string> sepVect = split(posString, ‘ ‘);
string str1 = sepVect[1];
if (sepVect[1] == “fen”)
{
// FEN board setup.
string stringBoard = “”;
string str2 = sepVect[2];
for (int i = 0; (unsigned)i < str2.length(); i++) {
char myChar = str2.at(i);
if (isdigit(myChar))
{ int imyChar = myChar – ‘0’;
for (int a = 0; a < imyChar; a++)
{ stringBoard = stringBoard + “-“; }
}
else if (isalpha(myChar))
{ stringBoard = stringBoard + myChar; }
} // end for.
int j = stringBoard.length();
for (int i = 0; i < j; i++) {
if (i < 64){ theBoard[i] = stringBoard.at(i); }
}
if (sepVect[3].at(0) == ‘w’)
{ whitesTurn = true; } else { whitesTurn = false; }
if (sepVect[4].at(0) == ‘K’)
{ Kcastle = true; } else { Kcastle = false; }
if (sepVect[4].at(1) == ‘Q’)
{ Qcastle = true; } else { Qcastle = false; }
if (sepVect[4].at(2) == ‘k’)
{ kcastle = true; } else { kcastle = false; }
if (sepVect[4].at(3) == ‘q’)
{ qcastle = true; } else { qcastle = false; }
if (sepVect[5].at(0) == ‘-‘)
{ enPassant = false;} else { enPassant = true;}
moveSince = stoi (sepVect[6]);
turnCount = stoi (sepVect[7]);
} // End FEN

I just broke down the string into vectors separated by spaces. Then in each vector, I figured out where the piece needed to go, or how many blank spaces I needed.

Now I still have to work out a few oddities, but I think this should work fairly well, and seems to have in the few test runs I’ve given it. You can check out the whole commit on my GitLab if you’d like!

Linux – keep it simple.

Blindfold Chess, Mr. picoEngine?

While building the picoEngine in C++, I will often be wondering what the engine is thinking. One of the problems is that I can not “see” the engine’s board, so I don’t know why it would make a particular move. So, I added a print function. This also is in keeping with my “engine mentor” Logic Crazy, who often used this technique so he could evaluate the board position.

The code is rather straight forward:

string printBoard = “”;

for (int i = 0; i < 64; i++){

printBoard = printBoard + theBoard[i];

}

cout << printBoard << ” ” << whitesTurn << ” ” << Kcastle

<< ” ” << Qcastle << ” ” << kcastle << ” ” << qcastle

<< ” ” << moveSince << ” ” << turnCount << endl;

Not only did it work, but it seems to be rather helpful. Now, as I work on inserting positions via FEN and the startpos moves system that is defined in the UCI, it makes checking my work easy. Here is a screen shot of it in action:

print_picoEngine

Linux – keep it simple.

Shaken or Stirred, picoEngine?

Well, more like Random or Normal, actually. I’m off to a bit of a slow start, but I am making progress on the picoEngine.

picoEngine_2

Here you can see picoEngine’s “many” options…..

First on my list was to give picoEngine an option for the GUI users to select from. Right now, the only option it has is to be completely random about moves, or to play as normal. Okay, technically it can’t play at all at the moment, but if it were to play, these are the two “styles” or play modes.

2018-09-13 12:50:37.472<–1:– engine[slotnr].EngineProcess.Running —
2018-09-13 12:50:37.475<–1:Waiting….
2018-09-13 12:50:37.489–>1:uci
2018-09-13 12:50:37.489<–1:id name picoEngine A01
2018-09-13 12:50:37.490<–1:id author Alaskalinuxuser
2018-09-13 12:50:37.490<–1:Apache 2.0 License.
2018-09-13 12:50:37.491<–1:https://thealaskalinuxuser.wordpress.com
2018-09-13 12:50:37.492<–1:option name Style type combo default Normal var Random var Normal
2018-09-13 12:50:37.492<–1:uciok
2018-09-13 12:50:37.507–>1:isready
2018-09-13 12:50:37.544<–1:readyok
2018-09-13 12:50:43.329–>1:setoption name Style value Normal
2018-09-13 12:50:43.330<–1:Setting Options….
2018-09-13 12:50:43.331<–1:Normal Mode.
2018-09-13 12:50:43.332<–1:Options set.
2018-09-13 12:51:37.602–>1:setoption name Style value Random
2018-09-13 12:51:37.603<–1:Setting Options….
2018-09-13 12:51:37.603<–1:Random Mode.
2018-09-13 12:51:37.603<–1:Options set.
2018-09-13 12:51:49.178–>1:setoption name Style value Normal
2018-09-13 12:51:49.179<–1:Setting Options….
2018-09-13 12:51:49.180<–1:Normal Mode.
2018-09-13 12:51:49.181<–1:Options set.
2018-09-13 12:54:43.028–>1:setoption name Style value Normal
2018-09-13 12:54:43.029<–1:Setting Options….
2018-09-13 12:54:43.030<–1:Normal Mode.
2018-09-13 12:54:43.030<–1:Options set.

Here you can see the chess GUI sending the signal to set the optional mode after each choice that I made while testing it out. My plan is to use a bool for styleRandom and have it make move choices based on that. You can check out the full commit on my GitLab if you’d like, but here is the main portion of the code:

void inputSetOption(string setString)
{
cout << “Setting Options….” << endl;
if (std::string::npos != inputString.find(“Random”))
{
styleRandom = true;
cout << “Random Mode.” << endl;
}
if (std::string::npos != inputString.find(“Normal”))
{
styleRandom = false;
cout << “Normal Mode.” << endl;
}
cout << “Options set.” << endl;
}

Linux – keep it simple.

Building picoEngine, a UCI Compatible Chess Engine

When I started programming, one of my early ideas was to build a chess engine. What sounds more sophisticated for a programmer, than building something like that? It just sounds smart!

Thus entered the “Beginner Engine” which I built for Android using a YouTube tutorial by Logic Crazy. It was great to have someone who programmed in Java explaining every step of making a playable engine. Unfortunately, it had some serious flaws for use on Android. None of which was Logic Crazy’s fault, he was showing the basics of building an engine. But it couldn’t castle, and bogged down my phone every time I used it.

After learning the ropes, I decided to build the “JustChess Engine”. It no longer bogged down the system, and despite being relatively stupid, it is quite playable (except for a pesky en passant bug that I still need to fix). The problem with using the JustChess Engine is that it was written by me for my application, so it does not respond to regular engine commands, like those from the UCI, or the Universal Chess Interface. Instead it only responds to my commands that I programmed it with.

picoEngine_1

Here you can see my new engine, picoEngine, talking to Arena Chess!

Hence, I’ve decided to build a UCI compatible chess engine, so I can learn how to interface with GUI’s through UCI. Thus, I’ve started a C++ project called “picoEngine”, which literally means very small engine. You can check out the first commit on my GitLab, and here is my first bit of working code:

/* Copyright 2018 by AlaskaLinuxUser (https://thealaskalinuxuser.wordpress.com)
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <iostream>
using namespace std;

string engineName = “picoEngine A01”;
string inputString;

void inputUCI();
void inputSetOption(string setString);
void inputIsReady();
void inputUCINewGame();
void inputPosition(string posString);
void inputGo();
void inputQuit();
void inputPrint();
bool runProgram = true;

int main()
{
cout << “Waiting….”;

while(runProgram) {
getline(cin, inputString);

if (inputString == “uci”)
{
inputUCI();
}
else if (inputString.rfind(“setoption”, 0) == 0)
{
inputSetOption(inputString);
}
else if (inputString.rfind(“isready”, 0) == 0)
{
inputIsReady();
}
else if (inputString.rfind(“ucinewgame”, 0) == 0)
{
inputUCINewGame();
}
else if (inputString.rfind(“position”, 0) == 0)
{
inputPosition(inputString);
}
else if (inputString.rfind(“go”, 0) == 0)
{
inputGo();
}
else if (inputString.rfind(“quit”, 0) == 0)
{
inputQuit();
}
else if (inputString.rfind(“print”, 0) == 0)
{
inputPrint();
}
}
return 0;
}

void inputUCI()
{
cout << “id name ” << engineName << endl;
cout << “id author Alaskalinuxuser” << endl;
cout << “Apache 2.0 License.” << endl;
cout << “https://thealaskalinuxuser.wordpress.com&#8221; << endl;
// Options can go in here
cout << “option name Style type combo default Normal var Random var Normal” << endl;
// End of Options
cout << “uciok” << endl;
}
void inputSetOption(string setString)
{
cout << “Setting Options….” << endl;
// Set your options like: setoption name Hash value 32
cout << “Options set.” << endl;
}
void inputIsReady()
{
// Are we ready for input?
// Do any initialization first.
cout << “readyok” << endl;
}
void inputUCINewGame()
{
cout << “Creating New Game….” << endl;
}
void inputPosition(string posString)
{
cout << “Accepting Position….” << endl;
}
void inputGo()
{
cout << “Going….” << endl;
}
void inputQuit()
{
cout << “Quiting….” << endl;
runProgram = false;
}
void inputPrint()
{
cout << “Printing Board….” << endl;
}

I was even able to load it into Arena Chess and talk to it through the interface. Obviously it ran out of time because it can’t actually make a move, but it did respond to standard uci commands, which is great!

Linux – keep it simple.

Fool’s Mate Friday: Bobby Fischer Teaches Chess

While looking at a few old chess programs for some ideas about my current programming lull, I stumbled across a rather useful old DOS program: Bobby Fischer Teaches Chess. Of course, I knew it originally was a book, but I did not know that it was later turned into a chess program.

The interface is a bit clunky, running in DosBox. Of note, the taller pieces, like queens or kings, often obscure smaller pieces like pawns, due to the board orientation. You might also have noticed that the first pic is our infamous Fool’s Mate! (I just couldn’t help but include it here!)

I went through the first chapter, 1 of 6. If you want to try it out, be prepared for long chapters! It appears to save bookmarks, so I suppose that you could come back to it later.

One thought that this gave me was that of using the JustChessEngine or JustChess interface for instructional purposes…. Perhaps some sort of “find the mate”, or “defend from mate”. Not sure, but it is an idea. I’ll have to think on it.

Linux – keep it simple.