summary refs log tree commit diff stats
path: root/src/direction.h
blob: 14ea0a5e5c9858cb71bde11d741e697c6a7effdc (plain) (blame)
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
#ifndef DIRECTION_H_42BDAFB9
#define DIRECTION_H_42BDAFB9

#include <string_view>
#include <stdexcept>
#include <tuple>

using coord = std::tuple<int, int>;

enum class Direction {
  up,
  down,
  left,
  right
};

inline Direction directionFromString(std::string_view str) {
  if (str == "up") return Direction::up;
  if (str == "right") return Direction::right;
  if (str == "down") return Direction::down;
  if (str == "left") return Direction::left;
  throw std::invalid_argument("Invalid direction: " + std::string(str));
}

inline coord coordInDirection(int x, int y, Direction dir) {
  switch (dir) {
    case Direction::up: return {x, y-1};
    case Direction::down: return {x, y+1};
    case Direction::left: return {x-1, y};
    case Direction::right: return {x+1, y};
  }
}

#endif /* end of include guard: DIRECTION_H_42BDAFB9 */