C#-使用哪种数据结构-以180位操作和存储国际象棋位置? [英] C# - which data structure to use - manipulate and store chess position in 180 bits ?

查看:113
本文介绍了C#-使用哪种数据结构-以180位操作和存储国际象棋位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发国际象棋应用程序,需要创建开头的书,一个文件
可能包含数百万个移动和位置.我们有64平方米,其中一些有
被一些碎片占据,而有些则是空的.让我们用以下位表示我们的作品
(用于霍夫曼编码技术).

Hi, I am working on a chess application and need to create opening book, a file which
could contains a millions of moves and positions. we have 64 squres, some of which have
occupied by pieces while some are empty. Let we represent our pieces with following bits
(using to Huffman encoding technique).

            White          Black
-Empty        0             0

-Pawn         110           100

-Rook         11111         11110

-Knight       10110         10101

-Bishop       10100         11100

-Queen        111010        111011

-King         101110        101111





在初始位置,我们有32平方米的不同部分占据着,而32平方米的是
空的.为了提高效率,我必须将位置存储在连续的位中.碎片将是
按顺序排列在位数组中,从a1正方形开始,然后是a2正方形,.. a8,
然后是b1,b2 ... b8正方形,依此类推.

因此对于起始位置,这等于32 x 1 + 16 x 3 + 12 x 5 + 4 x 6 = 164位.

另外,对于不同的游戏情况,还需要再增加16位,例如
是否启用连铸,并在适用的情况下定义包围平方.
所以我需要大约180位(或23字节= 184位)来存储
的单个位置 棋盘.

现在的问题是我必须执行一些按位运算,所以想知道
该方案如何在我的代码中进行操作以及如何存储在文件中.表示
我应该使用哪种数据结构.例如最大long(数据类型)将仅包含4个
字节= 64位.而且我想避免使用字符串.任何人都可以提出任何想法吗
继续进行.

我正在使用C#.Net,框架3.5.





At initial position, we have 32 squres occupied by different pieces and 32 squres are
empty. for efficiency I have to store position in consecutive bits. Pieces bits will be
placed in bit array in sequential order, starting from a1 square, then a2 square,..a8,
then b1, b2...b8 square and so on.

So for a starting position this equates to 32 x 1 + 16 x 3 + 12 x 5 + 4 x 6 = 164 bits.

Also some additional 16 more bits are required for different game situation, like
whether castling is enabled or not, and defined enpassant square if applicable.
so I need about 180 bits (or 23 bytes = 184 bits) to store a single position for
chessboard.

Now the problem is that I have to perform some bitwise operations, and so want to know
the scheme how could I manipulate this in my code and also how to store in file. means
which data strucure should I use. e.g. maximum long (data type) will contains only 4
bytes = 64 bits. and i want to avoid using string. Could any body suggest any idea how
to proceed with this.

I am using C#.Net, Framework 3.5.

推荐答案

您是否考虑过
Did you consider the BitArray[^] class?


尝试以下网站:

http://stackoverflow.com/questions/1831386/程序员-拼图-编码-棋盘状态-贯穿整个游戏 [ http://www.cforcoding.com/2009/12/programming-puzzles- Chess-positions-and.html [ ^ ]
Try this website:

http://stackoverflow.com/questions/1831386/programmer-puzzle-encoding-a-chess-board-state-throughout-a-game[^]

and more specifically this one:

http://www.cforcoding.com/2009/12/programming-puzzles-chess-positions-and.html[^]


这篇关于C#-使用哪种数据结构-以180位操作和存储国际象棋位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆