C++SDL扫雷旗在命中炸弹时结束游戏

C++ SDL Minesweeper Flags Ending Game When Hitting Bombs

本文关键字:炸弹 结束 游戏 扫雷 C++SDL      更新时间:2023-10-16

我的问题可能有一个简单的答案,但它让我很困惑,因为尽管它在我看来是正确的,但我似乎无法让它正常工作。正如标题中所读,当我用鼠标右键点击放置旗帜时,它是有效的,但当我击中地雷时,它会结束游戏,而不是将旗帜放在正方形上,让我继续游戏。如有任何帮助,我们将不胜感激。编辑:编辑了代码,并为两个bool语句添加了大括号,但我遇到的主要问题是,当我右键单击要放置标志的正方形时,它是一个地雷,它的行为就像地雷被击中一样,然后程序在5秒后通过并退出。如果有人能帮助我,当我右键点击放置国旗时,不要炸毁地雷,我将非常感激我的代码如下,大部分都是注释:

    /*Compile using:
* gcc MinesweeperGraphics.cpp `sdl-config --libs` -lstdc++ -o MinesweeperGraphics.bin 
* Supporting flags without ending program after it hits a bomb
*Win detection within program
*/
#include "SDL/SDL.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <time.h>
//The attributes of the screen
const int SCREEN_WIDTH = 270;
const int SCREEN_HEIGHT = 300;
const int SCREEN_BPP = 9;
//The surfaces that will be used
SDL_Surface *zero = NULL;
SDL_Surface *one = NULL;
SDL_Surface *two = NULL;
SDL_Surface *three = NULL;
SDL_Surface *four = NULL;
SDL_Surface *five = NULL;
SDL_Surface *six = NULL;
SDL_Surface *seven = NULL;
SDL_Surface *eight = NULL;
SDL_Surface *mine = NULL;
SDL_Surface *boom = NULL;
SDL_Surface *box = NULL;
SDL_Surface *flag = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
using namespace std;
////////////////////////////////////////////////////////////
SDL_Surface *load_image( string filename )
{
    SDL_Surface* loadedImage = NULL;        //Temporary storage for the image that's loaded
    SDL_Surface* optimizedImage = NULL;    //The optimized image that will be used
    loadedImage = SDL_LoadBMP( filename.c_str() );    //Load the image
    if( loadedImage != NULL )    //If nothing went wrong in loading the image
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );        //Create an optimized image
        SDL_FreeSurface( loadedImage );        //Free the old image
    }
    return optimizedImage;    //Return the optimized image
}
////////////////////////////////////////////////////////////
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    SDL_Rect offset;    //Make a temporary rectangle to hold the offsets
    offset.x = x;    //Give the offsets to the rectangle
    offset.y = y;
    SDL_BlitSurface( source, NULL, destination, &offset );    //Blit the surface
}
////////////////////////////////////////////////////////////
int initializemines(int mines[11][11][2])
{
    int x,y,z;
    for (x=0;x<11;x++)
        for (y=0;y<11;y++)
            for (z=0;z<2;z++)
                mines[x][y][z]=12;
    for (x=1;x<10;x++)
        for (y=1;y<11;y++)
            for (z=0;z<2;z++)
                mines[x][y][z]=0;
    for (x=1;x<10;x++)
        for (y=1;y<11;y++)
            mines[x][y][0]=9*(int(rand()%8/7.0)); // 1/8 Chance of being a mine
    return 0;
}
////////////////////////////////////////////////////////////
int calculatemines(int mines[11][11][2])
{
    int x,y;
    for (x=1;x<10;x++)
        for (y=1;y<11;y++)
        {
            if (mines[x-1][y-1][0]==9)  // Upper Left
                mines[x][y][1]++;
            if (mines[x][y-1][0]==9)        // Upper
                mines[x][y][1]++;
            if (mines[x+1][y-1][0]==9)  // Upper Right
                mines[x][y][1]++;
            if (mines[x-1][y][0]==9)        // Left
                mines[x][y][1]++;
            if (mines[x-1][y+1][0]==9)  // Lower Left
                mines[x][y][1]++;
            if (mines[x+1][y][0]==9)        // Right
                mines[x][y][1]++;
            if (mines[x][y+1][0]==9)        // Lower
                mines[x][y][1]++;
            if (mines[x+1][y+1][0]==9)  // Lower Right
                mines[x][y][1]++;
            if (mines[x][y][0]==0) mines[x][y][0]=mines[x][y][1];
        }
    return 0;
}
////////////////////////////////////////////////////////////
int selectmines(int mines[11][11][2],int MouseX,int MouseY)
{
    cout<<MouseX<<","<<MouseY<<"n";
    if (mines[MouseX][MouseY][0]==9)    //Hit a Mine
    {
        mines[MouseX][MouseY][0]=10;    //Boom
        for (int x=1; x<10;x++)
            for (int y=1; y<11;y++)
                if (mines[x][y][0]==9) mines[x][y][0]=11;  //Show other Mines
        return 1;
    }
    else
    {
        mines[MouseX][MouseY][1]=9;
        if (mines[MouseX][MouseY][0]==0)    //Shows Neighbors of Zeros
            for (int x=(MouseX-1); x<=(MouseX+1);x++)
                for (int y=(MouseY-1); y<=MouseY+1;y++)
                    if (mines[x][y][1]<9)   //If Neighbor <9
                        selectmines(mines,x,y); //Select Mines Again
    }
    return 0;
}
////////////////////////////////////////////////////////////
int printmines(int mines[11][11][2])
{
    int x,y,z,gx,gy;
    system("clear");
    cout<<"   ABCDEFGHIJKLMNOPQRSTUVWXYZn";
    for (z=0;z<1;z++)
    {
        for (y=1;y<11;y++)
        {
            cout<<y<<" ";
            if (y<10) cout<<" ";
            for (x=1;x<10;x++)
            {
        //      if (mines[x][y][0]==10) cout<<"☠";
        //      else if (mines[x][y][0]==11) cout<<"☢";
        //      else if (mines[x][y][1]<9) cout<<"⬜";
                cout<<mines[x][y][z];
                //////win conditions here I think
            }
            cout<<"n";
        }
        cout<<"n";
    }
    //Print Mines Graphically
    for (y=1;y<11;y++)
    {
        gy=(y-1)*30;
        for (x=1;x<10;x++)
        {
            gx=(x-1)*30;
            if (mines[x][y][0]==10) apply_surface( gx, gy, boom, screen );
            else if (mines[x][y][0]==11) apply_surface( gx, gy, mine, screen );
            else if (mines[x][y][1]<9) apply_surface( gx, gy, box, screen );
            else if (mines[x][y][0]==13) apply_surface( gx, gy, flag, screen );
            else switch (mines[x][y][0])
            {
                case 0:
                    apply_surface( gx, gy, zero, screen );
                break;
                case 1:
                    apply_surface( gx, gy, one, screen );
                break;
                case 2:
                    apply_surface( gx, gy, two, screen );
                break;
                case 3:
                    apply_surface( gx, gy, three, screen );
                break;
                case 4:
                    apply_surface( gx, gy, four, screen );
                break;
                case 5:
                    apply_surface( gx, gy, five, screen );
                break;
                case 6:
                    apply_surface( gx, gy, six, screen );
                break;
                case 7:
                    apply_surface( gx, gy, seven, screen );
                break;
                case 8:
                    apply_surface( gx, gy, eight, screen );
                break;
                case 9:
                    apply_surface( gx, gy, flag,screen);
                break;
            }
        }
    }
    if( SDL_Flip( screen ) == -1 ) return 1;
    return 0;
}
////////////////////////////////////////////////////////////
int main()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )    //Initialize all SDL subsystems
        return 1;
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );    //Set up the screen
    if( screen == NULL )    //If there was an error in setting up the screen
        return 1;
    SDL_WM_SetCaption( "Mine Sweeper", NULL );    //Set the window caption
    zero = load_image( "Zero.bmp" );    //Load the images
    one = load_image( "One.bmp" );
    two = load_image( "Two.bmp" );
    three = load_image( "Three.bmp" );
    four = load_image( "Four.bmp" );
    five = load_image( "Five.bmp" );
    six = load_image( "Six.bmp" );
    seven = load_image( "Seven.bmp" );
    eight = load_image( "Eight.bmp" );
    mine = load_image( "Mine.bmp" );
    boom = load_image( "Boom.bmp" );
    flag = load_image( "Flag.bmp" );
    box = load_image( "Box.bmp" );
    background = load_image( "Background.bmp" );
    char Letter;
    bool win=false;
    bool lose=false;
    bool done=false;
    int mines[11][11][2],MouseX=0,MouseY=0; // create mines array
    srand (time(NULL));     // initialize random seed
    initializemines(mines); // 
    calculatemines(mines);
    while (!done)
    {
        printmines(mines);
        while(SDL_PollEvent(&event))    //Did a Mouse Event Occur?
        {
            event.type == SDL_MOUSEBUTTONDOWN;  //Was a Mouse Button Pressed?
            switch (event.button.button)
            {
                case SDL_BUTTON_LEFT:   //If the Left Button was Pressed
                    MouseX=event.motion.x/30+1;
                    MouseY=event.motion.y/30+1;
                    if ((event.motion.x>0)&&(event.motion.y>0))
                        done=selectmines(mines,MouseX,MouseY);
                    break;
                case SDL_BUTTON_RIGHT:  //If the Right Button was Pressed
                    MouseX=event.motion.x/30+1;
                    MouseY=event.motion.y/30+1;
                    if ((event.motion.x>0)&&(event.motion.y>0)) //Trying to get right click to work without
                            done=selectmines(mines,MouseX,MouseY); //ending game after hitting a bomb
                            mines[MouseX][MouseY][0]=13;    //it currently lays the flag down but does not like bombs
                        if ((mines[MouseX][MouseY][0]==9)&&(mines[MouseX][MouseY][0]==10))
                        { //so it kills the program despite 
                        lose=false; //the bool statements
                        win=false;
                        mines[MouseX][MouseY][0]=13;
                        }
                        break;
            }
                for(int X=1;X<11;X++) //Checks to see if square hit was a bomb and if so it ends the program so it
                    for(int Y=1;Y<11;Y++) //won't randomly keep the program going until you decide to quit the program
                        if(mines[X][Y][0]==10) lose=true;
        }
        if(lose==true) done=true; //checks if done or not?
        if(win==true) done=true;
    }
    printmines(mines);
    SDL_Delay( 5000 );
    SDL_FreeSurface( zero );        //Free the surfaces
    SDL_FreeSurface( one );
    SDL_FreeSurface( two );
    SDL_FreeSurface( three );
    SDL_FreeSurface( four );
    SDL_FreeSurface( five );
    SDL_FreeSurface( six );
    SDL_FreeSurface( seven );
    SDL_FreeSurface( eight );
    SDL_FreeSurface( mine );
    SDL_FreeSurface( boom );
    SDL_FreeSurface( flag );
    SDL_FreeSurface( box );
    SDL_FreeSurface( background );
    SDL_Quit();    //Quit SDL
    return 0;
}
                    if ((mines[MouseX][MouseY][0]==9)&&(mines[MouseX][MouseY][0]==10)) //so it kills the program despite 
                    lose=false; //the bool statements
                    win=false;
                    break;

我猜您打算将contidional(if)同时应用于lose=false;win=false;行,但正如所写的,它只控制第一条语句。添加这样的大括号:

                    if ((mines[MouseX][MouseY][0]==9)&&(mines[MouseX][MouseY][0]==10)) //so it kills the program despite 
                    {
                        lose=false; //the bool statements
                        win=false;
                    }
                    break;
case SDL_BUTTON_RIGHT:  //If the Right Button was Pressed
                MouseX=event.motion.x/30+1;
                MouseY=event.motion.y/30+1;
                if ((event.motion.x>0)&&(event.motion.y>0)) //Trying to get right click to work without
                        done=selectmines(mines,MouseX,MouseY); //ending game after hitting a bomb
                        mines[MouseX][MouseY][0]=13;    //it currently lays the flag down but does not like bombs
                    if ((mines[MouseX][MouseY][0]==9)&&(mines[MouseX][MouseY][0]==10))
                    { //so it kills the program despite 
                      lose=false; //the bool statements
                      win=false;
                      mines[MouseX][MouseY][0]=13;
                    }
                    break;

在设置标志之前,您需要调用"selectmines"-函数。如果切换mines[MouseX][MouseY][0]=13;done=selectmines(mines,MouseX,MouseY);行(并删除下面的If语句,因为这似乎是为了检查mines[mouseX][MouseY]是9还是10,当您在上面的行中将其设置为13时),它应该可以工作。

我不确定if ((event.motion.x>0)&&(event.motion.y>0))部分应该做什么。它没有大括号,所以它只在按下鼠标右键时执行done=selectmines(mines,MouseX,MouseY);调用?我想你可能也需要一些花括号。