|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.japanesechess.util.BoardPackUtil
public class BoardPackUtil
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
| 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 |
|---|
java.util.ArrayList<java.lang.Integer> tkns
| Constructor Detail |
|---|
public BoardPackUtil()
| Method Detail |
|---|
public void clear()
public void setBoard(int[][] brd)
public boolean addToken(int tokenIn3Bits,
int colorIn1Bit,
int promotedIn1Bit,
int locationIn7Bits)
public byte[] getPackedBoard()
public int[][] unpackBoard(byte[] board)
board -
public static boolean[][] getTokenPositionsOnBoard(byte[] board,
Handicap handicap,
Tokens token,
boolean black)
public static boolean[][] getWhitePositions(byte[] board,
Handicap handicap)
public static boolean[][] getBlackPositions(byte[] board,
Handicap handicap)
public static int rowLetter2Number(java.lang.String r)
public static Tokens stringToToken(java.lang.String t)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||