Просмотр исходного кода

Merge pull request #1 from SimplyMinimal/simonController

Added the logic for the game in simonsays_controller.h. simonsays_cli contains a CLI version of the game.
SimplyMinimal 3 лет назад
Родитель
Сommit
2588baa7e1
6 измененных файлов с 276 добавлено и 2 удалено
  1. 55 2
      .gitignore
  2. 8 0
      .vscode/settings.json
  3. 28 0
      .vscode/tasks.json
  4. 67 0
      simonsays_cli.c
  5. 111 0
      simonsays_controller.h
  6. 7 0
      utilities.h

+ 55 - 2
.gitignore

@@ -1,4 +1,57 @@
-.vscode
 dist/debug/
 .editorconfig
-*.txt
+*.txt
+# Prerequisites
+*.d
+
+build/
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf

+ 8 - 0
.vscode/settings.json

@@ -0,0 +1,8 @@
+{
+    "files.associations": {
+        "assert.h": "c",
+        "utilities.h": "c",
+        "simonsays_controller.h": "c"
+    },
+    "editor.formatOnSave": true
+}

+ 28 - 0
.vscode/tasks.json

@@ -0,0 +1,28 @@
+{
+    "tasks": [
+        {
+            "type": "cppbuild",
+            "label": "C/C++: gcc-11 build active file",
+            "command": "/usr/bin/gcc-11",
+            "args": [
+                "-fdiagnostics-color=always",
+                "-g",
+                "${file}",
+                "-o",
+                "${fileDirname}/build/${fileBasenameNoExtension}"
+            ],
+            "options": {
+                "cwd": "${fileDirname}"
+            },
+            "problemMatcher": [
+                "$gcc"
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            },
+            "detail": "Task generated by Debugger."
+        }
+    ],
+    "version": "2.0.0"
+}

+ 67 - 0
simonsays_cli.c

@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <assert.h>
+#include "simonsays_controller.h"
+#include "utilities.h"
+
+int main()
+{
+    resetGame();
+
+    printf("Loading game...\n");
+    // TODO: LOADING SCREEN HERE
+    preLoadGame();
+
+    printf("Welcome to Simon Says!\nPress enter to start the game");
+    // TODO: MAIN MENU HERE
+    getchar();
+
+    startGame();
+
+    while (getCurrentGameState() == inGame)
+    {
+
+        printf("New Round!\n");
+        currentGame.currentScore++;
+        startNewRound();
+
+        enum shape_names currentSimonMove = getCurrentSimonMove();
+        printf("Simon Says.. Score(%d)\n", currentGame.currentScore - 1);
+        for (int index = 0; index < currentGame.currentScore; index++)
+        {
+            // TODO: TIMED SEQUENCE HERE
+            printf("%d\n", currentGame.simonMoves[index]);
+        }
+
+        printf("Your turn:\n");
+
+        while (!isRoundComplete())
+        {
+            char userInput = getchar();
+            if (userInput == '\n')
+                continue; // getchar registers enter as an input..
+            int playerAction = atoi(&userInput);
+
+            onPlayerSelectedShapeCallback(playerAction);
+
+            // TODO: INTERACT WITH USER INPUTS HERE
+
+            if (getCurrentGameState() != inGame)
+            {
+                break;
+            }
+        }
+    }
+
+    if (getCurrentGameState() == gameOver)
+    {
+        // TODO: GAMEOVER SCREEN HERE
+        printf("Game over!");
+    }
+    if (getCurrentGameState() == gameVictory)
+    {
+        // TODO: VICTORY SCREEN HERE
+        printf("You've beat Simon!");
+    }
+
+    return 0;
+}

+ 111 - 0
simonsays_controller.h

@@ -0,0 +1,111 @@
+#pragma once
+#include "utilities.h"
+#include <stdbool.h>
+#include <assert.h>
+
+enum game_state
+{
+    preloading = 0,
+    mainMenu,
+    inGame,
+    gameOver,
+    gameVictory
+};
+enum shape_names
+{
+    left = 0,
+    top,
+    right,
+    bottom
+};
+
+struct SimonSays
+{
+    enum game_state gameState;
+    int highScore;
+
+    int currentScore;
+    int numberOfMillisecondsBeforeShapeDisappears;
+
+    enum shape_names simonMoves[1000];
+    int playerMoveIndex;
+};
+
+struct SimonSays currentGame;
+
+void resetGame()
+{
+    currentGame = (struct SimonSays){
+        .gameState = preloading,
+        .currentScore = 0,
+        .playerMoveIndex = 0,
+        .numberOfMillisecondsBeforeShapeDisappears = 500,
+        .simonMoves[0] = getRandomIntInRange(0, 3)};
+}
+
+void preLoadGame()
+{
+    // TODO: load game stuff
+    currentGame.highScore = 0; // TODO: fetch this from storage
+
+    setCurrentGameState(mainMenu);
+}
+
+void startGame()
+{
+    assert(currentGame.gameState == mainMenu);
+    setCurrentGameState(inGame);
+}
+
+void setCurrentGameState(enum game_state gameState)
+{
+    currentGame.gameState = gameState;
+}
+
+enum game_state getCurrentGameState()
+{
+    return currentGame.gameState;
+}
+
+void addNewSimonMove(int addAtIndex)
+{
+    currentGame.simonMoves[addAtIndex] = getRandomIntInRange(0, 3);
+}
+
+void startNewRound()
+{
+    addNewSimonMove(currentGame.currentScore);
+    currentGame.playerMoveIndex = 0;
+}
+
+void onPlayerAnsweredCorrect()
+{
+    currentGame.playerMoveIndex++;
+}
+
+void onPlayerAnsweredWrong()
+{
+    setCurrentGameState(gameOver);
+}
+
+bool isRoundComplete()
+{
+    return currentGame.playerMoveIndex == currentGame.currentScore;
+}
+
+enum shape_names getCurrentSimonMove()
+{
+    return currentGame.simonMoves[currentGame.playerMoveIndex];
+}
+
+void onPlayerSelectedShapeCallback(enum shape_names shape)
+{
+    if (shape == getCurrentSimonMove())
+    {
+        onPlayerAnsweredCorrect();
+    }
+    else
+    {
+        onPlayerAnsweredWrong();
+    }
+}

+ 7 - 0
utilities.h

@@ -0,0 +1,7 @@
+#pragma once
+#include <stdlib.h>
+
+int getRandomIntInRange(int lower, int upper) {
+        return (rand() %
+        (upper - lower + 1)) + lower;
+}