// 例題5 包囲ゲーム // rule.c ルール判定部 #include #include // ヘッダファイル読み込み #include "game.h" #include "board.h" #include "move.h" #include "rule.h" // 方向検出用ベクトル const Point delta[] = { {0,0}, {0,-1}, {0,1}, {-1,0}, {1,0} }; // 方向検出 Dir getdir(Point from, Point to) { int dx = to.x - from.x; int dy = to.y - from.y; if (dx * dy) return dir_none; for (Dir d=dir_U; d <= dir_R; d++) { int x = dx * delta[d].x; int y = dy * delta[d].y; if (x >= 0 && y >= 0 && x+y > 0) return d; } return dir_none; } // 絶対値関数 inline int abs(int x) { return (x<0)?(-x):(x); } // 飛び越しの判定 bool isjump(Point from, Point to) { int x = abs(from.x - to.x); int y = abs(from.y - to.y); return (x==2 && y==0) || (x==0 && y==2); } // 隣接の判定 bool isnext(Point from, Point to) { int x = abs(from.x - to.x); int y = abs(from.y - to.y); return (x==1 && y==0) || (x==0 && y==1); } // 移動ルール const Rule rules[2][MAXRULE] = { { rule_black_move1, rule_black_move2, 0 }, { rule_white_move1, 0 } }; // ルール検査部 bool check_rules(const Move *mp, const Board *bp) { /* todo: 移動元/移動先点の判定の抽出 */ for (int i=0;iturn][i]; if (r == 0) break; if ((*r)(mp,bp)) return true; } return false; } // 黒の移動ルール1(隣接マス) bool rule_black_move1(const Move *mp, const Board *bp) { return getcolor(bp,mp->from) == c_black && getcolor(bp,mp->to) == c_empty && getdir(mp->from, mp->to) != dir_none && isnext(mp->from, mp->to); } // 中間点を求める補助関数 Point inter(Point from, Point to) { Point p; p.x = (from.x+to.x)/2; p.y = (from.y+to.y)/2; return p; } // 黒の移動ルール2(飛び越し) bool rule_black_move2(const Move *mp, const Board *bp) { return getcolor(bp,mp->from) == c_black && getcolor(bp,mp->to) == c_empty && isjump(mp->from, mp->to) && getcolor(bp, inter(mp->from, mp->to)) == c_white; } // 白の移動ルール1(隣接マス) bool rule_white_move1(const Move *mp, const Board *bp) { return getcolor(bp,mp->from) == c_white && getcolor(bp,mp->to ) == c_empty && getdir(mp->from, mp->to) != dir_none && isnext(mp->from, mp->to); } // 勝利条件 bool check_black_win(const Board *bp) { return bp->white_leave<=LIMIT; } bool check_white_win(const Board *bp) { return false; /* todo:実装 */} // 勝者判定 Winner check_winner(const Board *bp) { return check_black_win(bp)? black_win : check_white_win(bp)? white_win : none ; } // 勝者表示 const char *turnname[] = { "black", "white", 0 }; const char *winname[] = { "none", "black", "white", 0 }; void show_winner(Winner w) { printf("%s win\n", winname[w]); }