#include #include #include #include "higgs.h" using namespace std; int getIndex(int x, int y) { return x + y*S; } int getX(int i) { return i % S; } int getY(int i) { return i / S; } void Board::doMove( Move move ) { if( move.ndir != -1 ) { int new_neutron = pushPiece( neutron, move.ndir ); square[new_neutron] = -1; square[neutron] = 0; neutron = new_neutron; } int new_proton = pushPiece( move.p, move.dir ); square[new_proton] = square[move.p]; square[move.p] = 0; proton[square[new_proton] - 1] = new_proton; move_count++; } void Board::undoMove( Move move, int n ) { int new_proton = pushPiece( move.p, move.dir, true ); square[move.p] = square[new_proton]; square[new_proton] = 0; proton[square[move.p]-1] = move.p; if( move.ndir != -1 ) { square[neutron] = 0; square[n] = -1; neutron = n; } move_count--; } vector Board::generateMoves() { vector move_list; Move move; int n; for( move.ndir = 0; move.ndir < 8; move.ndir++ ) { n = pushPiece( neutron, move.ndir ); if( n != neutron ) { square[n] = -1; square[neutron] = 0; for( int i = 0; i < S; i++ ) { move.p = proton[i+S*(move_count % 2)]; for( move.dir = 0; move.dir < 8; move.dir++ ) { if( pushPiece( move.p, move.dir ) != move.p ) move_list.push_back( move ); } } square[neutron] = -1; square[n] = 0; } } return move_list; } int Board::neutronWin() { return ( getY(neutron) == 0 ) - ( getY(neutron) == S-1 ); } int Board::pushPiece( int p, int d, bool e ) { int dx = ( d > 0 && d < 4 ) - ( d > 4 && d < 8 ); int dy = ( d > 2 && d < 6 ) - ( d > 6 || d < 2 ); int x = getX( p ); int y = getY( p ); do { x += dx; y += dy; } while( x >= 0 && x < S && y >= 0 && y < S && square[getIndex(x,y)] == 0 ); if( !e ) { x -= dx; y -= dy; } return getIndex(x,y); } void Board::print() { for( int i = 0; i < SS; i++ ) { if( i % S == 0 ) cerr << "|"; if( square[i] == 0 ) cerr << " ."; else if( square[i] == -1 ) cerr << " N"; else if( square[i] <= S ) cerr << " +"; else cerr << " -"; if( i % S == S-1 ) cerr << " |\n"; } } Board::Board() { for( int x = 0; x < S; x++ ) { square[proton[x] = getIndex(x,0)] = x + 1; square[proton[S+x] = getIndex(x,S-1)] = S + 1 + x; } for( int i = S; i < (S-1)*S; i++ ) square[i] = 0; neutron = getIndex( S / 2, S / 2); square[neutron] = -1; move_count = 0; } Board::Board( Board& board ) { for( int i = 0; i < SS; i++ ) square[i] = board.square[i]; for( int i = 0; i < 2*S; i++ ) proton[i] = board.proton[i]; neutron = board.neutron; move_count = board.move_count; }