From c216e358fa9bd8758a1a1cc97751844b442cb31b Mon Sep 17 00:00:00 2001
From: "leo.muff" <leomuffmail@gmail.com>
Date: Mon, 16 Jan 2023 23:32:49 +0100
Subject: [PATCH] snake moving

---
 snake.c | 85 +++++++++++++++++++++++++++++++++++++--------------------
 snake.h | 24 ++++++++++++----
 2 files changed, 73 insertions(+), 36 deletions(-)

diff --git a/snake.c b/snake.c
index 5f87cfe..5e8cdf4 100644
--- a/snake.c
+++ b/snake.c
@@ -1,6 +1,10 @@
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdint.h>
+#include <assert.h>
+#include <time.h>
 #include "snake.h"
+#include "gfx/gfx.h"
 #include "queue/queue_ptr_int.h"
 
 snake_t create_snake(int size_x,int size_y){
@@ -16,48 +20,55 @@ snake_t create_snake(int size_x,int size_y){
 }
 
 
-void init_cases(int size_x, int size_y, cases_t tab[size_x][size_y]){
-    for (int i=0; i < size_y; i++){
-        for (int j=0; j< size_x; j++){
+board_t create_board(int size_x, int size_y){
+    uint32_t *tab = malloc(size_y*size_x*sizeof(uint32_t));
+    assert(tab != NULL);
+    for (int j=0; j<size_y; j++){
+        for (int i=0; i<size_x; i++){
             if(i == size_y-1 || i == 0 || j== size_x-1 || j == 0 ){
-                tab[i][j] = WALL;
+                tab[size_x*j+i] = WALL;
             }
             else{
-                tab[i][j] = EMPTY;
+                tab[size_x*j+i] = EMPTY;
             }
         }
     }
+        
+    board_t board;
+    board.pixels = tab;
+    board.size_x = size_x;
+    board.size_y = size_y;
+    return board;
 }
 
+void destroy_board(board_t *board){
+    free(board->pixels);
+    board->pixels = NULL;
+}
 
-game_status_t game_over(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]){
-            coord_t coord = queue_tete(snake.body);
-            if(tab[coord.x][coord.y] == SNAKE || tab[coord.x][coord.y] == WALL){
+game_status_t game_over(snake_t snake, board_t board){
+            coord_t coord = queue_debut(snake.body);
+            if(board.pixels[board.size_x*coord.y + coord.x] == SNAKE || board.pixels[board.size_x*coord.y + coord.x] == WALL){
                 return LOST;
             }
-            else {
-                for (int i=0; i < size_x; i++){
-                    for (int j=0; j< size_y; j++){
-                        if (tab[coord.x][coord.y] == EMPTY || tab[coord.x][coord.y] == FOOD){
-                            return NOT_OVER;
-                        }
-                    }
-                }
+            else if(queue_count(snake.body) == (board.size_x-2)*(board.size_y-2) ){
+                return WON;
             }
-            return WON;
+            return NOT_OVER;
 }
 
-void set_snake(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]){
+void set_snake(snake_t snake, board_t *board){
     while (snake.body.tete->next != NULL){
-        tab[snake.body.tete->coordinates.y][snake.body.tete->coordinates.x] = SNAKE;
+        board->pixels[board->size_x*snake.body.tete->coordinates.y+snake.body.tete->coordinates.x] = SNAKE;
         snake.body.tete = snake.body.tete->next;
     }
-    tab[snake.body.tete->coordinates.y][snake.body.tete->coordinates.x] = SNAKE;
+    board->pixels[board->size_x*snake.body.tete->coordinates.y+snake.body.tete->coordinates.x] = SNAKE;
 }
 
-game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][size_y]){
+game_status_t move(snake_t *snake, board_t *board){
     coord_t new_case;
     coord_t snake_tail;
+    game_status_t status;
     new_case = snake->body.debut->coordinates;
     switch(snake->dir){
         case W:
@@ -72,17 +83,31 @@ game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][s
         case D:
            new_case.x += 1;
     }
-    if(tab[new_case.y][new_case.x] == SNAKE || tab[new_case.y][new_case.x] == WALL ){
-        return LOST;
+    queue_inserer(&snake->body, new_case);
+    status = game_over(*snake, *board);
+    if(status == NOT_OVER){
+        if (board->pixels[board->size_x*new_case.y+new_case.x] != FOOD){
+            snake_tail =  queue_extraire(&snake->body);
+            board->pixels[board->size_x*snake_tail.y+snake_tail.x] = EMPTY;
+        }
+        set_snake(*snake, board);
     }
-    queue_inserer(&snake->body, new_case); //bouger le reste du serpent 
-    if (tab[new_case.y][new_case.x] != FOOD){
-        snake_tail =  queue_extraire(&snake->body);
-        tab[snake_tail.y][snake_tail.x] = EMPTY;
+    return status;
+}
+
+void gen_food(int max, board_t *board){
+    srand(time(NULL));
+    int nb_foob = rand() % max;
+    for(int i=0; i<nb_foob; i++){
+        int x = rand() % board->size_x;
+        int y = rand() % board->size_y;
+        if (board->pixels[board->size_x*y+x] != FOOD){
+            board->pixels[board->size_x*y+x] = FOOD;
+        }
+        else {
+            i--;
+        }
     }
-    
-    set_snake(*snake, size_x, size_y, tab);
-    return NOT_OVER;
 }
 
 /* 
diff --git a/snake.h b/snake.h
index 3f1981c..e65a230 100644
--- a/snake.h
+++ b/snake.h
@@ -1,10 +1,12 @@
 #ifndef _SNAKE_H_
 #define _SNAKE_H_
 #include <stdio.h>
+#include <stdint.h>
 #include "queue/queue_ptr_int.h"
+#include "gfx/gfx.h"
 
 typedef enum _cases {
-    EMPTY, SNAKE, FOOD, WALL 
+    EMPTY = COLOR_BLACK, SNAKE = COLOR_BLUE, FOOD = COLOR_GREEN, WALL = COLOR_WHITE 
 } cases_t;
 
 
@@ -13,7 +15,7 @@ typedef enum _game_status {
 } game_status_t;
 
 typedef enum _direction {
-    W,A,S,D
+    W = SDLK_w, A = SDLK_a, S = SDLK_s, D = SDLK_d
 } direction_t;
 
 typedef struct _snake{
@@ -21,14 +23,24 @@ typedef struct _snake{
     direction_t dir;
 }snake_t;
 
+typedef struct _board{
+    int size_x, size_y;
+    uint32_t *pixels;
+} board_t;
+
 snake_t create_snake(int size_x, int size_y);
 
-void init_cases(int size_x, int size_y, cases_t tab[size_x][size_y]);
+board_t create_board(int size_x, int size_y);
+
+void destroy_board(board_t *board);
+
+game_status_t game_over(snake_t snake, board_t board);
+
+void set_snake(snake_t snake, board_t *board);
 
-game_status_t game_over(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]);
+game_status_t move(snake_t *snake, board_t *board);
 
-void set_snake(snake_t snake, int size_x, int size_y, cases_t tab[size_x][size_y]);
+void gen_food(int max, board_t *board);
 
-game_status_t move(snake_t *snake, int size_x, int size_y, cases_t tab[size_x][size_y]);
 
 #endif
\ No newline at end of file
-- 
GitLab