-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.hpp
More file actions
64 lines (59 loc) · 2.17 KB
/
interfaces.hpp
File metadata and controls
64 lines (59 loc) · 2.17 KB
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
#pragma once
#include "board_model.hpp"
namespace FrogToad {
/**
* @brief Abstract view interface for rendering our game.
*
* Views must implement a single draw operation that receives a read-only
* snapshot of the current board state to render.
*/
class IView {
public:
virtual ~IView() = default;
/**
* @brief Render the provided board state.
*
* Accepts a const reference to the current @c BoardModel. The
* parameter is intentionally unnamed; implementers should fetch all
* data needed to draw without mutating the model.
*/
virtual void draw(const BoardModel&) = 0;
};
/**
* @brief Abstract controller interface plus a model helper.
*
* Controllers encapsulate player or AI input and advance the model by
* issuing moves.
*/
class IController {
public:
virtual ~IController() = default;
/**
* @brief Advance the game by producing the next move.
*
* Implementations should read input (human or AI) and apply it to the
* model—usually by delegating to applyCommand.
*/
virtual void nextMove(BoardModel&) = 0;
/**
* @brief Translate an ASCII character into a model command.
*
* @param m Reference to the model.
* @param key Input character representing a command (e.g., '1'..'9', 'r'/'R').
* @return True if the command was recognized and applied; otherwise false.
*
* @details
* - 'r' or 'R' resets the model via BoardModel::reset().
* - Digit characters are mapped to zero-based indices by subtracting the
* character '1' from the pressed key (e.g., '1'→0, '2'→1, '3'→2),
* and passed to BoardModel::tryMove().
*
* @note The digit-to-index conversion relies on ASCII ordering:
* '1' - '1' = 0, '2' - '1' = 1, '3' - '1' = 2, etc.
*/
static bool applyCommand(BoardModel& m, char key) {
if (key == 'r' || key == 'R') { m.reset(); return true; }
return m.tryMove(key - '1');
}
};
}