碁盤の表示 + オセロのbit演算
囲碁の場合はどうしたものか・・・・
サンプル様に作成したもの
#include <iostream> using namespace std; typedef unsigned long long uint64; const uint64 OUTSIDE_BOARD = 0x1FE0C183060FF; const wchar_t STONES[] = { L'○', L'●' }; const wchar_t BOARDS[] = { L'┏', L'┯', L'┓', L'┠', L'┼', L'┨', L'┗', L'┷', L'┛' }; const int GETSTONE_LEFT = 0; const int GETSTONE_RIGHT = 1; const int GETSTONE_UP = 2; const int GETSTONE_DOWN = 3; uint64 getStone(uint64 m, uint64 black, uint64 white, int pattern) { uint64 wh = 0; uint64 rev = 0; uint64 e1, e2, e3, e4, e5; // 空白から続く白石列 uint64 b1, b2, b3, b4, b5; // 黒石から続く白石列 if (pattern == 0) { wh = white & 0xF9F3E7CF9F3E; // 移動の為の番人 e1 = (m << 1) & wh; e2 = (e1 << 1) & wh; e3 = (e2 << 1) & wh; e4 = (e3 << 1) & wh; e5 = (e4 << 1) & wh; b1 = (black >> 1) & wh; b2 = (b1 >> 1) & wh; b3 = (b2 >> 1) & wh; b4 = (b3 >> 1) & wh; b5 = (b4 >> 1) & wh; } else if (pattern == 1) { wh = white & 0xF9F3E7CF9F3E; // 移動の為の番人 e1 = (m >> 1) & wh; e2 = (e1 >> 1) & wh; e3 = (e2 >> 1) & wh; e4 = (e3 >> 1) & wh; e5 = (e4 >> 1) & wh; b1 = (black << 1) & wh; b2 = (b1 << 1) & wh; b3 = (b2 << 1) & wh; b4 = (b3 << 1) & wh; b5 = (b4 << 1) & wh; } else if (pattern == 2) { wh = white & 0x3FFFFFFFF80; // 移動の為の番人 e1 = (m << 7) & wh; e2 = (e1 << 7) & wh; e3 = (e2 << 7) & wh; e4 = (e3 << 7) & wh; e5 = (e4 << 7) & wh; b1 = (black >> 7) & wh; b2 = (b1 >> 7) & wh; b3 = (b2 >> 7) & wh; b4 = (b3 >> 7) & wh; b5 = (b4 >> 7) & wh; } else { wh = white & 0x3FFFFFFFF80; // 移動の為の番人 e1 = (m >> 7) & wh; e2 = (e1 >> 7) & wh; e3 = (e2 >> 7) & wh; e4 = (e3 >> 7) & wh; e5 = (e4 >> 7) & wh; b1 = (black << 7) & wh; b2 = (b1 << 7) & wh; b3 = (b2 << 7) & wh; b4 = (b3 << 7) & wh; b5 = (b4 << 7) & wh; } rev |= e5 & b1; rev |= e4 & (b1 |= b2); rev |= e3 & (b1 |= b3); rev |= e2 & (b1 |= b4); rev |= e1 & (b1 |= b5); return rev; } uint64 getStone(uint64 point, uint64 black, uint64 white) { uint64 ret = 0; ret |= getStone(point, black, white, GETSTONE_LEFT); ret |= getStone(point, black, white, GETSTONE_RIGHT); ret |= getStone(point, black, white, GETSTONE_UP); ret |= getStone(point, black, white, GETSTONE_DOWN); return ret; } void viewBoard(uint64 black, uint64 white) { int bmask = 0; int wmask = 0; wchar_t board[25]; white >>= 7; black >>= 7; for (int i = 25; i; i--) { if (i % 5 == 0) { white >>= 1; black >>= 1; } bmask = black & 1; wmask = white & 1; if ((bmask ^ wmask) == 0) { if (i == 1) { board[i-1] = BOARDS[0]; } else if (1 < i && i < 5) { board[i-1] = BOARDS[1]; } else if (i == 5) { board[i-1] = BOARDS[2]; } else if (i == 21) { board[i-1] = BOARDS[6]; } else if (21 < i && i < 25) { board[i-1] = BOARDS[7]; } else if (i == 25) { board[i-1] = BOARDS[8]; } else if (i % 5 == 1) { board[i-1] = BOARDS[3]; } else if (i % 5 == 0) { board[i-1] = BOARDS[5]; } else { board[i-1] = BOARDS[4]; } } else { board[i-1] = STONES[(!wmask) | bmask]; } white >>= 1; black >>= 1; if (i % 5 == 1) { white >>= 1; black >>= 1; } } for (int i = 0; i < 25; i++) { wcout << board[i]; if (i % 5 == 4) { wcout << endl; } } wcout << endl; } int main(void) { locale::global(locale("japanese")); uint64 point = 0x1000000; uint64 black = 0x1F224489F00; uint64 white = 0x1C2870000; uint64 getstone = 0; getstone = getStone(point, black | OUTSIDE_BOARD, white); viewBoard(black, white); viewBoard(black | point, white); viewBoard(0, getstone); cin.get(); return 0; }
実行結果 ●●●●● ●○○○● ●○┼○● ●○○○● ●●●●● ●●●●● ●○○○● ●○●○● ●○○○● ●●●●● ┏┯┯┯┓ ┠┼○┼┨ ┠○┼○┨ ┠┼○┼┨ ┗┷┷┷┛
参考サイト bitboard反転パターン取得