net.japanesechess.util
Class BoardPackUtil

java.lang.Object
  extended by net.japanesechess.util.BoardPackUtil

public class BoardPackUtil
extends java.lang.Object

I created this from scratch (even the algorithm) in one day. I'm sure there is some way to compact a shogi board more, but it is probably detailed only in Japanese literature, and I don't read Japanese, ... yet. Packs and unpacks shogi boards into 60 bytes total. Details on the format are below. To use, add all the pieces to a new instance or 'clear()'ed instance of BoardPackUtil. Do this using the addToken() method. Finally call the getPackedBoard() method to get a 60 byte array representing the board. To unpack the board call unpackBoard().


 Board representation:
 
 
 *** bits needed=480 (or 60 bytes)
 *** 12 bits per token
 
 
 attribute: krbgslnp color promotion board_or_reserve_location
 bit count: 3        1     1         7
 
 
 ----------------------
 
 
 
 bits reference:
 
 2 4 8 16 32 64 128 256
 
 
 ----------------------
 
 type of token: krbgslnp in 3 bits
 
 king  : 0
 rook  : 1
 bishop: 2
 gold  : 3
 silver: 4
 lance : 5
 knight: 6
 pawn  : 7
 
 
 
 ----------------------
 
 color: in 1 bit
 
 black token: 0
 white token: 1
 
 
 ----------------------
 
 promotion: in 1 bit
 
 unpromoted token: 0
 promoted token  : 1
 
 
 ----------------------
 
 board_or_reserve_location: numbering in 7 bits
 
  9  8  7  6  5  4  3  2  1
 +---------------------------+
 |8  7  6  5  4  3  2  1  0  |a
 |                           |
 |17 16 15 14 13 12 11 10 9  |b
 |                           |
 |   You get the idea ... 18 |c
 |                           |
 |                           |d
 |                           |
 |                           |e
 |                           |
 |                           |f
 |                           |
 |                           |g
 |                           |
 |                           |h
 |                           |
 |                           |i
 +---------------------------+
 White reserve: 81
 black reserve: 82
 
 
 
 

Author:
T. Gene Davis

Field Summary
(package private)  java.util.ArrayList<java.lang.Integer> tkns
           
 
Constructor Summary
BoardPackUtil()
           
 
Method Summary
 boolean addToken(int tokenIn3Bits, int colorIn1Bit, int promotedIn1Bit, int locationIn7Bits)
          In preparation for packing into a byte array, all tokens are added one at a time to this object.
 void clear()
          Prepares the until instance to pack an additional board if it has just finished packing a board into a byte array.
static boolean[][] getBlackPositions(byte[] board, Handicap handicap)
          Returns values true in indices that contain black tokens.
 byte[] getPackedBoard()
          Byte array with each token stored in 12 bit segments.
static boolean[][] getTokenPositionsOnBoard(byte[] board, Handicap handicap, Tokens token, boolean black)
          Returns values true in indices that contain white tokens.
static boolean[][] getWhitePositions(byte[] board, Handicap handicap)
          Returns values true in indices that contain white tokens.
static int rowLetter2Number(java.lang.String r)
           
 void setBoard(int[][] brd)
           
static Tokens stringToToken(java.lang.String t)
           
 int[][] unpackBoard(byte[] board)
          Unpacks a packed board into four index int arrays repressenting: tokens (krbgslnp), color, promotion and board_or_reserve_location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tkns

java.util.ArrayList<java.lang.Integer> tkns
Constructor Detail

BoardPackUtil

public BoardPackUtil()
Method Detail

clear

public void clear()
Prepares the until instance to pack an additional board if it has just finished packing a board into a byte array.


setBoard

public void setBoard(int[][] brd)

addToken

public boolean addToken(int tokenIn3Bits,
                        int colorIn1Bit,
                        int promotedIn1Bit,
                        int locationIn7Bits)
In preparation for packing into a byte array, all tokens are added one at a time to this object. Not all 40 tokens need to be added, if the code is working properly, thus allowing for handicap boards.

Returns:
true if more room

getPackedBoard

public byte[] getPackedBoard()
Byte array with each token stored in 12 bit segments. Real pain to code.

Returns:
a packed shogi board including reserves

unpackBoard

public int[][] unpackBoard(byte[] board)
Unpacks a packed board into four index int arrays repressenting: tokens (krbgslnp), color, promotion and board_or_reserve_location.

Parameters:
board -
Returns:

getTokenPositionsOnBoard

public static boolean[][] getTokenPositionsOnBoard(byte[] board,
                                                   Handicap handicap,
                                                   Tokens token,
                                                   boolean black)
Returns values true in indices that contain white tokens. Note that x is first and from right to left. So, 0,0 would equal 1a in shogi notation.


getWhitePositions

public static boolean[][] getWhitePositions(byte[] board,
                                            Handicap handicap)
Returns values true in indices that contain white tokens. Note that x is first and from right to left. So, 0,0 would equal 1a in shogi notation.


getBlackPositions

public static boolean[][] getBlackPositions(byte[] board,
                                            Handicap handicap)
Returns values true in indices that contain black tokens. Note that x is first and from right to left. So, 0,0 would equal 1a in shogi notation.


rowLetter2Number

public static int rowLetter2Number(java.lang.String r)

stringToToken

public static Tokens stringToToken(java.lang.String t)