Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <sstream>
- #include <string>
- class CPosition {
- public:
- explicit CPosition( const std::string& source );
- // Передвижение пустышки вверх.
- CPosition Up() const;
- private:
- unsigned long long data; // 16 ячеек по 4 бита каждая.
- int nullPlace;
- void setAt( int place, unsigned char value );
- unsigned char getAt( int place ) const;
- };
- const unsigned long long Masks[] = {
- 0x000000000000000F,
- 0x00000000000000F0,
- 0x0000000000000F00,
- 0x000000000000F000,
- 0x00000000000F0000,
- 0x0000000000F00000,
- 0x000000000F000000,
- 0x00000000F0000000,
- 0x0000000F00000000,
- 0x000000F000000000,
- 0x00000F0000000000,
- 0x0000F00000000000,
- 0x000F000000000000,
- 0x00F0000000000000,
- 0x0F00000000000000,
- 0xF000000000000000,
- };
- const unsigned long long AntiMasks[] = {
- 0xFFFFFFFFFFFFFFF0,
- 0xFFFFFFFFFFFFFF0F,
- 0xFFFFFFFFFFFFF0FF,
- 0xFFFFFFFFFFFF0FFF,
- 0xFFFFFFFFFFF0FFFF,
- 0xFFFFFFFFFF0FFFFF,
- 0xFFFFFFFFF0FFFFFF,
- 0xFFFFFFFF0FFFFFFF,
- 0xFFFFFFF0FFFFFFFF,
- 0xFFFFFF0FFFFFFFFF,
- 0xFFFFF0FFFFFFFFFF,
- 0xFFFF0FFFFFFFFFFF,
- 0xFFF0FFFFFFFFFFFF,
- 0xFF0FFFFFFFFFFFFF,
- 0xF0FFFFFFFFFFFFFF,
- 0x0FFFFFFFFFFFFFFF
- };
- CPosition::CPosition( const std::string& source ) :
- data( 0 ),
- nullPlace( 0 )
- {
- std::istringstream stringStream( source );
- for( char i = 0; i < 16; ++i ) {
- unsigned short value = 0;
- stringStream >> value;
- setAt( i, static_cast<unsigned char>( value ) );
- if( value == 0 ) {
- nullPlace = i;
- }
- }
- }
- // Установка значения в некоторую позицию.
- void CPosition::setAt( int place, unsigned char value )
- {
- data = ( data & AntiMasks[place] ) | ( static_cast<long long>( value ) << ( place << 2 ) );
- }
- // Получение того, что лежит в некоторой позиции.
- unsigned char CPosition::getAt( int place ) const
- {
- return static_cast<unsigned char>( ( data & Masks[place] ) >> ( place << 2 ) );
- }
- CPosition CPosition::Up() const
- {
- assert( nullPlace >= 4 );
- CPosition result( *this );
- // Ставим пустышку выше.
- result.setAt( nullPlace - 4, 0 );
- // Ставим то, что было выше, на то место, где была пустышка.
- result.setAt( nullPlace, getAt( nullPlace - 4 ) );
- result.nullPlace -= 4;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement