aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
blob: ba00d88e191327d2a3ad2eae6ec926c5a1446b67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "terminal-game-utils.hpp"
#include <format>
#include <future>
#include <iostream>
#include <poll.h>
#include <queue>
#include <string>
#include <termios.h>
#include <unistd.h>

struct termios orig_termios;

class Snake {
private:
  struct mSnakeUnit {
    int x;
    int y;
    std::string direction;
    std::string appearance;
  };

  const std::string HEAD_ARROWS = "⇐⇓⇑⇒";
  const std::string SNAKE_BODY = "═║╔╗╚╝";

  bool mSnakeAteApple = false;
  char mSnakeDirection = 'r';
  bool running = true;
  int mWidth, mHeight, mStartingSpeed;
  double mAcceleration;
  std::queue<mSnakeUnit> mSnake;
  std::queue<TGU::GameKey> mKeyQueue;

  std::string mRepeat(int times, std::string str) {
    std::string returned;
    for (int i = 0; i < times; i++)
      returned += str;
    return returned;
  }

  std::string mGetTurningCharacter(std::string ldur) {
    if (ldur.length() != 2)
      return "";

    if (ldur == "lr")
      return "═";
    if (ldur == "du")
      return "║";
    if (ldur == "dr")
      return "╔";
    if (ldur == "ld")
      return "╗";
    if (ldur == "ur")
      return "╚";
    if (ldur == "lu")
      return "╝";

    return "";
  }

  void mPrintBoardOutlines() {
    TGU::MoveCursor(0, 0);

    std::string horizontalLine = mRepeat(mWidth * 2 - 1, "═");
    std::string horizontalSpaces = std::string(mWidth * 2 - 1, ' ');

    std::cout << "╔" << horizontalLine + "╗" << std::endl;

    for (int i = 0; i < mHeight; i++) {
      std::cout << "║" + horizontalSpaces << "║" << std::endl;
    }

    std::cout << "╚" << horizontalLine + "╝" << std::endl;
  }

  void mSetSnakeDirection() {
    TGU::GameKey popped = mKeyQueue.front();
    if (popped == TGU::GameKey::W || popped == TGU::GameKey::UPARROW)
      mSnakeDirection = 'u';
    if (popped == TGU::GameKey::A || popped == TGU::GameKey::LEFTARROW)
      mSnakeDirection = 'l';
    if (popped == TGU::GameKey::S || popped == TGU::GameKey::DOWNARROW)
      mSnakeDirection = 'd';
    if (popped == TGU::GameKey::D || popped == TGU::GameKey::RIGHTARROW)
      mSnakeDirection = 'r';
  }

  void mSnakeStep() {

    if (!mSnakeAteApple)
      mSnake.pop();
    mSnake.push({1, 2, mSnakeDirection, ""})
  }

  void mGameTick() {
    if (mKeyQueue.size() != 0) {
      mSetSnakeDirection();

      mSnakeStep();
    }
  }

  void mInputLoop() {
    TGU::GameKey key;
    while (running) {
      key = TGU::GetKeyPress();
    }
  }

  void mGameLoop() {
    while (running) {
      mGameTick();
    }
  }

public:
  struct GameSettings {
    int width = 14;
    int height = 14;
    int startingSpeed = 3;
    double acceleration = 0.3;
  };

  Snake(const GameSettings &settings)
      : mWidth(settings.width), mHeight(settings.height),
        mStartingSpeed(settings.startingSpeed),
        mAcceleration(settings.acceleration) {
    if (mWidth < 5)
      mWidth = 5;
    if (mHeight < 5)
      mHeight = 5;
  }

  void StartGame() {
    mPrintBoardOutlines();

    mSnake.push({0, 2, "lr", "═"});
    mSnake.push({1, 2, "lr", "═"});
    mSnake.push({2, 2, "lr", "═"});
    mSnake.push({3, 2, "lr", "⇒"});

    auto InputLoopResponse =
        std::async(std::launch::async, [this] { mInputLoop(); });

    mInputLoop();
  }
};

int main() {
  TGU::EnableRawMode();
  Snake hi({});
  hi.StartGame();
}