Showing posts with label jdk6. Show all posts
Showing posts with label jdk6. Show all posts

Saturday, April 5, 2014

Chess Coding Challenge - Part 4 - Solution in Human Readable Pseudo-code...

Part 1 - How I Leveraged Scala and FP to Find a Solution
Part 2 - Scala infrastructure source code (and two encoder/decoder implementations)
Part 3 - Scala solution source code (and interim encoder/decoder implementations)

Summary:

The final solution encoder/decoder, HuffmanSimpleKingsPawnOptimal, described in a human readable pseudo-code format.

At a very high level, the number of pawns and their ability to be promoted are the primary constraint of the problem domain. So, a layered approach is used to contextually select an optimal encoding strategy which is then leveraged by the next layer down. 


Details:
Here are the assumptions which were leveraged in the final solution:
  1. Both kings are always present in any valid board encoding - even the checkmate state is shown as the final board position prior to the capture of the king
  2. Of the 16 pawns present at the start of a game, a maximum of 12 may be promoted
  3. Due to the forced pawn promotion rule, there are never any pawns present on the first and last rows of the board

As this is not a description of the journey with which the solution was derived, I will jump straight into describing the decoder.

Decoding the header:
  • UseRowDecoder: The first bit the decoder reads is used to select between two pawn decoding strategies; either by row (true) or by column (false)
  • WhiteKing: The next 6 bits encode the position of the white king as a linear coordinate value; a value 0-63 inclusive
  • BlackKing: The next 6 bits encode the position of the black king as a linear coordinate value; a value 0-63 inclusive
  • PawnsToDecode: The next n bits (either 6 or 8 determined by UseRowDecoder) encode for each slice (either row or column) whether one or more pawns are present

Decoding each particular square:
  • Index: set to 0 - this is the board squares index which will contain a single integer value of 0 to 63 (inclusive), decode guided by information captured in the header
  • StartOfLoop: will decode guided by information captured in the header
  • If Index is not equal to WhiteKing, goto IsBlackKing
  • Mark the square at the current Index as White King
  • Increment Index
  • If Index less than 64, goto StartOfLoop else goto EndOfLoop
  • IsBlackKing: If Index is not equal to BlackKing, goto IsPiece
  • Mark the square at the current Index as Black King
  • Increment Index
  • If Index less than 64, goto StartOfLoop else goto EndOfLoop
  • IsPiece: read a single bit
  • If IsPiece is true, goto PieceColor
  • Mark the square at the current Index as Empty
  • Increment Index
  • If Index less than 64, goto StartOfLoop else goto EndOfLoop
  • PieceColor: read a single bit - black (0) or white (1)
  • SegmentContainsPawns: using both UseRowDecoder and PawnsToDecode from header, figure out if segment for Index must decode for the presence of pawns (true) or not (false)
  • If SegmentContainsPawns is false, goto Qbkr
  • IsPawn: read a single bit
  • If IsPawn is false, goto Qbkr
  • Mark the square at the current Index as a PieceColor Pawn
  • Increment Index
  • If Index less than 64, goto StartOfLoop else goto EndOfLoop
  • Qbkr: read two bits
  • Mark the square at the current Index as a PieceColor Rook (00), Bishop (01), Knight (10) or Queen (11)
  • Increment Index
  • If Index less than 64, goto StartOfLoop
  • EndOfLoop: input stream should be completely consumed

I know this would be quite a bit easier if I had some images to assist with seeing some of the data and logic. However, those take lots of extra time to generate. So, if/when I get some extra time (and motivation), I will come back and augment this post. Until then, please enjoy the human readable pseudo-code explanation above. :)

Monday, March 24, 2014

Chess Coding Challenge - Part 3 - Scala solution source code (and interim encoder/decoder implementations)...

Part 1 - How I Leveraged Scala and FP to Find a Solution
Part 2 - Scala infrastructure source code (and two encoder/decoder implementations)
Part 4 - Solution in Human Readable Pseudo-code

Summary:

The Scala code (dropbox public folder share - I STILL SO love this) expanding the set of encoder/decoders to include the solution (and interim steps on the path to the final solution). Included are the Coder implementations; HuffmanSimpleKingsPawnOptimal (solution at 167 bits) and the interim solutions of HuffmanSimple, HuffmanSimpleKings, HuffmanSimpleKingsPawnRows, HuffmanSimpleKingsPawnColumns


Details:
I'm still finding it difficult to make time to progress this project. I am glad to finally have had the time to sit down and get the solution coded up and available. To explain the final solution is going to take more time than I have available this evening. Suffice to say, it is actually a layered set of sub-solutions. And the entire thing is predicated pawns as the primary constraint, and then on specific rules around pawn promotion upon we (Bill and I) optimized. I will return here with one final post detailing the solution in an English (non-code) explanation. I'm committed to making it happen, but...due to circumstances in my personal life, it will likely be another two weeks before that post happens.

This has been very fun, though. I don't have the motivation right now to redo it in Java. That might change in a month or two. If it does, I will write it up and create a post dedicated just to it.

For those who have continued to have the patience to follow this for the last two months, thank you for playing along with me! I cannot wait to see Jim Nelson's and David Stafford's solutions.

Thursday, February 27, 2014

Chess Coding Challenge - Part 2 - Scala infrastructure source code (and two encoder/decoder implementations)...

Part 1 - How I Leveraged Scala and FP to Find a Solution
Part 3 - Scala solution source code (and interim encoder/decoder implementations)

Summary:
The Scala code (dropbox public folder share - I SO love this) defining the infrastructure for experimenting with different encoder/decoder pairs. Included the first two coder implementations; SquareBits4 (constant 256 bits) and PieceExistsBitMapPlusSquareBits4 (max of 192 and reduces by four bits for each piece captured)


Details:
Honestly, I don't have time to go into much detail this evening. So, below is the 1,000 foot view. The root folder contains two folders; DataSets and eclipse_workspace. The DataSets folder contains a text file containing the "test" boards. It ought to be mostly self-explanitory. The eclipse_workspace folder contains the Eclipse .project. I'll reference that a bit later. All of the Scala files all stored in a single package; org.public_domain.chess_coding_challenge:


  1. WorkSheet1.sc: This is a fantastic mechanism for "forming" code dynamically. The code you see there is recompiled and run every time you save the file. All the text in the columns to the right are the output from each of the lines. This way of developing is beyond awesome.
  2. Main.scala: This is the root class that is run. It is the Scala analog to any Java class containing a public static main method.
  3. Definitions.scala: All interface definitions. Everything can be understood in terms of the interfaces.
  4. Implementations.scala: All the factories which instantiate implementations of the interfaces in Definitions.scala.
  5. ConvertBits.scala: A helper class to facilitate twiddling bits.
  6. Coders.scala: This is where the actual encoder/decoder pairs are implemented. And this is where the SquareBits4 and PieceExistsBitMapPlusSquareBits4 implementations are defined.
If you are interested in the minimum fuss way of checking this out, I would highly recommend downloading the free Eclipse + Scala-IDE as it is entirely Scala self-contained; i.e. no need to download various Eclipse and Typesafe installs and work to get them all configured and working. You do need to have (the free) JDK7 installed first as Eclipse and Scala both depend completely upon it. You can also use JDK6, but I don't recommend it both for security as well as performance reasons.

Once you have Eclipse installed and have started it...:


  1. Eclipse will want to be pointed at an Eclipse Workspace - select the eclipse_workspace folder (using "Browse" to navigate to it if needed)
  2. Once Eclipse comes up, it will be blank - using the main menu, select File/Import
  3. On that dialog, open the General item which will show a list of items
  4. Select the item "Existing Projects into Workspace" and click "Next"
  5. Next to the "Select root directory", select "Browse" and immediately hit "Okay"
  6. Click "Finish" and the project will be loaded and ready to explore

Have fun! And please let me know if you end up experimenting with writing some encoder/decoder pairs yourself. I am hoping to find more time this weekend to post at least the initial Huffman encoding that launched me onto this tangent/fixation/whatever on Super Bowl Sunday!