Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by merlin on 2/26/2024.
- //
- #include <malloc.h>
- #include "move.h"
- void setup_move_stack(move_stack_t *stack)
- {
- for(int i=0;i<MOVE_HISTORY_SIZE;i++)
- {
- stack->move_history[i] = NO_MOVE;
- }
- stack->size = MOVE_HISTORY_SIZE;
- stack->stack_top = -1;
- }
- bool push(enum move_direction item, move_stack_t *stack) {
- if (!is_full(*stack)) {
- ++(stack->stack_top);
- stack->move_history[stack->stack_top] = item;
- return true;
- } else {
- return false;
- }
- }
- bool pop(move_stack_t *stack, enum move_direction *dir_popped) {
- if (!is_empty(*stack)) {
- *dir_popped = stack->move_history[stack->stack_top];
- --(stack->stack_top);
- return true;
- } else {
- return false;
- }
- }
- bool is_full(move_stack_t stack) {
- return (stack.stack_top >= stack.size - 1);
- }
- bool is_empty(move_stack_t stack) {
- return stack.stack_top < 0;
- }
- int height(move_stack_t stack)
- {
- return stack.stack_top+1;
- }
- #define CHANGE_FOR_X 0
- #define CHANGE_FOR_Y 1
- int change_in_position[][2] =
- {{0, 0},
- {0, 1},
- {1, 0},
- {0, -1},
- {-1, 0}};
- void move(player_t *player, enum move_direction direction)
- {
- player->x_position += change_in_position[direction][CHANGE_FOR_X];
- player->y_position += change_in_position[direction][CHANGE_FOR_Y];
- push(direction, &(player->move_stack));
- }
- void undo(player_t *player)
- {
- enum move_direction move_to_undo;
- pop(&(player->move_stack), &move_to_undo);
- player->x_position -= change_in_position[move_to_undo][CHANGE_FOR_X];
- player->y_position -= change_in_position[move_to_undo][CHANGE_FOR_Y];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement