扫雷C++ --- 问题:由于类中的网格构建而无法编译?[关闭]

Minesweeper C++ --- Issue: Not Compiling due to grid build in a class? [closed]

提问人:Tanner Lang 提问时间:11/14/2023 最后编辑:Tanner Lang 更新时间:11/14/2023 访问量:56

问:


想改进这个问题吗?更新问题,使其仅通过编辑这篇文章来关注一个问题。

6天前关闭。

这篇文章在 6 天前被编辑并提交审核,未能重新打开帖子:

原始关闭原因未解决

我目前正在为我的编程 1 课程开发一个用 C++ 和 SFML 编写的扫雷项目,并且在使用类来构建我的游戏板时遇到了问题。

我在我的 settings.h 和 settings.cpp 文件中定义了一个类,该文件在构造函数中填充了我的板。在 main.cpp 文件中,我通过事件处理函数传递类对象“b”,并将其公共值用于 SFML 库函数;虽然我没有语法错误,但我有一种感觉,类是导致编译失败的原因。任何帮助将不胜感激

// Two - Player Minesweeper
#include<SFML/Graphics.hpp>
#include<time.h> //for time and date functions from C++ standard library
#include"Settings.h"

//using namespace sf; //for classes provided by SFML

void Gameloop(sf::RenderWindow& MineSweep,sf::Sprite& Sp, sf::Texture& tile,Board& b)
{
    sf::Vector2i mous = sf::Mouse::getPosition(MineSweep);  //create a vector from sfml lib called mouse, set it equal to the real time mouse
    //location in the Minesweep window using the sfml getPosition lib function.
    int x = mous.x / b.w;
    int y = mous.y / b.w;                                   //initiate x and y vars and set them = to the current coordinates.

    sf::Event ev;
    while (MineSweep.pollEvent(ev))     //checks to see if SFML class Event ev is active, will proceed if true.
    {
        if (ev.type == sf::Event::Closed)
        {
            MineSweep.close();          //closes the MineSweep Window.
        }

        if (ev.type == sf::Event::MouseButtonPressed)   //using sfml, check to see if a mouse button was pressed.
        {
            if (ev.key.code == sf::Mouse::Left)
            {
                b.sgrid[x][y] = b.grid[x][y];   //if the left mouse button is clicked, set the sgrid cell spot (blank grid) at... 
                //the mouse coordinates equal to the populated grid to reveal what is in the cell beneath;
            }
            else if (ev.key.code == sf::Mouse::Right)
            {
                b.sgrid[x][y] = 11;         //if the right button is clicked, set the cell at the mouse coordinates = to 11 (flag);
            }
        }
    }
    MineSweep.clear(sf::Color::Cyan);
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= 10; j++)
        {

            if (b.sgrid[x][y] == 9)
            {
                b.sgrid[i][j] = b.grid[i][j];       //if u land on a mine;
            }


            Sp.setTextureRect(sf::IntRect(b.sgrid[i][j] * b.w, 0, b.w, b.w));
            Sp.setPosition(i * b.w, j * b.w);
            MineSweep.draw(Sp);
        }
    }
}

int main()
{
    srand(time(0));                         //RNG function, takes machine time for to generate new random seed.
    tile.loadFromFile("images/tiles.jpg");      //load the tiles image from the repo to be used for the grid
    
    Board b;
    while (MineSweep.isOpen())              
    {
        Gameloop(MineSweep,Sp, tile,b);
        MineSweep.display();    //SFML function "display()" by calling it with the "MineSweep" object we created using the RenderWindow SFML Class.
    }
    
}

以及 settings.h 包含的内容

#pragma 
#ifndef Settings
#define Settings
#include<SFML/Graphics.hpp>


sf::RenderWindow MineSweep(sf::VideoMode(400, 400), "Minesweeper");
sf::Texture tile;
sf::Sprite Sp(tile);                            //SFML library function to draw the texture.



class Board
{
public:
    int w = 32;
    int grid[12][12]; //hidden cells
    int sgrid[12][12]; //top of hidden cells

    Board();


private:

};

#endif

设置.cpp包含的内容

#include "Settings.h"
Board::Board()
{
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= 10; j++)
        {
            sgrid[i][j] = 10;


            if (rand() % 5 == 0) //if the random seed is divisible by 5 then set the grid equal to 9
            {
                grid[i][j] = 9;
            }
            else                //otherwise set grid equal to 0
            {
                grid[i][j] = 0;
            }
        }
    }
    //----------Fill Number Cells---------------------------------
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= 10; j++)
        {
            int n = 0;
            if (grid[i][j] == 9) continue;
            if (grid[i + 1][j] == 9) n++;
            if (grid[i][j + 1] == 9) n++;
            if (grid[i - 1][j] == 9) n++;
            if (grid[i][j - 1] == 9) n++;

            if (grid[i + 1][j + 1] == 9) n++;
            if (grid[i - 1][j - 1] == 9) n++;
            if (grid[i - 1][j + 1] == 9) n++;
            if (grid[i + 1][j - 1] == 9) n++;

            grid[i][j] = n;
        }
    }
C++ 文件 游戏开发

评论

3赞 user4581301 11/14/2023
粗略的方法。最好在设计的早期就决定你需要哪些类以及它们的职责是什么,而不是试图在接近尾声时附加 OOP。但。。。应用相同的原则。你需要代表什么?把它们写下来。每件事负责什么?也把它写下来。创建类定义并执行健全性检查。然后将代码片段迁移到您设计的类的方法中。如果您不得不抛弃并重写大量代码,请不要感到惊讶,因为它根本不适合新范式。

答: 暂无答案