fork(1) download
  1. import java.lang.*;
  2.  
  3. class Reversi
  4. {
  5. public static final String LINE_SEPARATOR
  6. = System.getProperty( "line.separator" ) ;
  7.  
  8. public static enum Turn
  9. {
  10. BLACK ,
  11. WHITE
  12. }
  13.  
  14. private long black = 0L ;
  15. private long white = 0L ;
  16. private Turn turn = Turn.BLACK ;
  17.  
  18. public Turn getTurn ()
  19. {
  20. return turn ;
  21. }
  22.  
  23. public void init ()
  24. {
  25. black = 0x0810000000L ;
  26. white = 0x1008000000L ;
  27. turn = Turn.BLACK ;
  28.  
  29. // black = 0xFL << 18 ;
  30. // white = 0xFL << 26 ;
  31. }
  32.  
  33. private long transferR ( long p )
  34. {
  35. return (p >> 1) & 0x7F7F7F7F7F7F7F7FL ;
  36. }
  37.  
  38. private long checkR ( long a , long b , long p )
  39. {
  40. long rev = 0L ;
  41. long mask = transferR( p ) ;
  42. while ( (mask != 0L) && ((b & mask) != 0L) )
  43. {
  44. rev |= mask ;
  45. mask = transferR( mask ) ;
  46. }
  47. if ( (mask & a) == 0L )
  48. {
  49. return 0L ;
  50. }
  51. else
  52. {
  53. return rev ;
  54. }
  55. }
  56.  
  57. private long transferL ( long p )
  58. {
  59. return (p << 1) & 0xFEFEFEFEFEFEFEFEL ;
  60. }
  61.  
  62. private long checkL ( long a , long b , long p )
  63. {
  64. long rev = 0L ;
  65. long mask = transferL( p ) ;
  66. while ( (mask != 0L) && ((b & mask) != 0L) )
  67. {
  68. rev |= mask ;
  69. mask = transferL( mask ) ;
  70. }
  71. if ( (a & mask) == 0L )
  72. {
  73. return 0L ;
  74. }
  75. else
  76. {
  77. return rev ;
  78. }
  79. }
  80.  
  81. private long transferB ( long p )
  82. {
  83. return (p >> 8) & 0x00FFFFFFFFFFFFFFL ;
  84. }
  85.  
  86. private long checkB ( long a , long b , long p )
  87. {
  88. long rev = 0L ;
  89. long mask = transferB( p ) ;
  90. while ( (mask != 0L) && ((b & mask) != 0L) )
  91. {
  92. rev |= mask ;
  93. mask = transferB( mask ) ;
  94. }
  95. if ( (a & mask) == 0L )
  96. {
  97. return 0L ;
  98. }
  99. else
  100. {
  101. return rev ;
  102. }
  103. }
  104.  
  105. private long transferT ( long p )
  106. {
  107. return (p << 8) & 0xFFFFFFFFFFFFFF00L ;
  108. }
  109.  
  110. private long checkT ( long a , long b , long p )
  111. {
  112. long rev = 0L ;
  113. long mask = transferT( p ) ;
  114. while ( (mask != 0L) && ((b & mask) != 0L) )
  115. {
  116. rev |= mask ;
  117. mask = transferT( mask ) ;
  118. }
  119. if ( (a & mask) == 0L )
  120. {
  121. return 0L ;
  122. }
  123. else
  124. {
  125. return rev ;
  126. }
  127. }
  128.  
  129. private long transferRB ( long p )
  130. {
  131. return (p >> 9) & 0x007F7F7F7F7F7F7FL ;
  132. }
  133.  
  134. private long checkRB ( long a , long b , long p )
  135. {
  136. long rev = 0L ;
  137. long mask = transferRB( p ) ;
  138. while ( (mask != 0L) && ((b & mask) != 0L) )
  139. {
  140. rev |= mask ;
  141. mask = transferRB( mask ) ;
  142. }
  143. if ( (a & mask) == 0L )
  144. {
  145. return 0L ;
  146. }
  147. else
  148. {
  149. return rev ;
  150. }
  151. }
  152.  
  153. private long transferLB ( long p )
  154. {
  155. return (p >> 7) & 0x00FEFEFEFEFEFEFEL ;
  156. }
  157.  
  158. private long checkLB ( long a , long b , long p )
  159. {
  160. long rev = 0L ;
  161. long mask = transferLB( p ) ;
  162. while ( (mask != 0L) && ((b & mask) != 0L) )
  163. {
  164. rev |= mask ;
  165. mask = transferLB( mask ) ;
  166. }
  167. if ( (a & mask) == 0L )
  168. {
  169. return 0L ;
  170. }
  171. else
  172. {
  173. return rev ;
  174. }
  175. }
  176.  
  177. private long transferLT ( long p )
  178. {
  179. return (p << 9) & 0xFEFEFEFEFEFEFE00L ;
  180. }
  181.  
  182. private long checkLT ( long a , long b , long p )
  183. {
  184. long rev = 0L ;
  185. long mask = transferLT( p ) ;
  186. while ( (mask != 0L) && ((b & mask) != 0L) )
  187. {
  188. rev |= mask ;
  189. mask = transferLT( mask ) ;
  190. }
  191. if ( (a & mask) == 0L )
  192. {
  193. return 0L ;
  194. }
  195. else
  196. {
  197. return rev ;
  198. }
  199. }
  200.  
  201. private long transferRT ( long p )
  202. {
  203. return (p << 7) & 0x7F7F7F7F7F7F7F00L ;
  204. }
  205.  
  206. private long checkRT ( long a , long b , long p )
  207. {
  208. long rev = 0L ;
  209. long mask = transferRT( p ) ;
  210. while ( (mask != 0L) && ((b & mask) != 0L) )
  211. {
  212. rev |= mask ;
  213. mask = transferRT( mask ) ;
  214. }
  215. if ( (a & mask) == 0L )
  216. {
  217. return 0L ;
  218. }
  219. else
  220. {
  221. return rev ;
  222. }
  223. }
  224.  
  225. private long putable ( int p , Turn t )
  226. {
  227. p &= 0x3F ;
  228.  
  229. long n = 0x1L << p ;
  230.  
  231. if ( ((black | white) & n) != 0L )
  232. {
  233. return 0L ;
  234. }
  235.  
  236. long a = white ;
  237. long b = black ;
  238. if ( t == Turn.BLACK )
  239. {
  240. a = black ;
  241. b = white ;
  242. }
  243. return checkR( a , b , n )
  244. | checkL( a , b , n )
  245. | checkB( a , b , n )
  246. | checkT( a , b , n )
  247. | checkRB( a , b , n )
  248. | checkLT( a , b , n )
  249. | checkRT( a , b , n )
  250. | checkLB( a , b , n ) ;
  251. }
  252.  
  253. private void nextTurn ()
  254. {
  255. if ( turn == Turn.BLACK )
  256. {
  257. turn = Turn.WHITE ;
  258. }
  259. else
  260. {
  261. turn = Turn.BLACK ;
  262. }
  263. }
  264.  
  265. public boolean put ( int x , int y )
  266. {
  267. int p = ((y & 0x7) << 3) | (x & 0x7) ;
  268. long rev = putable( p , turn ) ;
  269. if ( rev == 0L )
  270. {
  271. return false ;
  272. }
  273.  
  274. if ( turn == Turn.BLACK )
  275. {
  276. black ^= (1L << p) | rev ;
  277. white ^= rev ;
  278. }
  279. else
  280. {
  281. black ^= rev ;
  282. white ^= (1L << p) | rev ;
  283. }
  284. nextTurn() ;
  285. return true ;
  286. }
  287.  
  288. public void pass ()
  289. {
  290. nextTurn() ;
  291. }
  292.  
  293. public void print ()
  294. {
  295. StringBuilder str = new StringBuilder( 200 ) ;
  296. str.append( " " ) ;
  297. for ( int i = 0 ; i < 8 ; ++i )
  298. {
  299. str.append( (char)(i + 'A') )
  300. .append( ' ' ) ;
  301. }
  302. for ( int i = 0 ; i < 64 ; ++i )
  303. {
  304. if ( (i & 0x7) == 0 )
  305. {
  306. str.append( LINE_SEPARATOR )
  307. .append( 1 + (i >> 3) )
  308. .append( ' ' ) ;
  309. }
  310. if ( putable( i , turn ) != 0L )
  311. {
  312. str.append( "* " ) ;
  313. }
  314. else if ( ((black >> i) & 1L) > 0L )
  315. {
  316. str.append( "X " ) ;
  317. }
  318. else if ( ((white >> i) & 1L) > 0L )
  319. {
  320. str.append( "O " ) ;
  321. }
  322. else
  323. {
  324. str.append( "- " ) ;
  325. }
  326. }
  327. System.out.println( str ) ;
  328. }
  329.  
  330. public void countUp ()
  331. {
  332. long b = 0L ;
  333. long w = 0L ;
  334. for ( int i = 0 ; i < 64 ; ++i )
  335. {
  336. b += (black >> i) & 1L ;
  337. w += (white >> i) & 1L ;
  338. }
  339. System.out.println( "B" + b + " - W" + w ) ;
  340. }
  341.  
  342. public Reversi() {}
  343.  
  344. public static void main ( String[] args ) throws java.lang.Exception
  345. {
  346. Reversi reversi = new Reversi() ;
  347. reversi.init() ;
  348.  
  349. // reversi.put( 3 , 2 ) ;
  350. // reversi.put( 4 , 2 ) ;
  351. // reversi.pass() ;
  352.  
  353. boolean f = true ;
  354. boolean pass = false ;
  355. int n = 0 ;
  356. int x = 0 , y = 0 ;
  357. while ( f && (n < 100) )
  358. {
  359. ++n ;
  360. f = false ;
  361. for ( y = 0 ; !f && (y < 8) ; ++y )
  362. {
  363. for ( x = 0 ; !f && (x < 8) ; ++x )
  364. {
  365. f = reversi.put( x , y ) ;
  366. }
  367. }
  368. if ( !f )
  369. {
  370. if ( pass )
  371. {
  372. break ;
  373. }
  374. reversi.pass() ;
  375. pass = true ;
  376. f = true ;
  377. }
  378. else
  379. {
  380. if ( pass )
  381. {
  382. pass = false ;
  383. }
  384. }
  385. }
  386. reversi.print() ;
  387. reversi.countUp() ;
  388. }
  389. }
Success #stdin #stdout 0.07s 381248KB
stdin
Standard input is empty
stdout
  A B C D E F G H 
1 O O O O O O O X 
2 O O O O O O X X 
3 O O O O O X O X 
4 O O O O X O O X 
5 O O O O O O O X 
6 O O O X O O O X 
7 O O O O X X O X 
8 X X X X X X O O 
B19 - W45