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.

Advertisements

Qt Error: ‘string’ was not declared in this scope.

Oddly enough, I ran into this same problem while using SFML. Here in Qt-creator, I’m getting this error while trying to use a string. I declared something like so:

#include <QCoreApplication>
#include <QDebug>
#include <QList>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QList<String> myStringList;

    myStringList << "Howdy."
                 << " I "
                 << " like this"
                 << " course on Qt.";

    myStringList.append(" I hope");
    myStringList.append(" you do too!");

    //qDebug() << myStringList[1];

    for (int i=0; i < myStringList.count(); i++) {
        qDebug() << myStringList[i];
    }

    return a.exec();
}

And I get the error that the string is not declared in this scope! So, I tried adding:

#include <string>

to add the string library from C++. However, then I got errors when returning those strings to qDebug, which said it couldn’t take a ‘char’ as a ‘string’.

Well, just like SFML, Qt has its very own string library, and it is automatically included, called QString, like this:

QList<QString> myStringList;

QString automagically changes the string to character or strings or whatever to make it work when you need it to. Seems like a bit of laziness for me as a programmer, but pretty handy and very convenient in my Qt apps!

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.

QLayout::addChildLayout: layout already has a parent!

qt_4_31

While working with layouts in Qt creator, I ran into an interesting problem. I am trying to add my “gridLayout_2” (the box with 9 push buttons) into the “myGrid” (the box with 8 text labels). When I run this code:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QGridLayout * myGrid = new QGridLayout(this);


    myGrid->addWidget(ui->label,0,0);
    myGrid->addWidget(ui->label_2,0,1);
    myGrid->addWidget(ui->label_3,0,2);
    myGrid->addWidget(ui->label_4,1,0);
    myGrid->addWidget(ui->label_5,1,1);
    myGrid->addWidget(ui->label_6,1,2);
    myGrid->addWidget(ui->label_7,2,0);
    myGrid->addWidget(ui->label_8,2,1);
    myGrid->addLayout(ui->gridLayout_2,2,2);
    setLayout(myGrid);

}

Widget::~Widget()
{
    delete ui;
}

I get this error:

QLayout::addChildLayout: layout “gridLayout_2” already has a parent

And the “gridLayout_2” is not added to the “myGrid” layout. At first I thought I was doing something wrong (maybe I still am), but I found that this is a known bug in Qt Creator:

https://bugreports.qt.io/browse/QTBUG-24420

QBoxLayout::addLayout() and QGridLayout::addLayout() functions should reparent the added layout automatically if it already has a parent. Currently these functions call QLayout::addChildLayout() which does not add the child layout if it already has been added to some other layout i.e. has a parent.

Please note that QBoxLayout::addWidget() and QGridLayout::addWidget() reparent the added widget. Thus there is an inconsistency on the functionality.

So I didn’t figure out a way to add a grid layout inside one of the grids of another grid layout. If you figure this out, then be sure to let me know!

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.

Qt Creator, changing the default template for .pro files….

qt_change_default

The .pro file holds key information for your project that is used for compiling.

One issue that I have previously mentioned here was that certain styles of lambdas require you to use a C++ standard of 2011 or newer. This can be quickly overcome by adding the proper QMAKE flags to your .pro file in your project directory.

This becomes a bit tedious though, when you forget that and need to re-find the QMAKE flags so you can add them to your .pro file every time that you start a new project. Some web searching lead me to the conclusion that the templates for the files created in Qt Creator are held here:

/usr/share/qtcreator/templates/wizards

There are numerous folders here, each pertaining to a different kind of application/program that you can make. Within each folder are the template files used to create the default files you get when you create a new project. If you open them as a root user, you can edit them to your hearts content and then save them. Next time that you use Qt Creator to create a new project, these now edited files will be used (no restarting Qt Creator required).

This works great, and I have fixed numerous .pro files for console apps, etc., however, the one I can’t find is the Qt Widget Application .pro file, and that is the one I use the most! So I’ll keep looking. In the mean time, you can use these to edit your other default files.

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.

error: no matching function for call to ‘QAction::QAction(const char [5])’

qt_4_21

While working through section 4 of my Qt GUI creator course, I ran into an interesting error:

/home/alaskalinuxuser/Documents/qt_course/section_4_21_qmainwindow/mainwindow.cpp:19: error: no matching function for call to ‘QAction::QAction(const char [5])’
QAction * quitAction = new QAction(“Quit”);
^

What made the error interesting was that my code mirrored the instructors to a T:

// Now some QActions….
// Quiting
QAction * quitAction = new QAction(“Quit”);
// And connect it.
connect (quitAction,&QAction::triggered,[=](){
QApplication::quit();
});

However, after reading up a bit on it, I needed to add “this” to my QAction, like so:

// Now some QActions….
// Quiting
QAction * quitAction = new QAction(“Quit”, this);
// And connect it.
connect (quitAction,&QAction::triggered,[=](){
QApplication::quit();
});

Once I did that, it was smooth sailing. I don’t know for sure, it may be because the instructor was using Windows with MinGW and I am using Linux with GCC, or if it is a Qt version difference. Either way, if you run into this error, here’s the quick fix!

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.

Qt Creator warning: ‘auto’ changes meaning in C++11; please remove it

Just wrapped up section 3 of my Qt C++ beginner GUI course! Boy, this course is fun! I really appreciate the instructor taking the time to show 3 different ways to do things, and explaining why one might be more useful than others in different situations. I did run into a couple of errors though:

warning: ‘auto’ changes meaning in C++11; please remove it

And this one, too:

error: no matching function for call to ‘Widget::connect(QPushButton*&, void (QAbstractButton::*)(bool), Widget::Widget(QWidget*)::<lambda()>)’
});

Both of these are related, so I thought I’d bundle them together in this post. Apparently, GCC for Linux, while super smart, is so flexible that you can, of course, use different C++ standards, however, by default, it uses the OLDEST one. WHY?! So, in Qt creator, you have to tell it specifically to use the latest C++ standard, like so:

#-------------------------------------------------
#
# Project created by QtCreator 2018-06-22T09:40:37
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = section_3_18
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

QMAKE_CXXFLAGS = -std=c++14
QMAKE_LFLAGS = -std=c++14

Those last two QMAKE lines are the ones I added. You can set them to any standard you like that is in your compiler, such as c++11, c++14, c++1z (well, on mine that is the 2017 standard, some newer ones may have c++17). Adding those two lines and saving the file will fix it.

The second error was caused because I had Qt4 and Qt5 installed. Obviously, you should use the newest one available to you. So, I had to click:

Tools->Options->Kits->Desktop

qt_3_17

And choose the latest Qt5 version, rather than the default of the lower Qt4 version. It took me a while of web browsing to figure this out, so hopefully this will save someone else some time!

Linux – keep it simple.