From efa886a32cabc7387d539c728b4d99ebca0ecd45 Mon Sep 17 00:00:00 2001 From: knolax <1339802534.kk@gmail.com> Date: Sat, 10 Mar 2018 00:46:42 -0500 Subject: initial commit --- example/main | Bin 0 -> 12992 bytes example/main.c | 14 +++++++ example/main.o | Bin 0 -> 1952 bytes example/makefile | 6 +++ example/map | 21 +++++++++++ libturtle.a | Bin 0 -> 4594 bytes makefile | 14 +++++++ map | 21 +++++++++++ turtle.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ turtle.h | 27 ++++++++++++++ turtle.o | Bin 0 -> 4392 bytes 11 files changed, 215 insertions(+) create mode 100755 example/main create mode 100644 example/main.c create mode 100644 example/main.o create mode 100644 example/makefile create mode 100644 example/map create mode 100644 libturtle.a create mode 100644 makefile create mode 100644 map create mode 100644 turtle.c create mode 100644 turtle.h create mode 100644 turtle.o diff --git a/example/main b/example/main new file mode 100755 index 0000000..3d9dbad Binary files /dev/null and b/example/main differ diff --git a/example/main.c b/example/main.c new file mode 100644 index 0000000..ac68bd2 --- /dev/null +++ b/example/main.c @@ -0,0 +1,14 @@ +#include "turtle.h" +int main (void) { + if (loadmap("map") != 0 ) { + printf("%d",1); + } + go(3); + turn(LEFT); + go(5); + turn(DOWN); + go(7); + turn(RIGHT); + go(8); + return 0; +} diff --git a/example/main.o b/example/main.o new file mode 100644 index 0000000..6b6b172 Binary files /dev/null and b/example/main.o differ diff --git a/example/makefile b/example/makefile new file mode 100644 index 0000000..8fe13ba --- /dev/null +++ b/example/makefile @@ -0,0 +1,6 @@ +main : main.o + gcc main.o -o main -L../ -lturtle +main.o : main.c + gcc -c main.c +clean : + rm main.o main diff --git a/example/map b/example/map new file mode 100644 index 0000000..129f8d0 --- /dev/null +++ b/example/map @@ -0,0 +1,21 @@ +..,.,.,...,.,.,.,.,, +,.########..,.,..,,. +..#,,,,.,#,...,.,,., +,.#..,$.,#.,.,.,.,.. +..#.,.,.,#,,,...,,., +..####,##########.., +,,,,.#.#.,.....,#,,, +,,,..#,#,.#####.#.,. +,,,,.#.#,.#,,,,,#... +..####.#..#.#####,,, +..#,.,.,.,#,.,..#.,. +..#############.#.,. +,##,.,.,.,.,....#,., +,#....,,,,......#,,, +.#...,,,,.,.,,..#... +,#......,.,.,.,.#.., +,#########.,#####,,. +.....,,,.......,..., +....,..,.,.,.,,.,.,. +....,,,,,...,.,.,.,. + diff --git a/libturtle.a b/libturtle.a new file mode 100644 index 0000000..e720d94 Binary files /dev/null and b/libturtle.a differ diff --git a/makefile b/makefile new file mode 100644 index 0000000..e5f106f --- /dev/null +++ b/makefile @@ -0,0 +1,14 @@ +main : libturtle.a +libturtle.a : turtle.o + ar ru libturtle.a turtle.o + ranlib libturtle.a +turtle.o : turtle.c + gcc -c turtle.c +clean : + rm turtle.o libturtle.a +install: + cp turtle.h /usr/include + cp libturtle.a /lib +uninstall: + rm /usr/include/turtle.h + rm /lib/libturtle.a diff --git a/map b/map new file mode 100644 index 0000000..129f8d0 --- /dev/null +++ b/map @@ -0,0 +1,21 @@ +..,.,.,...,.,.,.,.,, +,.########..,.,..,,. +..#,,,,.,#,...,.,,., +,.#..,$.,#.,.,.,.,.. +..#.,.,.,#,,,...,,., +..####,##########.., +,,,,.#.#.,.....,#,,, +,,,..#,#,.#####.#.,. +,,,,.#.#,.#,,,,,#... +..####.#..#.#####,,, +..#,.,.,.,#,.,..#.,. +..#############.#.,. +,##,.,.,.,.,....#,., +,#....,,,,......#,,, +.#...,,,,.,.,,..#... +,#......,.,.,.,.#.., +,#########.,#####,,. +.....,,,.......,..., +....,..,.,.,.,,.,.,. +....,,,,,...,.,.,.,. + diff --git a/turtle.c b/turtle.c new file mode 100644 index 0000000..8dfeafc --- /dev/null +++ b/turtle.c @@ -0,0 +1,112 @@ +#include "turtle.h" +/*assigns initial values to turtle position*/ +int tx = 0; +int ty = 0; +int td = UP; +/*loads a map from a 20x20 ascii plaintext file excluding newlines*/ +/* returns any io errors*/ +/*returns 0 on sucess */ +int loadmap(char * filename) { + /*file of the map to load*/ + FILE * mapfile = fopen(filename,"r"); + if (mapfile == NULL) { + printf("error loading map file\n"); + return -1; + } + for (int yi = 0; yi < 20; yi++) { + for (int xi = 0; xi < 20; xi++) { + map[xi][yi] = fgetc(mapfile); + if (map[xi][yi] == EOF) { + return EOF; + } + } + /*to account for newlines*/ + fgetc(mapfile); + } + printmap(); + return 0; +} +/*moves the turtle, will keep moving until either blocks runs out or it can't + * moveforwards anymore, only non '#' chars can be moveable*/ +void go(int blocks){ + /*position of blocks to move to*/ + unsigned int mx, my; + for (int i = 0; i < blocks; i++) { + switch (td) { + /*using modulus to wrap them around*/ + case UP: + mx = (tx)%20; + my = (ty - 1)%20; + break; + case DOWN: + mx = (tx)%20; + my = (ty + 1)%20; + break; + case LEFT: + mx = (tx + 1)%20; + my = (ty)%20; + break; + case RIGHT: + mx = (tx - 1)%20; + my = (ty)%20; + break; + } + /*keeps turtle in the boundaries of the map*/ + if(mx < 0) { + mx = 0; + } + if(mx >= 20) { + mx = 19; + } + if(my < 0) { + my = 0; + } + if(my >= 20) { + my = 19; + } + if (map[mx][my] != '#') { + tx = mx; + ty = my; + } + printmap(); + /*victory screen*/ + if (map[tx][ty] == '$') { + printf("YOU WON! CONGRATS!\n"); + } + sleep(1); + } +} +/*sets the irction, no sanity checking*/ +void turn(int direction){ + td = direction; + printmap(); + sleep(1); + return; +} +void printmap() { + /*terminal escape code to move cursor to 0,0*/ + printf("\033[1;1H"); + for (int yi = 0; yi < 20; yi++) { + for (int xi = 0; xi < 20; xi++) { + if ((yi == ty) && (xi == tx)){ + switch (td) { + case UP: + printf("\033[32m^\033[37m"); + break; + case DOWN: + printf("\033[32mV\033[37m"); + break; + case LEFT: + printf("\033[32m>\033[37m"); + break; + case RIGHT: + printf("\033[32m<\033[37m"); + break; + } + } else { + printf("%c",map[xi][yi]); + } + } + printf("\n"); + } +} diff --git a/turtle.h b/turtle.h new file mode 100644 index 0000000..bac9557 --- /dev/null +++ b/turtle.h @@ -0,0 +1,27 @@ +#ifndef TURTLE_H +#define TURTLE_H +/*directions, CCwise like on a coordinate plane */ +#include +#include +#define RIGHT 0 +#define UP 1 +#define LEFT 2 +#define DOWN 3 +/* map everything is held in + */ +char map[20][20]; +/*turtle positions */ +extern int tx; +extern int ty; +/*turtle direction*/ +extern int td; +/* loads map from file, returns any IO errors, assumes map is 20x20 ascii + * chars + * this does the first printing of the screen + */ +extern int loadmap(char * filename); +/*movement of the turtle*/ +extern void go(int blocks); +extern void turn(int direction); +void printmap(); +#endif diff --git a/turtle.o b/turtle.o new file mode 100644 index 0000000..1236ea0 Binary files /dev/null and b/turtle.o differ -- cgit v1.1