Skip to content

Commit

Permalink
Upd. interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignotus committed Aug 24, 2012
1 parent 96621b4 commit 4d7fdc7
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 22 deletions.
33 changes: 28 additions & 5 deletions dottable.cpp
Expand Up @@ -24,6 +24,9 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "dottable.hpp" #include "dottable.hpp"
#include <KMessageBox>
#include <KLocalizedString>
#include <KDebug>
#include "polygonfinder.hpp" #include "polygonfinder.hpp"
#include "stepqueue.hpp" #include "stepqueue.hpp"


Expand Down Expand Up @@ -125,17 +128,17 @@ namespace KDots
//O(n) //O(n)
const PolyList& polyList = findPolygon (point); const PolyList& polyList = findPolygon (point);


const std::list<Point>& points = m_steps->getPoints (StepQueue::other (current)); const auto& points = m_steps->getPoints (StepQueue::other (current));
if (points.empty () || polyList.empty ()) if (points.empty () || polyList.empty ())
{ {
m_steps->nextStep (); continueStep ();
emit nextPlayer (point); emit nextPlayer (point);
return; return;
} }


const Owner otherOwner = StepQueue::other (current); const Owner otherOwner = StepQueue::other (current);


const std::list<Point>& otherOwnerPoints = m_steps->getPoints (otherOwner); const auto& otherOwnerPoints = m_steps->getPoints (otherOwner);
for (const Point& p : otherOwnerPoints) for (const Point& p : otherOwnerPoints)
{ {
GraphPoint& gpoint = graph[p]; GraphPoint& gpoint = graph[p];
Expand Down Expand Up @@ -177,16 +180,36 @@ namespace KDots
} }


drawPolygon (polyList); drawPolygon (polyList);
m_steps->nextStep ();
continueStep ();
emit nextPlayer (point); emit nextPlayer (point);
} }

void DotTable::continueStep ()
{
const auto& allPoints = m_steps->getAllPoints ();
if (allPoints.size () == m_graph->width () * m_graph->height ())
{
const int first = m_steps->getMarks (FIRST);
const int second = m_steps->getMarks (SECOND);

if (first > second)
KMessageBox::information (0, i18n ("The first player win!"), i18n ("The first player win!"));
else if (first < second)
KMessageBox::information (0, i18n ("The second player win!"), i18n ("The second player win!"));
else
KMessageBox::information (0, i18n ("Dead heat!"), i18n ("Dead heat!"));
}

m_steps->nextStep ();
}


//Hardcore undo process //Hardcore undo process
void DotTable::undo () void DotTable::undo ()
{ {
m_graph.reset (new Graph (m_config.m_width, m_config.m_height)); m_graph.reset (new Graph (m_config.m_width, m_config.m_height));
m_polygons.clear (); m_polygons.clear ();
std::list<Point> points (m_steps->getAllPoints ()); auto points (m_steps->getAllPoints ());


if (!points.empty ()) if (!points.empty ())
points.pop_back (); points.pop_back ();
Expand Down
2 changes: 2 additions & 0 deletions include/dottable.hpp
Expand Up @@ -69,6 +69,8 @@ namespace KDots
void nextPlayer (const Point& lastPoint); void nextPlayer (const Point& lastPoint);
private: private:
void drawPolygon (PolyList polygons); void drawPolygon (PolyList polygons);

void continueStep ();
}; };
} }


Expand Down
10 changes: 5 additions & 5 deletions include/graph.hpp
Expand Up @@ -37,7 +37,7 @@ namespace KDots
class graph_iterator : public std::iterator<std::forward_iterator_tag, A> class graph_iterator : public std::iterator<std::forward_iterator_tag, A>
{ {
Graph *m_graph; Graph *m_graph;
int m_x, m_y; std::size_t m_x, m_y;
public: public:


Point point () const Point point () const
Expand All @@ -52,14 +52,14 @@ namespace KDots
{ {
} }


graph_iterator (Graph *graph, int x = 0, int y = 0) graph_iterator (Graph *graph, std::size_t x = 0, std::size_t y = 0)
: m_graph (graph) : m_graph (graph)
, m_x (x) , m_x (x)
, m_y (y) , m_y (y)
{ {
} }


graph_iterator (const Graph *graph, int x = 0, int y = 0) graph_iterator (const Graph *graph, std::size_t x = 0, std::size_t y = 0)
: m_graph (const_cast<Graph*> (graph)) : m_graph (const_cast<Graph*> (graph))
, m_x (x) , m_x (x)
, m_y (y) , m_y (y)
Expand Down Expand Up @@ -120,12 +120,12 @@ namespace KDots
iterator end (); iterator end ();
const_iterator end () const; const_iterator end () const;


inline int width () const inline std::size_t width () const
{ {
return m_graph.size (); return m_graph.size ();
} }


inline int height () const inline std::size_t height () const
{ {
return m_graph.front ().size (); return m_graph.front ().size ();
} }
Expand Down
7 changes: 3 additions & 4 deletions include/stepqueue.hpp
Expand Up @@ -25,7 +25,6 @@
*/ */
#ifndef KDOTS_STEPQUEUE_HPP #ifndef KDOTS_STEPQUEUE_HPP
#define KDOTS_STEPQUEUE_HPP #define KDOTS_STEPQUEUE_HPP
#include <list>
#include "point.hpp" #include "point.hpp"
#include "constants.hpp" #include "constants.hpp"


Expand All @@ -34,7 +33,7 @@ namespace KDots
class KDOTS_EXPORT StepQueue class KDOTS_EXPORT StepQueue
{ {
Owner m_firstOwner; Owner m_firstOwner;
std::list<Point> m_firstPoints, m_secondPoints, m_points; std::vector<Point> m_firstPoints, m_secondPoints, m_points;
int m_first, m_second; int m_first, m_second;
protected: protected:
Owner m_owner; Owner m_owner;
Expand Down Expand Up @@ -76,12 +75,12 @@ namespace KDots
return owner == FIRST ? m_first : m_second; return owner == FIRST ? m_first : m_second;
} }


inline std::list<Point> getPoints (Owner owner) const inline std::vector<Point> getPoints (Owner owner) const
{ {
return owner == SECOND ? m_secondPoints : m_firstPoints; return owner == SECOND ? m_secondPoints : m_firstPoints;
} }


inline std::list<Point> getAllPoints () const inline std::vector<Point> getAllPoints () const
{ {
return m_points; return m_points;
} }
Expand Down
2 changes: 2 additions & 0 deletions kdotsui.rc
Expand Up @@ -11,6 +11,8 @@
<text>File</text> <text>File</text>
<Action name="NewGame" /> <Action name="NewGame" />
<Action name="EndGame" /> <Action name="EndGame" />
<Separator />
<Action name="Quit" />
</Menu> </Menu>
<Menu noMerge="1" name="Game"> <Menu noMerge="1" name="Game">
<text>Game</text> <text>Game</text>
Expand Down
7 changes: 6 additions & 1 deletion mainwindow.cpp
Expand Up @@ -137,13 +137,18 @@ namespace KDots
endAction->setShortcut (Qt::CTRL + Qt::Key_E); endAction->setShortcut (Qt::CTRL + Qt::Key_E);
endAction->setEnabled (false); endAction->setEnabled (false);


KAction *quitAction = actionCollection ()->addAction ("Quit", this, SLOT (close ()));
quitAction->setIcon (KIcon ("exit"));
quitAction->setText (i18n ("&Quit"));
quitAction->setShortcut (Qt::CTRL + Qt::Key_Q);

KAction *undoAction = actionCollection ()->addAction ("UndoGame", this, SLOT (undo ())); KAction *undoAction = actionCollection ()->addAction ("UndoGame", this, SLOT (undo ()));
undoAction->setIcon (KIcon ("undo")); undoAction->setIcon (KIcon ("undo"));
undoAction->setText (i18n ("&Undo")); undoAction->setText (i18n ("&Undo"));
undoAction->setEnabled (false); undoAction->setEnabled (false);
undoAction->setShortcut (Qt::CTRL + Qt::Key_Z); undoAction->setShortcut (Qt::CTRL + Qt::Key_Z);


actionCollection ()->addAction ("UndoGame", undoAction); //actionCollection ()->addAction ("UndoGame", undoAction);


connect (this, connect (this,
SIGNAL (endActionEnable (bool)), SIGNAL (endActionEnable (bool)),
Expand Down
13 changes: 9 additions & 4 deletions plugins/simpleai/rival.cpp
Expand Up @@ -97,8 +97,9 @@ namespace KDots
const int dy = currentPoint.y () - j; const int dy = currentPoint.y () - j;
const int newX = point.x () + dx; const int newX = point.x () + dx;
const int newY = point.y () + dy; const int newY = point.y () + dy;
if (newX < 0 || newY < 0 || newX >= graph.width () if (newX < 0 || newY < 0
|| newY >= graph.height ()) || static_cast<std::size_t> (newX) >= graph.width ()
|| static_cast<std::size_t> (newY) >= graph.height ())
goto endloop; goto endloop;


const MapElement el = map[j][i]; const MapElement el = map[j][i];
Expand Down Expand Up @@ -147,7 +148,8 @@ namespace KDots
{ {
const Point newPoint (point.x () + GRAPH_DX[i], point.y () + GRAPH_DY[i]); const Point newPoint (point.x () + GRAPH_DX[i], point.y () + GRAPH_DY[i]);
if (newPoint.x () < 0 || newPoint.y () < 0 if (newPoint.x () < 0 || newPoint.y () < 0
|| newPoint.x () >= graph.width () || newPoint.y () >= graph.height ()) || static_cast<std::size_t> (newPoint.x ()) >= graph.width ()
|| static_cast<std::size_t> (newPoint.y ()) >= graph.height ())
continue; continue;


if (graph[newPoint].owner () != NONE) if (graph[newPoint].owner () != NONE)
Expand Down Expand Up @@ -242,7 +244,9 @@ namespace KDots
const int tempx = point.x () + GRAPH_DX[i]; const int tempx = point.x () + GRAPH_DX[i];
const int tempy = point.y () + GRAPH_DY[i]; const int tempy = point.y () + GRAPH_DY[i];


if (tempx < 0 || tempy < 0 || tempx >= gr.width () || tempy >= gr.height ()) if (tempx < 0 || tempy < 0
|| static_cast<std::size_t> (tempx) >= gr.width ()
|| static_cast<std::size_t> (tempy) >= gr.height ())
continue; continue;


const Point newPoint (tempx, tempy); const Point newPoint (tempx, tempy);
Expand Down Expand Up @@ -273,6 +277,7 @@ namespace KDots


void Rival::setStatusBar (QStatusBar* bar) void Rival::setStatusBar (QStatusBar* bar)
{ {
Q_UNUSED (bar);
} }


} }
Expand Down
4 changes: 3 additions & 1 deletion polygonfinder.cpp
Expand Up @@ -112,7 +112,9 @@ namespace KDots
const int tempx = point.x () + GRAPH_DX[i]; const int tempx = point.x () + GRAPH_DX[i];
const int tempy = point.y () + GRAPH_DY[i]; const int tempy = point.y () + GRAPH_DY[i];


if (tempx < 0 || tempy < 0 || tempx >= m_graph.width () || tempy >= m_graph.height ()) if (tempx < 0 || tempy < 0
|| static_cast<std::size_t> (tempx) >= m_graph.width ()
|| static_cast<std::size_t> (tempy) >= m_graph.height ())
continue; continue;


findPolygons (Point (tempx, tempy)); findPolygons (Point (tempx, tempy));
Expand Down
6 changes: 4 additions & 2 deletions tablewidget.cpp
Expand Up @@ -131,7 +131,6 @@ namespace KDots
const float tableHeight = cellSize * m_height; const float tableHeight = cellSize * m_height;


QPixmap pixmap (QSize (tableWidth, tableHeight)); QPixmap pixmap (QSize (tableWidth, tableHeight));

pixmap.fill (Qt::white); pixmap.fill (Qt::white);


QPainter pixPainter (&pixmap); QPainter pixPainter (&pixmap);
Expand All @@ -143,7 +142,10 @@ namespace KDots


for (int i = cellSize, k = m_height * cellSize; i < k; i += cellSize) for (int i = cellSize, k = m_height * cellSize; i < k; i += cellSize)
pixPainter.drawLine (0, i, pixmap.width (), i); pixPainter.drawLine (0, i, pixmap.width (), i);


pixPainter.setPen (QPen (Qt::black, 3));
pixPainter.drawRect (0, 0, pixmap.width (), pixmap.height ());

const Graph& graph = m_table->graph (); const Graph& graph = m_table->graph ();
const Point& lastPoint = m_table->stepQueue ()->lastPoint (); const Point& lastPoint = m_table->stepQueue ()->lastPoint ();


Expand Down

0 comments on commit 4d7fdc7

Please sign in to comment.