// 例題5 包囲ゲーム // board.c 盤面管理部 #include #include // ヘッダファイル読み込み #include "game.h" #include "board.h" #include "move.h" // 特定箇所の色の読み書き inline void setcolor(Board *bp, Point p, Color c) { bp->b[p.y+1][p.x+1] = c; } inline Color getcolor(const Board *bp, Point p) { return bp->b[p.y+1][p.x+1]; } // 7x7盤面用のテストデータ const Point test_size = {7,7}; const Point test_black_pos = {4,4}; const int test_white_leave = 13; // 盤面初期化 void init_board(Board *bp) { int x,y; Point p; bp->size = test_size; // 盤外の設定 for (y=0;ysize.y+4;y++) for (x=0;xsize.x+4;x++) bp->b[y][x] = c_ob; // 空白の設定 for (p.y=1;p.y<=bp->size.y;p.y++) for (p.x=1;p.x<=bp->size.x;p.x++) setcolor(bp, p, c_empty); // 4隅の盤外領域の設定 for (p.y=1;p.y<=7;p.y++) if (p.y <=2 || p.y >=6) for (p.x=1;p.x<=7;p.x++) if (p.x <=2 || p.x >=6) setcolor(bp, p, c_ob); // 白石の配置 p.y = 1; for (p.x=3;p.x<=5;p.x++) // 1行目 setcolor(bp, p, c_white); p.y = 2; for (p.x=3;p.x<=5;p.x++) // 2行目 setcolor(bp, p, c_white); p.y = 3; for (p.x=1;p.x<=7;p.x++) // 3行目 setcolor(bp, p, c_white); // 黒石の位置 bp->white_leave = test_white_leave; bp->black_pos = test_black_pos; setcolor(bp, bp->black_pos ,c_black); // 手番の設定 bp->turn = black_turn; } // 手番交替 // bp->turn:0が黒番、1が白番 void change_turn(Board *bp) { bp->turn = 1-bp->turn; } // 盤面表示部 // DEBUGが定義されていたら盤の外側2マスも表示 void show_board(const Board *bp) { Point p; int x,y; printf("white=%d\n",bp->white_leave); // 白石残数 // ヘッダ表示は9x9の盤まで対応 // 盤をこれ以上大きくする場合には要変更 #ifndef DEBUG // 通常 char h[] = "123456789"; h[bp->size.x]=0; printf(" %s\n",h); for (p.y=1; p.y<=bp->size.y; p.y++) { printf("%d:", p.y); for (p.x=1; p.x<=bp->size.x; p.x++) { int c = getcolor(bp,p) ; printf("%c", c); } printf("\n"); } #else // デバッグ時 char h[] = "01234567890123"; h[bp->size.x+4]=0; printf(" %s\n",h); // 上下左右2マス分 for (y=0;ysize.y+4;y++) { printf("%d:", y); for (x=0;xsize.x+4;x++) printf("%c", bp->b[y][x]); printf("\n"); } #endif printf("\n"); }