HOG2
RubiksCube7Edges.h
Go to the documentation of this file.
1 //
2 // Rubik7Edge.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 1/4/13.
6 // Copyright (c) 2013 University of Denver. All rights reserved.
7 //
8 
9 #ifndef __hog2_glut__Rubik7Edge__
10 #define __hog2_glut__Rubik7Edge__
11 
12 #include <iostream>
13 #include <stdint.h>
14 #include <vector>
15 #include "SearchEnvironment.h"
16 
17 const int pieces = 10;
18 
20 {
21 public:
23  {
24  Reset();
25  }
26  void Reset()
27  {
28  state = 0;
29  for (int x = 0; x < 12; x++)
30  SetCubeInLoc(x, x);
31  }
32  void GetDual(Rubik7EdgeState &s) const;
33  int GetCubeInLoc(int whichLoc) const
34  {
35  return (state>>(12+4*whichLoc))&0xF;
36  }
37  void SetCubeInLoc(int whichLoc, int cube)
38  {
39  uint64_t blank = 0xF;
40  uint64_t value = cube&0xF;
41  state = state&(~(blank<<(12+4*whichLoc)));
42  state |= (value<<(12+4*whichLoc));
43  }
44  bool GetCubeOrientation(int whichCube) const
45  {
46  return state&(0x1<<whichCube);
47  }
48  void SetCubeOrientation(int whichCube, bool flip)
49  {
50  uint64_t blank = 0x1;
51  if (flip)
52  state |= (0x1<<whichCube);
53  else
54  state = state&(~(blank<<whichCube));
55  }
56  void FlipCubeOrientation(int whichCube)
57  {
58 // if (whichCube == 0xF)
59 // return;
60  // printf("Was: 0x%X [flip %d] -- ", state, whichCube);
61  state = state^(0x1<<whichCube);
62  // printf("Now: 0x%X \n", state);
63  }
64  uint64_t state;
65 };
66 
67 static bool operator==(const Rubik7EdgeState &l1, const Rubik7EdgeState &l2)
68 {
69  for (int x = 0; x < 12; x++)
70  {
71  if (l1.GetCubeInLoc(x) != l2.GetCubeInLoc(x))
72  return false;
73  //if (l1.GetCubeInLoc(x) != 15)
74  {
76  return false;
77  }
78  }
79  return true;
80  //return l1.state == l2.state;
81 }
82 
83 static std::ostream& operator <<(std::ostream & out, const Rubik7EdgeState &s)
84 {
85  for (int x = 0; x < 12; x++)
86  {
87  out << s.GetCubeInLoc(x) << "-" << (s.GetCubeOrientation(s.GetCubeInLoc(x))?1:0) << " ";
88  }
89  return out;
90 }
91 
92 
93 typedef int Rubik7EdgeAction;
94 
96 public:
99  int length() { if (next == 0) return 1; return 1+next->length(); }
100 };
101 
102 class Rubik7Edge : public SearchEnvironment<Rubik7EdgeState, Rubik7EdgeAction>
103 {
104 public:
106  {
107  for (int x = 0; x < 18; x++)
108  {
109  moves[x].act = x;
110  if (x != 17)
111  moves[x].next = &moves[x+1];
112  } moves[17].next = 0;
113  }
115  virtual void GetSuccessors(const Rubik7EdgeState &nodeID, std::vector<Rubik7EdgeState> &neighbors) const;
116  virtual void GetActions(const Rubik7EdgeState &nodeID, std::vector<Rubik7EdgeAction> &actions) const;
117  virtual Rubik7EdgeAction GetAction(const Rubik7EdgeState &s1, const Rubik7EdgeState &s2) const;
118  virtual void ApplyAction(Rubik7EdgeState &s, Rubik7EdgeAction a) const;
119 
120 
121  virtual double HCost(const Rubik7EdgeState &node1, const Rubik7EdgeState &node2) const { return 0; }
122 
123  virtual double GCost(const Rubik7EdgeState &node1, const Rubik7EdgeState &node2) const { return 1; }
124  virtual double GCost(const Rubik7EdgeState &node, const Rubik7EdgeAction &act) const { return 1; }
125  virtual bool GoalTest(const Rubik7EdgeState &node, const Rubik7EdgeState &goal) const { return node == goal; }
126 
127 
130  void unrankPlayer(uint64_t d, Rubik7EdgeState & s, int who)
131  {
132  GetStateFromHash(d, s);
133  }
134  //
135  int64_t getMaxSinglePlayerRank() const;
136  int64_t getMaxSinglePlayerRank2();
137  int64_t getMaxSinglePlayerRank2(int64_t firstIndex);
138 
141  virtual void GetNextState(const Rubik7EdgeState &, Rubik7EdgeAction , Rubik7EdgeState &) const;
142 
143  virtual bool InvertAction(Rubik7EdgeAction &a) const
144  { if (2 == a%3)
145  return true;
146  if (1 == a%3)
147  {
148  a -= 1;
149  return true;
150  }
151  a += 1;
152  return true;
153  }
154 
155  int64_t rankPlayer(Rubik7EdgeState &s, int who)
156  { return GetStateHash(s); }
157  void rankPlayerFirstTwo(const Rubik7EdgeState &s, int who, int64_t &rank);
158  void rankPlayerRemaining(const Rubik7EdgeState &s, int who, int64_t &rank);
159  void rankPlayer(const Rubik7EdgeState &s, int who, int64_t &index1, int64_t &index2)
160  { rankPlayerFirstTwo(s, 0, index1); rankPlayerRemaining(s, 0, index2); }
161 
162  virtual uint64_t GetStateHash(const Rubik7EdgeState &node) const;
163  virtual uint64_t GetActionHash(Rubik7EdgeAction act) const { return 0; }
164  virtual void GetStateFromHash(uint64_t hash, Rubik7EdgeState &node) const;
165 
166  virtual void OpenGLDraw() const;
167  virtual void OpenGLDraw(const Rubik7EdgeState&) const;
169  virtual void OpenGLDraw(const Rubik7EdgeState&, const Rubik7EdgeState&, float) const;
170  virtual void OpenGLDraw(const Rubik7EdgeState&, const Rubik7EdgeAction&) const;
171 private:
172  void MRUnrank(int n, uint64_t r, uint64_t &perm) const;
173  void MRUnrank2(int n, uint64_t r, uint64_t &perm) const;
174 
175  void SetCubeColor(int which, bool face, const Rubik7EdgeState&) const;
177 };
178 
179 #endif /* defined(__hog2_glut__Rubik7Edge__) */
Rubik7EdgeState
Definition: RubiksCube7Edges.h:19
Rubik7EdgeState::Reset
void Reset()
Definition: RubiksCube7Edges.h:26
Rubik7Edge::getMoves
Rubik7EdgeMove * getMoves(Rubik7EdgeState &)
Definition: RubiksCube7Edges.h:139
Rubik7EdgeState::FlipCubeOrientation
void FlipCubeOrientation(int whichCube)
Definition: RubiksCube7Edges.h:56
Rubik7Edge::Rubik7Edge
Rubik7Edge()
Definition: RubiksCube7Edges.h:105
Rubik7Edge::getMaxSinglePlayerRank
int64_t getMaxSinglePlayerRank() const
Definition: RubiksCube7Edges.cpp:371
Rubik7Edge::InvertAction
virtual bool InvertAction(Rubik7EdgeAction &a) const
Definition: RubiksCube7Edges.h:143
Rubik7EdgeState::SetCubeOrientation
void SetCubeOrientation(int whichCube, bool flip)
Definition: RubiksCube7Edges.h:48
Rubik7EdgeMove::act
Rubik7EdgeAction act
Definition: RubiksCube7Edges.h:97
Rubik7EdgeMove
Definition: RubiksCube7Edges.h:95
Rubik7Edge::GetActions
virtual void GetActions(const Rubik7EdgeState &nodeID, std::vector< Rubik7EdgeAction > &actions) const
Definition: RubiksCube7Edges.cpp:37
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
Rubik7Edge::GoalTest
virtual bool GoalTest(const Rubik7EdgeState &node, const Rubik7EdgeState &goal) const
Definition: RubiksCube7Edges.h:125
Rubik7Edge::moves
Rubik7EdgeMove moves[18]
Definition: RubiksCube7Edges.h:176
pieces
const int pieces
Definition: RubiksCube7Edges.h:17
Rubik7Edge::GCost
virtual double GCost(const Rubik7EdgeState &node1, const Rubik7EdgeState &node2) const
Definition: RubiksCube7Edges.h:123
Rubik7EdgeState::SetCubeInLoc
void SetCubeInLoc(int whichLoc, int cube)
Definition: RubiksCube7Edges.h:37
Rubik7Edge::OpenGLDraw
virtual void OpenGLDraw() const
Definition: RubiksCube7Edges.cpp:530
Rubik7Edge::rankPlayerFirstTwo
void rankPlayerFirstTwo(const Rubik7EdgeState &s, int who, int64_t &rank)
Definition: RubiksCube7Edges.cpp:399
Rubik7Edge::unrankPlayer
void unrankPlayer(uint64_t d, Rubik7EdgeState &s, int who)
Definition: RubiksCube7Edges.h:130
Rubik7Edge::GetNextState
virtual void GetNextState(const Rubik7EdgeState &, Rubik7EdgeAction, Rubik7EdgeState &) const
Definition: RubiksCube7Edges.cpp:329
Rubik7Edge::MRUnrank2
void MRUnrank2(int n, uint64_t r, uint64_t &perm) const
Rubik7Edge::GCost
virtual double GCost(const Rubik7EdgeState &node, const Rubik7EdgeAction &act) const
Definition: RubiksCube7Edges.h:124
Rubik7Edge::GetStateFromHash
virtual void GetStateFromHash(uint64_t hash, Rubik7EdgeState &node) const
Definition: RubiksCube7Edges.cpp:469
Rubik7Edge::ApplyAction
virtual void ApplyAction(Rubik7EdgeState &s, Rubik7EdgeAction a) const
Definition: RubiksCube7Edges.cpp:72
Rubik7EdgeState::Rubik7EdgeState
Rubik7EdgeState()
Definition: RubiksCube7Edges.h:22
Rubik7Edge::GetActionHash
virtual uint64_t GetActionHash(Rubik7EdgeAction act) const
Definition: RubiksCube7Edges.h:163
Rubik7Edge::GetStateHash
virtual uint64_t GetStateHash(const Rubik7EdgeState &node) const
Definition: RubiksCube7Edges.cpp:438
Rubik7Edge::rankPlayer
int64_t rankPlayer(Rubik7EdgeState &s, int who)
Definition: RubiksCube7Edges.h:155
operator<<
static std::ostream & operator<<(std::ostream &out, const Rubik7EdgeState &s)
Definition: RubiksCube7Edges.h:83
Rubik7Edge::ApplyMove
void ApplyMove(Rubik7EdgeState &s, Rubik7EdgeMove *a)
Definition: RubiksCube7Edges.cpp:53
Rubik7Edge::getMaxSinglePlayerRank2
int64_t getMaxSinglePlayerRank2()
Definition: RubiksCube7Edges.cpp:382
Rubik7EdgeState::GetCubeInLoc
int GetCubeInLoc(int whichLoc) const
Definition: RubiksCube7Edges.h:33
Rubik7Edge::rankPlayerRemaining
void rankPlayerRemaining(const Rubik7EdgeState &s, int who, int64_t &rank)
Definition: RubiksCube7Edges.cpp:411
Rubik7Edge::UndoMove
void UndoMove(Rubik7EdgeState &s, Rubik7EdgeMove *a)
Definition: RubiksCube7Edges.cpp:58
Rubik7Edge::SetCubeColor
void SetCubeColor(int which, bool face, const Rubik7EdgeState &) const
Definition: RubiksCube7Edges.cpp:732
Rubik7Edge::GetSuccessors
virtual void GetSuccessors(const Rubik7EdgeState &nodeID, std::vector< Rubik7EdgeState > &neighbors) const
Definition: RubiksCube7Edges.cpp:27
Rubik7Edge
Definition: RubiksCube7Edges.h:102
Rubik7EdgeState::GetDual
void GetDual(Rubik7EdgeState &s) const
Definition: RubiksCube7Edges.cpp:13
Rubik7Edge::MRUnrank
void MRUnrank(int n, uint64_t r, uint64_t &perm) const
Rubik7Edge::~Rubik7Edge
~Rubik7Edge()
Definition: RubiksCube7Edges.h:114
Rubik7EdgeMove::next
Rubik7EdgeMove * next
Definition: RubiksCube7Edges.h:98
Rubik7Edge::HCost
virtual double HCost(const Rubik7EdgeState &node1, const Rubik7EdgeState &node2) const
Heuristic value between two arbitrary nodes.
Definition: RubiksCube7Edges.h:121
Rubik7EdgeAction
int Rubik7EdgeAction
Definition: RubiksCube7Edges.h:93
Rubik7Edge::GetAction
virtual Rubik7EdgeAction GetAction(const Rubik7EdgeState &s1, const Rubik7EdgeState &s2) const
Definition: RubiksCube7Edges.cpp:46
Rubik7EdgeState::state
uint64_t state
Definition: RubiksCube7Edges.h:64
Rubik7EdgeMove::length
int length()
Definition: RubiksCube7Edges.h:99
Rubik7Edge::rankPlayer
void rankPlayer(const Rubik7EdgeState &s, int who, int64_t &index1, int64_t &index2)
Definition: RubiksCube7Edges.h:159
Rubik7EdgeState::GetCubeOrientation
bool GetCubeOrientation(int whichCube) const
Definition: RubiksCube7Edges.h:44
operator==
static bool operator==(const Rubik7EdgeState &l1, const Rubik7EdgeState &l2)
Definition: RubiksCube7Edges.h:67
Rubik7Edge::freeMove
void freeMove(Rubik7EdgeMove *m)
Definition: RubiksCube7Edges.h:140
SearchEnvironment
Definition: SearchEnvironment.h:30
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
SearchEnvironment.h