i just want to store the starting square and the finish square, i dont want to store if it was a capture or a check, only store the moves.
Chess Moves in a byte
- 
				Kaisen2100
 - Enthusiast

 - Posts: 121
 - Joined: Fri May 28, 2004 4:16 pm
 - Location: Madeira Island, Portugal
 - Contact:
 
Chess Moves in a byte
Hello people i am wondering if it is posible to store a chess move with only a byte ... the are 64 squares (you can conut it from 0 to 63), so you can use a byte to store the starting square ... and ... what i can't see is how can include in that byte the finishing square. I think it is posible ... what you think? 
i just want to store the starting square and the finish square, i dont want to store if it was a capture or a check, only store the moves.
			
			
									
									i just want to store the starting square and the finish square, i dont want to store if it was a capture or a check, only store the moves.
PureBasic File Relocator [NEW VERSION 1.1] ==> http://pbfilerelocator.atspace.com/
						- Kaeru Gaman
 - Addict

 - Posts: 4826
 - Joined: Sun Mar 19, 2006 1:57 pm
 - Location: Germany
 
if you want to store the fields, it's clearly NOT possible, 
because you need 6bit to store a number of 0-63,
so you'll need 12bit to store two of them, not 8bit.
you could also store the moving unit.
you have 16 units on each side, so you would only need 4 bit to store the moving unit,
but the queen has more than 16 possibilities where it can move,
and the pawn has only 2 possibilities, so this will make the whole thing unconsistent.
take a word for each move, store the start in the lower and the aim in the higher byte,
and just forget about the lost 4 bit per date.
			
			
									
									because you need 6bit to store a number of 0-63,
so you'll need 12bit to store two of them, not 8bit.
you could also store the moving unit.
you have 16 units on each side, so you would only need 4 bit to store the moving unit,
but the queen has more than 16 possibilities where it can move,
and the pawn has only 2 possibilities, so this will make the whole thing unconsistent.
take a word for each move, store the start in the lower and the aim in the higher byte,
and just forget about the lost 4 bit per date.
oh... and have a nice day.
						- 
				Kaisen2100
 - Enthusiast

 - Posts: 121
 - Joined: Fri May 28, 2004 4:16 pm
 - Location: Madeira Island, Portugal
 - Contact:
 
Yeah ... after thinking a bit more .. i think it is not posible using only a byte ... so i need at least 12 bits ... One and Half byte :p lo0ol ... and because i cant declare a variable like "Dim ChessMove as '1.5 Byte'"  i need to use 2 bytes at least ... a word 
Thanks any way !!!
			
			
									
									Thanks any way !!!
PureBasic File Relocator [NEW VERSION 1.1] ==> http://pbfilerelocator.atspace.com/
						Why should it not be possible handling data using a byte? What about following? A chess board has 64 possible fields... So each field can have its own ID (1-64), and you know everytime which field you are ^^
Hope this hels... good luck... Btw, using LONG is better and faster isntead handling datas using BYTE... but for size optimizing you can use topic example...
			
			
									
									Code: Select all
+----+----+----+----+----+----+----+----+
|  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |
+----+----+----+----+----+----+----+----+
|  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
+----+----+----+----+----+----+----+----+
| 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
+----+----+----+----+----+----+----+----+
| 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
+----+----+----+----+----+----+----+----+
| 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
+----+----+----+----+----+----+----+----+
| 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
+----+----+----+----+----+----+----+----+
| 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 |
+----+----+----+----+----+----+----+----+
| 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 |
+----+----+----+----+----+----+----+----+
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
						Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Another idea is to handle each field as X and Y ... you have for each coorinate X and Y with a max value of 8....
A byte is max 255 or $FF.... so you can split the byte $FF into $0F and $F0 ... in the first $F0 you can store the X pos... in the second $0F you can save the Y pos... so you calculate the pos by X and Y or X * Y
You you understand what i mean 
			
			
									
									A byte is max 255 or $FF.... so you can split the byte $FF into $0F and $F0 ... in the first $F0 you can store the X pos... in the second $0F you can save the Y pos... so you calculate the pos by X and Y or X * Y
Code: Select all
    X1   X2   X3   X4   X5   X6   X7   X8
   +----+----+----+----+----+----+----+----+
Y1 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |
   +----+----+----+----+----+----+----+----+
Y2 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
   +----+----+----+----+----+----+----+----+
Y3 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
   +----+----+----+----+----+----+----+----+
Y4 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
   +----+----+----+----+----+----+----+----+
Y5 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
   +----+----+----+----+----+----+----+----+
Y6 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
   +----+----+----+----+----+----+----+----+
Y7 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 |
   +----+----+----+----+----+----+----+----+
Y8 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 |
   +----+----+----+----+----+----+----+----+
$24 will be field 26.. $24 = split into $2# = XPos2 And $#4 = YPos 4
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
						Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
sorry i oversaw, you want to store start and endpos in one byte... after thinking a bit it could be possible. it was just a fast cross idea...
One idea could be, that each row has a value from 0 to 7... each line you can do shifts or something and calculate a result to store in a byte (for start and endpos...) thats very tricky and you must think very carefull about it... as long as you work with values from  0-7 it should be possible... good luck ^^
			
			
									
									Code: Select all
    X1   X2   X3   X4   X5   X6   X7   X8
   +----+----+----+----+----+----+----+----+
Y1 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 0
   +----+----+----+----+----+----+----+----+ 
Y2 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 1
   +----+----+----+----+----+----+----+----+
Y3 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 2
   +----+----+----+----+----+----+----+----+
Y4 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 3
   +----+----+----+----+----+----+----+----+
Y5 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 4
   +----+----+----+----+----+----+----+----+
Y6 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 5
   +----+----+----+----+----+----+----+----+  
Y7 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 6 
   +----+----+----+----+----+----+----+----+
Y8 |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 | = 7
   +----+----+----+----+----+----+----+----+ 
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
						Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
- 
				dracflamloc
 - Addict

 - Posts: 1648
 - Joined: Mon Sep 20, 2004 3:52 pm
 - Contact:
 
- 
				Kaisen2100
 - Enthusiast

 - Posts: 121
 - Joined: Fri May 28, 2004 4:16 pm
 - Location: Madeira Island, Portugal
 - Contact:
 
I dont understand youva!n wrote:
One idea could be, that each row has a value from 0 to 7... each line you can do shifts or something and calculate a result to store in a byte (for start and endpos...) thats very tricky and you must think very carefull about it... as long as you work with values from 0-7 it should be possible... good luck ^^
PureBasic File Relocator [NEW VERSION 1.1] ==> http://pbfilerelocator.atspace.com/
						- Kaeru Gaman
 - Addict

 - Posts: 4826
 - Joined: Sun Mar 19, 2006 1:57 pm
 - Location: Germany
 
@dracflamloc
good idea.
10bit will do, because the color is determined by the counter of the moves:
white is odd, black is even (or vice versa, if you start with move 0)
but sparing this 2 bits makes it just a bit more complicated.
I think about a struct to handle the nibbles of the first, 12bit example.
this is a three byte struct wich can hold 2 moves = 1 round.
if you access it like a LONG for reading, you can access the four included positions by bitwise-and:
I wrote the Bitmask in binary, to make obvious wich bits are accessed...
so you could set up an structures array, wich holds all moves and makes full use of this method to spare mem.
note that it is less performant than using a 4byte struct with 1 byte per position,
also because of the way memory is accessed, but it only needs 75% memory in total,
so if you want to store millions of moves it's the better choice, but only then.
also be aware that you have to be careful with writing:
if you write a complete long to the structured array, the Lo-byte of the following element is affected.
if you want to set up an exchange-struct to get data safely in and out of the array,
better use an aditional struct that makes sure the following, fourth byte is also correctly allocated.
			
			
									
									good idea.
10bit will do, because the color is determined by the counter of the moves:
white is odd, black is even (or vice versa, if you start with move 0)
but sparing this 2 bits makes it just a bit more complicated.
I think about a struct to handle the nibbles of the first, 12bit example.
Code: Select all
Structure Moves
  Lo.b
  Mid.b
  Hi.b
EndStructureif you access it like a LONG for reading, you can access the four included positions by bitwise-and:
Code: Select all
Structure Moves
  Lo.b
  Mid.b
  Hi.b
EndStructure
Define Move.moves
Define *LongPointer.l
Define ActRound.l
*LongPointer = @Move
ActRound = PeekL(*LongPointer)
WhiteStart  = ( ActRound & %000000000000000000111111 )
WhiteFinish = ( ActRound & %000000000000111111000000 ) >> 6
BlackStart  = ( ActRound & %000000111111000000000000 ) >> 12
BlackFinish = ( ActRound & %111111000000000000000000 ) >> 18
so you could set up an structures array, wich holds all moves and makes full use of this method to spare mem.
note that it is less performant than using a 4byte struct with 1 byte per position,
also because of the way memory is accessed, but it only needs 75% memory in total,
so if you want to store millions of moves it's the better choice, but only then.
also be aware that you have to be careful with writing:
if you write a complete long to the structured array, the Lo-byte of the following element is affected.
if you want to set up an exchange-struct to get data safely in and out of the array,
better use an aditional struct that makes sure the following, fourth byte is also correctly allocated.
oh... and have a nice day.
						Why do you want use 24 bit (3 bytes) to store x,y(start) and x,y(end) or a movement? Why not just only in 16 bit (2 bytes?) or just 8 bit (1 byte to handling x + y pos?)
This is one way i tried to explain in one my postings before... following example is just an idea and has potential to optimize!
			
			
									
									This is one way i tried to explain in one my postings before... following example is just an idea and has potential to optimize!
Code: Select all
StartPosX = Byte1 %00001111   ; 0 to 15
StartPosY = Byte1 %11110000
EndPosX   = Byte2 %00001111
EndPosY   = Byte2 %11110000
or just...
StartPosX = %0000000000001111   ; 0 to 15
StartPosY = %0000000011110000
EndPosX   = %0000111100000000
EndPosY   = %1111000000000000
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
						Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
- 
				dracflamloc
 - Addict

 - Posts: 1648
 - Joined: Mon Sep 20, 2004 3:52 pm
 - Contact:
 
- Kaeru Gaman
 - Addict

 - Posts: 4826
 - Joined: Sun Mar 19, 2006 1:57 pm
 - Location: Germany
 
@Va!n
you don't get it.
you don't spare any room needed for information, if you split a position into coordinates.it's still 24bit for 1 round = 2 moves = 4 positions = 8 coordinates
this is the most compressed way the information can be strored, there is nothing beyond.
except a different concept like dracflamloc suggested,
to store not the starting position but the moving unit,
which will spare 2bit per move = 4bit per round.
but that method makes it even more complicated,
because you'll need a bit more cryptic struct,
4 moves (= 2rounds) equal 5 bytes.
PS:
for purposes like this I miss the Structure-possibilities like in C,
where I could name/access single Bits in a Union...
			
			
									
									you don't get it.
you don't spare any room needed for information, if you split a position into coordinates.
Code: Select all
WhiteStartX  = %000000000000000000000111
WhiteStartY  = %000000000000000000111000
WhiteFinishX = %000000000000000111000000
WhiteFinishY = %000000000000111000000000
BlackStartX  = %000000000111000000000000
BlackStartY  = %000000111000000000000000
BlackFinishX = %000111000000000000000000
BlackFinishY = %111000000000000000000000this is the most compressed way the information can be strored, there is nothing beyond.
except a different concept like dracflamloc suggested,
to store not the starting position but the moving unit,
which will spare 2bit per move = 4bit per round.
but that method makes it even more complicated,
because you'll need a bit more cryptic struct,
4 moves (= 2rounds) equal 5 bytes.
PS:
for purposes like this I miss the Structure-possibilities like in C,
where I could name/access single Bits in a Union...
oh... and have a nice day.
						- 
				dracflamloc
 - Addict

 - Posts: 1648
 - Joined: Mon Sep 20, 2004 3:52 pm
 - Contact:
 
You theoretically could actually encode every single possible move into 8 bits. I'm pretty sure anyway. Though you may have a hard time if you encode moves that a pawn can make if it gets turned into a queen. You'd probably run over 0-255 limit in that case.
Example:
Pawn 1: Move 1 forward, Move 1 up-left, Move 1 up-right
3 moves. Each pawn would have 3 moves so thats a total of 24 moves. 0-23 would be for pawn moves.
Just analyze every move a piece can make. You'll probably be right on the border of the 8-byte limit.
			
			
									
									
						Example:
Pawn 1: Move 1 forward, Move 1 up-left, Move 1 up-right
3 moves. Each pawn would have 3 moves so thats a total of 24 moves. 0-23 would be for pawn moves.
Just analyze every move a piece can make. You'll probably be right on the border of the 8-byte limit.
- Kaeru Gaman
 - Addict

 - Posts: 4826
 - Joined: Sun Mar 19, 2006 1:57 pm
 - Location: Germany
 
I already thought about this "possible moves" thing, 
but I think it will take much more space than assumed.
take in account an empty board with only one unit on it,
to mesure the maxmoves of each unit.
e.g. the higher units can make 7 moves in each possible direction,
wich are 4 for the queen, 2 for bishop and tower.
knight has 8 moves, king 8, 4 each pawn...
....hmmmmm.... maybe it fits in 255
so it just fits into it, but if a pawn turns into a queen the whole system is smashed, 
because that means a former 4-poss-unit turns into a 28-poss-unit....
additionally, there will be a really, really large overhead of calculation using this system.
you can read the value "white queen is making possible move #13"
it will be quite a calculation to determine wich move it actually is...
[edit]
that's right derek. it's more a "theory tea talk" here....
 
like I said above:
> so if you want to store millions of moves it's the better choice, but only then.
			
			
									
									but I think it will take much more space than assumed.
take in account an empty board with only one unit on it,
to mesure the maxmoves of each unit.
e.g. the higher units can make 7 moves in each possible direction,
wich are 4 for the queen, 2 for bishop and tower.
knight has 8 moves, king 8, 4 each pawn...
....hmmmmm.... maybe it fits in 255
Code: Select all
each queen  28 (*2)     =   56
each bishop 14 (*4)     =   56
each tower  14 (*4)     =   56
each knight  8 (*4)     =   32
each king    8 (*2)     =   16
each pawn    4 (*16)    =   32
                        ------
                           248because that means a former 4-poss-unit turns into a 28-poss-unit....
additionally, there will be a really, really large overhead of calculation using this system.
you can read the value "white queen is making possible move #13"
it will be quite a calculation to determine wich move it actually is...
[edit]
that's right derek. it's more a "theory tea talk" here....
like I said above:
> so if you want to store millions of moves it's the better choice, but only then.
oh... and have a nice day.