HOG2
FlipSide.h
Go to the documentation of this file.
1 /*
2  * FlipSide.h
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 #ifndef FLIPSIDE_H
11 #define FLIPSIDE_H
12 
13 #include <stdint.h>
14 #include <iostream>
15 #include "SearchEnvironment.h"
16 #include "UnitSimulation.h"
17 
19 public:
21 { width = 5;
22  puzzle.resize(width*2);
23  for (unsigned int x = 0; x < puzzle.size(); x++)
24  puzzle[x] = x;
25 }
26  FlipSideState(int _width)
27  :width(_width)
28 {
29  puzzle.resize(width*2);
30  for (unsigned int x = 0; x < puzzle.size(); x++)
31  puzzle[x] = x;
32 }
33 unsigned int width;
34 std::vector<int> puzzle;
35 };
36 
37 class flipMove {
38 public:
39  flipMove() { top = 0; bottom = 0; }
40  flipMove(int t, int b) :top(t), bottom(b) {}
41  uint16_t top, bottom;
42 };
43 
44 static std::ostream& operator <<(std::ostream & out, const FlipSideState &loc)
45 {
46  for (unsigned int x = 0; x < loc.puzzle.size(); x++)
47  {
48  if (x == loc.puzzle.size()/2)
49  out << std::endl;
50  out << loc.puzzle[x] << " ";
51  }
52  out << std::endl;
53  return out;
54 }
55 
56 static bool operator==(const FlipSideState &l1, const FlipSideState &l2)
57 {
58  if (l1.width != l2.width)
59  return false;
60  for (unsigned int x = 0; x < l1.puzzle.size(); x++)
61  if (l1.puzzle[x] != l2.puzzle[x])
62  return false;
63  return true;
64 }
65 
66 class FlipSide : public SearchEnvironment<FlipSideState, flipMove> {
67 public:
68  FlipSide(int width = 5);
69  ~FlipSide();
70  void GetSuccessors(const FlipSideState &stateID, std::vector<FlipSideState> &neighbors) const;
71  void GetActions(const FlipSideState &stateID, std::vector<flipMove> &actions) const;
72  flipMove GetAction(const FlipSideState &s1, const FlipSideState &s2) const;
73  void ApplyAction(FlipSideState &s, flipMove a) const;
74  bool InvertAction(flipMove &) const { return true; } // applying the same action inverts it
75 
76  //OccupancyInterface<FlipSideState, flipMove> *GetOccupancyInfo() { return 0; }
77 
78  double HCost(const FlipSideState &) const {
79  fprintf(stderr, "ERROR: Single State HCost not implemented for FlipSide\n");
80  exit(1); return -1.0;}
81  double HCost(const FlipSideState &state1, const FlipSideState &state2) const;
82  double GCost(const FlipSideState &state1, const FlipSideState &state2) const;
83  double GCost(const FlipSideState &, const flipMove &) const { return 1.0; }
84  bool GoalTest(const FlipSideState &state, const FlipSideState &goal) const;
85 
86  bool GoalTest(const FlipSideState &) const{
87  fprintf(stderr, "ERROR: Single State Goal Test not implemented for FlipSide\n");
88  exit(1); return false;}
89 
90  uint64_t GetStateHash(const FlipSideState &state) const;
91  uint64_t GetActionHash(flipMove act) const;
92  void OpenGLDraw() const;
93  void OpenGLDraw(const FlipSideState &s) const;
94  void OpenGLDraw(const FlipSideState &, const flipMove &) const{ /* currently not drawing moves */ }
95  void OpenGLDraw(const FlipSideState&, const FlipSideState&, float) const { /* currently not drawing moves */ }
96 
98  void ClearGoal(){}
99  bool IsGoalStored() const {return false;}
100 private:
101  int width;
102 };
103 
105 
106 #endif
FlipSide::GetStateHash
uint64_t GetStateHash(const FlipSideState &state) const
Definition: FlipSide.cpp:119
FlipSideState::width
unsigned int width
Definition: FlipSide.h:33
UnitSimulation.h
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::StoreGoal
void StoreGoal(FlipSideState &)
Stores the goal for use by single-state HCost.
Definition: FlipSide.h:97
FlipSide::GetActionHash
uint64_t GetActionHash(flipMove act) const
Definition: FlipSide.cpp:129
operator==
static bool operator==(const FlipSideState &l1, const FlipSideState &l2)
Definition: FlipSide.h:56
FlipSide::~FlipSide
~FlipSide()
Definition: FlipSide.cpp:17
FlipSideState
Definition: FlipSide.h:18
FlipSide::OpenGLDraw
void OpenGLDraw(const FlipSideState &, const flipMove &) const
Definition: FlipSide.h:94
FlipSide::IsGoalStored
bool IsGoalStored() const
Definition: FlipSide.h:99
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
loc
Definition: MapGenerators.cpp:296
FlipSide::GetAction
flipMove GetAction(const FlipSideState &s1, const FlipSideState &s2) const
Definition: FlipSide.cpp:46
FlipSide
Definition: FlipSide.h:66
FlipSideState::FlipSideState
FlipSideState()
Definition: FlipSide.h:20
FlipSide::FlipSide
FlipSide(int width=5)
Definition: FlipSide.cpp:12
FlipSide::OpenGLDraw
void OpenGLDraw() const
Definition: FlipSide.cpp:134
flipMove
Definition: FlipSide.h:37
operator<<
static std::ostream & operator<<(std::ostream &out, const FlipSideState &loc)
Definition: FlipSide.h:44
FlipSideState::FlipSideState
FlipSideState(int _width)
Definition: FlipSide.h:26
flipMove::flipMove
flipMove(int t, int b)
Definition: FlipSide.h:40
FlipSide::width
int width
Definition: FlipSide.h:101
UnitSimulation
The basic simulation class for the world.
Definition: UnitSimulation.h:85
FlipSide::GetActions
void GetActions(const FlipSideState &stateID, std::vector< flipMove > &actions) const
Definition: FlipSide.cpp:34
FlipSide::ClearGoal
void ClearGoal()
Clears the goal from memory.
Definition: FlipSide.h:98
FlipPuzzleSimulation
UnitSimulation< FlipSideState, flipMove, FlipSide > FlipPuzzleSimulation
Definition: FlipSide.h:104
flipMove::bottom
uint16_t bottom
Definition: FlipSide.h:41
FlipSide::GoalTest
bool GoalTest(const FlipSideState &) const
Goal Test if the goal is stored.
Definition: FlipSide.h:86
FlipSide::ApplyAction
void ApplyAction(FlipSideState &s, flipMove a) const
Definition: FlipSide.cpp:52
FlipSide::GCost
double GCost(const FlipSideState &, const flipMove &) const
Definition: FlipSide.h:83
FlipSide::HCost
double HCost(const FlipSideState &) const
Heuristic value between node and the stored goal.
Definition: FlipSide.h:78
SearchEnvironment
Definition: SearchEnvironment.h:30
FlipSide::OpenGLDraw
void OpenGLDraw(const FlipSideState &, const FlipSideState &, float) const
Draw the transition at some percentage 0...1 between two states.
Definition: FlipSide.h:95
flipMove::flipMove
flipMove()
Definition: FlipSide.h:39
SearchEnvironment.h
FlipSide::InvertAction
bool InvertAction(flipMove &) const
Definition: FlipSide.h:74