summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-06-14 21:57:47 +0930
committerDaniel Jones <admin@danieljon.es>2020-06-14 21:57:47 +0930
commit3a41ff521bd1e95a85bf93ac77d69108a1d9c0c5 (patch)
treeb018937e0e6b768db1f84704efda9f1004309d5a
parent971db58ef48db64151a31d4857104ede69475996 (diff)
downloadfoxminesweeper-3a41ff521bd1e95a85bf93ac77d69108a1d9c0c5.tar.gz
foxminesweeper-3a41ff521bd1e95a85bf93ac77d69108a1d9c0c5.zip
work on getting new game working
new games work only if the width and height are the same values
-rw-r--r--Board.cpp70
-rw-r--r--Board.h5
-rw-r--r--MainWindow.cpp47
-rw-r--r--MainWindow.h3
-rw-r--r--Tile.cpp3
-rw-r--r--Tile.h2
6 files changed, 87 insertions, 43 deletions
diff --git a/Board.cpp b/Board.cpp
index f6eb593..6fe3c71 100644
--- a/Board.cpp
+++ b/Board.cpp
@@ -15,8 +15,10 @@
#include "Board.h"
-Board:: Board(int width, int height, int minecount)
+//Board:: Board(int width, int height, int minecount)
+Board:: Board()
{
+ /*
this->width = width;
this->height = height;
this->tilecount = width*height;
@@ -24,7 +26,7 @@ Board:: Board(int width, int height, int minecount)
this->game_running = true;
this->game_won = false;
- /* create tiles */
+ // create tiles
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
@@ -35,21 +37,23 @@ Board:: Board(int width, int height, int minecount)
}
generate_mines();
retrieve_neighbors();
+ */
}
Board::~Board()
{
+ tiles.clear();
}
Tile *Board::get_tile_at(int x, int y)
{
- return tiles.at(this->width*x+y).get();
+ return tiles.at(this->height*x+y).get();
}
+
bool
Board::reveal_tile_at(int x, int y)
{
Tile *tile = get_tile_at(x, y);
- printf("revealing x: %d y: %d\n", tile->get_x(), tile->get_y());
if (tile->is_mine())
{
/* lose */
@@ -72,17 +76,36 @@ Board::reveal_tile_at(int x, int y)
}
void
-Board::new_game(int x, int y)
+Board::new_game(int width, int height, int minecount)
{
+ this->width = width;
+ this->height = height;
+ this->tilecount = width*height;
+ this->minecount = minecount;
+ this->game_running = true;
+ this->game_won = false;
+ tiles.clear();
+ /* =reate tiles */
+ for (int x = 0; x < height; x++)
+ {
+ for (int y = 0; y < width; y++)
+ {
+ std::unique_ptr<Tile> t(new Tile(x, y));
+ tiles.push_back(std::move(t));
+ }
+ }
+ generate_mines();
+ retrieve_neighbors();
+
}
void
Board::reveal_all_mines()
{
Tile *tile;
- for(auto t = tiles.begin(); t != tiles.end(); ++t)
+ for(auto& t: tiles)
{
- tile = (*t).get();
+ tile = t.get();
/* explicitly unflag tile */
tile->clear_flag(Tile::FLAGGED); // FIXME: doesnt work as intended, it should unflag all tiles
if (tile->is_mine())
@@ -96,26 +119,29 @@ void
Board::retrieve_neighbors()
{
Tile *tile;
- for(auto t = tiles.begin(); t != tiles.end(); ++t)
+ int x, y;
+ for (auto& t: tiles)
{
- tile = (*t).get();
+ tile = t.get();
+ x = t->get_x();
+ y = t->get_y();
int badup = 0, baddown = 0, badleft = 0, badright = 0;
- if (tile->get_x()-1<0) badleft = 1;
- if (tile->get_x()+1>width-1) badright = 1;
+ if (x-1<0) badleft = 1;
+ if (x+1>width-1) badright = 1;
- if (tile->get_y()-1<0) badup = 1;
- if (tile->get_y()+1>height-1) baddown = 1;
+ if (y-1<0) badup = 1;
+ if (y+1>height-1) baddown = 1;
- if (!badleft && !badup) tile->neighbors[0] = get_tile_at(tile->get_x()-1, tile->get_y()-1);
- if (!badup) tile->neighbors[1] = get_tile_at(tile->get_x(), tile->get_y()-1);
- if (!badright && !badup) tile->neighbors[2] = get_tile_at(tile->get_x()+1, tile->get_y()-1);
+ if (!badleft && !badup) tile->neighbors[0] = get_tile_at(x-1, y-1);
+ if (!badup) tile->neighbors[1] = get_tile_at(x, y-1);
+ if (!badright && !badup) tile->neighbors[2] = get_tile_at(x+1, y-1);
- if (!badleft) tile->neighbors[3] = get_tile_at(tile->get_x()-1, tile->get_y());
- if (!badright) tile->neighbors[4] = get_tile_at(tile->get_x()+1, tile->get_y());
+ if (!badleft) tile->neighbors[3] = get_tile_at(x-1, y);
+ if (!badright) tile->neighbors[4] = get_tile_at(x+1, y);
- if (!badleft && !baddown) tile->neighbors[5] = get_tile_at(tile->get_x()-1, tile->get_y()+1);
- if (!baddown) tile->neighbors[6] = get_tile_at(tile->get_x(), tile->get_y()+1);
- if (!badright && !baddown) tile->neighbors[7] = get_tile_at(tile->get_x()+1, tile->get_y()+1);
+ if (!badleft && !baddown) tile->neighbors[5] = get_tile_at(x-1, y+1);
+ if (!baddown) tile->neighbors[6] = get_tile_at(x, y+1);
+ if (!badright && !baddown) tile->neighbors[7] = get_tile_at(x+1, y+1);
count_neighbor_mines(tile);
}
}
@@ -137,7 +163,7 @@ Board::count_neighbor_mines(Tile *tile)
}
}
tile->set_neighbor_mine_count(mines);
- printf("init tile: x: %d, y: %d, neighbor mines: %d\n", tile->get_x(), tile->get_y(), tile->get_neighbor_mine_count());
+ //printf("init tile: x: %d, y: %d, neighbor mines: %d\n", tile->get_x(), tile->get_y(), tile->get_neighbor_mine_count());
}
void
diff --git a/Board.h b/Board.h
index 50e9fa4..e2a9da0 100644
--- a/Board.h
+++ b/Board.h
@@ -25,12 +25,13 @@
class Board
{
public:
- Board(int width, int height, int minecount);
+ //Board(int width, int height, int minecount);
+ Board();
~Board();
int get_tile_count(){ return tilecount; };
Tile *get_tile_at(int x, int y);
bool reveal_tile_at(int x, int y);
- void new_game(int x, int y);
+ void new_game(int width, int height, int minecount);
bool is_game_running() { return this->game_running; };
bool is_game_won() { return this->game_won; };
bool check_win();
diff --git a/MainWindow.cpp b/MainWindow.cpp
index d800d33..a27890f 100644
--- a/MainWindow.cpp
+++ b/MainWindow.cpp
@@ -33,15 +33,16 @@ FXIMPLEMENT(MainWindow, FXMainWindow, MainWindow_Map, ARRAYNUMBER(MainWindow_Map
MainWindow::MainWindow(FXApp *a)
: FXMainWindow(a, "foxminesweeper", nullptr, nullptr, DECOR_ALL, 0,0, 500, 500)
{
- board = nullptr;
+ board = new Board();
matrix = nullptr;
app = a;
create_ui();
- int width = 9;
- int height = 9;
+ int w = 9;
+ int h = 9;
int minecount = 10;
+ game_count = 0;
game_over = false;
- new_game(width, height, minecount);
+ new_game(w, h, minecount);
}
MainWindow::~MainWindow()
@@ -134,25 +135,30 @@ MainWindow::create_ui()
}
void
-MainWindow::new_game(int width, int height, int minecount)
+MainWindow::new_game(int w, int h, int minecount)
{
+ tile_buttons.clear();
+ game_count++;
seconds = 0;
ticking = false;
- delete board;
+ //delete board;
delete matrix;
- board = nullptr;
- matrix = nullptr;
puts("starting new game..");
- int tilecount = width*height;
- board = new Board(width, height, minecount);
- matrix = new FXMatrix(scroll_area, width, MATRIX_BY_COLUMNS|LAYOUT_CENTER_Y|LAYOUT_CENTER_X);
+ int tilecount = w*h;
+ printf("tile count will be %d\n", tilecount);
+ //board = new Board(width, height, minecount);
+ board->new_game(w, h, minecount);
+ matrix = new FXMatrix(scroll_area, w, MATRIX_BY_COLUMNS|LAYOUT_CENTER_Y|LAYOUT_CENTER_X);
+ if (game_count > 1)
+ matrix->create();
for (int i = 0; i < tilecount; i++)
{
std::unique_ptr<FXButton> b(new FXButton(matrix, "", nullptr, this, UI_Tile));
+ b->setIcon(empty_icon);
+ if (game_count > 1)
+ b->create();
tile_buttons.push_back(std::move(b));
- tile_buttons.at(i)->setIcon(empty_icon);
}
- printf("matrix thinks rows: %d columns: %d\n", matrix->getNumRows(), matrix->getNumColumns());
draw_buttons();
}
@@ -167,6 +173,8 @@ MainWindow::draw_buttons()
button = (*b).get();
x = matrix->colOfChild(button);
y = matrix->rowOfChild(button);
+ if (x < 0 || y < 0)
+ return;
tile = board->get_tile_at(x, y);
if (tile->is_flagged())
{
@@ -226,18 +234,26 @@ MainWindow::draw_buttons()
long
MainWindow::on_Tile_Click(FXObject *sender, FXSelector sel, void *data)
{
+ puts("click");
if (!ticking)
{
app->addTimeout(this, UI_Timer_Tick, 1000);
ticking = true;
}
if (game_over == true)
+ {
+ puts("game is over");
return 1;
+ }
int x = 0, y = 0;
Tile *tile;
FXButton *button = dynamic_cast<FXButton*>(sender);
if (!button)
- return 0;
+ {
+ printf("button is null");
+ return 1;
+ }
+ puts("running click methods");
button->killFocus(); // let user control with keyboard?
x = matrix->colOfChild(button);
y = matrix->rowOfChild(button);
@@ -329,6 +345,9 @@ MainWindow::on_New_Click(FXObject *sender, FXSelector sel, void *data)
}
printf("new game width = %d height = %d mines = %d\n", w, h, m);
+ game_over = false;
+ time_label->setText("0");
+ app->removeTimeout(this, UI_Timer_Tick);
new_game(w, h, m);
}
return 1;
diff --git a/MainWindow.h b/MainWindow.h
index 38b8bd6..50d7493 100644
--- a/MainWindow.h
+++ b/MainWindow.h
@@ -55,11 +55,12 @@ class MainWindow : public FXMainWindow
private:
void create_ui();
- void new_game(int width, int height, int minecount);
+ void new_game(int w, int h, int minecount);
void draw_buttons();
bool game_over;
long unsigned int seconds;
bool ticking;
+ int game_count;
FXHorizontalFrame *contents; // Content frame
FXVerticalFrame *canvasFrame; // Canvas frame
FXVerticalFrame *buttonFrame; // Button frame
diff --git a/Tile.cpp b/Tile.cpp
index dfad786..6a86eca 100644
--- a/Tile.cpp
+++ b/Tile.cpp
@@ -21,9 +21,6 @@ Tile::Tile(int x, int y)
this->y = y;
this->neighbor_mine_count = 0;
//
- // better way?
- for (int x = 0; x < 8; x++)
- neighbors[x] = 0;
this->flags = HIDDEN; /* make sure we init the flags */
}
diff --git a/Tile.h b/Tile.h
index 371e121..d151ae7 100644
--- a/Tile.h
+++ b/Tile.h
@@ -44,7 +44,7 @@ class Tile
void set_neighbor_mine_count(int count) { this->neighbor_mine_count = count; };
Tile *get_neighbor(int i);
- Tile *neighbors[8]; // one day make private with getters/setters
+ Tile *neighbors[8] = {0}; // one day make private with getters/setters
private:
void set_flags(enum STATE flags) { this->flags = flags; };
int x;