import java.lang.*;
class Reversi
{
public static final String LINE_SEPARATOR
= System.
getProperty( "line.separator" ) ;
public static enum Turn
{
BLACK ,
WHITE
}
private long black = 0L ;
private long white = 0L ;
private Turn turn = Turn.BLACK ;
public Turn getTurn ()
{
return turn ;
}
public void init ()
{
black = 0x0810000000L ;
white = 0x1008000000L ;
turn = Turn.BLACK ;
// black = 0xFL << 18 ;
// white = 0xFL << 26 ;
}
private long transferR ( long p )
{
return (p >> 1) & 0x7F7F7F7F7F7F7F7FL ;
}
private long checkR ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferR( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferR( mask ) ;
}
if ( (mask & a) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferL ( long p )
{
return (p << 1) & 0xFEFEFEFEFEFEFEFEL ;
}
private long checkL ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferL( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferL( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferB ( long p )
{
return (p >> 8) & 0x00FFFFFFFFFFFFFFL ;
}
private long checkB ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferB( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferB( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferT ( long p )
{
return (p << 8) & 0xFFFFFFFFFFFFFF00L ;
}
private long checkT ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferT( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferT( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferRB ( long p )
{
return (p >> 9) & 0x007F7F7F7F7F7F7FL ;
}
private long checkRB ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferRB( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferRB( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferLB ( long p )
{
return (p >> 7) & 0x00FEFEFEFEFEFEFEL ;
}
private long checkLB ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferLB( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferLB( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferLT ( long p )
{
return (p << 9) & 0xFEFEFEFEFEFEFE00L ;
}
private long checkLT ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferLT( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferLT( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long transferRT ( long p )
{
return (p << 7) & 0x7F7F7F7F7F7F7F00L ;
}
private long checkRT ( long a , long b , long p )
{
long rev = 0L ;
long mask = transferRT( p ) ;
while ( (mask != 0L) && ((b & mask) != 0L) )
{
rev |= mask ;
mask = transferRT( mask ) ;
}
if ( (a & mask) == 0L )
{
return 0L ;
}
else
{
return rev ;
}
}
private long putable ( int p , Turn t )
{
p &= 0x3F ;
long n = 0x1L << p ;
if ( ((black | white) & n) != 0L )
{
return 0L ;
}
long a = white ;
long b = black ;
if ( t == Turn.BLACK )
{
a = black ;
b = white ;
}
return checkR( a , b , n )
| checkL( a , b , n )
| checkB( a , b , n )
| checkT( a , b , n )
| checkRB( a , b , n )
| checkLT( a , b , n )
| checkRT( a , b , n )
| checkLB( a , b , n ) ;
}
private void nextTurn ()
{
if ( turn == Turn.BLACK )
{
turn = Turn.WHITE ;
}
else
{
turn = Turn.BLACK ;
}
}
public boolean put ( int x , int y )
{
int p = ((y & 0x7) << 3) | (x & 0x7) ;
long rev = putable( p , turn ) ;
if ( rev == 0L )
{
return false ;
}
if ( turn == Turn.BLACK )
{
black ^= (1L << p) | rev ;
white ^= rev ;
}
else
{
black ^= rev ;
white ^= (1L << p) | rev ;
}
nextTurn() ;
return true ;
}
public void pass ()
{
nextTurn() ;
}
public void print ()
{
StringBuilder str = new StringBuilder( 200 ) ;
str.append( " " ) ;
for ( int i = 0 ; i < 8 ; ++i )
{
str.append( (char)(i + 'A') )
.append( ' ' ) ;
}
for ( int i = 0 ; i < 64 ; ++i )
{
if ( (i & 0x7) == 0 )
{
str.append( LINE_SEPARATOR )
.append( 1 + (i >> 3) )
.append( ' ' ) ;
}
if ( putable( i , turn ) != 0L )
{
str.append( "* " ) ;
}
else if ( ((black >> i) & 1L) > 0L )
{
str.append( "X " ) ;
}
else if ( ((white >> i) & 1L) > 0L )
{
str.append( "O " ) ;
}
else
{
str.append( "- " ) ;
}
}
}
public void countUp ()
{
long b = 0L ;
long w = 0L ;
for ( int i = 0 ; i < 64 ; ++i )
{
b += (black >> i) & 1L ;
w += (white >> i) & 1L ;
}
System.
out.
println( "B" + b
+ " - W" + w
) ; }
public Reversi() {}
{
Reversi reversi = new Reversi() ;
reversi.init() ;
// reversi.put( 3 , 2 ) ;
// reversi.put( 4 , 2 ) ;
// reversi.pass() ;
boolean f = true ;
boolean pass = false ;
int n = 0 ;
int x = 0 , y = 0 ;
while ( f && (n < 100) )
{
++n ;
f = false ;
for ( y = 0 ; !f && (y < 8) ; ++y )
{
for ( x = 0 ; !f && (x < 8) ; ++x )
{
f = reversi.put( x , y ) ;
}
}
if ( !f )
{
if ( pass )
{
break ;
}
reversi.pass() ;
pass = true ;
f = true ;
}
else
{
if ( pass )
{
pass = false ;
}
}
}
reversi.print() ;
reversi.countUp() ;
}
}