Permalink
Browse files

visual board improvement

  • Loading branch information...
1 parent c702704 commit 78236a14b84ed7461d0bf17dc67f69d88b564e6a @NikolaYolov committed Dec 12, 2010
Showing with 107 additions and 27 deletions.
  1. +14 −5 inc/gm_brd.h
  2. +1 −1 inc/player.h
  3. +13 −5 inc/vis_brd.h
  4. +1 −0 rmk_prj.sh
  5. +26 −2 src/gm_brd.cpp
  6. +1 −1 src/main.cpp
  7. +51 −13 src/vis_brd.cpp
View
@@ -12,11 +12,6 @@
*/
class gm_brd
{
-/**
- * This array is the main board.
- */
- std::vector<plr_clr> brd_;
-
public:
/**
* Constructor, initializing the size of the board.
@@ -37,6 +32,20 @@ class gm_brd
* This method generates all moves for a given player
*/
void gen_all_mvs(/*in*/ plr_clr , /*out*/ std::vector<move>& ) const;
+
+ plr_clr get_cell(int i, int j);
+ void set_cell(int i, int j, plr_clr );
+
+ int get_wdth() const;
+ int get_hgth() const;
+
+private:
+/**
+ * This array is the main board.
+ */
+ std::vector<plr_clr> brd_;
+
+ int wdth_;
};
#endif //_GAME_BOARD_H__
View
@@ -12,9 +12,9 @@ class gm_brd;
*/
enum plr_clr : char
{
+ pc_free,
pc_white,
pc_black,
- pc_free,
};
/**
View
@@ -3,24 +3,32 @@
#include <QWidget>
+#include "gm_brd.h"
#include "player.h"
class vis_brd
: public QWidget
{
public:
- vis_brd(QWidget *, int hz_flds, int vt_flds, int pos_x, int pos_y);
+ vis_brd(QWidget *, int pos_x, int pos_y, const gm_brd &);
- void paintEvent(QPaintEvent* );
int get_hz_sz() const;
int get_vt_sz() const;
+protected:
+ virtual void mousePressEvent(QMouseEvent *);
+ virtual void mouseMoveEvent(QMouseEvent *);
+ virtual void paintEvent(QPaintEvent *);
+ virtual void leaveEvent(QEvent *);
+
private:
- void _drw_fld(int i, int j, plr_clr , bool hlght);
+ void _drw_fld(int i, int j, bool hlght);
+
+ gm_brd snpsht_;
- int hz_flds_;
- int vt_flds_;
+ int hlght_x_;
+ int hlght_y_;
};
#endif //_VISUAL_BOARD_H__
View
@@ -3,3 +3,4 @@ rm fmi-reversi.pro
qmake -project 2> log && cat build_options >> fmi-reversi.pro &&
qmake fmi-reversi.pro 2> log &&
make 2> log
+vim log
View
@@ -1,14 +1,17 @@
#include <vector>
+#include <cassert>
#include "gm_brd.h"
#include "defs.h"
#include "turn.h"
#include "player.h"
-gm_brd::gm_brd(index_t x_len, index_t y_wdt)
+gm_brd::gm_brd(index_t x_len, index_t y_len)
+ : brd_(x_len * y_len)
+ , wdth_(x_len)
{
-
+ assert(0 == pc_free);
}
void gm_brd::do_mv(move mv, plr_clr plr, gm_brd &res) const
@@ -26,3 +29,24 @@ void gm_brd::gen_all_mvs(plr_clr plr, std::vector<move>& res) const
}
+plr_clr gm_brd::get_cell(int i, int j)
+{
+ return brd_[i + j * get_wdth()];
+}
+
+void gm_brd::set_cell(int i, int j, plr_clr clr)
+{
+ brd_[i + j * get_wdth()] = clr;
+}
+
+int gm_brd::get_wdth() const
+{
+ return wdth_;
+}
+
+int gm_brd::get_hgth() const
+{
+ assert(brd_.size() % get_wdth() == 0);
+ return brd_.size() / get_wdth();
+}
+
View
@@ -20,7 +20,7 @@ int main(int argc, char *argv[])
opts opt;
new mn_bar(&window, &app, opt);
- new vis_brd(&window, 6, 8, 45, 30);
+ new vis_brd(&window, 45, 30, gm_brd(6, 8));
window.show();
return app.exec();
View
@@ -1,34 +1,40 @@
#include <cassert>
#include <QPainter>
+#include <QMouseEvent>
#include "vis_brd.h"
static const int fld_sz = 50;
static const int wdt = 2;
-vis_brd::vis_brd(QWidget *pnt, int hz_flds, int vt_flds, int pos_x, int pos_y)
+vis_brd::vis_brd(QWidget *pnt, int pos_x, int pos_y, const gm_brd &brd)
: QWidget(pnt)
- , hz_flds_(hz_flds)
- , vt_flds_(vt_flds)
+ , snpsht_(brd)
+ , hlght_x_(-1)
+ , hlght_y_(-1)
{
setGeometry(pos_x, pos_y, get_hz_sz(), get_vt_sz());
+ setMouseTracking(true);
//setPalette(QPalette(QColor(250, 250, 200)));
//setAutoFillBackground(true);
}
int vis_brd::get_hz_sz() const
{
- return fld_sz * hz_flds_ + wdt;
+ return fld_sz * snpsht_.get_wdth() + wdt;
}
int vis_brd::get_vt_sz() const
{
- return fld_sz * vt_flds_ + wdt;
+ return fld_sz * snpsht_.get_hgth() + wdt;
}
-void vis_brd::_drw_fld(int i, int j, plr_clr p, bool hlght)
+#include <cstdio>
+void vis_brd::_drw_fld(int i, int j, bool hlght)
{
- assert(i < hz_flds_ && j < vt_flds_);
+ assert(0 <= i && 0 <= j);
+ assert(i < snpsht_.get_wdth() && j < snpsht_.get_hgth());
+ plr_clr p = snpsht_.get_cell(i, j);
static const int spc = fld_sz / 8;
QPainter pnt(this);
@@ -73,13 +79,45 @@ void vis_brd::_drw_fld(int i, int j, plr_clr p, bool hlght)
pnt.drawEllipse(fr_x + spc, fr_y + spc, fld_sz - 2 * spc, fld_sz - 2 * spc);
}
-void vis_brd::paintEvent(QPaintEvent* )
+/*virtual*/
+void vis_brd::paintEvent(QPaintEvent *)
{
- for (int i = 0; i < hz_flds_; ++i)
- for (int j = 0; j < vt_flds_; ++j)
- _drw_fld(i, j, pc_free, false);
+ for (int i = 0; i < snpsht_.get_wdth(); ++i)
+ for (int j = 0; j < snpsht_.get_hgth(); ++j)
+ _drw_fld(i, j, false);
+
+ if (hlght_x_ != -1)
+ _drw_fld(hlght_x_, hlght_y_, true);
+}
+
+static int clc_idx(int x)
+{
+ return (x - (wdt / 1))/ fld_sz;
+}
+
+/*virtual*/
+void vis_brd::mousePressEvent(QMouseEvent *ev)
+{
+ const QPoint& pnt = ev->pos();
+ int x = clc_idx(pnt.x());
+ int y = clc_idx(pnt.y());
+ snpsht_.set_cell(x, y, pc_white);
+ repaint();
+}
+
+/*virtual*/
+void vis_brd::mouseMoveEvent(QMouseEvent *ev)
+{
+ const QPoint& pnt = ev->pos();
+ hlght_x_ = clc_idx(pnt.x());
+ hlght_y_ = clc_idx(pnt.y());
+ repaint();
+}
- _drw_fld(3, 2, pc_white, false);
- _drw_fld(2, 3, pc_black, true);
+/*virtual*/
+void vis_brd::leaveEvent(QEvent *)
+{
+ hlght_x_ = hlght_y_ = -1;
+ repaint();
}

0 comments on commit 78236a1

Please sign in to comment.