Would you like to en passant, picoEngine?

After hours of toil and pain, I realized that I was going about this all wrong, and then in 15 minutes enabled the ability for picoEngine to choose to en passant. It is amazing how far off track I was, and how quickly it could be fixed when I was on the right path.

enpassGood

I was attempting to call a private product of the “Board” class, when I needed to simply make it a public class method that could be called. It’s a bit lengthy, but you can always take a look at my GitLab for the full commit. Here is the important snippet:

if (isPass){
if (i > 23 && i < 32) {
int passedPawn = 8;
if (enPass == ‘a’){ passedPawn = 0; }
else if (enPass == ‘b’){ passedPawn = 1; }
else if (enPass == ‘c’){ passedPawn = 2; }
else if (enPass == ‘d’){ passedPawn = 3; }
else if (enPass == ‘e’){ passedPawn = 4; }
else if (enPass == ‘f’){ passedPawn = 5; }
else if (enPass == ‘g’){ passedPawn = 6; }
else if (enPass == ‘h’){ passedPawn = 7; }
if (passedPawn – colNum == 1) {
theseMoves.push_back(i-7);
}
if (colNum – passedPawn == 1) {
theseMoves.push_back(i-9);
}
}}// End en passant moves

The best part is that picoEngine can now suggest an en passant move, which is an important part of the game of chess.

Linux – keep it simple.

Advertisements

picoEngine, in passing?

One of the key things that any chess engine needs to be able to do, is take the input from the calling program with regards to en passant. With a little algebra, it wasn’t too hard to set up:

enpassworked

Fortunately, it was pretty easy to add the ability for the engine to check if the move was an en passant move, but it relied on two parts, both of which you can check out in the commit on my GitLab.

The first part was to make a flag that en passant was enabled, because the enemy pawn moved two squares. The second part was to calculate if the conditions were met on the next move to be an en passant move or not. If so, it then needed to adjust it’s own board and remove the “extra” pawn that was left behind!

Sounds simple enough! Guess you’ll have to be the judge of that by looking at the commit though.

Linux – keep it simple.

To castle, or not to castle, that is the question, picoEngine….

will_castle

As you can see from the above screenshot, one of the engines possible moves is e8g8, which is for the black king to castle on the king’s side. This is great news. Previously, I had given the engine the ability to accept the input for castling, but it could not choose to do so on it’s own. But not anymore! Now it can decide to castle if it thinks that is the best thing to do!

Of course, you can read through the entire commit on my GitLab, but here is the focal point:

if (theBoard[60] == ‘k’) {
cout << “The king is on square 60” << endl;
if (theBoard[59] == ‘-‘ && theBoard[58] == ‘-‘ &&
theBoard[57] == ‘-‘ && theBoard[56] == ‘r’ && m_qcastle == true) {
cout << “Path is clear to castle queenside.” << endl;
if (isKingSafe(theBoard, turn)) {
//The king is not in check, so…
theBoard[59] = ‘k’;
theBoard[60] = ‘-‘;
if (isKingSafe(theBoard, turn)) {
//The king is safe in between, so…
theseMoves.push_back(58);
//Then the move will get checked if king is safe at destination.
}
theBoard[60] = ‘k’;
theBoard[59] = ‘-‘;}
} //queenside

This portion covers the black king’s queen’s side castle options. It is more or less a convoluted set of if/then statements. If the king is on the normal king starting square, and the “hallway” is empty between it and the rook, and the rook and king have never moved (m_qcastle == true) THEN check if the king is in check, and if he will be safe every step of the way. If he is safe, then suggest that as a move to consider.

While it is not the cleanest code, and will cause problems for “non-classical” variants of chess, such as chess960, this gets the job done for traditional chess use.

Linux – keep it simple.

Castle, picoEngine?

castleinput

After about an hour of accomplishing nothing, I finally gave up on what I was doing and fixed the UCI input for “movpos” when castling. So now, if given the move for castling, it moves the rook as well as the king!

You can check out the whole commit on my GitLab, but here is the only important part:

if (m_theBoard[first] == ‘k’ || m_theBoard[first] == ‘K’){
cout << “King Move” << endl;
if (first == 4 && second == 6) {
// White Castles King Side
m_theBoard[7] = ‘-‘;
m_theBoard[5] = ‘R’;cout << “castleWKS” << endl;
} else if (first == 4 && second == 2) {
// White Castles Queen Side
m_theBoard[0] = ‘-‘;
m_theBoard[3] = ‘R’;cout << “castleWQS” << endl;
} else if (first == 60 && second == 62) {
// Black Castles King Side
m_theBoard[63] = ‘-‘;
m_theBoard[61] = ‘r’;cout << “castleBKS” << endl;
} else if (first == 60 && second == 58) {
// Black Castles Queen Side
m_theBoard[56] = ‘-‘;
m_theBoard[59] = ‘r’;cout << “castleBQS” << endl;
}
} // Not a king move…..

As you can see, I just have the engine check if the input move was a king (k for black king, K for white king, which is standard computer notation). If it was a king that moved, it checks to see if it is one of the four possible castle moves, and then, if it is, the engine moves the rook as well. Nothing fancy, but it gets the job done!

Linux – keep it simple.

Android Remote Control through ADB

Today, I stumbled on perhaps the most ingenious use of ADB ever:

Remote control your Android phone through adb

A program and write-up by Marian Schedenig, that they shared on their website. This is perhaps the best adb tool I have ever seen, in that it takes three simple principles and puts them together to allow you to remote control your phone over ADB with your mouse, without ROOT permission!

output3

Here is a small gif that I put together from a screen recording of me using this awesome java tool. Essentially, it uses adb to take screen shots of the phone. Then it downloads the screen shot and updates it’s display. Finally, when you click on the screen, it sends an x/y co-ordinate to the phone over adb to tell it you swiped or clicked! It is absolutely brilliant!

The updates are a little slow, and you do have to edit the config file for your specific needs, but it works great, and is a really useful tool if you broke your screen or have touch screen issues, or just need to display your screen for an audience to view while you press buttons or work with apps. Since it is written in java, it is cross platform as well!

Linux – keep it simple.

Mini to Nano: Cutting a Sim Card

The other day, a friend asked me about what phone I recommend that they get. They had some unique needs, and I recommended a phone for them. One thing that I didn’t consider was that the new phone used a nano sim card slot, and their old phone used a micro sim card slot.

Fortunately, this wasn’t really a problem for me, as I have cut down sim cards before. It always causes the person I’m helping to raise an eyebrow, though, and sometimes it can be difficult to get them to accept that it is okay for them to hand over the sim card to me and my scissors. I think that they were particularly doubtful, since their new phone came with specific instructions and warnings against cutting down a card to fit.

However, with a little calm reassurance, I convinced them to let me cut down their sim card. I used this template:

https://www.appletips.nl/wp-content/uploads/downloads/2012/09/nanosim.pdf

gsmrestart

WARNING: cutting your sim card could cause it to never work again, and at the least, it will not be the bigger size that it was before, and certainly can’t go back into the phone it came out of. Do this at your own risk!

One thing that card cutters often forget is the thickness of the card. On the above pdf file, they mention this as a foot note:

The difference in thickness between NanoSIMs and previous SIM cards (90µm) corresponds to the thickness of a human hair and should therefore not make any difference in practice. Nevertheless you may slightly grind the plastic side of your SIM card. Never grind the chip side!

I typically just sand the back plastic side for a few seconds with a 200 grit sand paper, and I have always had a good fit. So, if you are willing to accept the risks, and run into a similar problem, then this might be a good option for you, too.

Linux – keep it simple.

Merging Two MP4 Files From The Command Line

term

One of the problems I find with video editing, is sometimes it becomes very complicated to do something very simple. Take my current need. I needed to simply tie two mp4 files together, unedited, just hook one to the next. There are lots of gui types, but that requires opening them up, adjusting settings, loading or importing the files, and just a little more time and work than I want for such a simple task.

Fortunately, I found a really easy to use method to do that from the command line: MP4Box!

You can install it like so:

$ sudo apt-get install gpac

$ sudo ln -s /usr/local/lib64/libgpac.so /usr/lib/libgpac.so

Once installed, it is very, very simple to use:

 

$ MP4Box -cat myFirstVideo.mp4 -cat mySecondVideo.mp4 -new myNewVideoFile.mp4

That’s it! Now, whenever I need to add two videos together, all I have to do is run one simple command, and in seconds, it is done! That’s what I like to call simple!

Linux – keep it simple.

Promotion time picoEngine!

picoEnginePawns

It’s really important to note that if a pawn gets promoted, it becomes the new piece, whatever that piece is. Either a knight, bishop, rook, or queen. While this was happening in the interface, it was not being recognized by picoEngine. Since picoEngine didn’t know it was promoted, it would “assume” a pawn was sitting there. This caused a lot of trouble with king safety calculations, as I’m sure you could understand.

if (sepVect[k].size() == 5) {
char newPiece = ‘-‘;
if (!m_whitesTurn) {
newPiece = tolower(sepVect[k].at(4);
} else {
newPiece = toupper(sepVect[k].at(4));
}
m_theBoard[second] = newPiece;
}

Fortunately the fix was pretty simple. I initiated a check, if there were 5 digits, then the fifth digit is the promotion piece! All I had to do was consider if it should be upper or lower case for my board, which was simply solved by inputting whose turn it was.

Linux – keep it simple.

picoEngine?! Where is the king?!

picoengineKingStuck

What good is king safety if the king refuses to capture it’s attacker? I guess I had programmed a cowardly lion… er… uh… king. Actually the real problem was that of where the king “was” on the board, per my code.

  // For checking if the king is safe.

int z;

if (whoseTurn){

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

switch (boardPositions[i]) {

–  case ‘K’: z = boardPositions[i];break;

+  case ‘K’: z = i;break;

}

}

So rather than the king being where he was supposed to be, the engine believed the king to be off the board at square “75” (it is the integer equivalent of the character ‘k’). So, above, you can see the minus of “case ‘K’: z = boardPositions[i];break;” and the plus replacing it with “case ‘K’: z = i;break;”. We needed it’s integer position on the board, not the integer equivalent of the character ‘k’…. Sometimes I marvel at my own foolishness.

Linux – keep it simple.

Pawns, one step at a time, picoEngine….

picoEnginePawns

Okay, it’s time to work on moves for the last piece: the pawn. Believe it or not, the pawn is the most difficult piece for me to program. It has so many small idiosyncrasies. If you are on the home row, you can go two spaces, but not any other time. If you were just passed by a pawn from it’s home row doing a two step, you can en passant. Oh, and you attack diagonally, but only move directly forward. I think you get the idea.

Just the basic one step, two step, and attack diagonally moves took 50 lines of code for me to write! You can check out the full commit on my GitLab, but here is the important part:

string Moves::pawnMoves(string boardPositions, int i) {
string list = “”;
vector <int> theseMoves;
string moveSquare;
string theBoard = boardPositions;
bool turn = true;
int rowNum = i/8;
int colNum = i%8;
int k = i + 16;
cout << rowNum << endl;
if (rowNum == 1) {
// The standard catch for moving two spaces forward.
if (theBoard[i+8] == ‘-‘ && theBoard[i+16] == ‘-‘) {
theseMoves.push_back(k);
}
}

// The standard catch for moving one space forward.
k = i + 8;
if (theBoard[k] == ‘-‘) {
theseMoves.push_back(k);
}
k = i + 7;// Attacking to the left and up.
if (colNum > 0 && islower(theBoard[k])) {
theseMoves.push_back(k);
}
k = i + 9;// Attacking to the right and up.
if (colNum < 7 && islower(theBoard[k])) {
theseMoves.push_back(k);
}
// End boring pawn moves.

// Need en passant moves //

for(int l=0; (unsigned)l<theseMoves.size();l++) {
int k = theseMoves[l];
if (islower(theBoard[k]) || theBoard[k] == ‘-‘) {
moveSquare = theBoard[k];
theBoard[k] = ‘P’;
theBoard[i] = ‘-‘;
if (isKingSafe(theBoard, turn)) {
char F = (char)(‘a’ + colNum);
char G = (char)(‘1’ + rowNum);
int rowNumK = k/8;
int colNumK = k%8;
char T = (char)(‘a’ + colNumK);
char U = (char)(‘1’ + rowNumK);
list = list + F + G + T + U + “,”;
}
theBoard[k] = moveSquare[0];
theBoard[i] = ‘P’;
}
}
return list;
} // End white pawn moves.

Now I just have to work on en passant, and work on the king’s castle moves. Almost done with moves so I can actually focus on building the “thinking” part of the engine!

Linux – keep it simple.