Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DrShahinstein committed Jul 20, 2024
0 parents commit a5baaf2
Show file tree
Hide file tree
Showing 19 changed files with 791 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.cache
build
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.14)

project(minesweeper LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets)

option(RELEASE_BUILD "Enable Release build" OFF)
if(RELEASE_BUILD)
set(CMAKE_BUILD_TYPE Release)
endif()

file(GLOB SOURCES "src/*.cpp")
file(GLOB HEADERS "include/*.hpp" "include/*.h")

qt6_wrap_cpp(MOC_SOURCES ${HEADERS})

if (WIN32)
add_executable(minesweeper WIN32 ${SOURCES} ${MOC_SOURCES})
else()
add_executable(minesweeper ${SOURCES} ${MOC_SOURCES})
endif()

target_link_libraries(minesweeper PRIVATE Qt6::Widgets)
target_include_directories(minesweeper PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_definitions(minesweeper PRIVATE
$<$<CONFIG:Release>:RELEASE_BUILD>
)
Binary file added assets/bomb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/flag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/misplacedbomb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/redbomb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions include/cell.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef CELL_HPP
#define CELL_HPP

#include "workingdir.h"
#include <QColor>
#include <QMouseEvent>
#include <QPixmap>
#include <QPushButton>

#define STRAIGHTBOMB_IMG_PATH WDIR "assets/bomb.png"
#define MISPLACED_BOMB_IMG_PATH WDIR "assets/misplacedbomb.png"
#define REDBOMB_IMG_PATH WDIR "assets/redbomb.png"
#define FLAG_IMG_PATH WDIR "assets/flag.png"

#define BLACK_RGB QColor(20, 20, 20)
#define BLUE_RGB QColor(0, 0, 205)
#define GREEN_RGB QColor(0, 128, 0)
#define LIGHTRED_RGB QColor(237, 28, 36)
#define DARKBLUE_RGB QColor(25, 80, 232)
#define DARKRED_RGB QColor(216, 0, 0)
#define AQUA_RGB QColor(7, 147, 131)
#define PURPLE_RGB QColor(149, 3, 168)
#define YELLOWISH_RGB QColor(206, 140, 8)
#define AMETHYST_RGB QColor(191, 50, 242)

enum FontColor {
BLACK,
BLUE,
GREEN,
LIGHTRED,
DARKBLUE,
DARKRED,
AQUA,
PURPLE,
YELLOWISH,
AMETHYST,
};

class CellBtn : public QPushButton {
Q_OBJECT

public:
explicit CellBtn(QWidget *parent = nullptr);

void set_font(FontColor font_color = BLACK);
void set_icon(QString icon_path);
void remove_icon();
void disable();
void flag();
void unflag();
bool is_flagged();

private:
QFont cell_font;
QPixmap pixmap;
bool flagged;

private slots:
void mousePressEvent(QMouseEvent *e);

signals:
void leftClicked();
void rightClicked();
};

#endif // CELL_HPP
54 changes: 54 additions & 0 deletions include/game.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef GAME_HPP
#define GAME_HPP

#define WINDOW_TITLE "Minesweeper"
#define CELL_SIZE 30

#include "cell.hpp"
#include "gamebar.hpp"
#include <QDialog>
#include <QGridLayout>
#include <QPushButton>
#include <QSet>
#include <QVector>

class GameWindow : public QDialog {
Q_OBJECT

public:
explicit GameWindow(int rows, int cols, int mines);

private:
int rows;
int cols;
int mines;
bool is_first_reveal;
GameBar *gamebar;

QGridLayout *grid_layout;
QVBoxLayout *main_layout;
QVector<QVector<CellBtn *>> grid_buttons;
QVector<QVector<bool>> mine_map;
QSet<QPair<int, int>> opening_cells;
QTimer *timer;
int elapsed_time;

bool get_first_reveal();
void set_first_reveal(bool boolean);
void create_board();
void make_opening(int start_row, int start_col);
void set_mines();
void reveal_cell(int row, int col);
void put_flag(int row, int col);
int count_adjacent_mines(int row, int col);

private slots:
void update_timer();
void reset_game();

signals:
void gameReset();
void gameClosed();
};

#endif // GAME_HPP
30 changes: 30 additions & 0 deletions include/gamebar.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef GAMEBAR_HPP
#define GAMEBAR_HPP

#include <QHBoxLayout>
#include <QLCDNumber>
#include <QPushButton>
#include <QWidget>

class GameBar : public QWidget {
Q_OBJECT

public:
explicit GameBar(int mines, QWidget *parent = nullptr);

void update_timer(int seconds);
void update_mine_count(int mines);

private:
QLCDNumber *timer_display;
QLCDNumber *mine_count_display;
QPushButton *reset_button;

signals:
void reset_game();

private slots:
void handle_reset_button();
};

#endif // GAMEBAR_HPP
42 changes: 42 additions & 0 deletions include/mainmenu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef MAINMENU_HPP
#define MAINMENU_HPP

#include "game.hpp"
#include <QDialog>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>

#define MENU_WINDOW_TITLE "Select Game Variant"
#define MENU_FIXED_WIDTH 900
#define MENU_FIXED_HEIGHT 600

enum GameVariants {
_16x16_40,
_16x16_50,
_16x16_60,
_20x20_75,
_20x20_90,
_20x20_120,
_24x24_140,
_24x24_160,
_24x24_180,
};

class MainMenu : public QDialog {
Q_OBJECT

public:
explicit MainMenu(QWidget *parent = nullptr);
GameVariants get_variant();

private:
void setup_layout();
void select_variant();

GameVariants variant;
GameWindow *game_window;
};

#endif // MAINMENU_HPP
7 changes: 7 additions & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef UTILS_HPP

namespace utils {
int rng(int start, int end);
}

#endif // UTILS_HPP
10 changes: 10 additions & 0 deletions include/workingdir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef WORKINGDIR
#define WORKINGDIR

#ifdef RELEASE_BUILD
#define WDIR "./"
#else
#define WDIR "../"
#endif

#endif // WORKINGDIR
19 changes: 19 additions & 0 deletions run
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

if [[ "$1" == "--release-build" ]]; then
cmake_options="-DRELEASE_BUILD=ON"
else
cmake_options=""
fi

mkdir -p "build"
cd "build"

if [[ "$OS" == "Windows_NT" ]]; then
cmake -G "MinGW Makefiles" $cmake_options ..
else
cmake $cmake_options ..
fi

make
./minesweeper
74 changes: 74 additions & 0 deletions src/cell.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "cell.hpp"

CellBtn::CellBtn(QWidget *parent) : QPushButton(parent) {
setFocusPolicy(Qt::NoFocus);
};

void CellBtn::set_font(FontColor font_color) {
cell_font.setPointSize(13);
cell_font.setBold(true);
QPalette palette = this->palette();

switch (font_color) {
case BLUE:
palette.setColor(QPalette::ButtonText, BLUE_RGB);
break;
case GREEN:
palette.setColor(QPalette::ButtonText, GREEN_RGB);
break;
case LIGHTRED:
palette.setColor(QPalette::ButtonText, LIGHTRED_RGB);
break;
case DARKBLUE:
palette.setColor(QPalette::ButtonText, DARKBLUE_RGB);
break;
case DARKRED:
palette.setColor(QPalette::ButtonText, DARKRED_RGB);
break;
case AQUA:
palette.setColor(QPalette::ButtonText, AQUA_RGB);
break;
case PURPLE:
palette.setColor(QPalette::ButtonText, PURPLE_RGB);
break;
case YELLOWISH:
palette.setColor(QPalette::ButtonText, YELLOWISH_RGB);
break;
case AMETHYST:
palette.setColor(QPalette::ButtonText, AMETHYST_RGB);
break;
default:
palette.setColor(QPalette::ButtonText, Qt::black);
cell_font.setBold(false);
cell_font.setPointSize(11);
break;
}

setFont(cell_font);
setPalette(palette);
}

void CellBtn::remove_icon() { setIcon(QIcon()); }
void CellBtn::set_icon(QString icon_path) {
pixmap = QPixmap(icon_path);
setIcon(pixmap);
setIconSize(size());
}

void CellBtn::disable() {
disconnect(this, &CellBtn::leftClicked, nullptr, nullptr);
disconnect(this, &CellBtn::rightClicked, nullptr, nullptr);
}

bool CellBtn::is_flagged() { return flagged; }
void CellBtn::flag() { flagged = true; }
void CellBtn::unflag() { flagged = false; }

void CellBtn::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::RightButton) {
emit rightClicked();
} else if (event->button() == Qt::LeftButton) {
emit leftClicked();
}
QPushButton::mouseReleaseEvent(event);
}
Loading

0 comments on commit a5baaf2

Please sign in to comment.