Permalink
Browse files

Score counter and basic gui

  • Loading branch information...
1 parent e242fa5 commit 1f80cb6ea2018871311fba7036dd6b17615242a8 @Ignotus committed Jan 11, 2014
Showing with 148 additions and 38 deletions.
  1. +20 −3 Board.cpp
  2. +13 −4 Board.h
  3. +3 −0 CMakeLists.txt
  4. +43 −0 MainWindow.cpp
  5. +15 −0 MainWindow.h
  6. +22 −0 PlayerManagerWET.h
  7. +29 −0 ScoreCounter.h
  8. +3 −31 main.cpp
View
@@ -5,10 +5,14 @@
#include "Board.h"
#include "BorderFinder.h"
#include "IPlayerManager.h"
+#include "ScoreCounter.h"
-Board::Board(std::size_t width, std::size_t height)
- : m_cells(width, std::vector<Cell>(height))
+Board::Board(std::size_t width, std::size_t height, QObject *parent)
+ : QObject(parent)
+ , m_cells(width, std::vector<Cell>(height))
, m_player_manager(NULL)
+ , m_score_counter(NULL)
+ , m_processed_points(0)
{
}
@@ -26,6 +30,11 @@ void Board::setPlayerManager(IPlayerManager *manager)
m_player_manager = manager;
}
+void Board::setScoreCounter(ScoreCounter *counter)
+{
+ m_score_counter = counter;
+}
+
const TBoardMatrix& Board::data() const
{
return m_cells;
@@ -48,6 +57,7 @@ bool Board::put(const QPoint& point)
if (ccell.m_owner != -1)
return false;
+ ++m_processed_points;
ccell.m_owner = m_player_manager->currentPlayer();
bool isCaptured = false;
@@ -60,8 +70,12 @@ bool Board::put(const QPoint& point)
{
BorderFinder finder(m_cells, ccell.m_owner, point);
if (!finder(&polygon))
+ {
+ if (m_processed_points == width * height)
+ emit gameOver();
return false;
}
+ }
std::list<QPoint> empty_points;
bool has_captured = false;
@@ -84,7 +98,7 @@ bool Board::put(const QPoint& point)
has_captured = true;
cell.capture();
- // TODO: Increase the score
+ m_score_counter->increase(ccell.m_owner);
}
}
@@ -100,5 +114,8 @@ bool Board::put(const QPoint& point)
}
}
+ if (m_processed_points == width * height)
+ emit gameOver();
+
return true;
}
View
17 Board.h
@@ -1,23 +1,28 @@
#pragma once
#include <vector>
-#include <list>
-#include <QPolygon>
+#include <list>
#include <functional>
+#include <QObject>
+#include <QPolygon>
+
#include "Cell.h"
class IPlayerManager;
+class ScoreCounter;
typedef std::vector<std::vector<Cell>> TBoardMatrix;
-class Board
+class Board : public QObject
{
+ Q_OBJECT
public:
- Board(std::size_t width, std::size_t height);
+ Board(std::size_t width, std::size_t height, QObject *parent = 0);
~Board();
QSize size() const;
void setPlayerManager(IPlayerManager *manager);
+ void setScoreCounter(ScoreCounter *counter);
bool put(const QPoint& point);
@@ -28,4 +33,8 @@ class Board
TBoardMatrix m_cells;
std::list<std::pair<QPolygon, int>> m_polygons;
IPlayerManager *m_player_manager;
+ ScoreCounter *m_score_counter;
+ int m_processed_points;
+signals:
+ void gameOver();
};
View
@@ -19,13 +19,16 @@ set(SRCS
BorderFinder.cpp
Board.cpp
BoardView.cpp
+ MainWindow.cpp
)
set(HDRS
BorderFinder.h
Board.h
BoardView.h
Cell.h
+ MainWindow.h
+ ScoreCounter.h
)
QT4_WRAP_CPP(HEADERS_MOC ${HDRS})
View
@@ -0,0 +1,43 @@
+#include <QStatusBar>
+#include <QMessageBox>
+#include "MainWindow.h"
+#include "Board.h"
+#include "BoardView.h"
+#include "PlayerManagerWET.h"
+#include "ScoreCounter.h"
+
+MainWindow::MainWindow(QWidget *parent)
+ : QMainWindow(parent)
+ , m_player_manager(new PlayerMangerWET)
+{
+ setMinimumSize(640, 480);
+ Board *board = new Board(3, 3, this);
+ board->setPlayerManager(m_player_manager.get());
+
+ ScoreCounter *counter = new ScoreCounter(this);
+ board->setScoreCounter(counter);
+
+ QStatusBar *bar = statusBar();
+ bar->showMessage("First: 0 - Second: 0");
+
+ connect(counter,
+ SIGNAL(statusUpdated(QString)),
+ bar,
+ SLOT(showMessage(const QString&)));
+
+ connect(board,
+ SIGNAL(gameOver()),
+ this,
+ SLOT(makeGameOver()));
+
+
+ BoardView *view = new BoardView(this);
+ view->setModel(board);
+
+ setCentralWidget(view);
+}
+
+void MainWindow::makeGameOver()
+{
+ QMessageBox::information(this, "Game over!", "Game Over!");
+}
View
@@ -0,0 +1,15 @@
+#pragma once
+#include <memory>
+#include <QMainWindow>
+#include "IPlayerManager.h"
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+public:
+ MainWindow(QWidget *parent = 0);
+private slots:
+ void makeGameOver();
+private:
+ std::unique_ptr<IPlayerManager> m_player_manager;
+};
View
@@ -0,0 +1,22 @@
+#pragma once
+#include "IPlayerManager.h"
+
+class PlayerMangerWET : public IPlayerManager
+{
+ int m_player;
+public:
+ inline PlayerMangerWET()
+ {
+ m_player = 0;
+ }
+
+ inline int nextPlayer(bool is_captured)
+ {
+ return (m_player = (m_player == 0 ? 1 : 0));
+ }
+
+ inline int currentPlayer() const
+ {
+ return m_player;
+ }
+};
View
@@ -0,0 +1,29 @@
+#pragma once
+#include <QObject>
+
+class ScoreCounter : public QObject
+{
+ Q_OBJECT
+public:
+ inline ScoreCounter(QObject *parent = 0)
+ : QObject(parent)
+ {
+ m_mark[0] = 0;
+ m_mark[1] = 0;
+ }
+
+ inline void increase(int owner)
+ {
+ if (owner > -1 && owner < 2)
+ {
+ ++m_mark[owner];
+ emit statusUpdated(QString("First: %1 - Second: %2").arg(QString::number(m_mark[0]), QString::number(m_mark[1])));
+ }
+ }
+
+private:
+ int m_mark[2];
+
+signals:
+ void statusUpdated(const QString&);
+};
View
@@ -1,40 +1,12 @@
#include <QApplication>
-#include "Board.h"
-#include "BoardView.h"
-#include "IPlayerManager.h"
-
-class PlayerManger : public IPlayerManager
-{
- int m_player;
-public:
- PlayerManger()
- {
- m_player = 0;
- }
-
- inline int nextPlayer(bool is_captured)
- {
- return (m_player = (m_player == 0 ? 1 : 0));
- }
-
- inline int currentPlayer() const
- {
- return m_player;
- }
-};
+#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
- Board board(10, 10);
- PlayerManger manager;
- board.setPlayerManager(&manager);
-
- BoardView view;
- view.setModel(&board);
-
- view.show();
+ MainWindow w;
+ w.show();
return app.exec();
}

0 comments on commit 1f80cb6

Please sign in to comment.