Cheat Engine Minesweeper Auto-Solver (Lua Script)
Some old code I wrote as an example for using Cheat Engine 6.x's Lua engine to do various things. This was written when Lua was first added to Cheat Engine as an example, so it may/may not still work with the latest release.
This will automatically solve Minesweepers board (Windows XP version) using the games internal functions to determine if a square is a mine as well as auto-clicking the blocks.
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();