Choice by chance, picoEngine?

choiceply2

Today I added another set of options to picoEngine. Since it can now think through up to 8 ply, I needed to give it an option to choose how many ply you want it to think through. The commit is simple enough, which you can check out, but here is the gist of it:

	cout << "option name Ply type combo default 8 var 1 var 2 var 3 var 4 var 5 var 6 var 7 var 8" << endl;
	// End of Options
	cout << "uciok" << endl;
}
                styleRandom = false;
                cout << "Normal Mode." << endl;
            }
    if (std::string::npos != setString.find("1"))
            {
                chosenPly = 1;
                cout << "Ply 1." << endl;
            }

With further blocks for 1 through 8. I could have used case, but this worked well enough. Now just to make it smarter….

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.