Ending and Restarting Games

Adding a Hint Feature

Is it possible to add a "hint" feature that reveals a safe cell?

Abstract art representing computer programming

Absolutely! Adding a hint feature to reveal a safe cell is a great way to enhance the gameplay experience, especially for beginners or when players are stuck. Let's implement this feature step by step:

1. Modifying the MinesweeperGrid Class

First, we'll add a method to our MinesweeperGrid class to find and reveal a safe cell:

// Minesweeper/Grid.h
class MinesweeperGrid {
public:
  // ... existing code ...
  bool RevealHint();

private:
  // ... existing code ...
  std::vector<MinesweeperCell*> GetSafeCells();
};

2. Implementing the Hint Logic

Now, let's implement the RevealHint method:

// Minesweeper/Grid.cpp
#include <random>

bool MinesweeperGrid::RevealHint() {
  auto SafeCells = GetSafeCells();
  if (SafeCells.empty()) {
    return false; // No safe cells left
  }

  // Randomly select a safe cell
  std::random_device              rd;
  std::mt19937                    gen(rd());
  std::uniform_int_distribution<> dis(
    0, SafeCells.size() - 1);
  int randomIndex = dis(gen);

  SafeCells[randomIndex]->ClearCell();
  return true;
}

std::vector<MinesweeperCell*>
MinesweeperGrid::GetSafeCells() {
  std::vector<MinesweeperCell*> SafeCells;
  for (auto& Cell : Children) {
    if (!Cell.GetHasBomb()
      && !Cell.IsCleared()) {
      SafeCells.push_back(&Cell);
    }
  }
  return SafeCells;
}

3. Adding a Hint Button

Let's add a hint button to our UI:

// Minesweeper/UI.h
#include "Minesweeper/HintButton.h"

class MinesweeperUI {
public:
  // ... existing code ...

private:
  MinesweeperGrid Grid;
  NewGameButton   NewGameBtn;
  HintButton      HintBtn; 
};

4. Implementing the HintButton Class

Now, let's create the HintButton class:

// Minesweeper/HintButton.h
#pragma once
#include "Engine/Button.h"
#include "Engine/Text.h"
#include "Minesweeper/Grid.h"

class HintButton : public Engine::Button {
public:
  HintButton(int     x, int y, int w, int h,
    MinesweeperGrid& grid)
    : Button{ x, y, w, h },
      Text{ x, y, w, h, "HINT", {}, 20 },
      Grid{ grid } {}

  void Render(SDL_Surface* Surface) override {
    Button::Render(Surface);
    Text.Render(Surface);
  }

  void HandleLeftClick() override {
    if (Grid.RevealHint()) {
      // Hint was successful
      SetColor(Config::BUTTON_SUCCESS_COLOR);
    } else {
      // No safe cells left
      SetColor(Config::BUTTON_FAILURE_COLOR);
    }
  }

private:
  Engine::Text     Text;
  MinesweeperGrid& Grid;
};

5. Updating the UI Constructor

Finally, let's update the MinesweeperUI constructor to include the new hint button:

// Minesweeper/UI.cpp
MinesweeperUI::MinesweeperUI()
  : Grid{ Config::PADDING, Config::PADDING },
    NewGameBtn{
      Config::PADDING,
      Config::GRID_HEIGHT + Config::PADDING * 2,
      (Config::WINDOW_WIDTH - Config::PADDING *
        3) / 2,
      Config::FOOTER_HEIGHT - Config::PADDING
    },
    HintBtn{
      Config::PADDING * 2 + (
        Config::WINDOW_WIDTH
        - Config::PADDING * 3) / 2,
      Config::GRID_HEIGHT + Config::PADDING * 2,
      (Config::WINDOW_WIDTH - Config::PADDING *
        3) / 2,
      Config::FOOTER_HEIGHT - Config::PADDING,
      Grid } {}

With these changes, we've added a hint feature to our Minesweeper game. Players can now click the "HINT" button to reveal a safe cell when they're stuck. The button will change color to indicate success or failure (when no safe cells are left).

Remember to update your event handling in the MinesweeperUI class to include the new HintButton:

void MinesweeperUI::HandleEvent(
  const SDL_Event& E) {
  Grid.HandleEvent(E);
  NewGameBtn.HandleEvent(E);
  HintBtn.HandleEvent(E); 
}

This hint feature adds an extra layer of interactivity and assistance to your Minesweeper game, making it more accessible to players of all skill levels.

Answers to questions are automatically generated and may not have been reviewed.

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved