⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g15-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
📖 第 1 页 / 共 2 页
字号:

// these functions add a rock to one of the piles, as long as there are
// less than 'ROCKS' rocks in the game already.
void Up1()
{
    if( g_startingState.m_rocks[0] +
        g_startingState.m_rocks[1] + 
        g_startingState.m_rocks[2] +
        g_startingState.m_rocks[3] +
        g_startingState.m_rocks[4] < ROCKS )
    {
        g_startingState.m_rocks[0]++;
    }
}

void Up2()
{
    if( g_startingState.m_rocks[0] +
        g_startingState.m_rocks[1] + 
        g_startingState.m_rocks[2] +
        g_startingState.m_rocks[3] +
        g_startingState.m_rocks[4] < ROCKS )
    {
        g_startingState.m_rocks[1]++;
    }
}

void Up3()
{
    if( g_startingState.m_rocks[0] + 
        g_startingState.m_rocks[1] + 
        g_startingState.m_rocks[2] +
        g_startingState.m_rocks[3] +
        g_startingState.m_rocks[4] < ROCKS )
    {
        g_startingState.m_rocks[2]++;
    }
}


void Up4()
{
    if( g_startingState.m_rocks[0] + 
        g_startingState.m_rocks[1] + 
        g_startingState.m_rocks[2] +
        g_startingState.m_rocks[3] +
        g_startingState.m_rocks[4] < ROCKS )
    {
        g_startingState.m_rocks[3]++;
    }
}

void Up5()
{
    if( g_startingState.m_rocks[0] + 
        g_startingState.m_rocks[1] + 
        g_startingState.m_rocks[2] +
        g_startingState.m_rocks[3] +
        g_startingState.m_rocks[4] < ROCKS )
    {
        g_startingState.m_rocks[4]++;
    }
}


// these functions add a rock to one of the piles, as long as there are
// more than 0 rocks in the pile.
void Down1()
{
    if( g_startingState.m_rocks[0] > 0 )
    {
        g_startingState.m_rocks[0]--;
    }
}

void Down2()
{
    if( g_startingState.m_rocks[1] > 0 )
    {
        g_startingState.m_rocks[1]--;
    }
}

void Down3()
{
    if( g_startingState.m_rocks[2] > 0 )
    {
        g_startingState.m_rocks[2]--;
    }
}

void Down4()
{
    if( g_startingState.m_rocks[3] > 0 )
    {
        g_startingState.m_rocks[3]--;
    }
}

void Down5()
{
    if( g_startingState.m_rocks[4] > 0 )
    {
        g_startingState.m_rocks[4]--;
    }
}

void Play()
{
    int x;

    // draw the GUI with the "processing" sign.
    g_gui->GetItem( 1 )->SetVisibility( true );
    g_gui->Draw();

    // draw the rocks
    DrawRocks( g_startingState, 220, 16 );
    g_gui->Update();

    // calculate the game tree.
    GameTree();

    // calculate the minimax tree
    MiniMax( g_tree, true );

    // hide the proccessing sign
    g_gui->GetItem( 1 )->SetVisibility( false );

    // hide the up and down buttons.
    for( x = 4; x < 15; x++ )
    {
        g_gui->GetItem( x )->SetVisibility( false );
    }

    // start the game!
    g_gui->GetItem( 2 )->SetVisibility( true );
    g_gui->GetItem( 15 )->SetVisibility( true );
    g_gui->GetItem( 16 )->SetVisibility( true );
    g_playing = true;
    g_current = g_tree;
}


void Hint()
{
    g_hint = !g_hint;
}


// ============================================================================
//  Main
// ============================================================================



int main( int argc, char* argv[] )
{
    int x;
    int y;

    srand( time(0) );

    //initialize systems
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
    SDL_WM_SetCaption( PROGRAM_NAME, 0); 
    TTF_Init();
    g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
    g_gui->SetFont( "arial.ttf", ARIAL, 20, TTF_STYLE_NORMAL );


    // load the bmp
    g_rock = SDL_LoadBMP( "rock.bmp" );

    // set the color keys of the BMPs
    SDL_SetColorKey( g_rock, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_rock->format, 255, 255, 255 ));


    // add the items to the g_gui
    g_gui->AddLabel( 270, 384, "You Win!", ARIAL, BLACK, WHITE );
    g_gui->AddLabel( 480, 0, "PROCESSING...", ARIAL, RED, WHITE );
    g_gui->GetItem( 0 )->SetVisibility( false );
    g_gui->GetItem( 1 )->SetVisibility( false );

    g_gui->AddLabel( 270, 384, "Your Turn", ARIAL, BLACK, WHITE );
    g_gui->AddLabel( 270, 384, "My Turn!", ARIAL, BLACK, WHITE );
    g_gui->GetItem( 2 )->SetVisibility( false );
    g_gui->GetItem( 3 )->SetVisibility( false );


    g_gui->AddButton( 228, 0, "upbuttonup.bmp", "upbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Up1 );
    g_gui->AddButton( 268, 0, "upbuttonup.bmp", "upbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Up2 );
    g_gui->AddButton( 308, 0, "upbuttonup.bmp", "upbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Up3 );
    g_gui->AddButton( 348, 0, "upbuttonup.bmp", "upbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Up4 );
    g_gui->AddButton( 388, 0, "upbuttonup.bmp", "upbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Up5 );

    g_gui->AddButton( 228, 150, "downbuttonup.bmp", "downbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Down1 );
    g_gui->AddButton( 268, 150, "downbuttonup.bmp", "downbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Down2 );
    g_gui->AddButton( 308, 150, "downbuttonup.bmp", "downbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Down3 );
    g_gui->AddButton( 348, 150, "downbuttonup.bmp", "downbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Down4 );
    g_gui->AddButton( 388, 150, "downbuttonup.bmp", "downbuttondown.bmp", " ", ARIAL,
                      BLACK, WHITE, Down5 );

    g_gui->AddButton( 0, 192, "gbup.bmp", "gbdown.bmp", "Play!", ARIAL,
                      BLACK, WHITE, Play );
    g_gui->AddButton( 0, 128, "gbup.bmp", "gbdown.bmp", "Hint", ARIAL,
                      BLACK, WHITE, Hint );
    g_gui->GetItem( 15 )->SetVisibility( false );
    
    g_gui->AddButton( 0, 192, "gbup.bmp", "gbdown.bmp", "Computer", ARIAL,
                      BLACK, WHITE, OpponentMove );
    g_gui->GetItem( 16 )->SetVisibility( false );

    g_gui->AddLabel( 270, 385, "YOU LOSE!", ARIAL, BLACK, WHITE );
    g_gui->GetItem( 17 )->SetVisibility( false );


    // set our at exit function
    atexit( SDL_Quit ) ;

    // declare event variable
    SDL_Event event;

    g_gui->Draw();
    g_gui->Update();

    // loop until we get a quit message.
    while( 1 )
    {
        //look for an event
        if( SDL_PollEvent( &event ) )
        {
            //an event was found
            if( event.type == SDL_QUIT ) 
                break;

            // mouse button was pressed
            if( event.type == SDL_MOUSEBUTTONDOWN ) 
            {
                // get the mouse g_state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been pressed
                g_gui->MouseDown( x, y );

            }

            // mouse button was released
            if( event.type == SDL_MOUSEBUTTONUP ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been released
                g_gui->MouseUp( x, y );

                if( g_playing == true && g_yourturn == true )
                {
                    if( x >= 220 && x <= 420 )
                    {
                        // calculate which pile was clicked on
                        x = (x - 220) / 40;
                    
                        // calculate which rock was clicked on
                        y = ((150 - y) / 16) + 1;
                 
                        // if y was valid, proccess the click
                        if( y >= 1 && y <= 8 )
                        {
                            ClickRock( x, y );
                            if( g_current->m_data.Empty() )
                            {
                                // you lose!
                                g_gui->GetItem( 17 )->SetVisibility( true );
                                GameOver();
                            }
                            else
                            {
                                // computers turn
                                g_yourturn = false;
                                g_gui->GetItem( 3 )->SetVisibility( true );
                            }
                            g_gui->GetItem( 2 )->SetVisibility( false );

                        }
                    }
                }
            }

            if( event.type == SDL_KEYDOWN )
            {
                // a key was pressed.
                if( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    // if ESC was pressed, quit the program.
                    SDL_Event quit;
                    quit.type = SDL_QUIT;
                    SDL_PushEvent( &quit );
                }

                // tell the GUI that a key was pressed.
                g_gui->KeyDown( event.key.keysym.sym, 
                                event.key.keysym.mod,
                                event.key.keysym.unicode );
            }
        }


        // draw the g_gui at the end of each loop.
        g_gui->Draw();

        if( g_playing == false && g_gameOver == false )
        {
            // draw the starting game
            DrawRocks( g_startingState, 220, 16 );
        }
        else
        {
            // draw the current game, while the game is in progress
            DrawRocks( g_current->m_data, 220, 16 );
        }


        // draw the hint, if needed.
        if( g_hint == true && g_current != 0 && g_gameOver == false )
        {
            if( g_current->m_data.m_nextState != 0 )
                DrawRocks( g_current->m_data.m_nextState->m_data, 220, 256 );
        }

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -