HOG2
FlipSide.cpp
Go to the documentation of this file.
1 /*
2  * FlipSide.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 5/22/07.
6  * Copyright 2007 Nathan Sturtevant, University of Alberta. All rights reserved.
7  *
8  */
9 
10 #include "FlipSide.h"
11 
12 FlipSide::FlipSide(int _width)
13 :width(_width)
14 {
15 }
16 
18 {
19 }
20 
21 void FlipSide::GetSuccessors(const FlipSideState &stateID, std::vector<FlipSideState> &neighbors) const
22 {
23  std::vector<flipMove> acts;
24  GetActions(stateID, acts);
25  neighbors.resize(0);
26  for (unsigned int x = 0; x < acts.size(); x++)
27  {
28  FlipSideState s = stateID;
29  ApplyAction(s, acts[x]);
30  neighbors.push_back(s);
31  }
32 }
33 
34 void FlipSide::GetActions(const FlipSideState &stateID, std::vector<flipMove> &actions) const
35 {
36  actions.resize(0);
37  for (unsigned int x = 0; x < stateID.width-2; x++)
38  {
39  for (unsigned int y = 0; y < stateID.width-2; y++)
40  {
41  actions.push_back(flipMove(x, y));
42  }
43  }
44 }
45 
47 {
48  assert(false);
49  return flipMove();
50 }
51 
53 {
54  for (int x = 0; x < 3; x++)
55  {
56  int tmp = s.puzzle[a.top+x];
57  s.puzzle[a.top+x] = s.puzzle[s.width+a.bottom+x];
58  s.puzzle[s.width+a.bottom+x] = tmp;
59  }
60 }
61 
62 double FlipSide::HCost(const FlipSideState &state1, const FlipSideState &state2) const
63 {
64  assert(state1.width==state2.width);
65  std::vector<unsigned int> xloc(state2.width*2);
66  std::vector<unsigned int> yloc(state2.width*2);
67  double hval = 0;
68 
69  for (unsigned int x = 0; x < state2.width; x++)
70  {
71  for (unsigned int y = 0; y < 2; y++)
72  {
73  xloc[state2.puzzle[x + y*state2.width]] = x;
74  yloc[state2.puzzle[x + y*state2.width]] = y;
75  }
76  }
77  for (unsigned int x = 0; x < state1.width; x++)
78  {
79  for (unsigned int y = 0; y < 2; y++)
80  {
81  if ((yloc[state1.puzzle[x + y*state1.width]] == y) &&
82  (xloc[state1.puzzle[x + y*state1.width]] == x))
83  {
84  }
85  else if (yloc[state1.puzzle[x + y*state1.width]] == y)
86  {
87  int tmp = (xloc[state1.puzzle[x + y*state1.width]] - x)/4;
88  hval += (1+tmp)*2;
89  }
90  else {
91  int tmp = ((xloc[state1.puzzle[x + y*state1.width]] - x)+1)/4;
92  hval += 1+tmp*2;
93  }
94 
95  int tmp = (xloc[state1.puzzle[x + y*state1.width]] - x)/2;
96  if (xloc[state1.puzzle[x + y*state1.width]] != x)
97  {
98  if ((tmp%2) != (yloc[state1.puzzle[x + y*state1.width]] - y))
99  tmp+=1;
100  else if (tmp == 0)
101  tmp += 2;
102  }
103  hval += tmp;
104  }
105  }
106  return ceil(hval/6);
107 }
108 
109 double FlipSide::GCost(const FlipSideState &, const FlipSideState &) const
110 {
111  return 1;
112 }
113 
114 bool FlipSide::GoalTest(const FlipSideState &state, const FlipSideState &goal) const
115 {
116  return (state == goal);
117 }
118 
119 uint64_t FlipSide::GetStateHash(const FlipSideState &state) const
120 {
121  uint64_t hashVal = 0;
122  for (unsigned int x = 0; x < state.puzzle.size(); x++)
123  {
124  hashVal = hashVal*state.puzzle.size() + state.puzzle[x];
125  }
126  return hashVal;
127 }
128 
130 {
131  return (act.top<<16)|act.bottom;
132 }
133 
135 {
136 }
137 
138 
140 {
141  glLineWidth(1.0);
142  glEnable(GL_LINE_SMOOTH);
143 
144  float w = width;
145  float h = 2;
146  float fscale = 120;
147 
148  for (int y = 0; y < 2; y++)
149  {
150  for (int x = 0; x < width; x++)
151  {
152  glPushMatrix();
153  glColor3f(0.0, 1.0, 0.0);
154  glTranslatef(x*2.0/w-1.0+1/(2*w), (1+y)*2.0/h-1.0-1/(2*h), -0.001);
155  glScalef(1.0/(w*fscale), 1.0/(h*fscale), 1);
156  glRotatef(180, 0.0, 0.0, 1.0);
157  glRotatef(180, 0.0, 1.0, 0.0);
158  //glTranslatef((float)x/width-0.5, (float)y/2-0.5, 0);
159  //if (s.puzzle[x+y*width] > 9)
160  // glutStrokeCharacter(GLUT_STROKE_ROMAN, '0'+(((s.puzzle[x+y*width])/10)%10));
161  //glutStrokeCharacter(GLUT_STROKE_ROMAN, '0'+((s.puzzle[x+y*width])%10));
162  //glTranslatef(-x/width+0.5, -y/2+0.5, 0);
163  glPopMatrix();
164  }
165  }
166 
167  glBegin(GL_LINES);
168  for (int y = 0; y <= 2; y++)
169  {
170  for (int x = 0; x <= width; x++)
171  {
172  glVertex3f(x*2.0/w-1.0, -1, -0.001);
173  glVertex3f(x*2.0/w-1.0, 1, -0.001);
174  glVertex3f(-1, (y)*2.0/h-1.0, -0.001);
175  glVertex3f(1, (y)*2.0/h-1.0, -0.001);
176  }
177  }
178  glEnd();
179 
180  //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
181  //glEnable(GL_BLEND);
182  //glEnable(GL_LINE_SMOOTH);
183  //output(200, 225, "This is antialiased.");
184 
185  //int width, height;
186  //std::vector<int> puzzle;
187 }
FlipSide::GetStateHash
uint64_t GetStateHash(const FlipSideState &state) const
Definition: FlipSide.cpp:119
FlipSideState::width
unsigned int width
Definition: FlipSide.h:33
FlipSide::GetSuccessors
void GetSuccessors(const FlipSideState &stateID, std::vector< FlipSideState > &neighbors) const
Definition: FlipSide.cpp:21
FlipSide::GCost
double GCost(const FlipSideState &state1, const FlipSideState &state2) const
Definition: FlipSide.cpp:109
flipMove::top
uint16_t top
Definition: FlipSide.h:41
FlipSide::GetActionHash
uint64_t GetActionHash(flipMove act) const
Definition: FlipSide.cpp:129
FlipSide.h
FlipSide::~FlipSide
~FlipSide()
Definition: FlipSide.cpp:17
FlipSideState
Definition: FlipSide.h:18
width
int width
Definition: SFML_HOG.cpp:54
FlipSide::GoalTest
bool GoalTest(const FlipSideState &state, const FlipSideState &goal) const
Definition: FlipSide.cpp:114
FlipSideState::puzzle
std::vector< int > puzzle
Definition: FlipSide.h:34
FlipSide::GetAction
flipMove GetAction(const FlipSideState &s1, const FlipSideState &s2) const
Definition: FlipSide.cpp:46
FlipSide::FlipSide
FlipSide(int width=5)
Definition: FlipSide.cpp:12
FlipSide::OpenGLDraw
void OpenGLDraw() const
Definition: FlipSide.cpp:134
flipMove
Definition: FlipSide.h:37
FlipSide::width
int width
Definition: FlipSide.h:101
FlipSide::GetActions
void GetActions(const FlipSideState &stateID, std::vector< flipMove > &actions) const
Definition: FlipSide.cpp:34
flipMove::bottom
uint16_t bottom
Definition: FlipSide.h:41
FlipSide::ApplyAction
void ApplyAction(FlipSideState &s, flipMove a) const
Definition: FlipSide.cpp:52
FlipSide::HCost
double HCost(const FlipSideState &) const
Heuristic value between node and the stored goal.
Definition: FlipSide.h:78