static void search(gamestate_t *gs, alist_t *moveStack) { if (gamestate_pegs_remaining(gs) == 1) { // printf("Found a winning sequence. Final state:\n"); // gamestate_print(gs); alist_add(solutions, alist_new_copy(moveStack)); gamesPlayed++; return; } alist_t *legalMoves = gamestate_legal_moves(gs); if (alist_is_empty(legalMoves)) { gamesPlayed++; return; } for (int i = 0; i < legalMoves->size; i++) { move_t *m = alist_get(legalMoves, i); gamestate_t *nextState = gamestate_apply_move(gs, m); alist_add(moveStack, m); search(nextState, moveStack); alist_remove_last(moveStack); } }
static void alist_build(alist_t * alist, const board_t * board, int to, int colour) { const sq_t * ptr; int from; int piece; int delta; int inc; int sq; int pawn; ASSERT(alist!=NULL); ASSERT(board!=NULL); ASSERT(SQUARE_IS_OK(to)); ASSERT(COLOUR_IS_OK(colour)); // piece attacks for (ptr = &board->piece[colour][0]; (from=*ptr) != SquareNone; ptr++) { piece = board->square[from]; delta = to - from; if (PSEUDO_ATTACK(piece,delta)) { inc = DELTA_INC_ALL(delta); ASSERT(inc!=IncNone); sq = from; do { sq += inc; if (sq == to) { // attack alist_add(alist,from,board); break; } } while (board->square[sq] == Empty); } } // pawn attacks inc = PAWN_MOVE_INC(colour); pawn = PAWN_MAKE(colour); from = to - (inc-1); if (board->square[from] == pawn) alist_add(alist,from,board); from = to - (inc+1); if (board->square[from] == pawn) alist_add(alist,from,board); }
static void alists_hidden(alists_t * alists, const board_t * board, int from, int to) { int inc; int sq, piece; ASSERT(alists!=NULL); ASSERT(board!=NULL); ASSERT(SQUARE_IS_OK(from)); ASSERT(SQUARE_IS_OK(to)); inc = DELTA_INC_LINE(to-from); if (inc != IncNone) { // line sq = from; do sq -= inc; while ((piece=board->square[sq]) == Empty); if (SLIDER_ATTACK(piece,inc)) { ASSERT(piece_is_ok(piece)); ASSERT(PIECE_IS_SLIDER(piece)); alist_add(alists->alist[PIECE_COLOUR(piece)],sq,board); } } }
static void add_mapping(alist_t *ta, tid_t srcid, tid_t tgtid) { debug(3, "Adding mapping %u <%x> => %u <%x>\n", srcid, srcid, tgtid, tgtid); assert(!alist_find(ta, (void *)(uintptr_t)srcid, NULL)); assert(srcid != 0 && tgtid != 0); alist_add(ta, (void *)(uintptr_t)srcid, (void *)(uintptr_t)tgtid); }
alist_t *coord_possible_moves(coord_t *c, int rowCount) { alist_t *moves = alist_new(); // upward (needs at least 2 rows above) if (c->row >= 3) { // up-left if (c->hole >= 3) { alist_add(moves, move_new( c, coord_new(c->row - 1, c->hole - 1), coord_new(c->row - 2, c->hole - 2))); } // up-right if (c->row - c->hole >= 2) { alist_add(moves, move_new( c, coord_new(c->row - 1, c->hole), coord_new(c->row - 2, c->hole))); } } // leftward (needs at least 2 pegs to the left) if (c->hole >= 3) { alist_add(moves, move_new( c, coord_new(c->row, c->hole - 1), coord_new(c->row, c->hole - 2))); } // rightward (needs at least 2 holes to the right) if (c->row - c->hole >= 2) { alist_add(moves, move_new( c, coord_new(c->row, c->hole + 1), coord_new(c->row, c->hole + 2))); } // downward (needs at least 2 rows below) if (rowCount - c->row >= 2) { // down-left (always possible when there are at least 2 rows below) alist_add(moves, move_new( c, coord_new(c->row + 1, c->hole), coord_new(c->row + 2, c->hole))); // down-right (always possible when there are at least 2 rows below) alist_add(moves, move_new( c, coord_new(c->row + 1, c->hole + 1), coord_new(c->row + 2, c->hole + 2))); } return moves; }
void exampleArrayList() { ArrayList* l = newArrayList(4); alist_add(l, "Aye"); alist_add(l, "Bee"); alist_add(l, "Sea"); alist_add(l, "Dee"); alist_add(l, "Eee"); alist_add(l, "Eff"); alist_add(l, "Gee"); alist_add(l, "Ach"); alist_add(l, "Eye"); alist_add(l, "Jay"); alist_add(l, "Kay"); alist_add(l, "Ell"); printf("Size: %d\n", l->size); alist_traverseForward(l, &process); printf("\n"); printf("Remove 'Sea'\n"); alist_remove(l, "Sea", equals); alist_traverseForward(l, &process); printf("\n"); printf("Remove at 0\n"); alist_removeAt(l, 0); alist_traverseForward(l, &process); printf("\n"); printf("Remove at size\n"); alist_removeAt(l, l->size - 1); alist_traverseForward(l, &process); printf("\n"); printf("Insert 'Aye' at 0\n"); alist_insert(l, "Aye", 0); alist_traverseForward(l, &process); printf("\n"); printf("Insert 'Ell' at size\n"); alist_insert(l, "Ell", l->size); alist_traverseForward(l, &process); printf("\n"); printf("Insert 'Sea' at 3\n"); alist_insert(l, "Sea", 2); alist_traverseForward(l, &process); printf("\n"); printf("0: '%s'\n", (char*)alist_get(l, 0)); printf("3: '%s'\n", (char*)alist_get(l, 3)); alist_free(l); }
int stack_add(stack_t* stack, void* e) { return alist_add(stack, e); }