, .
. . , , "" .
:
. , AI , . , , : .
, , . , /// . , , .
bit-board: (, ), 64- unsigned int , , , . , , .
( o, x) . connect-4 6 * 7 , 64 , 32b . popcount(board.o) , os . assert(o & x == 0) , o x .
42b , /. 48- ( ) /. , . .
- board[0][0] && board[0][1] && board[0][2] && board[0][3] ( ) -. , , , , , . || &&, . o x o|x . , .
, , , . , , , , , 2--.
, 64 , 8x8, 7x6. , 64- . , , , , - , , , . . 8 ( ). , 42- . x86, 4 , 8- (AX (low16 RAX) AL AH), ( , , , ) 7 4 - bsr (--) .
struct game_state {
struct board_state {
uint64_t o, x;
} board;
enum winlose { GAME_UNDECIDED=0, GAME_WIN_O, GAME_WIN_X, GAME_DRAW } victory;
};
: . . board[row][col] C, 42 * 2 . , . 64 . ( , o|x. , , . , / , /. .
: . board[i][j] && board[i][j+1] - , , , - . , x86 , . .
x, o, , x, o. , . , x o .
enum boardpos {
POS_EMPTY = 0,
POS_O = 1<<0,
POS_X = 1<<1,
POS_OCCUPIED = 1<<3
};
struct game_state {
struct board_state {
uint8_t pos[6][7];
} board;
enum winlose { GAME_UNDECIDED=0, GAME_WIN_O, GAME_WIN_X, GAME_DRAW } victory;
};
:
, xbbb...ooxbbw. , 43 ( 43, ). - , , , , . 'n'.
, . . , xb2o1xb1w. , . , , x x, capital x xes. , . LZOP LZ4 , , .
:
, . , . 2 , , , , x o . , . 4531,985,219,092 , 0 42 . 2 ^ 42. , 43 , , . IDK, 43- , , (.. , , .)
, , o x w/d/d 12 16 , .
struct __attribute__ ((__packed__)) compact_game_state {
struct __attribute__ ((__packed__)) compact_board_state {
uint64_t o:42, x:42;
} board;
uint8_t victory;
};
struct semi_compact_game_state {
struct __attribute__ ((__packed__)) semi_compact_board_state {
uint64_t o:48, x:48;
} board;
enum winlose victory;
};
g++: . godbolt.
- endian-agnostic code, , . , , , . -endian, , , / . - , , .
, 2- . , , , , . 2 , { POS_EMPTY, POS_O|POS_OCCUPIED, POS_X|POS_OCCUPIED }. - , . 84 , 32 64- . 128- . // 2- .
12- uint32_t uint64_t uint32_t - . uint8_t, . 11 , - . 1/12- , . , , x86, . (64- 64b , 12B-, , , , -, . )
, . , .
, , .
, , . 2- (, . ).
, . ( .)