fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #define countof( arr ) ( sizeof arr / sizeof *arr )
  6. #define CSTR
  7. #define _____
  8. #define rigid constexpr
  9.  
  10. using i32 = int;
  11. using u32 = unsigned int;
  12.  
  13. //---------------------------------------------------------------------------------------
  14.  
  15. union EVENT
  16. {
  17. u32 data;
  18. struct
  19. {
  20. u32 se : 3, s : 3, sw : 3;
  21. u32 e : 3, w : 3;
  22. u32 ne : 3, n : 3, nw : 3;
  23.  
  24. u32 y : 3, x : 3, t : 2;
  25. };
  26.  
  27. CSTR EVENT() : data( 0 ) {}
  28. CSTR EVENT( const i32 value ) : data( value ) {}
  29. CSTR EVENT( const u32 stone, const u32 x, const u32 y )
  30. : t( stone ), x( x ), y( y ) { data &= 0xF0000000; }
  31.  
  32. std::string to_string()
  33. {
  34. char temp[12];
  35. sprintf( temp, "%11o", data );
  36. return temp;
  37. }
  38.  
  39. auto score()
  40. {
  41. return (u32)se + s + sw + e + w + ne + n + nw;
  42. }
  43.  
  44. auto is_valid()
  45. {
  46. return ( data & 0xFFFFFF ) > 0;
  47. }
  48. };
  49.  
  50. rigid u32 othello_length = 8;
  51. _____ EVENT history[ othello_length * othello_length - 4 ];
  52.  
  53. //---------------------------------------------------------------------------------------
  54.  
  55. enum class CELL : u32
  56. {
  57. empty,
  58. black,
  59. white,
  60. wall,
  61. };
  62.  
  63. enum class MODE : u32
  64. {
  65. valid,
  66. eval,
  67. action,
  68. };
  69.  
  70. class BOARD
  71. {
  72. private:
  73. CELL board[ othello_length + 2 ][ othello_length + 2 ];
  74.  
  75. void put_( const u32 x, const u32 y, const CELL stone )
  76. {
  77. board[ y + 1 ][ x + 1 ] = stone;
  78. }
  79.  
  80. public:
  81. auto get( const u32 x, const u32 y ) const
  82. {
  83. return board[ y + 1 ][ x + 1 ];
  84. }
  85.  
  86. template< MODE M >
  87. EVENT put( const u32 x, const u32 y, const CELL stone )
  88. {
  89. EVENT event( (u32)stone, x, y );
  90.  
  91. if( board[ y + 1 ][ x + 1 ] != CELL::empty )
  92. return event;
  93.  
  94. const CELL opposite =
  95. static_cast< CELL >( (int)stone ^ (int)CELL::wall );
  96.  
  97. typedef struct DIRECTION
  98. {
  99. i32 x, y;
  100. }
  101. DIRECTION;
  102. rigid DIRECTION directions[]
  103. {
  104. { -1, -1 }, { +0, -1 }, { +1, -1 },
  105. { -1, +0 }, { +1, +0 },
  106. { -1, +1 }, { +0, +1 }, { +1, +1 },
  107. };
  108.  
  109. for( u32 i = 0; i < countof( directions ); ++i )
  110. {
  111. const DIRECTION d = directions[ i ];
  112. u32 count = 0;
  113. while( ++count, get( x + count * d.x, y + count * d.y ) == opposite );
  114. if( get( x + count * d.x, y + count * d.y ) == stone && count > 1 )
  115. {
  116. event.data |= ( count - 1 ) << ( 7 * 3 - i * 3 );
  117. if( M == MODE::valid )
  118. break;
  119. if( M == MODE::action )
  120. for( u32 c = 1; c < count; ++c )
  121. put_( x + c * d.x, y + c * d.y, stone );
  122. }
  123. }
  124.  
  125. if( M == MODE::action && event.is_valid() )
  126. {
  127. put_( x, y, stone );
  128. // add to history
  129. }
  130.  
  131. return event;
  132. }
  133.  
  134. CSTR BOARD()
  135. {
  136. for( u32 x = 0; x < othello_length + 2; ++x )
  137. board[ 0 ][ x ] = CELL::wall;
  138. for( u32 y = 1; y < othello_length + 1; ++y )
  139. {
  140. board[ y ][ 0 ] = CELL::wall;
  141. for( u32 x = 1; x < othello_length + 1; ++x )
  142. board[ y ][ x ] = CELL::empty;
  143. board[ y ][ othello_length + 1 ] = CELL::wall;
  144. }
  145. for( u32 x = 0; x < othello_length + 2; ++x )
  146. board[ othello_length + 1 ][ x ] = CELL::wall;
  147.  
  148. put_( 3, 3, CELL::black );
  149. put_( 4, 4, CELL::black );
  150. put_( 3, 4, CELL::white );
  151. put_( 4, 3, CELL::white );
  152. }
  153.  
  154. friend std::ostream& operator<<( std::ostream& os, const BOARD& b )
  155. {
  156. const std::string tile[] = { "·", "○", "●", "▨" };
  157. for( u32 y = 0; y < othello_length + 2; ++y )
  158. {
  159. for( u32 x = 0; x < othello_length + 2; ++x )
  160. os << tile[ (int)b.board[ y ][ x ] ];
  161. os << std::endl;
  162. }
  163. return os;
  164. }
  165. };
  166. //---------------------------------------------------------------------------------------
  167.  
  168. #include <sstream>
  169.  
  170. int main()
  171. {
  172. std::istringstream cin_( "53 52 42 32 22 54 55 23 62 " );
  173.  
  174. BOARD board;
  175. CELL turn = CELL::black;
  176.  
  177. while( 1 )
  178. {
  179. cout << board << endl;
  180. int x, y, i;
  181. cin_ >> i;
  182. x = i / 10;
  183. y = i % 10;
  184. if( cin_.eof() )
  185. break;
  186. EVENT event = board.put< MODE::action >( x, y, turn );
  187. cout << event.to_string() << " " << event.score() << endl;
  188. turn = static_cast< CELL >( (u32)turn ^ (u32)CELL::wall );
  189. }
  190.  
  191. getchar();
  192. return 0;
  193. }
  194. //---------------------------------------------------------------------------------------
  195.  
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨········▨
▨···○●···▨
▨···●○···▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

14000010000 1
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨········▨
▨···○○○··▨
▨···●○···▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

24000000100 1
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨·····●··▨
▨···○●○··▨
▨···●○···▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

14000000010 1
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨····○●··▨
▨···○○○··▨
▨···●○···▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

22000001010 2
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨···●●●··▨
▨···●○○··▨
▨···●○···▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

12000000001 1
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨··○●●●··▨
▨···○○○··▨
▨···●○···▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

24011010000 3
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨··○●●●··▨
▨···○●●··▨
▨···●●●··▨
▨········▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

14010000000 1
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨··○●●●··▨
▨···○●●··▨
▨···●○●··▨
▨·····○··▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

22000001000 1
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨··○●●●··▨
▨··●●●●··▨
▨···●○●··▨
▨·····○··▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨

16000030100 4
▨▨▨▨▨▨▨▨▨▨
▨········▨
▨········▨
▨··○○○○○·▨
▨··●●●○··▨
▨···●○●··▨
▨·····○··▨
▨········▨
▨········▨
▨▨▨▨▨▨▨▨▨▨