瀏覽代碼

added more documentation

Aaro Saila 7 月之前
父節點
當前提交
da36cbe79c
共有 3 個文件被更改,包括 71 次插入 和 11 次删除
  1. 26 5
      README.md
  2. 12 1
      compile.sh
  3. 33 5
      src/args.c

+ 26 - 5
README.md

@@ -1,15 +1,36 @@
 # Sanke
 A simple snake game in the terminal. No third party libraries needed.
 
-## Demo
-Click image for video
+# Compiling
+Run:
+```sh
+compile.sh release
+```
+to compile an optimized build. The script creates a directory called 'target' that contains the executable.
 
-[![Sanke demo](https://img.youtube.com/vi/_coOVAW9qa4/0.jpg)](https://www.youtube.com/watch?v=_coOVAW9qa4)
+See:
+```sh
+compile.sh help
+```
+for more.
 
-## How it works
+# Running
+Run:
+```sh
+sanke 
+```
+to play with defaults settings.
+
+See:
+```sh
+sanke --help
+```
+for setting game options.
+
+# How it works
 The program makes the terminal use non-canonical input and output by using termios. Then it updates and prints a 2D character array containing the board borders, snake, and food. Before terminating, the program restores canonical mode.
 
-## Info
+# Info
 Only tested with the combination of
 - GCC
 - Linux

+ 12 - 1
compile.sh

@@ -6,6 +6,18 @@ if [ -v 1 ]; then
   arg=$1
 fi
 
+if [ $arg = "help" ]; then
+    echo "USAGE: compile.sh <option>"
+    echo ""
+    echo "Compiles debug build when called without an option."
+    echo "Options:"
+    echo "  help   : Show this help text."
+    echo "  clean  : Clean target directory."
+    echo "  release: Build with optimizations."
+    echo "  run    : Build debug build and run."
+    exit
+fi
+
 if [ $arg = "clean" ]; then
   rm -rf ./target/*
   exit
@@ -37,6 +49,5 @@ if [ $? -gt 0 ]; then
 fi
 
 if [ $arg = "run" ]; then
-  echo 
   ./target/sanke
 fi

+ 33 - 5
src/args.c

@@ -4,13 +4,31 @@
 
 #include "args.h"
 
-static void handle_version() {
+static void check_bounds(const char* arg_name, const int i, const int argc) {
+    if (i + 1 >= argc) {
+        printf("%s needs an argument. See: sanke --help\n", arg_name);
+        exit(0);
+    }
+}
+
+static void show_version() {
   printf("Sanke version %s\n", VERSION);
   exit(0);
 }
 
+static void show_help() {
+  printf(
+          "USAGE: sanke <options>\n\n"
+          "Options:\n"
+          "  --width,  -w: Set the width (10 - 50) of the board. Default 15.\n"
+          "  --height, -h: Set the height (10 - 50) of the board. Default 15.\n"
+          "  --speed,  -s: Set the speed (1 - 1000) of the game in squares per second. Default 7.\n"
+          "  --help      : Display this help text.\n"
+          );
+  exit(0);
+}
+
 static void set_width(Arguments* args, char* width_str) {
-  printf("width_str: %s\n", width_str);
   int width = atoi(width_str);
   if (width < 10) {
     printf("Width must be between 10 and 50. Was %d.\n", width);
@@ -23,7 +41,7 @@ static void set_width(Arguments* args, char* width_str) {
 static void set_height(Arguments* args, char* height_str) {
   int height = atoi(height_str);
   if (height < 10) {
-    printf("Width must be between 10 and 50. Was %d.\n", height);
+    printf("Width must be between 10 and 50. Was %s.\n", height_str);
     exit(0);
   }
 
@@ -44,7 +62,7 @@ Arguments cmd_args(int argc, char** argv) {
   Arguments args = {
     .width = 15,
     .height = 15,
-    .sleep_ms = 150
+    .sleep_ms = 150,
   };
 
   for (int i = 1; i < argc; i++) {
@@ -53,13 +71,20 @@ Arguments cmd_args(int argc, char** argv) {
         || strcmp(argv[i], "-v") == 0
         )
     {
-      handle_version();
+      show_version();
+
+    } else if (
+        strcmp(argv[i], "--help") == 0
+        )
+    {
+      show_help();
 
     } else if (
         strcmp(argv[i], "--width") == 0
         || strcmp(argv[i], "-w") == 0
         )
     {
+      check_bounds("width", i, argc);
       set_width(&args, argv[i + 1]);
 
     } else if (
@@ -67,6 +92,7 @@ Arguments cmd_args(int argc, char** argv) {
         || strcmp(argv[i], "-h") == 0
         )
     {
+      check_bounds("height", i, argc);
       set_height(&args, argv[i + 1]);
 
     } else if (
@@ -74,7 +100,9 @@ Arguments cmd_args(int argc, char** argv) {
         || strcmp(argv[i], "-s") == 0
         )
     {
+      check_bounds("speed", i, argc);
       set_speed(&args, argv[i + 1]);
+
     }
   }