summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknolax <1339802534.kk@gmail.com>2018-03-10 00:46:42 -0500
committerknolax <1339802534.kk@gmail.com>2018-03-10 00:46:42 -0500
commitefa886a32cabc7387d539c728b4d99ebca0ecd45 (patch)
tree10020923df8fc5fb460d4b9eaccefab4c34ea46a
initial commit
-rwxr-xr-xexample/mainbin0 -> 12992 bytes
-rw-r--r--example/main.c14
-rw-r--r--example/main.obin0 -> 1952 bytes
-rw-r--r--example/makefile6
-rw-r--r--example/map21
-rw-r--r--libturtle.abin0 -> 4594 bytes
-rw-r--r--makefile14
-rw-r--r--map21
-rw-r--r--turtle.c112
-rw-r--r--turtle.h27
-rw-r--r--turtle.obin0 -> 4392 bytes
11 files changed, 215 insertions, 0 deletions
diff --git a/example/main b/example/main
new file mode 100755
index 0000000..3d9dbad
--- /dev/null
+++ b/example/main
Binary files 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
--- /dev/null
+++ b/example/main.o
Binary files 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
--- /dev/null
+++ b/libturtle.a
Binary files 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<stdio.h>
+#include<unistd.h>
+#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
--- /dev/null
+++ b/turtle.o
Binary files differ