SNAKE GAME

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
0:24041b847eb5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/map.cpp	Wed Nov 25 04:25:25 2020 +0000
@@ -0,0 +1,314 @@
+// Copyright 2020 Georgia Tech.  All rights reserved.
+// The materials provided by the instructor in this course are for
+// the use of the students currently enrolled in the course.
+// Copyrighted course materials may not be further disseminated.
+// This file must not be made publicly available anywhere.
+
+#include "map.h"
+
+#include "globals.h"
+#include "graphics.h"
+
+/**
+ * The Map structure. This holds a HashTable for all the MapItems, along with
+ * values for the width and height of the Map.
+ */
+struct Map {
+    HashTable* items;
+    int w, h;
+};
+
+#define NUM_MAPS 2
+#define MAP_WIDTH   50
+#define MAP_HEIGHT  50
+static Map maps[NUM_MAPS];
+static int active_map;
+
+static const MapItem CLEAR_SENTINEL = {
+    .type = CLEAR,
+    .draw = draw_nothing
+};
+
+/**
+ * The first step in HashTable access for the map is turning the two-dimensional
+ * key information (x, y) into a one-dimensional unsigned integer.
+ * This function should uniquely map (x,y) onto the space of unsigned integers.
+ */
+static unsigned XY_KEY(int X, int Y) {
+     
+     return X*maps[0].h + Y;
+     
+}
+
+/**
+ * This is the hash function actually passed into createHashTable. It takes an
+ * unsigned key (the output of XY_KEY) and turns it into a hash value (some
+ * small non-negative integer).
+ */
+unsigned map_hash(unsigned key)
+{
+    return key%(NUM_MAPS);
+}
+
+void maps_init()
+{
+    maps[0].items = createHashTable(map_hash, NUM_MAPS);
+    maps[0].w = MAP_WIDTH;
+    maps[0].h = MAP_HEIGHT;
+}
+
+Map* get_active_map()
+{
+    return &maps[active_map];
+}
+
+Map* set_active_map(int m)
+{
+    active_map = m;
+    return &maps[active_map];
+}
+
+void print_map()
+{
+    char lookup[] = {'W', 'D', 'P', 'A', 'K', 'C', 'N',' ','S'};
+    Map* map = get_active_map();
+    for(int j = 0; j < map->h; j++)
+    {
+        for (int i = 0; i < map->w; i++)
+        {
+            MapItem* item = (MapItem*)getItem(map->items, XY_KEY(i, j));
+            if (item) pc.printf("%c", lookup[item->type]);
+            else pc.printf(" ");
+        }
+        pc.printf("\r\n");
+    }
+}
+
+int map_width()
+{
+    return get_active_map()->w;
+}
+
+int map_height()
+{
+    return get_active_map()->h;
+}
+
+int map_area()
+{
+    return map_width() * map_height();
+}
+
+MapItem* get_north(int x, int y)
+{
+    return get_here(x, y-1);
+}
+
+MapItem* get_south(int x, int y)
+{
+    return get_here(x, y+1);
+}
+
+MapItem* get_east(int x, int y)
+{
+    return get_here(x+1, y);
+}
+
+MapItem* get_west(int x, int y)
+{
+    return get_here(x-1, y);
+}
+
+MapItem* get_here(int x, int y)
+{
+    return (MapItem*) getItem(get_active_map()->items, XY_KEY(x, y));
+}
+
+void map_erase(int x, int y)
+{
+    MapItem* item = get_here(x, y);
+    if (item && item->data)
+        free(item->data);
+    deleteItem(get_active_map()->items, XY_KEY(x, y));
+}
+void add_wall(int x, int y, int dir, int len)
+{
+    for(int i = 0; i < len; i++)
+    {
+        MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+        w1->type = WALL;
+        w1->draw = draw_wall;
+        w1->walkable = false;
+        w1->data = NULL;
+        unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i);
+        void* val = insertItem(get_active_map()->items, key, w1);
+        if (val) free(val); // If something is already there, free it
+    }
+}
+
+void add_plant(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = PLANT;
+    w1->draw = draw_plant;
+    w1->walkable = false;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_goodie(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = GOODIE;
+    w1->draw = draw_goodie;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_inclength(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = INCLENGTH;
+    w1->draw = draw_inclength;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void remove_goodie(int x, int y) // 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = PLANT;
+    w1->draw = draw_plant;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_snake_body(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SNAKE_BODY;
+    w1->draw = draw_snake_body;
+    w1->walkable = false;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_snake_head(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SNAKE_HEAD;
+    w1->draw = draw_snake_head;
+    w1->walkable = false;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_snake_tail(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SNAKE_BODY;
+    w1->draw = draw_snake_tail;
+    w1->walkable = false;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_poison(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = POISON;
+    w1->draw = draw_poison;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_speedup(int x, int y) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SPEEDUP;
+    w1->draw = draw_speedup;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+    
+}
+
+void add_slowdown(int x, int y) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = SLOWDOWN;
+    w1->draw = draw_slowdown;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+    
+}
+
+void add_decrease_length(int x, int y) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = DECLENGTH;
+    w1->draw = draw_decrease_length;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+    
+}
+
+void add_random(int x, int y, int d) 
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = RANDOM;
+    w1->draw = draw_random;
+    w1->walkable = true;
+    d = d % 4;
+    w1->data = (int*)(d + 13);
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_moving(int x, int y) {
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = MOVING;
+    w1->draw = draw_moving;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_invinc(int x, int y) {
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = INVINC;
+    w1->draw = draw_invinc;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_nothing(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = CLEAR;
+    w1->draw = draw_nothing;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}