int main(void){ pos* table = init_table(); build_positions(table, 10); move_onto(table, 9,1); move_over(table, 8,1); move_over(table, 7,1); move_over(table, 6,1); pile_over(table, 8,6); pile_over(table, 8,5); move_over(table, 2,1); move_over(table, 4,9); display_all(table); return 0; }
void test_pile_over(){ /* Set up*/ world_t* world = world_create(7); pile_over(world, 1, 2); pile_over(world, 3, 4); pile_over(world, 5, 6); /* Test case 1 - move all left stack to right stack*/ pile_over(world, 2, 4); int i = 0; for(i = 1; i <= 4; i++) { assert(block_get_stack(block_get(world, i)) == 4); } assert(world->position_blocks_bottom[2] == NULL && world->position_blocks_top[2] == NULL); /* Test case 2 - move part of left stack to right stack. Choose middle elem in right stack*/ /* Elements should be on top of 6*/ pile_over(world, 3, 5); int stack[] = {1, 2, 3, 5, 6}; for(i = 0; i < 5; i++) { assert(block_get_stack(block_get(world, stack[i])) == 6); } assert(world->position_blocks_bottom[4]->value == 4 && world->position_blocks_top[4]->value == 4); assert(world->position_blocks_bottom[6]->value == 6 && world->position_blocks_top[6]->value == 1); /* Test case 3 - move block that is already on top of another*/ pile_over(world, 3, 5); for(i = 0; i < 5; i++) { assert(block_get_stack(block_get(world, stack[i])) == 6); } assert(world->position_blocks_bottom[4]->value == 4 && world->position_blocks_top[4]->value == 4); assert(world->position_blocks_bottom[6]->value == 6 && world->position_blocks_top[6]->value == 1); /* Test case 4 - move stack to middle of stack*/ pile_over(world, 0, 4); pile_over(world, 4, 2); for (i = 0; i < 7; i++) { assert(block_get_stack(block_get(world, i)) == 6); } /* Tear down */ world_delete(&world); }
int main() { int i, a, b, blk_count; char *w, what, how; blk_count = atoi(getword()); for (i = 0; i < MAXN; i++) { tab[i].count = (i < blk_count) ? 1 : 0; tab[i].b[0] = i; } while ((w = getword()) != NULL) { if (strcmp(w, "move") == 0) what = 'm'; else if (strcmp(w, "pile") == 0) what = 'p'; else break; a = atoi(getword()); how = (strcmp(getword(), "onto") == 0) ? 'n' : 'v'; b = atoi(getword()); if (a < 0 || a >= blk_count || b < 0 || b >= blk_count) continue; if (where(a) == where(b)) continue; if (what == 'm') /* move */ free_top_of(a); if (how == 'n') /* onto */ free_top_of(b); pile_over(a, b); } for (i = 0; i < blk_count; i++) { printf("%d:", i); for (a = 0; a < tab[i].count; a++) printf(" %d", tab[i].b[a]); printf("\n"); } return 0; }
void run_cmd(Stack stacks[N], int locations[N], char cmd[M]) { char action[M], type[M]; int a, b; sscanf (cmd, "%s %d %s %d\n", action, &a, type, &b); if (a == b || locations[a] == locations[b]) return; if (!strcmp (action, "move") && !strcmp (type, "onto")) move_onto (stacks, locations, a, b); else if (!strcmp (action, "move") && !strcmp (type, "over")) move_over (stacks, locations, a, b); else if (!strcmp (action, "pile") && !strcmp (type, "onto")) pile_onto (stacks, locations, a, b); else if (!strcmp (action, "pile") && !strcmp (type, "over")) pile_over (stacks, locations, a, b); else return; }