Change in osmocom-bb[master]: Wrote my not yet feature complete implementation of Snake.

2020-08-06 Thread laforge
laforge has abandoned this change. ( 
https://gerrit.osmocom.org/c/osmocom-bb/+/19486 )

Change subject: Wrote my not yet feature complete implementation of Snake.
..


Abandoned
--
To view, visit https://gerrit.osmocom.org/c/osmocom-bb/+/19486
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-Change-Id: Ib146f749b60e5851ba0723546697e09a6a05ae85
Gerrit-Change-Number: 19486
Gerrit-PatchSet: 1
Gerrit-Owner: roox 
Gerrit-Reviewer: Jenkins Builder
Gerrit-MessageType: abandon


Change in osmocom-bb[master]: Wrote my not yet feature complete implementation of Snake.

2020-08-01 Thread roox
roox has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmocom-bb/+/19486 )


Change subject: Wrote my not yet feature complete implementation of Snake.
..

Wrote my not yet feature complete implementation of Snake.

Change-Id: Ib146f749b60e5851ba0723546697e09a6a05ae85
---
M src/target/firmware/apps/snake_game/main.c
1 file changed, 267 insertions(+), 37 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/86/19486/1

diff --git a/src/target/firmware/apps/snake_game/main.c 
b/src/target/firmware/apps/snake_game/main.c
index 63dba3f..a4e8296 100644
--- a/src/target/firmware/apps/snake_game/main.c
+++ b/src/target/firmware/apps/snake_game/main.c
@@ -25,6 +25,10 @@
 #include 

 #include 
+#define DEBUG 1
+#define KNRM  "\x1B[0m"
+#define UNDERLINE  "\x1B[4m"
+
 #include 
 #include 
 #include 
@@ -44,6 +48,227 @@
 #include 
 #include 

+#define BLANK 0
+#define HEAD 1
+#define TAIL 2
+#define HEAD_FOOD 3
+#define FOOD 9
+#define SBODY 20
+/* The numbers above 20 are the distance to the head.
+ * 21 is direly behind the head.
+*/
+#define STDLEN 3
+#define HEIGHT 7
+#define WIDTH 16
+struct position {
+   int x;
+   int y;
+} pos;
+
+uint8_t field[WIDTH][HEIGHT];
+uint16_t score = 0, lenght = 0;
+enum errors { ALLRIGHT, SNAKE_COL,  } err;
+
+void printField();
+void setItem(int, int, int);
+void movepos(char);
+void increaseBodyAge();
+void setFood() {
+   int x, y, c;
+   /* for (c = 0; c < 10;c++) { */
+   /*  x = rand() % (WIDTH -1); */
+   /*  y = rand() % (HEIGHT -1); */
+   /*  if (field[x][y] == BLANK) { */
+   /*  field[x][y] = FOOD; */
+   /*  return; */
+   /*  } */
+   /* } */
+   for (x = 0; x < WIDTH; x++) {
+   for (y = 0; y < HEIGHT; y++) {
+   if (field[x][y] == BLANK) {
+   field[x][y] = FOOD;
+   if (DEBUG > 0) printf("Set without rand (%d|%d) 
%d\n", x, y, c);
+   return;
+   }
+   }
+   }
+}
+
+static void print_snake_str(char *text, int16_t x, int16_t y)
+{
+   x = 6 * x;
+   y = 8 * (y+1) -3;
+   printf("Put string %s to (%d|%d)\n", text, x, y);
+   fb_gotoxy(x, y);
+   fb_putstr(text, framebuffer->width);
+}
+void movepos(char dim) {
+   setItem(pos.x, pos.y, SBODY);
+   switch (dim) {
+   case 'Y': pos.y++;break;
+   case 'y': pos.y--;break;
+   case 'X': pos.x++;break;
+   case 'x': pos.x--;break;
+   }
+   switch (dim) {
+   case 'Y': case 'y':
+   if (pos.y == -1) pos.y = HEIGHT -1;
+   else if (pos.y == HEIGHT) pos.y = 0;
+   increaseBodyAge();
+   break;
+   case 'X': case 'x':
+   if (pos.x == -1) pos.x = WIDTH -1;
+   else if (pos.x == WIDTH) pos.x = 0;
+   increaseBodyAge();
+   break;
+   }
+   setItem(pos.x, pos.y, HEAD);
+   printField();
+}
+
+void increaseBodyAge() {
+   int y, x;
+   lenght = SBODY + STDLEN + score;
+   for (x = 0; x < WIDTH; x++) {
+   for (y = 0; y < HEIGHT; y++) {
+   if (field[x][y] >= lenght) field[x][y] = BLANK;
+   else if (field[x][y] >= SBODY) field[x][y]++;
+   }
+   }
+}
+
+void setItem(int x, int y, int item) {
+   if (item == HEAD) {
+   switch (field[x][y]) {
+   case FOOD: score++; setFood(); item = HEAD_FOOD; break;
+   case BLANK: break;
+   default: err = SNAKE_COL;
+   }
+   }
+   field[x][y] = item;
+}
+
+void resetField() {
+   /* system("clear"); */
+   printf("\033[H\033[2J");
+}
+
+void printField() {
+   fb_clear();
+   int x, y;
+   for (y = 0; y < HEIGHT; y++) {
+   for (x = 0; x < WIDTH; x++) {
+   switch (field[x][y]) {
+   case BLANK:break;
+   case HEAD:  print_snake_str("O", x, y);break;
+   case HEAD_FOOD: print_snake_str("P", x, 
y);break;
+   case FOOD: print_snake_str("#", x, y);break;
+   default:
+   if (field[x][y] == lenght) 
print_snake_str(";", x, y);
+   else print_snake_str("o", x, y);
+   }
+   }
+
+   }
+   printf("Score: %d\n", score);
+   fb_gotoxy(0, framebuffer->height+-9);
+   fb_lineto(framebuffer->width-1, framebuffer->cursor_y);
+   fb_gotoxy(0, framebuffer->height-1);
+   char text[16];
+