summaryrefslogtreecommitdiff
path: root/MainWindow.cpp
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-06-12 00:03:37 +0930
committerDaniel Jones <admin@danieljon.es>2020-06-12 00:03:37 +0930
commit6739115f07b39da3ed59572faf393f9846bd4784 (patch)
treefef79a608906f46267b0e7e498cc81e058579aa8 /MainWindow.cpp
parent49d97e71dae1955317a316cea16f9987585ef256 (diff)
downloadfoxminesweeper-6739115f07b39da3ed59572faf393f9846bd4784.tar.gz
foxminesweeper-6739115f07b39da3ed59572faf393f9846bd4784.zip
basic game working
Diffstat (limited to 'MainWindow.cpp')
-rw-r--r--MainWindow.cpp151
1 files changed, 129 insertions, 22 deletions
diff --git a/MainWindow.cpp b/MainWindow.cpp
index 5e3f300..bdb0a84 100644
--- a/MainWindow.cpp
+++ b/MainWindow.cpp
@@ -14,6 +14,7 @@
*/
#include "MainWindow.h"
+#include "fxdefs.h"
#include <cstdlib> // rand
#include <memory>
#include <stdio.h>
@@ -23,19 +24,22 @@ FXDEFMAP(MainWindow) MainWindow_Map[]=
//________Message_Type____________ID____________Message_Handler_______
FXMAPFUNC(SEL_COMMAND, MainWindow::UI_Tile, MainWindow::on_Tile_Click),
FXMAPFUNC(SEL_COMMAND, MainWindow::UI_New, MainWindow::on_New_Click),
+ FXMAPFUNC(SEL_RIGHTBUTTONPRESS, MainWindow::UI_Tile, MainWindow::on_Tile_Right_Click),
};
FXIMPLEMENT(MainWindow, FXMainWindow, MainWindow_Map, ARRAYNUMBER(MainWindow_Map))
MainWindow::MainWindow(FXApp *a)
- : FXMainWindow(a, "MyProgram 0.1", nullptr, nullptr, DECOR_ALL, 0,0,200,150)
+ : FXMainWindow(a, "foxminesweeper", nullptr, nullptr, DECOR_ALL, 0,0,200,150)
{
board = nullptr;
matrix = nullptr;
app = a;
create_ui();
- int width = 9;
- int height = 9;
- new_game(width, height, 10);
+ int width = 15;
+ int height = 15;
+ int minecount = 33;
+ game_over = false;
+ new_game(width, height, minecount);
}
MainWindow::~MainWindow()
@@ -47,6 +51,17 @@ void
MainWindow::create()
{
FXMainWindow::create();
+ bomb_icon->create();
+ flag_icon->create();
+ empty_icon->create();
+ tile_1_icon->create();
+ tile_2_icon->create();
+ tile_3_icon->create();
+ tile_4_icon->create();
+ tile_5_icon->create();
+ tile_6_icon->create();
+ tile_7_icon->create();
+ tile_8_icon->create();
show(PLACEMENT_SCREEN);
}
@@ -73,14 +88,13 @@ MainWindow::create_ui()
// Horizontal divider line
new FXHorizontalSeparator(buttonFrame, SEPARATOR_RIDGE|LAYOUT_FILL_X);
- FXText *gametxt = new FXText(buttonFrame);
- gametxt->setText("test");
// Exit button
new FXButton(buttonFrame, "&Exit", nullptr, app, FXApp::ID_QUIT, BUTTON_NORMAL|LAYOUT_FILL_X);
new FXButton(buttonFrame, "&New Game", nullptr, this, MainWindow::UI_New, BUTTON_NORMAL|LAYOUT_FILL_X);
/* create icons */
- bomb_icon = new FXGIFIcon(app, tile_1, IMAGE_KEEP);
+ bomb_icon = new FXGIFIcon(app, bomb, IMAGE_KEEP);
+ flag_icon = new FXGIFIcon(app, flag, IMAGE_KEEP);
empty_icon = new FXGIFIcon(app, empty, IMAGE_KEEP);
tile_1_icon = new FXGIFIcon(app, tile_1, IMAGE_KEEP);
tile_2_icon = new FXGIFIcon(app, tile_2, IMAGE_KEEP);
@@ -90,16 +104,6 @@ MainWindow::create_ui()
tile_6_icon = new FXGIFIcon(app, tile_6, IMAGE_KEEP);
tile_7_icon = new FXGIFIcon(app, tile_7, IMAGE_KEEP);
tile_8_icon = new FXGIFIcon(app, tile_8, IMAGE_KEEP);
- bomb_icon->create();
- empty_icon->create();
- tile_1_icon->create();
- tile_2_icon->create();
- tile_3_icon->create();
- tile_4_icon->create();
- tile_5_icon->create();
- tile_6_icon->create();
- tile_7_icon->create();
- tile_8_icon->create();
}
void
@@ -110,13 +114,13 @@ MainWindow::new_game(int width, int height, int minecount)
puts("starting new game..");
int tilecount = width*height;
tile_buttons.clear();
- board = new Board(width, height);
+ board = new Board(width, height, minecount);
matrix = new FXMatrix(scroll_area, width, MATRIX_BY_COLUMNS|LAYOUT_CENTER_Y|LAYOUT_CENTER_X);
for (int i = 0; i < tilecount; i++)
{
- std::shared_ptr<FXButton> b(new FXButton(matrix, "ok", nullptr, this, UI_Tile));
+ std::shared_ptr<FXButton> b(new FXButton(matrix, "", nullptr, this, UI_Tile));
tile_buttons.push_back(b);
- b->setIcon(tile_1_icon);
+ b->setIcon(empty_icon);
}
printf("rows: %d columns: %d\n", matrix->getNumRows(), matrix->getNumColumns());
draw_buttons();
@@ -125,21 +129,124 @@ MainWindow::new_game(int width, int height, int minecount)
void
MainWindow::draw_buttons()
{
-
+ int x, y;
+ FXButton *button;
+ Tile *tile;
+ for(auto b = tile_buttons.begin(); b != tile_buttons.end(); ++b)
+ {
+ button = (*b).get();
+ x = matrix->colOfChild(button);
+ y = matrix->rowOfChild(button);
+ tile = board->get_tile_at(x, y);
+ if (tile->is_flagged())
+ {
+ printf("flagged at: %d, %d\n", tile->get_x(), tile->get_y());
+ button->setIcon(flag_icon);
+ button->setFrameStyle(0);
+ continue;
+ }
+ else if (!tile->is_revealed())
+ continue;
+ else if (tile->is_mine())
+ {
+ button->setIcon(bomb_icon);
+ button->setFrameStyle(0);
+ }
+ else
+ {
+ switch (tile->get_neighbor_mine_count())
+ {
+ case 1:
+ button->setIcon(tile_1_icon);
+ break;
+ case 2:
+ button->setIcon(tile_2_icon);
+ break;
+ case 3:
+ button->setIcon(tile_3_icon);
+ break;
+ case 4:
+ button->setIcon(tile_4_icon);
+ break;
+ case 5:
+ button->setIcon(tile_5_icon);
+ break;
+ case 6:
+ button->setIcon(tile_6_icon);
+ break;
+ case 7:
+ button->setIcon(tile_7_icon);
+ break;
+ case 8:
+ button->setIcon(tile_8_icon);
+ break;
+ default:
+ button->setIcon(empty_icon);
+ break;
+ }
+ button->setFrameStyle(0);
+ }
+ }
}
long
MainWindow::on_Tile_Click(FXObject *sender, FXSelector sel, void *data)
{
+ if (game_over == true)
+ return 1;
int x = 0, y = 0;
+ Tile *tile;
FXButton *button = dynamic_cast<FXButton*>(sender);
if (!button)
return 0;
-
+// button->killFocus(); // let user control with keyboard
x = matrix->colOfChild(button);
y = matrix->rowOfChild(button);
+ tile = board->get_tile_at(x, y);
+ if (tile->is_revealed())
+ return 1;
+ if (tile->is_flagged())
+ return 1;
printf("button pressed: x=%d, y=%d\n", x, y);
board->reveal_tile_at(x, y);
+ if (!board->is_game_running() && !board->is_game_won())
+ {
+ /* lost */
+ puts("you lose the game");
+ FXMessageBox *msgbox = new FXMessageBox(app, "Game Over", "You lost", nullptr, FX::MBOX_OK);
+ msgbox->create();
+ msgbox->show();
+ game_over = true;
+ }
+ else if (!board->is_game_running() && board->is_game_won())
+ {
+ /* won */
+ puts("you won the game");
+ FXMessageBox *msgbox = new FXMessageBox(app, "Game Over", "You win", nullptr, FX::MBOX_OK);
+ msgbox->create();
+ msgbox->show();
+ game_over = true;
+ }
+ button->setFrameStyle(0);
+ draw_buttons();
+ return 1;
+}
+
+long
+MainWindow::on_Tile_Right_Click(FXObject *sender, FXSelector sel, void *data)
+{
+ if (game_over == true)
+ return 1;
+ int x = 0, y = 0;
+ Tile *tile;
+ FXButton *button = dynamic_cast<FXButton*>(sender);
+ if (!button)
+ return 0;
+ x = matrix->colOfChild(button);
+ y = matrix->rowOfChild(button);
+ tile = board->get_tile_at(x, y);
+ printf("right click on: %d, %d\n", tile->get_x(), tile->get_y());
+ tile->toggle_flag(Tile::FLAGGED);
draw_buttons();
return 1;
}