-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard_model.hpp
More file actions
89 lines (71 loc) · 2.77 KB
/
board_model.hpp
File metadata and controls
89 lines (71 loc) · 2.77 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
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
#pragma once
#include <array>
namespace FrogToad {
/**
* @brief Game model holding board state, move logic, and solution evaluation.
*/
class BoardModel {
public:
static constexpr int BoardSize = 7;
enum class Cell { Empty, Frog, Toad }; // Empty, Frog moves right, Toad moves left
// --- Public API ---
/** @brief Reset to the starting configuration. */
void reset();
/** @brief Has the game been solved? */
bool isSolved() const;
/**
* @brief Cell at index i.
* @param i Zero-based index into the board.
* @return Cell value; returns Cell::Empty if i is out of range.
*/
Cell cellAt(int i) const;
/**
* @brief Convert a cell to a character representation.
* @param c Cell value.
* @return '.' for Empty, 'F' for Frog, 'T' for Toad.
*/
static char toChar(Cell c);
/**
* @brief Attempt to move the piece at index i.
*
* Applies a slide or jump is a space is available (direction depends
* on the piece type). Updates internal state on success.
*
* @param i Zero-based index of the piece to move.
* @return True if a move was applied; otherwise false.
*/
bool tryMove(int i);
private:
/** @brief Initial board layout. */
static constexpr std::array<Cell, BoardSize> StartingBoard{ Cell::Frog, Cell::Frog, Cell::Frog, Cell::Empty, Cell::Toad, Cell::Toad, Cell::Toad };
/** @brief Goal board layout. */
static constexpr std::array<Cell, BoardSize> GoalBoard{ Cell::Toad, Cell::Toad, Cell::Toad, Cell::Empty, Cell::Frog, Cell::Frog, Cell::Frog };
// Object State
/** @brief Cached game-solved flag. */
bool isGameSolved{ false };
/** @brief The actual game board. */
std::array<Cell, BoardSize> board{ StartingBoard };
// Static Constexpr Helpers
/**
* @brief Bounds check for board indices.
* @param i Index to test.
* @return @c true if @p k is within [0, BoardSize).
*/
static constexpr bool isIndexValid(int i);
/**
* @brief Direction of motion for a piece.
* @param c Cell value.
* @return +1 (right) for Frog, -1 (left) for Toad.
*/
static constexpr int moveDirection(Cell c);
// Instance Helpers
/**
* @brief Test if a cell is empty.
* @param i Index to test (assumed valid).
* @return True if the cell is Cell::Empty, else false.
*/
bool isCellEmpty(int k) const;
/** @brief Recompute isGameSolved based on the current board. */
void evaluateBoard();
};
}