Skip to content

Commit

Permalink
Another bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
kentuckyfriedcock committed Nov 25, 2018
1 parent 6b24f06 commit 9707fc9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
Binary file modified build/minesweeper
Binary file not shown.
3 changes: 2 additions & 1 deletion logic.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "logic.h"
#include "time.h"
#include <iostream>

void GameLogic::newField(unsigned xpos, unsigned ypos) {
remainingMines = numberOfMines;
srand(time(NULL));
for(int i = 0; i < numberOfMines; i++) {
unsigned x = rand() % fieldWidth;
Expand Down Expand Up @@ -82,7 +84,6 @@ int GameLogic::open(unsigned x, unsigned y) {
return 0;

recOpen(x, y);
//field[ax + ay * fieldWidth].isOpen = true;

return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion logic.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

constexpr unsigned fieldWidth = 16;
constexpr unsigned fieldHeight = 16;
constexpr unsigned numberOfMines = (fieldWidth*fieldHeight) / 32;
constexpr unsigned numberOfMines = (fieldWidth*fieldHeight) / 16;

struct Cell {
bool isOpen = false;
Expand Down
31 changes: 22 additions & 9 deletions renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <unistd.h>

Renderer::Renderer() {
window = new sf::RenderWindow(sf::VideoMode(windowWidth, windowHeight), "Minesweeper");
window = new sf::RenderWindow(sf::VideoMode(windowWidth, windowHeight), "Minesweeper", sf::Style::Titlebar | sf::Style::Close);
window->setFramerateLimit(25);
for(int i = 0; i < 13; i++) {
textures[i].loadFromFile("contents/cells.png", sf::IntRect(i*16, 0, 16, 16));
Expand All @@ -17,6 +17,11 @@ void Renderer::mainLoop() {
if(event.type == sf::Event::Closed) {
window->close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
logic.isInMainMenu = true;
logic.isFirstTurn = true;
logic.isLoseOrWin = false;
}
if (event.type == sf::Event::MouseButtonPressed) {
mouseClick(event);
}
Expand Down Expand Up @@ -99,10 +104,12 @@ void Renderer::mouseClick(const sf::Event& event) {
const unsigned cellX = x / columnWidth;
const unsigned cellY = y / rowHeight;



if(logic.isLoseOrWin) {
logic.isLoseOrWin = false;
logic.isInMainMenu = true;
logic.isFirstTurn = true;
logic.isLoseOrWin = false;
return;
}

Expand All @@ -122,18 +129,24 @@ void Renderer::mouseClick(const sf::Event& event) {
logic.newField(cellX, cellY);
logic.open(cellX, cellY);

} else if (event.mouseButton.button == sf::Mouse::Right) {

logic.mark(cellX, cellY);

} else if (event.mouseButton.button == sf::Mouse::Left
&& !logic.getCell(cellX, cellY).isOpen
&& !logic.getCell(cellX, cellY).isFlagged
&& !logic.getCell(cellX, cellY).isSuspect
&& !logic.isFirstTurn) {

int res = logic.open(cellX, cellY);
if(res == 0 || logic.remainingMines == 0) {
if(res == 0) {
logic.isLoseOrWin = true;
}
}
}
if(logic.remainingMines == 0) {
logic.isLoseOrWin = true;
}

} else if (event.mouseButton.button == sf::Mouse::Right) {
logic.mark(cellX, cellY);
if(logic.remainingMines == 0) {
logic.isLoseOrWin = true;
}
}
}

0 comments on commit 9707fc9

Please sign in to comment.