laforge has submitted this change. (
https://gerrit.osmocom.org/c/osmocom-bb/+/19490 )
Change subject: firmware/app: Initial commit for the game Snake
..
firmware/app: Initial commit for the game Snake
Change-Id: I3c3f012552f2a7474ade911fc071c89e55e19352
---
A src/target/firmware/apps/snake_game/main.c
1 file changed, 521 insertions(+), 0 deletions(-)
Approvals:
laforge: Looks good to me, approved
Jenkins Builder: Verified
diff --git a/src/target/firmware/apps/snake_game/main.c
b/src/target/firmware/apps/snake_game/main.c
new file mode 100644
index 000..44dda9a
--- /dev/null
+++ b/src/target/firmware/apps/snake_game/main.c
@@ -0,0 +1,521 @@
+/* The game Snake as Free Software for Calypso Phone */
+
+/* (C) 2013 by Marcel `sdrfnord` McKinnon
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include
+#include
+
+#include
+#define DEBUG 1
+#define KNRM "\x1B[0m"
+#define UNDERLINE "\x1B[4m"
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+unsigned long next = 1;
+/* This is not a good random number generator ... */
+int rand(void)
+{
+ next = next * 110351 + 12;
+ return (unsigned int)(next & 0x7fff);
+}
+
+void srand(unsigned int seed)
+{
+ next = seed;
+}
+
+#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
+
+/* Time in ms to wait to the next auto move of the snake. */
+#define WAIT_TIME_AUTOMOVE 300
+
+struct position {
+ int x;
+ int y;
+} pos;
+
+uint8_t field[WIDTH][HEIGHT];
+int16_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 DEBUG > 0
+ printf("Next %u\n", next);
+ printf("Rand (%d|%d)\n", x, y);
+#endif
+ 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);
+#endif
+ return;
+ }
+ }
+ }
+}
+
+static void print_snake_str(char *text, int16_t x, int16_t y)
+{
+ x = 6 * x;
+ y = 8 * (y + 1) - 3;
+#if DEBUG > 1
+ printf("Put string %s to (%d|%d)\n", text, x, y);
+#endif
+ fb_gotoxy(x, y);
+ fb_putstr(text, framebuffer->width);
+}
+
+char Move;
+void movepos(char move)
+{
+ Move = move;
+ setItem(pos.x, pos.y, SBODY);
+ switch (move) {
+ case 'h': pos.x--; break;
+ case 'j': pos.y++; break;
+ case 'k': pos.y--; break;
+ case 'l': pos.x++; break;
+ }
+ switch (move) {
+ case 'j':
+ case 'k':
+ if (pos.y == -1)
+ pos.y = HEIGHT - 1;
+ else if (pos.y == HEIGHT)
+ pos.y = 0;
+ increaseBodyAge();
+ break;
+ case 'l':
+ case 'h':
+ 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 movepos_timer_cb(void *p)
+{
+ struct osmo_timer_list *tmr = (struct osmo_timer_list *)p;
+#if DEBUG > 0
+ printf("Auto move %c\n", Move);
+#endif
+ movepos(Move);
+
+ osmo_timer_schedule(tmr,