Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGibson2 authored Apr 20, 2024
0 parents commit 8ff0a25
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 0 deletions.
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.16)
project(minesweeper LANGUAGES CXX)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

include(FetchContent)
FetchContent_Declare(SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 2.6.x)
FetchContent_MakeAvailable(SFML)

add_executable(minesweeper main.cpp
board.h
welcomeWindow.h
textureManager.h
gameWindow.h
tile.h)
target_link_libraries(minesweeper PRIVATE sfml-graphics)
target_compile_features(minesweeper PRIVATE cxx_std_17)

if(WIN32)
add_custom_command(
TARGET minesweeper
COMMENT "Copy OpenAL DLL"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:minesweeper>
VERBATIM)
endif()

install(TARGETS minesweeper)
53 changes: 53 additions & 0 deletions board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by aaron on 4/15/2024.
//
#include <iostream>
#include <fstream>
#pragma once
using namespace std;

struct board {
int colNum;
int rowNum;
int mineNum;
bool isExit;

void setcolNum(int colNum) {
this->colNum = colNum;
}
void setrowNum(int rowNum) {
this->rowNum = rowNum;
}
void setMine(int mine) {
this->mineNum = mine;
}

int getcolNum() {
return this->colNum;
}
int getrowNum() {
return this->rowNum;
}
int getMine() {
return this->mineNum;
}
};

void readBoard(board& board) {
string line;
ifstream infile("config.cfg");
getline(infile, line);
board.setcolNum(stoi(line));
getline(infile, line);
board.setrowNum(stoi(line));
getline(infile, line);
board.setMine(stoi(line));
infile.close();
}



#ifndef MINESWEEPER_BOARD_H
#define MINESWEEPER_BOARD_H

#endif //MINESWEEPER_BOARD_H
74 changes: 74 additions & 0 deletions gameWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Created by aaron on 4/18/2024.
//
#include <iostream>
#include <cctype>
#include "board.h"
#include "textureManager.h"
#include "tile.h"
#include <cstdlib>
#include <ctime>
#pragma once
using namespace std;

struct gameWindow {
gameWindow(board& board, textureManager& textureManager, bool isExit) {
if (!isExit) {
sf::RenderWindow gWindow(sf::VideoMode((board.getcolNum()*32), ((board.getrowNum()*32)+100)), "Minesweeper", sf::Style::Close);
tile t(textureManager.icons,0,0);
vector<tile> tiles;
for (int i = 0; i < board.getrowNum(); i++) {
for (int j = 0; j < board.getcolNum(); j++) {
tiles.push_back(tile(textureManager.icons, j * 32, i * 32));
}
}
srand(time(nullptr));
int numMines = 0;
while (numMines < board.getMine()) {
int randRow = rand() % board.getrowNum();
int randCol = rand() % board.getcolNum();
int index = randRow * board.getcolNum() + randCol;
if (!tiles[index].isMine) {
tiles[index].isMine = true;
numMines++;
}
}
while (gWindow.isOpen()) {
sf::Event event;
sf::Mouse mouse;
while (gWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
gWindow.close();
}
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
//t.tileClicked(mouse.getPosition(gWindow).x,mouse.getPosition(gWindow).y);
for (int i = 0; i < tiles.size(); i++) {
if (tiles[i].back.getGlobalBounds().contains(mouse.getPosition(gWindow).x,mouse.getPosition(gWindow).y) && !tiles[i].isFlagged) {
tiles[i].isRevealed = true;
}
}
}
if (event.mouseButton.button == sf::Mouse::Right) {
for (int i = 0; i < tiles.size(); i++) {
if (!tiles[i].isRevealed && tiles[i].back.getGlobalBounds().contains(mouse.getPosition(gWindow).x,mouse.getPosition(gWindow).y)) {
tiles[i].isFlagged = !tiles[i].isFlagged;
}
}
}
}
}
gWindow.clear(sf::Color::White);
for (int i = 0; i < tiles.size(); i++) {
tiles[i].draw(gWindow);
}
gWindow.display();
}
}
}
};

#ifndef MINESWEEPER_GAMEWINDOW_H
#define MINESWEEPER_GAMEWINDOW_H

#endif //MINESWEEPER_GAMEWINDOW_H
18 changes: 18 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#include "welcomeWindow.h"
#include "gameWindow.h"
#include "board.h"
using namespace std;

int main() {
board board;
readBoard(board);
welcomeWindow window(board);
textureManager textureManager;
gameWindow gWindow(board, textureManager, board.isExit);


//sf::RenderWindow leaderboardWindow(sf::VideoMode((board.getcolNum())*16, (board.getrowNum()*16)+50), "Minesweeper", sf::Style::Close);
return 0;
}
61 changes: 61 additions & 0 deletions textureManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Created by aaron on 4/18/2024.
//
#include <iostream>
#include <map>
using namespace std;
#include <SFML/Graphics.hpp>
#pragma once

struct textureManager {
map<string, sf::Texture> icons;
textureManager() {
sf::Texture temp;
temp.loadFromFile("images/debug.png");
icons.emplace("debug",temp);
temp.loadFromFile("images/digits.png");
icons.emplace("digits", temp);
temp.loadFromFile("images/face_happy.png");
icons.emplace("happy",temp);
temp.loadFromFile("images/face_lose.png");
icons.emplace("lose",temp);
temp.loadFromFile("images/face_win.png");
icons.emplace("win", temp);
temp.loadFromFile("images/flag.png");
icons.emplace("flag",temp);
temp.loadFromFile("images/leaderboard.png");
icons.emplace("leaderboard",temp);
temp.loadFromFile("images/mine.png");
icons.emplace("mine",temp);
temp.loadFromFile("images/number_1.png");
icons.emplace("number_1",temp);
temp.loadFromFile("images/number_2.png");
icons.emplace("number_2",temp);
temp.loadFromFile("images/number_3.png");
icons.emplace("number_3",temp);
temp.loadFromFile("images/number_4.png");
icons.emplace("number_4",temp);
temp.loadFromFile("images/number_5.png");
icons.emplace("number_5",temp);
temp.loadFromFile("images/number_6.png");
icons.emplace("number_6",temp);
temp.loadFromFile("images/number_7.png");
icons.emplace("number_7",temp);
temp.loadFromFile("images/number_8.png");
icons.emplace("number_8",temp);
temp.loadFromFile("images/pause.png");
icons.emplace("pause",temp);
temp.loadFromFile("images/play.png");
icons.emplace("play",temp);
temp.loadFromFile("images/tile_hidden.png");
icons.emplace("tile_hidden",temp);
temp.loadFromFile("images/tile_revealed.png");
icons.emplace("tile_revealed",temp);
}
};


#ifndef MINESWEEPER_TEXTUREMANAGER_H
#define MINESWEEPER_TEXTUREMANAGER_H

#endif //MINESWEEPER_TEXTUREMANAGER_H
70 changes: 70 additions & 0 deletions tile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Created by aaron on 4/18/2024.
//

#include <iostream>
#include <cctype>
#include "board.h"
#include "textureManager.h"
#pragma once
using namespace std;

struct tile {
bool isRevealed;
bool isFlagged;
bool isMine;
vector<tile*> neighbors;
sf::Sprite back;
sf::Sprite front;
sf::Sprite icon;
sf::Sprite flag;

map<string,sf::Texture> textures;

tile(map<string,sf::Texture>& textures, float xpos, float ypos) {
back.setPosition(xpos, ypos);
front.setPosition(xpos, ypos);
icon.setPosition(xpos,ypos);
flag.setPosition(xpos,ypos);

back.setTexture(textures["tile_revealed"]);
front.setTexture(textures["tile_hidden"]);
flag.setTexture(textures["flag"]);

//flag.setColor(sf::Color(255,255,255,0));
isRevealed = false;
isFlagged = false;
isMine = false;
this->textures = textures;
}

// bool tileClicked(float positionx, float positiony) {
// if(back.getGlobalBounds().contains(positionx,positiony)) {
// cout << isRevealed << "The tile has been revealed" << endl;
// this->isRevealed = true;
// return true;
// }
// return false;
// }

void draw(sf::RenderWindow& window) {
if (!isRevealed) {
window.draw(front);
}
else if (isRevealed){
window.draw(back);
}
if (isFlagged) {
window.draw(flag);
}
if (isMine && isRevealed) {
icon.setTexture(textures["mine"]);
window.draw(icon);
}
}
};

#ifndef MINESWEEPER_TILE_H
#define MINESWEEPER_TILE_H

#endif //MINESWEEPER_TILE_H
Loading

0 comments on commit 8ff0a25

Please sign in to comment.