Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Minesweeper Board Solver Lua Example

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials
View previous topic :: View next topic  
Author Message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Sat Jan 15, 2011 3:01 pm    Post subject: Minesweeper Board Solver Lua Example Reply with quote

This example shows how to call functions inside an application. For this example, this calls the left and right-click handlers that happen when Minesweeper is told the user has either left-clicked a square to "activate" it, or that they have right-clicked it to make it with a flag meaning its a bomb.

Please note because of limitations on how Lua interacts with Cheat Engine calling functions requires the use of threads. Since this script shares memory locations, there can be race conditions if a thread starts slow. This can cause the solver to mess up here and there.

This script is intended to be tested with "Beginner" mode on Minesweeper.

Code:

--[[

    Minesweeper Solver via Lua Example
    by atom0s

]]--

-- ## DO NOT EDIT BELOW THIS LINE!! ##

local Minesweeper_Example = { }

----------------------------------------------------------------------------------
-- func: Minesweeper_Example.CallStepSquare( .. )
-- desc: Calls our StepSquare cave to internally call left-click function.
----------------------------------------------------------------------------------
function Minesweeper_Example.CallStepSquare( x, y )
    -- Write current X and Y to memory.
    autoAssemble(
        "alloc(tempFunc, 1024)\n" ..
        "CreateThread(tempFunc)\n" ..
        "tempFunc:\n" ..
        "mov [currXPos], " .. tostring( x ) .. "\n" ..
        "mov [currYPos], " .. tostring( y ) .. "\n" ..
        "ret\n"
        );
       
    -- Create thread calling our StepSquare function.
    autoAssemble( "CreateThread(StepSquareCave)\n" );

    -- Cleanup temp function.
    autoAssemble( "dealloc(tempFunc)\n" );
end

----------------------------------------------------------------------------------
-- func: Minesweeper_Example.CallMakeGuess( .. )
-- desc: Calls our MakeGuess cave to internally call right-click function.
----------------------------------------------------------------------------------
function Minesweeper_Example.CallMakeGuess( x, y )
    -- Write current X and Y to memory.
    autoAssemble(
        "alloc(tempFunc, 1024)\n" ..
        "CreateThread(tempFunc)\n" ..
        "tempFunc:\n" ..
        "mov [currXPos], " .. tostring( x ) .. "\n" ..
        "mov [currYPos], " .. tostring( y ) .. "\n" ..
        "ret\n"
        );
   
    -- Create thread calling our MakeGuess function.
    autoAssemble( "CreateThread(MakeGuessCave)\n" );

    -- Cleanup temp function.
    autoAssemble( "dealloc(tempFunc)\n" );
end

----------------------------------------------------------------------------------
-- func: Minesweeper_Example.Main( .. )
-- desc: Main script function that handles everything of the script.
----------------------------------------------------------------------------------
function Minesweeper_Example.Main ( )

    -- Obtain current board sizes.
    Minesweeper_Example.BoardWidth  = readInteger( "10056AC" );
    Minesweeper_Example.BoardHeight = readInteger( "10056A8" );

    -- Board layout starting position.
    Minesweeper_Example.BoardStart = 16798528;
   
    -- Allocate memory locations for x and y positions.
    autoAssemble(
        "globalalloc(currXPos, 4)\n" ..
        "globalalloc(currYPos, 4)\n"
        );
   
    -- Allocate cave for StepSquare function.
    autoAssemble(
        "globalalloc(StepSquareCave, 1024)\n" ..
        "StepSquareCave:\n" ..
        "push [currYPos]\n" ..
        "push [currXPos]\n" ..
        "mov eax, 01003512\n" ..
        "call eax\n" ..
        "ret\n"
        );
       
    -- Allocate cave for MakeGuess function.
    autoAssemble(
        "globalalloc(MakeGuessCave, 1024)\n" ..
        "MakeGuessCave:\n" ..
        "push [currYPos]\n" ..
        "push [currXPos]\n" ..
        "mov eax, 0100374F\n" ..
        "call eax\n" ..
        "ret\n"
        );
   
    -- Attempt to solve the board.
    local boardX = 1;
    local boardY = 1;
   
    for boardX = 1, Minesweeper_Example.BoardWidth do
        for boardY = 1, Minesweeper_Example.BoardHeight do
            local bCurrentPos = Minesweeper_Example.BoardStart + ( boardX + ( boardY * 32 ) );
            local bValue = readBytes( bCurrentPos, 1 );
            if( bValue ~= 0x8F ) and ( bValue == 0x0F ) then
                Minesweeper_Example.CallStepSquare( boardX, boardY );
            else
                Minesweeper_Example.CallMakeGuess( boardX, boardY );
            end
        end
    end
   
    -- Cleanup allocations.
    autoAssemble(
        "dealloc(currXPos)\n" ..
        "dealloc(currYPos)\n" ..
        "dealloc(MakeGuessCave)\n" ..
        "dealloc(StepSquareCave)\n"
        );
   
    return true;
end

-- Execute our script.
Minesweeper_Example.Main();

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Tutorials -> LUA Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites