From 57f4e2caa55a2c43a9074baf388ca69df838dc88 Mon Sep 17 00:00:00 2001
From: Efe ERKEN <efe.erken@etu.unistra.fr>
Date: Tue, 8 Nov 2022 22:57:02 +0100
Subject: [PATCH] :sparkles: NEW: Add move_player func and its tests

Added new function to move the player and some tests to go along.
---
 player.c | 27 +++++++++++++++++++++++++++
 test.c   | 19 +++++++++++++++----
 2 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100644 player.c

diff --git a/player.c b/player.c
new file mode 100644
index 0000000..4745b78
--- /dev/null
+++ b/player.c
@@ -0,0 +1,27 @@
+#include "player.h"
+#include "grid.h"
+
+void move_player(grid* G, enum Direction D) {
+    int target_row = G->player.y, target_column = G->player.x;
+    switch (D) {
+        case LEFT:
+            target_column--;
+            break;
+        case BOTTOM:
+            target_row++;
+            break;
+        case TOP:
+            target_row--;
+            break;
+        case RIGHT:
+            target_column++;
+    }
+    enum CaseType target = G->game_grid[target_row][target_column];
+    if (target == WALL || target == BOX || target == GOAL) {
+        return;
+    }
+    G->game_grid[target_row][target_column] = G->game_grid[G->player.y][G->player.x];
+    G->game_grid[G->player.y][G->player.x] = target;
+    G->player.x = target_column;
+    G->player.y = target_row;
+}
diff --git a/test.c b/test.c
index 27bfd06..99ebc4c 100644
--- a/test.c
+++ b/test.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include "grid.h"
+#include "player.h"
 
 int main01()
 {
@@ -38,15 +39,25 @@ int main02() {
 }
 
 int main03() {
-    // Test de la fonction display_stdio() et display_ncurses() dans grid.c
+    // Test de la fonction display() dans grid.c
     grid* level = init_level("level1.txt");
-    // display_stdio(level);
-    display_ncurses(level);
+    display(level);
     free_grid(level);
     return 0;
 }
 
 int main() {
-
+    // Test de la fonction move_player() dans player.c
+    grid* level = init_level("level1.txt");
+    char quitCar = '\0';
+    while (quitCar != 'q') {
+        printf("Appuyez sur \"q\" pour quitter\n");
+        printf("Appuyez sur \"h, j, k, l\" pour vous déplacer\n\n");
+        display(level);
+        printf("Votre choix : ");
+        scanf(" %c", &quitCar);
+        move_player(level, quitCar);
+    }
+    free_grid(level);
     return 0;
 }
-- 
GitLab