Problem z przekazaniem parametru

0

Siemka, mam problem z przekazaniem wartości parametru introGameMode, mianowicie: chciałbym go przekazać z tej klasy

#include "IntroController.h"
#include "GameManager.h"

IntroController::IntroController(IntroView &v) : view(v)
{}

void IntroController::handleEvent(sf::Event &event) {
    auto mouse_position = sf::Mouse::getPosition(view.getWindow());
    auto translated_pos = view.getWindow().mapPixelToCoords(mouse_position);

    //std::cout<<game_mode<<std::endl;
    if(view.getRect_easy_mode().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            introGameMode = INTRO_EASY;
        }
    }
    else if(view.getRect_normal_mode().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            introGameMode = INTRO_NORMAL;

        }
    }
    else if(view.getRect_hard_mode().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            introGameMode = INTRO_HARD;

        }
    }

    if(view.getRect_small_size().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            introSize = intro_small;
        }
    }
    else if(view.getRect_normal_size().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            introSize = intro_normal;
        }
    }
    else if(view.getRect_big_size().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            introSize = intro_big;
        }
    }
    if(view.getRect().getGlobalBounds().contains(translated_pos)){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
        {
            finished = true;
        }
    }
}


IntroSize IntroController::getIntroSize() const {
    return introSize;
}

IntroGameMode IntroController::getIntroGameMode()  {
    return introGameMode;
}


do tego konstruktora :

Minesweeperboard::Minesweeperboard(int width, int high, IntroController &introController):introController(introController) {
    srand(time(NULL));
    game_status = RUNNING;
    firstMove = true;
    this ->width = width;
    this ->high = high;

    double amountsOfMines=0;
    int row=0, column=0;
//    for(int i=0; i<high; i++){//row = i
//        for(int j=0; j<width; j++){//column = j
//            board[i][j].hasMine = false;
//            board[i][j].hasFlag = false;
//            board[i][j].isRevealed = false;
//
//        }
//    }
//    if(introController.getIntroSize()==intro_small){
//        width=10;
//        high=10;
//    }
//    if(introController.getIntroSize()==intro_normal){
//        width=15;
//        high=20;
//    }
//    if(introController.getIntroSize()==intro_big){
//        width=20;
//        high=30;
//    }
    if(introController.getIntroGameMode() == INTRO_EASY){
        amountsOfMines = 0.1*width*high;
        for(int i=0; i<amountsOfMines; i++){
            row = rand()% high;
            column = rand()% width;
            board[row][column].hasMine = true;
        }
    }
    if(introController.getIntroGameMode()==INTRO_NORMAL){
        amountsOfMines = 0.2*width*high;
        for(int i=0; i<amountsOfMines; i++){
            row = rand()% high;
            column = rand()% width;
            board[row][column].hasMine = true;
        }
    }
    if(introController.getIntroGameMode()==INTRO_HARD){
        amountsOfMines = 0.3*width*high;
        for(int i=0; i<amountsOfMines; i++){
            row = rand()% high;
            column = rand()% width;
            board[row][column].hasMine = true;
        }
    }

void Minesweeperboard::debug_display() const {
    for(int i=0; i<high; i++){
        for(int j=0; j<width; j++){
            if(board[i][j].hasMine) std::cout<<"[M";
            else std::cout<<"[.";
            if(board[i][j].isRevealed) std::cout<<"o";
            else std::cout<<".";
            if(board[i][j].hasFlag) std::cout<<"f]";
            else std::cout<<".]";
        }
        std::cout<<std::endl;
    }
}

dodam ze eventy działając poprawnie.

0

Źle to robisz.
Zrób sobie taką klasę:

struct SweeperConfig
{
    int width;
    int height;
    int mines;
};

SweeperConfig easyConfig() {
    return {
         10, 10, 4
    };
}

Okaże się, że ten twój kod znacznie się skurczy i uprości.

0

tak tylko muszę potem jeszcze (za pomocą intro) wybrać wysokość i szerokość planszy. Dlatego w ten sposób też nie pójdzie.
Pytanie, co złego jest w podejściu które przedstawiłem wyżej w kodzie? przekazuje referencyjnie gettera na dane pole introGameMode przecież :/

0
Bartek24 napisał(a):

tak tylko muszę potem jeszcze (za pomocą intro) wybrać wysokość i szerokość planszy. Dlatego w ten sposób też nie pójdzie.

A niby czemu to wprowadzi z tym jakiś problem?

Bartek24 napisał(a):

Pytanie, co złego jest w podejściu które przedstawiłem wyżej w kodzie? przekazuje referencyjnie gettera na dane pole introGameMode przecież :/

Popraw a zobaczysz. Twój kod będzie bardziej "suchy" DRY.

0
MarekR22 napisał(a):
Bartek24 napisał(a):

tak tylko muszę potem jeszcze (za pomocą intro) wybrać wysokość i szerokość planszy. Dlatego w ten sposób też nie pójdzie.

A niby czemu?

Tak ma wyglądać gra, w intro wybierasz (z pośród trzech) wielkość planszy i jej poziom(w zależności od poziomu wygeneruje się ilość min).

0

Problem jest ogólnie taki że wartość jest przekazywana aleee, nie ta zmieniona przez event tylko ta domyślna zainicjalizowana w konstruktorze. Kompletnie nie mam pojęcia jak to rozwiązać...

1 użytkowników online, w tym zalogowanych: 0, gości: 1