Aaro Saila 2 years ago
parent
commit
aa0a2c5fb9
4 changed files with 124 additions and 26 deletions
  1. 16 0
      globals.h
  2. 100 20
      main.c
  3. 6 4
      test_utils/test_utils.c
  4. 2 2
      test_utils/test_utils.h

+ 16 - 0
globals.h

@@ -0,0 +1,16 @@
+#ifndef GLOBALS_H
+#define GLOBALS_H
+
+#define BUF_SIZE 256
+
+// Total board size
+#define BRD_SIZE_X 30
+#define BRD_SIZE_Y 15
+
+// Playable board coord range values
+#define PL_BRD_XS 1
+#define PL_BRD_XE BRD_SIZE_X - 2
+#define PL_BRD_YS 1
+#define PL_BRD_YE BRD_SIZE_Y - 2
+
+#endif // GLOBALS_H

+ 100 - 20
main.c

@@ -2,11 +2,25 @@
 #include <unistd.h>
 #include <termios.h>
 #include <stdlib.h>
-#include "./global_macros.h"
+#include <time.h>
+#include <ctype.h>
+#include "./globals.h"
 
 //TEMP
 #include "./test_utils/test_utils.h"
 
+typedef struct {
+  int x;
+  int y;
+  char visChar;
+} brdSymbol;
+
+int randomInt(const int start, const int end);
+void sleep_ms(const int ms);
+void mvSymbol(brdSymbol* symbol, const char dir);
+
+char board[BRD_SIZE_Y][BRD_SIZE_X];
+
 int main() {
   // Termios setup
   struct termios attr;
@@ -22,18 +36,16 @@ int main() {
   tcsetattr(STDIN_FILENO, 0,&attr);
 
   // Game board setup
-  char board[BRD_SIZE_X][BRD_SIZE_Y];
-
-  for (int i = 0; i < BRD_SIZE_X; i++) {
-    for (int j = 0; j < BRD_SIZE_Y; j++) {
+  for (int i = 0; i < BRD_SIZE_Y; i++) {
+    for (int j = 0; j < BRD_SIZE_X; j++) {
       if (
-          (i > 0 && i < BRD_SIZE_X - 1)
-          && (j == 0 || j == BRD_SIZE_Y - 1)
+          (i > 0 && i < BRD_SIZE_Y - 1)
+          && (j == 0 || j == BRD_SIZE_X - 1)
           ) {
         board[i][j] = '|';
       } else if (
-          (i == 0 || i == BRD_SIZE_X - 1)
-          && (j > 0 && j < BRD_SIZE_Y - 1)
+          (i == 0 || i == BRD_SIZE_Y - 1)
+          && (j > 0 && j < BRD_SIZE_X - 1)
         ) {
         board[i][j] = '-';
       } else {
@@ -43,28 +55,96 @@ int main() {
   }
 
   board[0][0] = '+';
-  board[BRD_SIZE_X - 1][0] = '+';
-  board[0][BRD_SIZE_Y - 1] = '+';
-  board[BRD_SIZE_X - 1][BRD_SIZE_Y - 1] = '+';
+  board[BRD_SIZE_Y - 1][0] = '+';
+  board[0][BRD_SIZE_X - 1] = '+';
+  board[BRD_SIZE_Y - 1][BRD_SIZE_X - 1] = '+';
 
-  printBoard(board);
+  brdSymbol player;
+  player.x = randomInt(PL_BRD_XS, PL_BRD_XE);
+  player.y = randomInt(PL_BRD_YS, PL_BRD_YE);
+  player.visChar = '#';
+
+  board[player.y][player.x] = player.visChar;
+
+  printBoard();
 
-  /*
   while (1) {
     fflush(stdout);
-    char buf[BUF_SIZE] = {0};
-    read(STDIN_FILENO, buf, 2);
+    char buf[1] = {0};
+    read(STDIN_FILENO, buf, 1);
+    char input = buf[0];
 
-    if (buf[0] == 0)
+    if (input == 0 || !isalpha(input))
       continue;
 
-    write(STDOUT_FILENO, buf, 2);
-    //printf("\n");
+    input = tolower(input);
+    
+    switch (input) {
+      case 'w':
+        mvSymbol(&player, 'u');
+        break;
+      case 's':
+        mvSymbol(&player, 'd');
+        break;
+      case 'a':
+        mvSymbol(&player, 'l');
+        break;
+      case 'd':
+        mvSymbol(&player, 'r');
+        break;
+      default:
+        NULL;
+    }
+  
+    system("clear");
+    printBoard();
+    printf("x: %d\n", player.x);
+    printf("y: %d\n", player.y);
+
+    sleep_ms(50);
   }
-  */
 
   // Termios reset
   tcsetattr(STDIN_FILENO, 0,&ATTR_ORIG);
 
   return 0;
 }
+
+int randomInt(const int start, const int end) {
+  /*
+   * Gets random int from range
+   * [start, end[
+  */
+  srand(time(NULL));
+  int result = rand() % end;
+  result = result >= start ? result : result + start;
+  return result;
+}
+
+void sleep_ms(const int ms) {
+  usleep(ms * 1000);
+}
+
+void mvSymbol(brdSymbol* symbol, const char dir) { 
+  board[symbol->y][symbol->x] = ' ';
+
+  switch (dir) {
+    case 'u':
+      symbol->y = symbol->y - 1 < PL_BRD_YS ? PL_BRD_YE : symbol->y - 1;
+      break;
+    case 'd':
+      symbol->y = symbol->y + 1 > PL_BRD_YE ? PL_BRD_YS : symbol->y + 1;
+      break;
+    case 'l':
+      symbol->x = symbol->x - 2 < PL_BRD_XS ? PL_BRD_XE : symbol->x - 2;
+      break;
+    case 'r':
+      symbol->x = symbol->x + 2 > PL_BRD_XE ? PL_BRD_XS : symbol->x + 2;
+      break;
+    default:
+      printf("ERROR in func mvSymbol\n");
+      exit(1);
+  }
+
+  board[symbol->y][symbol->x] = symbol->visChar;
+}

+ 6 - 4
test_utils/test_utils.c

@@ -1,10 +1,12 @@
 #include <stdio.h>
 #include "test_utils.h"
-#include "../global_macros.h"
+#include "../globals.h"
 
-void printBoard(char board[BRD_SIZE_X][BRD_SIZE_Y]) {
-  for (int i = 0; i < BRD_SIZE_X; i++) {
-    for (int j = 0; j < BRD_SIZE_Y; j++) {
+extern char board[BRD_SIZE_Y][BRD_SIZE_X];
+
+void printBoard() {
+  for (int i = 0; i < BRD_SIZE_Y; i++) {
+    for (int j = 0; j < BRD_SIZE_X; j++) {
       printf("%c", board[i][j]);
     }
     printf("\n");

+ 2 - 2
test_utils/test_utils.h

@@ -1,8 +1,8 @@
 #ifndef TEST_UTILS_H
 #define TEST_UTILS_H
 
-#include "../global_macros.h"
+#include "../globals.h"
 
-void printBoard(char board[BRD_SIZE_X][BRD_SIZE_Y]);
+void printBoard();
 
 #endif // TEST_UTILS_H