Go to the first, previous, next, last section, table of contents.


Karel Programmer's Guide

The Karel Simulator Architecture

Building applications

Extending Karel

API Reference

Error API

You can override the default error handling routines by defining replacements and including them in the link command prior to libkarel. Each error handler is defined separately, so it is possible to redefine one, and use the default behavior of the other two.

Error Handler: void ktr_err_nomem (size_t size)
Library function that is invoked when the Karel engine cannot allocate memory.

Error Handler: void ktr_err_fatal (char * format, ...)
Library function that is invoked when the Karel engine has a fatal error. The default implementation prints the arguments ala fprintf(stderr, format, ...) and exits the program.

Error Handler: void ktr_err_parse (char * format, ...)
Library function that is invoked when the Karel parser has an error. The default implementation prints the arguments ala fprintf(stderr, format, ...) and exits the program.

Memory API

You can override the default memory management routines by defining replacements and including them in the link command prior to libkarel. If you do decide to override the memory management routines, they must all be redefined. It is not possible to redefine some memory management routines and not others.

The default implementation uses the standard C library calls calloc, malloc, free, and realloc.

Memory Management: void * ktr_calloc (size_t nmemb, size_t size)
Allocates an array of nmemb elements, each of size bytes. Returns a pointer to the allocated memory, or NULL if the allocation failed.

Memory Management: void * ktr_malloc (size_t size)
Allocates size bytes. Returns a pointer to the allocated memory, or NULL if the allocaiton failed.

Memory Management: void ktr_free (void *ptr);
Frees memory previously allocated by ktr_calloc, ktr_malloc, or ktr_realloc.

Memory Management: void * ktr_realloc (void *ptr, size_t size);
Attempts to resize the memory block pointed to by ptr. If ptr is NULL, the call is equivalent to malloc(size). If size is equal to zero, the call is equivalent to free(ptr). Unless ptr is NULL, it must have been allocated by a previous call to ktr_calloc or ktr_malloc. Returns a pointer to the new block of memory. If the allocation fails, NULL is returned and the original block of memory is untouched.

Robot API

Robot: ktr_robot_t
The robot data type.

Robot: ktr_robot_t * ktr_robot_create (ktr_world_t *world, int street, int avenue, ktr_direction_t dir, int n_beepers)
Creates a new robot that lives in world. The starting position is given as (street, avenue), facing direction dir. The robot starts with n_beepers in his beeper bag.

Robot: void ktr_robot_set_move_callback (ktr_robot_t *r, ktr_robot_move_callback_t cb)
Establishes the function to invoke whenever karel moves.

Robot: void ktr_robot_set_turn_callback (ktr_robot_t *r, ktr_robot_turn_callback_t cb)
Establishes the function to invoke whenever karel turns.

Robot: void ktr_robot_get_pos (ktr_robot_t *r, int *street, int *avenue);
Sets street and avenue to the coordinates of the given robot's (r) current intersection.

Robot: char * ktr_robot_dir_to_string (ktr_direction_t dir);
Converts the given direction (dir) to a string for printing the robot's current direction.

World API

World: ktr_world_t
The world data type.

World: ktr_world_t * ktr_world_create (int n_streets, int n_avenues)
Creates a new world of size n_streets by n_avenues.

World: ktr_world_t * ktr_world_read (FILE *in_file)
Reads a world description from the open file in_file. The file should be opened with a call to fopen(). Returns a newly allocated world matching the description found in the file, or NULL if there is an error reading the file.

World: void ktr_world_add_ew_wall (ktr_world_t *w, int north_of_street, int at_avenue, int length_to_east)
Adds an east-west wall immediately north of north_of_street, starting at at_avenue, and extending length_to_east blocks to the east.

World: void ktr_world_add_ns_wall (ktr_world_t *w, int at_street, int east_of_avenue, int length_to_north)
Adds a north-south wall immediately east of east_of_avenue, starting at at_street, and extending length_to_north blocks to the north.

World: int ktr_world_check_ew_wall (ktr_world_t *w, int north_of_street, int at_avenue)
Returns 1 if there is an east-west wall immediately north of street north_of_street at avenue at_avenue. Returns 0 otherwise.

World: int ktr_world_check_ns_wall (ktr_world_t *w, int at_street, int east_of_avenue)
Returns 1 if there is an north-south wall immediately east of avenue east_of_avenue at street at_street. Returns 0 otherwise.

World: int ktr_world_check_beeper (ktr_world_t *w, int street, int avenue)
Returns 1 if there is a beeper at intersection (street,avenue). Returns 0 otherwise.

World: int ktr_world_pick_beeper (ktr_world_t *w, int street, int avenue)
Decrements the number of beepers at intersection (street,avenue). Returns 0 if a beeper was successfully picked. Returns -1 if there were no beepers to pick up.

World: int ktr_world_put_beeper (ktr_world_t *w, int street, int avenue)
Increments the number of beepers at intersection (street,avenue). Returns 0.

Engine API

Engine: ktr_engine_t * ktr_load_program (FILE *in_file);
Loads the karel program in the open file in_file. The file should be opened with a call to fopen(). Returns the resulting engine. Errors are presently undetected. This obviously needs work!


Go to the first, previous, next, last section, table of contents.