HOG2
Fling.h
Go to the documentation of this file.
1 /*
2  * Fling.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 3/5/10.
6  * Copyright 2010 NS Software. All rights reserved.
7  *
8  */
9 
10 #include <iostream>
11 #include "SearchEnvironment.h"
12 
13 class FlingMove;
14 
16 {
17 public:
18  FlingBoard(unsigned int len=7, unsigned int high=8) :width(len), height(high)
19  { board = 0; obstacles = 0; holes = 0; /*board.resize(len*high);*/ currId = 0;}
20  void Reset()
21  {
22  currId = 0;
23  board = 0; locs.resize(0);
24  holes = 0; obstacles = 0;
25  }
26  void AddFling(unsigned int x, unsigned int y);
27  void AddFling(unsigned int offset);
28  void RemoveFling(unsigned int x, unsigned int y);
29  void RemoveFling(unsigned int offset);
30  bool CanMove(int which, int x, int y) const;
31  void Move(int which, int x, int y);
33  int NumPieces() const { return int(locs.size()); }
34  int GetPieceLocation(int which) const { return locs[which].first; }
35  bool HasPiece(int x, int y) const;
36  bool HasPiece(int offset) const;
37  bool HasHole(int x, int y) const;
38  bool HasHole(int offset) const;
39  bool HasObstacle(int x, int y) const;
40  bool HasObstacle(int offset) const;
41  int GetIndexInLocs(int x, int y) const;
42  int GetIndexInLocs(int offset) const;
43  unsigned int width;
44  unsigned int height;
45 
46  std::vector<std::pair<int, int>> locs;
47 
48  void SetPiece(int which);
49  void ClearPiece(int which);
50  void SetHole(int which);
51  void ClearHole(int which);
52  void SetObstacle(int which);
53  void ClearObstacle(int which);
54  uint64_t GetObstacles() { return obstacles; }
55  void SetObstacles(uint64_t o) { obstacles = o; }
56  uint64_t GetRawBoard() const { return board; }
57  uint64_t GetRawObstacles() const { return obstacles; }
58  uint64_t GetRawHoles() const { return holes; }
59 
60  int currId;
61 private:
62  uint64_t board;
63  uint64_t obstacles;
64  uint64_t holes;
65 };
66 
67 inline int GetX(int loc) { return loc%7; }
68 inline int GetY(int loc) { return loc/7; }
69 void GetMirror(const FlingBoard &in, FlingBoard &out, bool horiz, bool vert);
70 void ShiftToCorner(FlingBoard &in);
71 uint64_t GetCanonicalHash(uint64_t which);
72 
73 enum tFlingDir {
74  kLeft = 0, kUp = 1, kRight = 2,kDown = 3
75 };
76 
77 class FlingMove
78 {
79 public:
80  uint8_t startLoc;
82 };
83 
84 static std::ostream& operator <<(std::ostream & out, const FlingBoard &loc)
85 {
86  for (unsigned int y= 0; y < loc.height; y++)
87  {
88  for (unsigned int x = 0; x < loc.width; x++)
89  {
90  if (loc.HasPiece(x, y))// board[y*loc.width+x])
91  {
92  out << "o";
93  }
94  else if (loc.HasHole(x, y))
95  {
96  out << " ";
97  }
98  else if (loc.HasObstacle(x, y))
99  {
100  out << "x";
101  }
102  else {
103  out << ".";
104  }
105  }
106  out << std::endl;
107  }
108  return out;
109 }
110 
111 static std::ostream& operator <<(std::ostream & out, const FlingMove &loc)
112 {
113  out << +loc.startLoc << "->";
114  switch (loc.dir)
115  {
116  case kLeft: out << "Left"; break;
117  case kRight: out << "Right"; break;
118  case kUp: out << "Up"; break;
119  case kDown: out << "Down"; break;
120  }
121  return out;
122 }
123 
124 static bool operator==(const FlingBoard &l1, const FlingBoard &l2) {
125  return (l1.width == l2.width && l1.height == l2.height && l1.GetRawBoard() == l2.GetRawBoard());
126 }
127 
128 static bool operator!=(const FlingBoard &l1, const FlingBoard &l2) {
129  return !(l1.width == l2.width && l1.height == l2.height && l1.GetRawBoard() == l2.GetRawBoard());
130 }
131 
132 static bool operator==(const FlingMove &l1, const FlingMove &l2) {
133  return (l1.startLoc == l2.startLoc && l1.dir == l2.dir);
134 }
135 
136 
137 class Fling : public SearchEnvironment<FlingBoard, FlingMove> {
138 public:
139  Fling();
140 
141  virtual void GetSuccessors(const FlingBoard &nodeID, std::vector<FlingBoard> &neighbors) const;
142  virtual void GetActions(const FlingBoard &nodeID, std::vector<FlingMove> &actions) const;
143 
144  virtual FlingMove GetAction(const FlingBoard &s1, const FlingBoard &s2) const;
145  virtual void ApplyAction(FlingBoard &s, FlingMove a) const;
146  virtual void UndoAction(FlingBoard &s, FlingMove a) const;
147  virtual void GetNextState(const FlingBoard &, FlingMove , FlingBoard &) const;
148  bool LegalMove(const FlingBoard &, FlingMove);
149  virtual bool InvertAction(FlingMove &a) const { assert(false); }
150 
152  virtual double HCost(const FlingBoard &node1, const FlingBoard &node2) const { return 0; }
153 
154  virtual double GCost(const FlingBoard &node1, const FlingBoard &node2) const { return 1; }
155  virtual double GCost(const FlingBoard &node, const FlingMove &act) const { return 1; }
156 
157  void SetGoalPanda(int which);
158  void ClearGoalPanda();
159  void SetGoalLoc(int val);
160  void ClearGoalLoc();
161  virtual bool GoalTest(const FlingBoard &node, const FlingBoard &goal) const;
162 
163  virtual uint64_t GetStateHash(const FlingBoard &node) const;
164  virtual void GetStateFromHash(uint64_t parent, FlingBoard &s) const;
165  virtual uint64_t GetActionHash(FlingMove act) const;
166 
167  bool GetXYFromPoint(const FlingBoard &b, point3d loc, int &x, int &y) const;
168  void IncrementRank(FlingBoard &b) const;
169 
170  int64_t getMaxSinglePlayerRank(int spots, int numPieces);
171  int64_t getMaxSinglePlayerRank2(int spots, int numPieces);
172  int64_t getMaxSinglePlayerRank2(int spots, int numPieces, int64_t firstIndex);
173  int64_t rankPlayer(FlingBoard &s);
174  void rankPlayer(FlingBoard &s, int64_t &index1, int64_t &index2);
175  void rankPlayerFirstTwo(FlingBoard &s, int64_t &index1);
176  void rankPlayerRemaining(FlingBoard &s, int64_t &index2);
177  // returns true if it is a valid unranking given existing pieces
178  bool unrankPlayer(int64_t theRank, int pieces, FlingBoard &s);
179 
180 // void initBinomialSums();
181  int64_t binomialSum(unsigned int n1, unsigned int n2, unsigned int k);
182  void initBinomial();
183  int64_t binomial(unsigned int n, unsigned int k);
184  int64_t bi(unsigned int n, unsigned int k);
185 
186 
187  virtual void OpenGLDraw() const {}
188  virtual void OpenGLDraw(const FlingBoard&) const;
189  virtual void OpenGLDrawAlternate(const FlingBoard&) const;
190  virtual void OpenGLDrawPlain(const FlingBoard&b) const;
191 
192  virtual void OpenGLDraw(const FlingBoard&, const FlingMove&) const;
193  virtual void GLLabelState(const FlingBoard&, const char *) const;
194 
195 private:
196  int IncrementRank(FlingBoard &b, int piece) const;
197 
200  int goalLoc;
201  std::vector<int64_t> theSums;
202  std::vector<int64_t> binomials;
203 
204 };
205 
206 namespace std {
207 
208  template <>
209  struct hash<FlingBoard>
210  {
211  std::size_t operator()(const FlingBoard& node) const
212  {
213  std::size_t hash = 0;
214  for (unsigned int x = 0; x < node.locs.size(); x++)
215  {
216  hash |= (1ull<<node.locs[x].first);
217  }
218  return hash;
219  }
220  };
221 
222 }
FlingBoard::HasHole
bool HasHole(int x, int y) const
Definition: Fling.cpp:69
FlingBoard::Move
void Move(int which, int x, int y)
Definition: Fling.cpp:179
Fling::GetXYFromPoint
bool GetXYFromPoint(const FlingBoard &b, point3d loc, int &x, int &y) const
Definition: Fling.cpp:774
Fling::ClearGoalPanda
void ClearGoalPanda()
Definition: Fling.cpp:379
FlingBoard::GetRawObstacles
uint64_t GetRawObstacles() const
Definition: Fling.h:57
Fling::UndoAction
virtual void UndoAction(FlingBoard &s, FlingMove a) const
Definition: Fling.cpp:497
Fling::binomials
std::vector< int64_t > binomials
Definition: Fling.h:202
Fling::OpenGLDraw
virtual void OpenGLDraw() const
Definition: Fling.h:187
numPieces
const int numPieces
Definition: Hexagon.h:38
FlingBoard::obstacles
uint64_t obstacles
Definition: Fling.h:63
FlingBoard::GetIndexInLocs
int GetIndexInLocs(int x, int y) const
Definition: Fling.cpp:174
operator==
static bool operator==(const FlingBoard &l1, const FlingBoard &l2)
Definition: Fling.h:124
Fling::getMaxSinglePlayerRank2
int64_t getMaxSinglePlayerRank2(int spots, int numPieces)
Definition: Fling.cpp:879
Fling
Definition: Fling.h:137
FlingBoard::FlingBoard
FlingBoard(unsigned int len=7, unsigned int high=8)
Definition: Fling.h:18
FlingBoard::SetObstacle
void SetObstacle(int which)
Definition: Fling.cpp:39
FlingBoard::SetHole
void SetHole(int which)
Definition: Fling.cpp:27
Fling::goalLoc
int goalLoc
Definition: Fling.h:200
Fling::ApplyAction
virtual void ApplyAction(FlingBoard &s, FlingMove a) const
Definition: Fling.cpp:486
FlingBoard::ClearHole
void ClearHole(int which)
Definition: Fling.cpp:33
FlingBoard::height
unsigned int height
Definition: Fling.h:44
FlingBoard::GetObstacles
uint64_t GetObstacles()
Definition: Fling.h:54
FlingBoard::width
unsigned int width
Definition: Fling.h:43
std::hash< FlingBoard >::operator()
std::size_t operator()(const FlingBoard &node) const
Definition: Fling.h:211
pieces
const int pieces
Definition: RubiksCube7Edges.h:17
Fling::GetActionHash
virtual uint64_t GetActionHash(FlingMove act) const
Definition: Fling.cpp:563
FlingBoard::LocationAfterAction
int LocationAfterAction(FlingMove m)
Definition: Fling.cpp:228
operator<<
static std::ostream & operator<<(std::ostream &out, const FlingBoard &loc)
Definition: Fling.h:84
Fling::binomial
int64_t binomial(unsigned int n, unsigned int k)
Definition: Fling.cpp:1074
kLeft
@ kLeft
Definition: Fling.h:74
operator!=
static bool operator!=(const FlingBoard &l1, const FlingBoard &l2)
Definition: Fling.h:128
FlingBoard::Reset
void Reset()
Definition: Fling.h:20
Fling::GetStateHash
virtual uint64_t GetStateHash(const FlingBoard &node) const
Definition: Fling.cpp:530
Fling::IncrementRank
void IncrementRank(FlingBoard &b) const
Definition: Fling.cpp:850
loc
Definition: MapGenerators.cpp:296
Fling::Fling
Fling()
Definition: Fling.cpp:365
kUp
@ kUp
Definition: Fling.h:74
FlingBoard::GetRawHoles
uint64_t GetRawHoles() const
Definition: Fling.h:58
FlingBoard::GetRawBoard
uint64_t GetRawBoard() const
Definition: Fling.h:56
GetX
int GetX(int loc)
Definition: Fling.h:67
kDown
@ kDown
Definition: Fling.h:74
point3d
#define point3d
Definition: GLUtil.h:123
FlingBoard::SetPiece
void SetPiece(int which)
Definition: Fling.cpp:15
FlingBoard::CanMove
bool CanMove(int which, int x, int y) const
Definition: Fling.cpp:138
FlingBoard::holes
uint64_t holes
Definition: Fling.h:64
GetMirror
void GetMirror(const FlingBoard &in, FlingBoard &out, bool horiz, bool vert)
Definition: Fling.cpp:261
Fling::SetGoalLoc
void SetGoalLoc(int val)
Definition: Fling.cpp:384
Fling::getMaxSinglePlayerRank
int64_t getMaxSinglePlayerRank(int spots, int numPieces)
Definition: Fling.cpp:874
FlingMove::startLoc
uint8_t startLoc
Definition: Fling.h:80
Fling::unrankPlayer
bool unrankPlayer(int64_t theRank, int pieces, FlingBoard &s)
Definition: Fling.cpp:985
Fling::GetAction
virtual FlingMove GetAction(const FlingBoard &s1, const FlingBoard &s2) const
Definition: Fling.cpp:508
Fling::OpenGLDrawPlain
virtual void OpenGLDrawPlain(const FlingBoard &b) const
Definition: Fling.cpp:629
Fling::GoalTest
virtual bool GoalTest(const FlingBoard &node, const FlingBoard &goal) const
Definition: Fling.cpp:395
Fling::rankPlayer
int64_t rankPlayer(FlingBoard &s)
Definition: Fling.cpp:911
FlingBoard::HasPiece
bool HasPiece(int x, int y) const
Definition: Fling.cpp:57
Fling::theSums
std::vector< int64_t > theSums
Definition: Fling.h:201
FlingBoard::NumPieces
int NumPieces() const
Definition: Fling.h:33
Fling::HCost
virtual double HCost(const FlingBoard &node1, const FlingBoard &node2) const
Heuristic value between two arbitrary nodes.
Definition: Fling.h:152
FlingBoard::AddFling
void AddFling(unsigned int x, unsigned int y)
Definition: Fling.cpp:87
Fling::SetGoalPanda
void SetGoalPanda(int which)
Definition: Fling.cpp:373
Fling::specificGoalPanda
bool specificGoalPanda
Definition: Fling.h:199
Fling::GLLabelState
virtual void GLLabelState(const FlingBoard &, const char *) const
Definition: Fling.cpp:791
Fling::binomialSum
int64_t binomialSum(unsigned int n1, unsigned int n2, unsigned int k)
Definition: Fling.cpp:1046
Fling::GCost
virtual double GCost(const FlingBoard &node1, const FlingBoard &node2) const
Definition: Fling.h:154
Fling::LegalMove
bool LegalMove(const FlingBoard &, FlingMove)
Definition: Fling.cpp:472
Fling::GetSuccessors
virtual void GetSuccessors(const FlingBoard &nodeID, std::vector< FlingBoard > &neighbors) const
Definition: Fling.cpp:407
Fling::specificGoalLoc
bool specificGoalLoc
Definition: Fling.h:198
Fling::GetStateFromHash
virtual void GetStateFromHash(uint64_t parent, FlingBoard &s) const
Definition: Fling.cpp:549
Fling::GCost
virtual double GCost(const FlingBoard &node, const FlingMove &act) const
Definition: Fling.h:155
FlingBoard::GetPieceLocation
int GetPieceLocation(int which) const
Definition: Fling.h:34
GetCanonicalHash
uint64_t GetCanonicalHash(uint64_t which)
Definition: Fling.cpp:337
FlingMove::dir
tFlingDir dir
Definition: Fling.h:81
std
Definition: CanonicalGraphEnvironment.h:26
FlingBoard::locs
std::vector< std::pair< int, int > > locs
Definition: Fling.h:46
tFlingDir
tFlingDir
Definition: Fling.h:73
FlingBoard::SetObstacles
void SetObstacles(uint64_t o)
Definition: Fling.h:55
FlingBoard::HasObstacle
bool HasObstacle(int x, int y) const
Definition: Fling.cpp:81
Fling::rankPlayerFirstTwo
void rankPlayerFirstTwo(FlingBoard &s, int64_t &index1)
Definition: Fling.cpp:951
FlingBoard::RemoveFling
void RemoveFling(unsigned int x, unsigned int y)
Definition: Fling.cpp:114
Fling::GetNextState
virtual void GetNextState(const FlingBoard &, FlingMove, FlingBoard &) const
Definition: Fling.cpp:502
Fling::rankPlayerRemaining
void rankPlayerRemaining(FlingBoard &s, int64_t &index2)
Definition: Fling.cpp:967
Fling::GetActions
virtual void GetActions(const FlingBoard &nodeID, std::vector< FlingMove > &actions) const
Definition: Fling.cpp:439
Fling::OpenGLDrawAlternate
virtual void OpenGLDrawAlternate(const FlingBoard &) const
Definition: Fling.cpp:694
FlingBoard::ClearPiece
void ClearPiece(int which)
Definition: Fling.cpp:21
FlingBoard::board
uint64_t board
Definition: Fling.h:62
FlingBoard::ClearObstacle
void ClearObstacle(int which)
Definition: Fling.cpp:45
ShiftToCorner
void ShiftToCorner(FlingBoard &in)
Definition: Fling.cpp:283
Fling::InvertAction
virtual bool InvertAction(FlingMove &a) const
Definition: Fling.h:149
Fling::bi
int64_t bi(unsigned int n, unsigned int k)
Definition: Fling.cpp:1080
GetY
int GetY(int loc)
Definition: Fling.h:68
FlingMove
Definition: Fling.h:77
SearchEnvironment
Definition: SearchEnvironment.h:30
FlingBoard
Definition: Fling.h:15
Fling::ClearGoalLoc
void ClearGoalLoc()
Definition: Fling.cpp:390
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h
FlingBoard::currId
int currId
Definition: Fling.h:60
Fling::initBinomial
void initBinomial()
Definition: Fling.cpp:1060
kRight
@ kRight
Definition: Fling.h:74