TDT4102-Ovinger/Oving10/Oving 10/MinesweeperWindow.cpp

73 lines
1.7 KiB
C++
Raw Normal View History

2019-03-14 14:36:03 +01:00
#include "MinesweeperWindow.h"
MinesweeperWindow::MinesweeperWindow(Point xy, int width, int height, int mines, const string& title) :
Graph_lib::Window(xy, width * cellSize, height*cellSize, title), width(width), height(height), mines(mines)
//Initialiser medlemsvariabler, bruker ogs<67> konstrukt<6B>ren til Windowsklassen
{
// Legg til alle tiles p<> vinduet
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int y = i* cellSize,
x = j * cellSize;
tiles.push_back(new Tile{ Point{x, y}, cellSize, cb_click });
attach(tiles.back());
}
}
//Legg til miner p<> tilfeldige posisjoner
// Fjern window reskalering
resizable(nullptr);
size_range(x_max(), y_max(), x_max(), y_max());
}
int MinesweeperWindow::countMines(vector<Point> points) const {
return 0;
};
vector<Point> MinesweeperWindow::adjacentPoints(Point xy) const {
vector<Point> points;
for (int di = -1; di <= 1; ++di) {
for (int dj = -1; dj <= 1; ++dj) {
if (di == 0 && dj == 0) {
continue;
}
Point neighbour{ xy.x + di * cellSize,xy.y + dj * cellSize };
if (inRange(neighbour)) {
points.push_back(neighbour);
}
}
}
return points;
}
void MinesweeperWindow::openTile(Point xy) {
}
void MinesweeperWindow::flagTile(Point xy) {
}
//Kaller opentile ved venstreklikk og flagTile ved h<>yreklikk
void MinesweeperWindow::cb_click(Address, Address pw)
{
Point xy{ Fl::event_x(),Fl::event_y() };
MouseButton mb = static_cast<MouseButton>(Fl::event_button());
auto& win = reference_to<MinesweeperWindow>(pw);
if (!win.inRange(xy)) {
return;
}
switch (mb) {
case MouseButton::left:
win.openTile(xy);
break;
case MouseButton::right:
win.flagTile(xy);
break;
}
win.flush();
}