Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removing monkey codes

  • Loading branch information...
commit e19cda3e3a507299f970828eff4b49c8aabd3fd2 1 parent 4919d35
Minh Ngo authored
11 dottable.cpp
View
@@ -244,16 +244,11 @@ namespace KDots
{
for (int i = 0; i < DIRECTION_COUNT; ++i)
{
- const int tempx = itr->x () + GRAPH_DX[i];
- const int tempy = itr->y () + GRAPH_DY[i];
-
- if (tempx < 0 || tempy < 0
- || static_cast<std::size_t> (tempx) >= m_graph->width ()
- || static_cast<std::size_t> (tempy) >= m_graph->height ())
+ const Point newPoint (itr->x () + GRAPH_DX[i], itr->y () + GRAPH_DY[i]);
+ if (!m_graph->isValid (newPoint))
continue;
- const Point newPoint (tempx, tempy);
- const GraphPoint& graphPoint = (*m_graph)[newPoint];
+ const GraphPoint& graphPoint = m_graph->operator[] (newPoint);
if (graphPoint.owner () != current || graphPoint.isCaptured ())
continue;
155 graph.hpp
View
@@ -34,6 +34,63 @@ namespace KDots
class Graph;
template<class A>
+ class graph_iterator;
+
+ class KDOTS_EXPORT Graph
+ {
+ std::vector<std::vector<GraphPoint>> m_graph;
+ public:
+ typedef graph_iterator<GraphPoint> iterator;
+ typedef graph_iterator<const GraphPoint> const_iterator;
+
+ Graph (int width, int height);
+
+ iterator begin ();
+ const_iterator begin () const;
+
+ iterator end ();
+ const_iterator end () const;
+
+ bool isValid (const Point& point) const
+ {
+ return point > Point () && point < Point (width (), height ());
+ }
+
+ std::size_t width () const
+ {
+ return m_graph.size ();
+ }
+
+ std::size_t height () const
+ {
+ return m_graph.front ().size ();
+ }
+
+ GraphPoint& operator[] (const Point& index)
+ {
+ return m_graph[index.x ()][index.y ()];
+ }
+
+ const GraphPoint& operator[] (const Point& index) const
+ {
+ return m_graph[index.x ()][index.y ()];
+ }
+
+ std::vector<GraphPoint>& operator[] (int index)
+ {
+ return m_graph[index];
+ }
+
+ const std::vector<GraphPoint>& operator[] (int index) const
+ {
+ return m_graph[index];
+ }
+
+ void addEdge (const KDots::Point& first, const KDots::Point& second);
+ void removeEdge (const Point& first, const Point& second);
+ };
+
+ template<class A>
class graph_iterator : public std::iterator<std::forward_iterator_tag, A>
{
Graph *m_graph;
@@ -91,94 +148,36 @@ namespace KDots
return m_graph != other.m_graph || m_x != other.m_x || m_y != other.m_y;
}
- A& operator* ();
-
- A* operator-> ();
-
- graph_iterator& operator++ ();
-
- graph_iterator operator++ (int)
+ A& operator* ()
{
- graph_iterator res (*this);
- ++(*this);
- return res;
+ return (*m_graph)[m_x][m_y];
}
- };
-
- class KDOTS_EXPORT Graph
- {
- std::vector<std::vector<GraphPoint>> m_graph;
- public:
- typedef graph_iterator<GraphPoint> iterator;
- typedef graph_iterator<const GraphPoint> const_iterator;
- Graph (int width, int height);
-
- iterator begin ();
- const_iterator begin () const;
-
- iterator end ();
- const_iterator end () const;
-
- std::size_t width () const
- {
- return m_graph.size ();
- }
-
- std::size_t height () const
+ A* operator-> ()
{
- return m_graph.front ().size ();
+ return &(*m_graph)[m_x][m_y];
}
-
- GraphPoint& operator[] (const Point& index)
- {
- return m_graph[index.x ()][index.y ()];
- }
-
- const GraphPoint& operator[] (const Point& index) const
- {
- return m_graph[index.x ()][index.y ()];
- }
-
- std::vector<GraphPoint>& operator[] (int index)
+
+ graph_iterator& operator++ ()
{
- return m_graph[index];
+ if (m_x + 1 == m_graph->width ())
+ {
+ m_x = 0;
+ ++m_y;
+ }
+ else
+ ++m_x;
+
+ return *this;
}
-
- const std::vector<GraphPoint>& operator[] (int index) const
+
+ graph_iterator operator++ (int)
{
- return m_graph[index];
+ graph_iterator res (*this);
+ ++(*this);
+ return res;
}
-
- void addEdge (const KDots::Point& first, const KDots::Point& second);
- void removeEdge (const Point& first, const Point& second);
};
-
- template<class A>
- A& graph_iterator<A>::operator* ()
- {
- return (*m_graph)[m_x][m_y];
- }
-
- template<class A>
- A* graph_iterator<A>::operator-> ()
- {
- return &(*m_graph)[m_x][m_y];
- }
-
- template<class A>
- graph_iterator<A>& graph_iterator<A>::operator++ ()
- {
- if (m_x + 1 == m_graph->width ())
- {
- m_x = 0;
- ++m_y;
- }
- else
- ++m_x;
-
- return *this;
- }
}
#endif
123 plugins/simpleai/rival.cpp
View
@@ -79,65 +79,69 @@ namespace KDots
namespace
{
- float calcImportance(const Graph& graph, const Point& point, const Owner currentOwner)
+ bool hasMask (const Graph& graph, const Point& point, const MapData& mask)
{
- float priority = -0.5;
+ const Owner currentOwner = graph[point].owner ();
const Owner otherOwner = StepQueue::other (currentOwner);
-
- for (const MapData& table : PriorityMap::instance ().priorityMap ())
- {
- const MapType& map = table.m_map;
- const Point& currentPoint = table.m_current;
+ const MapType& map = mask.m_map;
+ const Point& currentPoint = mask.m_current;
- for (std::size_t j = 0, height = map.size (), i,
- width = map.front ().size (); j < height; ++j)
+ for (std::size_t j = 0, height = map.size (), i,
+ width = map.front ().size (); j < height; ++j)
+ {
+ for (i = 0; i < width; ++i)
{
- for (i = 0; i < width; ++i)
+ const Point newPoint (currentPoint.x () - i + point.x (),
+ currentPoint.y () - j + point.y ());
+
+ if (!graph.isValid (newPoint))
+ return false;
+
+ const MapElement el = map[j][i];
+ const GraphPoint& graphPoint = graph[newPoint];
+ const Owner own = graphPoint.owner ();
+ const bool captured = graphPoint.isCaptured ();
+ switch (el)
{
- const int dx = currentPoint.x () - i;
- const int dy = currentPoint.y () - j;
- const int newX = point.x () + dx;
- const int newY = point.y () + dy;
- if (newX < 0 || newY < 0
- || static_cast<std::size_t> (newX) >= graph.width ()
- || static_cast<std::size_t> (newY) >= graph.height ())
- goto endloop;
-
- const MapElement el = map[j][i];
- const GraphPoint& graphPoint = graph[newX][newY];
- const Owner own = graphPoint.owner ();
- const bool captured = graphPoint.isCaptured ();
- switch (el)
- {
- case EM: //Empty
- if (own != NONE || captured)
- goto endloop;
- break;
- case FI: //First
- if (own != otherOwner || captured)
- goto endloop;
- break;
- case SE: //Second
- if (own != currentOwner || captured)
- goto endloop;
- break;
- case PF: // Possibly first
- if (own == currentOwner || captured)
- goto endloop;
- break;
- case PS: // Possibly second
- if (own == otherOwner || captured)
- goto endloop;
- default:
- break;
- }
+ case EM: //Empty
+ if (own != NONE || captured)
+ return false;
+ break;
+ case FI: //First
+ if (own != otherOwner || captured)
+ return false;
+ break;
+ case SE: //Second
+ if (own != currentOwner || captured)
+ return false;
+ break;
+ case PF: // Possibly first
+ if (own == currentOwner || captured)
+ return false;
+ break;
+ case PS: // Possibly second
+ if (own == otherOwner || captured)
+ return false;
+ default:
+ break;
}
}
+ }
+
+ return true;
+ }
+
+ float calcImportance(const Graph& graph, const Point& point)
+ {
+ float priority = -0.5;
+
+ for (const MapData& table : PriorityMap::instance ().priorityMap ())
+ {
+ if (!hasMask (graph, point, table))
+ continue;
if (table.m_priority > priority)
priority = table.m_priority;
-endloop:
- ;
}
return priority;
@@ -145,12 +149,10 @@ namespace KDots
bool isEmptyAround (const Graph& graph, const Point& point)
{
- for (int i = 0; i < 8; ++i)
+ for (int i = 0; i < DIRECTION_COUNT; ++i)
{
const Point newPoint (point.x () + GRAPH_DX[i], point.y () + GRAPH_DY[i]);
- if (newPoint.x () < 0 || newPoint.y () < 0
- || static_cast<std::size_t> (newPoint.x ()) >= graph.width ()
- || static_cast<std::size_t> (newPoint.y ()) >= graph.height ())
+ if (!graph.isValid (newPoint))
continue;
if (graph[newPoint].owner () != NONE)
@@ -240,14 +242,12 @@ namespace KDots
++id;
}
- kDebug () << "Min size:" << distance;
return index;
};
if (!points.empty ())
{
const int index = minSize (point);
- //kDebug () << "index: " << index;
m_table->pushPoint (points[index]);
}
}
@@ -256,7 +256,7 @@ namespace KDots
{
const Graph& gr = m_table->graph ();
- importance += calcImportance (gr, point, m_current);
+ importance += calcImportance (gr, point);
m_pointStack.push_back (point);
if (iteration == m_iterations) // Need configure this feature
return;
@@ -266,15 +266,10 @@ namespace KDots
int i = 0;
for (; i < DIRECTION_COUNT; ++i)
{
- const int tempx = point.x () + GRAPH_DX[i];
- const int tempy = point.y () + GRAPH_DY[i];
-
- if (tempx < 0 || tempy < 0
- || static_cast<std::size_t> (tempx) >= gr.width ()
- || static_cast<std::size_t> (tempy) >= gr.height ())
+ const Point newPoint (point.x () + GRAPH_DX[i], point.y () + GRAPH_DY[i]);
+ if (!gr.isValid (newPoint))
continue;
- const Point newPoint (tempx, tempy);
const GraphPoint& newGrPoint = gr[newPoint];
if (!newGrPoint.isCaptured () && newGrPoint.owner () == NONE
@@ -288,7 +283,7 @@ namespace KDots
}
}
- if (i != 8)
+ if (i != DIRECTION_COUNT)
importance += max_imp;
}
10 point.hpp
View
@@ -79,6 +79,16 @@ namespace KDots
{
return Point (m_x + val, m_y + val);
}
+
+ bool operator< (const Point& other) const
+ {
+ return m_x < other.m_x && m_y < other.m_y;
+ }
+
+ bool operator> (const Point& other) const
+ {
+ return m_y > other.m_y && m_x > other.m_x;
+ }
bool operator== (const Point& a) const
{
12 polygonfinder.cpp
View
@@ -102,15 +102,11 @@ namespace KDots
for (int i = 0; i < DIRECTION_COUNT; ++i)
{
- const int tempx = point.x () + GRAPH_DX[i];
- const int tempy = point.y () + GRAPH_DY[i];
-
- if (tempx < 0 || tempy < 0
- || static_cast<std::size_t> (tempx) >= m_graph.width ()
- || static_cast<std::size_t> (tempy) >= m_graph.height ())
- continue;
+ const Point newPoint (point.x () + GRAPH_DX[i], point.y () + GRAPH_DY[i]);
- const Point newPoint (tempx, tempy);
+ if (!m_graph.isValid (newPoint))
+ continue;
+
const GraphPoint& graphPoint = m_graph[newPoint];
if (graphPoint.isCaptured () || graphPoint.owner () != m_current)
Please sign in to comment.
Something went wrong with that request. Please try again.