HOG2
Hexagon.h
Go to the documentation of this file.
1 //
2 // Hexagon.h
3 // Hexagon
4 //
5 // Created by Nathan Sturtevant on 11/13/21.
6 // Copyright © 2021 MovingAI. All rights reserved.
7 //
8 
9 #ifndef Hexagon_h
10 #define Hexagon_h
11 
12 #include <cstdio>
13 #include <vector>
14 #include <array>
15 #include "SearchEnvironment.h"
16 #include "FPUtil.h"
17 #include "NBitArray.h"
18 
19 enum tPieceName {
20  kHexagon = 0,
22  kElbow = 2,
23  kLine = 3,
25  kWrench = 5,
26  kTriangle = 6,
27  kHook = 7,
29  kSnake = 9
30 };
31 
32 enum tFlipType {
33  kCanFlip = 0,
34  kSide1 = 1,
35  kSide2 = 2
36 };
37 
38 const int numPieces = 10;
39 const std::string pieceNames[numPieces] =
40 {
41  "Hexagon",
42  "Butterfly",
43  "Elbow",
44  "Line",
45  "Mountains",
46  "Wrench",
47  "Triangle",
48  "Hook",
49  "Trapezoid",
50  "Snake"
51 };
52 
54 {
55  unsigned int piece : 8; // Support up to 256 pieces for analysis
56  unsigned int location : 8; // Each piece can be in up to 256(?) different locations
57  // Up to 324 (54x6) locations
58 // int location : 6; // 54 possible locations
59 // int rotation : 3; // 3 rotations x 2 flips = 6
60 };
61 
63 {
64 public:
66  :state(60) {}
68 };
69 
70 static bool operator==(const HexagonState &l1, const HexagonState &l2)
71 {
72  return l1.state == l2.state;
73 }
74 
75 static std::ostream &operator<<(std::ostream &out, const HexagonAction &a)
76 {
77  return out;
78 }
79 
80 
81 class HexagonEnvironment;
82 
83 // (Old) code for loading and displaying files
84 class Hexagon : public SearchEnvironment<HexagonState, HexagonAction>
85 {
86 public:
87  Hexagon();
88  ~Hexagon();
89  void LoadPuzzle(const char *, HexagonState &s);
90  void LoadSolution(const char *, HexagonState &s);
91  void GetSuccessors(const HexagonState &nodeID, std::vector<HexagonState> &neighbors) const;
92  void GetActions(const HexagonState &nodeID, std::vector<HexagonAction> &actions) const;
93 
94  HexagonAction GetAction(const HexagonState &s1, const HexagonState &s2) const;
95  void ApplyAction(HexagonState &s, HexagonAction a) const;
96 
97  void GetNextState(const HexagonState &, HexagonAction , HexagonState &) const;
98  bool InvertAction(HexagonAction &a) const;
99 
100  void RotateCW(HexagonState &s) const;
101 
103  bool GoalTest(const HexagonState &node) const;
104 
105  double HCost(const HexagonState &node1, const HexagonState &node2) const { return 0; }
106  double GCost(const HexagonState &node1, const HexagonState &node2) const { return 1; }
107  double GCost(const HexagonState &node, const HexagonAction &act) const { return 1; }
108  bool GoalTest(const HexagonState &node, const HexagonState &goal) const
109  { return GoalTest(node); }
110 
111  uint64_t GetStateHash(const HexagonState &node) const;
112  uint64_t GetActionHash(HexagonAction act) const;
113 
114  void Draw(Graphics::Display &display) const;
116  void DrawSetup(Graphics::Display &display) const;
117  void Draw(Graphics::Display &display, const HexagonState&) const;
118 private:
120  void Load(const char *, HexagonState &s, bool solution);
121  void GetCorners(int x, int y, Graphics::point &p1, Graphics::point &p2, Graphics::point &p3) const;
122  bool GetBorder(int x, int y, int xoff, int yoff, Graphics::point &p1, Graphics::point &p2) const;
123  bool Valid(int x, int y) const;
125  std::vector<rgbColor> pieceColors;
126  std::vector<int> diagPieces;
127  std::vector<int> noFlipPieces;
128  std::vector<int> notTouchPieces;
129  std::vector<int> touchPieces;
130 };
131 
133 {
134 public:
136  :bits(0), cnt(0)
137  {
138  }
139  void Reset()
140  { bits = 0; cnt = 0; }
141  uint64_t bits;
142  int cnt;
143  std::array<HexagonAction, 12> state;
144 // NBitArray<4> state;
145 };
146 
147 static bool operator==(const HexagonAction &l1, const HexagonAction &l2)
148 {
149  return l1.piece == l2.piece && l1.location == l2.location;
150 }
151 
152 static bool operator==(const HexagonSearchState &l1, const HexagonSearchState &l2)
153 {
154  if (l1.bits != l2.bits || l1.cnt != l2.cnt)
155  return false;
156  for (int x = 0; x < l1.cnt; x++)
157  {
158  bool found = false;
159  for (int y = 0; y < l1.cnt; y++) // needed if we have multiples of the same object
160  {
161  if (l1.state[x] == l2.state[y])
162  {
163  found = true;
164  break;
165  }
166  }
167  if (!found)
168  return false;
169  }
170  return true;
171 }
172 
173 
174 // efficient search implementation
175 class HexagonEnvironment : public SearchEnvironment<HexagonSearchState, HexagonAction>
176 {
177 public:
180  void SetPieces(const std::vector<tPieceName> &pieces);
181  void SetFlippable(const std::array<tFlipType, numPieces> &flips);
182 
183  void GetSuccessors(const HexagonSearchState &nodeID, std::vector<HexagonSearchState> &neighbors) const;
184  void GetActions(const HexagonSearchState &nodeID, std::vector<HexagonAction> &actions) const;
185 
186  HexagonAction GetAction(const HexagonSearchState &s1, const HexagonSearchState &s2) const;
187  void ApplyAction(HexagonSearchState &s, HexagonAction a) const;
188  void UndoAction(HexagonSearchState &s, HexagonAction a) const;
189 
191  bool InvertAction(HexagonAction &a) const;
192 
193  void RotateCW(HexagonSearchState &s) const;
195  void Flip(HexagonSearchState &s) const;
197 
199  bool GoalTest(const HexagonSearchState &node) const;
200 
201  double HCost(const HexagonSearchState &node1, const HexagonSearchState &node2) const { return 0; }
202  double GCost(const HexagonSearchState &node1, const HexagonSearchState &node2) const { return 1; }
203  double GCost(const HexagonSearchState &node, const HexagonAction &act) const { return 1; }
204  bool GoalTest(const HexagonSearchState &node, const HexagonSearchState &goal) const
205  { return GoalTest(node); }
206 
207  uint64_t GetStateHash(const HexagonSearchState &node) const;
208  uint64_t GetActionHash(HexagonAction act) const;
209 
213  void GenerateBoardBorder();
214 
215  void Draw(Graphics::Display &display) const;
217  void DrawSetup(Graphics::Display &display) const;
218  void Draw(Graphics::Display &display, const HexagonSearchState&) const;
219 private:
220  void BuildRotationTable();
221  void BuildFlipTable();
222  void IndexToXY(int index, int &x, int &y) const;
223  void GetCorners(int x, int y, Graphics::point &p1, Graphics::point &p2, Graphics::point &p3) const;
224  bool GetBorder(int x, int y, int xoff, int yoff, Graphics::point &p1, Graphics::point &p2) const;
225  bool Valid(int x, int y) const;
226  std::vector<rgbColor> pieceColors;
227  std::vector<int> pieces;
228  std::array<tFlipType, numPieces> flippable;
230  int rotate30Map[numPieces][14*6*2+1];
231  int flipMap[numPieces][14*6*2+1];
232 };
233 
234 #endif /* Hexagon_h */
Hexagon::Draw
void Draw(Graphics::Display &display) const
Definition: Hexagon.cpp:1388
Hexagon::RotateCW
void RotateCW(HexagonState &s) const
Definition: Hexagon.cpp:1215
Graphics::point
Definition: Graphics.h:32
HexagonEnvironment::GetNextState
void GetNextState(const HexagonSearchState &, HexagonAction, HexagonSearchState &) const
Definition: Hexagon.cpp:788
HexagonState::state
NBitArray< 4 > state
Definition: Hexagon.h:67
HexagonEnvironment::GenerateBoardBorder
void GenerateBoardBorder()
Prints out the outer coorsinates of the board.
Definition: Hexagon.cpp:870
Hexagon::~Hexagon
~Hexagon()
Definition: Hexagon.cpp:1034
kElbow
@ kElbow
Definition: Hexagon.h:22
HexagonEnvironment::pieces
std::vector< int > pieces
Definition: Hexagon.h:227
tFlipType
tFlipType
Definition: Hexagon.h:32
NBitArray< 4 >
HexagonEnvironment::InvertAction
bool InvertAction(HexagonAction &a) const
Definition: Hexagon.cpp:795
HexagonEnvironment::DrawSetup
void DrawSetup(Graphics::Display &display) const
Draws available pieces and constraints.
Definition: Hexagon.cpp:901
HexagonEnvironment::HCost
double HCost(const HexagonSearchState &node1, const HexagonSearchState &node2) const
Heuristic value between two arbitrary nodes.
Definition: Hexagon.h:201
Hexagon::GetSuccessors
void GetSuccessors(const HexagonState &nodeID, std::vector< HexagonState > &neighbors) const
Definition: Hexagon.cpp:1167
Hexagon::touchPieces
std::vector< int > touchPieces
Definition: Hexagon.h:129
kCanFlip
@ kCanFlip
Definition: Hexagon.h:33
tPieceName
tPieceName
Definition: Hexagon.h:19
numPieces
const int numPieces
Definition: Hexagon.h:38
Hexagon::GetBorder
bool GetBorder(int x, int y, int xoff, int yoff, Graphics::point &p1, Graphics::point &p2) const
Definition: Hexagon.cpp:1221
HexagonEnvironment::hex
Hexagon hex
Definition: Hexagon.h:229
kLine
@ kLine
Definition: Hexagon.h:23
Hexagon::GCost
double GCost(const HexagonState &node1, const HexagonState &node2) const
Definition: Hexagon.h:106
HexagonEnvironment::BuildFlipTable
void BuildFlipTable()
Definition: Hexagon.cpp:641
HexagonEnvironment::Valid
bool Valid(int x, int y) const
Definition: Hexagon.cpp:1018
HexagonState
Definition: Hexagon.h:62
HexagonSearchState::HexagonSearchState
HexagonSearchState()
Definition: Hexagon.h:135
HexagonState::HexagonState
HexagonState()
Definition: Hexagon.h:65
HexagonEnvironment::pieceColors
std::vector< rgbColor > pieceColors
Definition: Hexagon.h:226
HexagonEnvironment::GetAction
HexagonAction GetAction(const HexagonSearchState &s1, const HexagonSearchState &s2) const
Definition: Hexagon.cpp:768
FPUtil.h
HexagonEnvironment::HexagonEnvironment
HexagonEnvironment()
Definition: Hexagon.cpp:570
HexagonAction::piece
unsigned int piece
Definition: Hexagon.h:55
kMountains
@ kMountains
Definition: Hexagon.h:24
Hexagon::ApplyAction
void ApplyAction(HexagonState &s, HexagonAction a) const
Definition: Hexagon.cpp:1183
HexagonAction
Definition: Hexagon.h:53
HexagonEnvironment::GetBorder
bool GetBorder(int x, int y, int xoff, int yoff, Graphics::point &p1, Graphics::point &p2) const
Definition: Hexagon.cpp:1013
HexagonEnvironment
Definition: Hexagon.h:175
HexagonSearchState
Definition: Hexagon.h:132
HexagonEnvironment::GetCorners
void GetCorners(int x, int y, Graphics::point &p1, Graphics::point &p2, Graphics::point &p3) const
Definition: Hexagon.cpp:1008
HexagonEnvironment::GetStateHash
uint64_t GetStateHash(const HexagonSearchState &node) const
Definition: Hexagon.cpp:808
Hexagon::GetActionHash
uint64_t GetActionHash(HexagonAction act) const
Definition: Hexagon.cpp:1204
HexagonEnvironment::GoalTest
bool GoalTest(const HexagonSearchState &node) const
Goal Test if the goal is stored.
Definition: Hexagon.cpp:801
Hexagon::GetCorners
void GetCorners(int x, int y, Graphics::point &p1, Graphics::point &p2, Graphics::point &p3) const
Definition: Hexagon.cpp:1336
HexagonEnvironment::Flip
void Flip(HexagonSearchState &s) const
Definition: Hexagon.cpp:696
Hexagon::Hexagon
Hexagon()
Definition: Hexagon.cpp:1030
kTrapezoid
@ kTrapezoid
Definition: Hexagon.h:28
kSide1
@ kSide1
Definition: Hexagon.h:34
HexagonSearchState::Reset
void Reset()
Definition: Hexagon.h:139
Graphics::Display
Definition: Graphics.h:146
HexagonEnvironment::RotateCW
void RotateCW(HexagonSearchState &s) const
Definition: Hexagon.cpp:680
NBitArray.h
Hexagon::GoalTest
bool GoalTest(const HexagonState &node, const HexagonState &goal) const
Definition: Hexagon.h:108
Hexagon::HexagonEnvironment
friend HexagonEnvironment
Definition: Hexagon.h:119
HexagonEnvironment::GetActions
void GetActions(const HexagonSearchState &nodeID, std::vector< HexagonAction > &actions) const
Definition: Hexagon.cpp:725
HexagonEnvironment::SetFlippable
void SetFlippable(const std::array< tFlipType, numPieces > &flips)
Definition: Hexagon.cpp:591
Hexagon::GCost
double GCost(const HexagonState &node, const HexagonAction &act) const
Definition: Hexagon.h:107
kWrench
@ kWrench
Definition: Hexagon.h:25
Hexagon::InvertAction
bool InvertAction(HexagonAction &a) const
Definition: Hexagon.cpp:1193
Hexagon
Definition: Hexagon.h:84
operator<<
static std::ostream & operator<<(std::ostream &out, const HexagonAction &a)
Definition: Hexagon.h:75
HexagonEnvironment::GCost
double GCost(const HexagonSearchState &node1, const HexagonSearchState &node2) const
Definition: Hexagon.h:202
kSnake
@ kSnake
Definition: Hexagon.h:29
HexagonEnvironment::BuildRotationTable
void BuildRotationTable()
Definition: Hexagon.cpp:602
HexagonEnvironment::Draw
void Draw(Graphics::Display &display) const
Definition: Hexagon.cpp:895
HexagonSearchState::bits
uint64_t bits
Definition: Hexagon.h:141
Hexagon::DrawSetup
void DrawSetup(Graphics::Display &display) const
Draws available pieces and constraints.
Definition: Hexagon.cpp:1434
Hexagon::pieceColors
std::vector< rgbColor > pieceColors
Definition: Hexagon.h:125
HexagonEnvironment::SetPieces
void SetPieces(const std::vector< tPieceName > &pieces)
Definition: Hexagon.cpp:584
HexagonEnvironment::ApplyAction
void ApplyAction(HexagonSearchState &s, HexagonAction a) const
Definition: Hexagon.cpp:774
Hexagon::LoadSolution
void LoadSolution(const char *, HexagonState &s)
Definition: Hexagon.cpp:1039
HexagonAction::location
unsigned int location
Definition: Hexagon.h:56
Hexagon::Load
void Load(const char *, HexagonState &s, bool solution)
Definition: Hexagon.cpp:1049
kHexagon
@ kHexagon
Definition: Hexagon.h:20
kSide2
@ kSide2
Definition: Hexagon.h:35
Hexagon::GoalTest
bool GoalTest(const HexagonState &node) const
Goal Test if the goal is stored.
Definition: Hexagon.cpp:1210
HexagonSearchState::state
std::array< HexagonAction, 12 > state
Definition: Hexagon.h:143
HexagonEnvironment::flipMap
int flipMap[numPieces][14 *6 *2+1]
Definition: Hexagon.h:231
Hexagon::noFlipPieces
std::vector< int > noFlipPieces
Definition: Hexagon.h:127
HexagonEnvironment::~HexagonEnvironment
~HexagonEnvironment()
Definition: Hexagon.cpp:597
HexagonEnvironment::UndoAction
void UndoAction(HexagonSearchState &s, HexagonAction a) const
Definition: Hexagon.cpp:781
HexagonEnvironment::IndexToXY
void IndexToXY(int index, int &x, int &y) const
Definition: Hexagon.cpp:940
pieceNames
const std::string pieceNames[numPieces]
Definition: Hexagon.h:39
Hexagon::Valid
bool Valid(int x, int y) const
Definition: Hexagon.cpp:1374
HexagonEnvironment::flippable
std::array< tFlipType, numPieces > flippable
Definition: Hexagon.h:228
Hexagon::GetAction
HexagonAction GetAction(const HexagonState &s1, const HexagonState &s2) const
Definition: Hexagon.cpp:1177
Hexagon::GetNextState
void GetNextState(const HexagonState &, HexagonAction, HexagonState &) const
Definition: Hexagon.cpp:1188
Hexagon::diagPieces
std::vector< int > diagPieces
Definition: Hexagon.h:126
HexagonEnvironment::GetSuccessors
void GetSuccessors(const HexagonSearchState &nodeID, std::vector< HexagonSearchState > &neighbors) const
Definition: Hexagon.cpp:712
HexagonEnvironment::GCost
double GCost(const HexagonSearchState &node, const HexagonAction &act) const
Definition: Hexagon.h:203
Hexagon::HCost
double HCost(const HexagonState &node1, const HexagonState &node2) const
Heuristic value between two arbitrary nodes.
Definition: Hexagon.h:105
Hexagon::GetStateHash
uint64_t GetStateHash(const HexagonState &node) const
Definition: Hexagon.cpp:1199
operator==
static bool operator==(const HexagonState &l1, const HexagonState &l2)
Definition: Hexagon.h:70
HexagonSearchState::cnt
int cnt
Definition: Hexagon.h:142
HexagonEnvironment::GoalTest
bool GoalTest(const HexagonSearchState &node, const HexagonSearchState &goal) const
Definition: Hexagon.h:204
HexagonEnvironment::GetActionHash
uint64_t GetActionHash(HexagonAction act) const
Definition: Hexagon.cpp:813
Hexagon::LoadPuzzle
void LoadPuzzle(const char *, HexagonState &s)
Definition: Hexagon.cpp:1044
Hexagon::solution
HexagonState solution
Definition: Hexagon.h:124
Hexagon::notTouchPieces
std::vector< int > notTouchPieces
Definition: Hexagon.h:128
kTriangle
@ kTriangle
Definition: Hexagon.h:26
kButterfly
@ kButterfly
Definition: Hexagon.h:21
SearchEnvironment
Definition: SearchEnvironment.h:30
Hexagon::GetActions
void GetActions(const HexagonState &nodeID, std::vector< HexagonAction > &actions) const
Definition: Hexagon.cpp:1172
HexagonEnvironment::rotate30Map
int rotate30Map[numPieces][14 *6 *2+1]
Definition: Hexagon.h:230
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h
kHook
@ kHook
Definition: Hexagon.h:27
HexagonEnvironment::GeneratePieceCoordinates
void GeneratePieceCoordinates(tPieceName p)
Prints out the triangles used for this piece in HOG2 coordinates.
Definition: Hexagon.cpp:819