Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Minesweeper Auto-Solver


  • Please log in to reply
14 replies to this topic
fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Enjoy!

Old code:
;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Platform:       Windows XP with XP theme enabled
; Author:         Fincs <[email protected]>
;
; Script Function:
;	Minesweeper auto-solver
;

; Results
; =======

; Nascent:			2 sec.
; Intermediate:		4 sec.
; Advanced:			8 sec.

level := 1	; 1 - nascent, 2 - intermediate, 3 - advanced

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.

; Process level
level ++
If level = 2			; Nascent
	c := 9				; 9 columns
If level = 3			; Intermediate
	c := 16				; 16 columns
If level = 4			; Advanced
	c := 30, r := 16	; 30 columns, 16 rows

If r =
	r := c

Run, winmine	; Execute Minesweeper
Sleep, 100
WinGetActiveTitle, title	; Get the title of Minesweeper
WinMove, %title%,, 80, 80	; Move the window to the position 80, 80
Send, {ALT}{DOWN %level%}{ENTER}	; Select level
Sleep, 10
Send, xyzzy{LShift}		; Enter cheat code
Sleep, 10

CoordMode, Mouse, Relative

; Define constants
x = 21
y = 111
sum = 16

Loop, %r%
{
	d := 0
	Loop, %c%
	{
		MouseMove, %x%, %y%		; Move the mouse to the next square
		Sleep, 10
		PixelGetColor, color, -80, -80	; Check the color of the first pixel
		If color <> 0x000000
			Click	; The pixel says "secure"
		x += sum
		d ++
	}
	x = 21
	y += sum
}

New code:
;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Platform:       WinXP with XP theme enabled
; Author:         Fincs <[email protected]>
;
; Script Function:
;	Minesweeper auto-solver
;

; Results
; =======

; Nascent:			2 sec.
; Intermediate:		4 sec.
; Advanced:			8 sec.

level := 3	; 1 - nascent, 2 - intermediate, 3 - advanced
sleep := 1	; 0 - no sleep when mouse is clicked, 1 - sleep

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetMouseDelay -1 ; Do the mouse operations immediately

; Process level
level ++
If level = 2			; Nascent
	c := 9				; 9 columns
If level = 3			; Intermediate
	c := 16				; 16 columns
If level = 4			; Advanced
	c := 30, r := 16	; 30 columns, 16 rows

If r =
	r := c

Run, winmine,,, pid	; Execute Minesweeper
WinWait, ahk_pid %pid%
IfWinNotActive,,, WinActivate
WinWaitActive
WinMove,,, 80, 80	; Move the window to the position 80, 80
Send, {ALT}{DOWN %level%}{ENTER}	; Select level
Sleep, 10
Send, xyzzy{LShift}		; Enter cheat code
Sleep, 10

CoordMode, Mouse, Relative

; Define constants
x = 21
y = 111
sum = 16

Loop, %r%
{
	d := 0
	Loop, %c%
	{
		MouseMove, %x%, %y%, 0		; Move the mouse to the next square
		PixelGetColor, color, -80, -80	; Check the color of the first pixel
		If color <> 0x000000
			Click	; The pixel says "secure"
		If sleep
			Sleep, 10
		x += sum
		d ++
	}
	x = 21
	y += sum
}
WinWait, ahk_pid %pid%
IfWinNotActive,,, WinActivate
WinWaitActive


Justineo14
  • Members
  • 43 posts
  • Last active: Dec 21 2012 09:26 AM
  • Joined: 30 Oct 2006
;





level := 





Gui, Add, Button, x6 y10 w110 h30 gbutton1, Easy

Gui, Add, Button, x6 y50 w110 h30 gbutton2, Intermediate

Gui, Add, Button, x6 y90 w110 h30 gbutton3, Advanced



;Guicontrol, -Disable, MinimizeBox







Gui, Show, x257 y110 h130 w125, Minesweeper auto-solver

Return



button1:

gui, hide

level := 1

goto, here

return



button2:

gui, hide

level := 2

goto, here

return



button3:

gui, hide

level := 3

goto, here

return



here:



#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.

SendMode Input ; Recommended for new scripts due to its superior speed and reliability.



; Process level

level ++

If level = 2         ; Nascent

   c := 9            ; 9 columns

If level = 3         ; Intermediate

   c := 16            ; 16 columns

If level = 4         ; Advanced

   c := 30, r := 16   ; 30 columns, 16 rows



If r =

   r := c





Run, winmine   ; Execute Minesweeper

me:

Sleep, 300

IfWinActive, ahk_class Minesweeper,,,

{

BlockInput,Mouse

BlockInput,on

WinMove, ahk_class Minesweeper,, 80, 80   ; Move the window to the position 80, 80

Send, {ALT}{DOWN %level%}{ENTER}   ; Select level

Sleep, 10

Send, xyzzy{LShift}      ; Enter cheat code

Sleep, 10



CoordMode, Mouse, Relative



; Define constants

x = 21

y = 111

sum = 16



Loop, %r%

{

   d := 0

   Loop, %c%

   {

      MouseMove, %x%, %y%      ; Move the mouse to the next square

      Sleep, 10

      PixelGetColor, color, -80, -80   ; Check the color of the first pixel

      If color <> 0x000000

         Click   ; The pixel says "secure"

      x += sum

      d ++

   }

   x = 21

   y += sum

}

BlockInput,off

exitapp

}

else

{

;msgbox, try again

goto, me

;GuiClose:

;exitapp

}

return


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Hi, Justineo14

I tried your version and the class of Minesweeper is different for each language. I'm using the spanish version of Windows XP and the class of Minesweeper is "Buscaminas" and the title is the same.

>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Buscaminas
ahk_class Buscaminas

>>>>>>>>>>>>( Mouse Position )<<<<<<<<<<<<<
On Screen: 368, 179 (less often used)
In Active Window: 288, 99

>>>>>>>>>( Now Under Mouse Cursor )<<<<<<<<

Color: 0xD8E9EC (Blue=D8 Green=E9 Red=EC)

>>>>>>>>>>( Active Window Position )<<<<<<<<<<
left: 80 top: 80 width: 170 height: 259

>>>>>>>>>>>( Status Bar Text )<<<<<<<<<<

>>>>>>>>>>>( Visible Window Text )<<<<<<<<<<<

>>>>>>>>>>>( Hidden Window Text )<<<<<<<<<<<

>>>>( TitleMatchMode=slow Visible Text )<<<<

>>>>( TitleMatchMode=slow Hidden Text )<<<<


For that reason my version captures the window title of Minesweeper.
Anyway, thanks for the GUI!

ricke
  • Members
  • 32 posts
  • Last active: Nov 29 2012 08:59 AM
  • Joined: 14 Sep 2006
Hi fincs

I made a little diffrent version.

With this u play like "normal" ;)
But...
If u Leftclick on a bomb it rightclicks instead....
and if u rightclick where there is no bomb it leftclicks.

So u are guaranteed to solve it ;)

EDIT: new code below....

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
CoordMode, Pixel, screen    ;So that pixelgetcolor,0,0 is upper left pixel of the screen

Gui -AlwaysOnTop -caption
Gui, Color, 0000FF
Gui Show, w1 h1 x0 y0, blocker    ;creates a blue "pixel" using a window

Run, winmine,,, pid 
WinWait, ahk_pid %pid% ; wait for the new window to exist 
WinActivate, ahk_pid %pid% ; activate it
Send, xyzzy{LShift}      ; Enter cheat code

#X::exitapp   ; Win+X close the script if needed

#IfWinActive, ahk_class Minesweeper   ;Left Button only active when minesweeper is
LButton::
#IfWinActive, ahk_class Minesweeper   ;Right Button only active when minesweeper is
RButton::
mousegetpos,StartXX, StarYY
if (StartXX<>EndXX AND StartYY<>EndYY){  ;makes sure the mouse has moved to avoid errors
  PixelGetColor, color, 0, 0   ; Check the color of the first pixel
  If color = 0x000000
    send {RButton}
  If color = 0xFFFFFF
    send {LButton}
  If color = 0xFF0000
  {
    send {LButton Down}          
    keywait LButton
    send {Lbutton up}
  }
  winset,Redraw,,blocker  ;the cheat never redraw the 0,0 pixel.... will be latest color even if u close winmine.
}
 mousegetpos, EndXX, EndYY
Return
Return

If u want to impress with nice times on the "ScoreBoard" all the times and names etc are stored cleartext in the registry so its easy to modify:
HKEY_CURRENT_USER\Software\Microsoft\Winmine\

would be easy to make an ahkscript to edit the scoreboard ;)


///Ricke

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
So how does it work...? By checking some cheat pixel? It'd be a whole lot more interesting if it (tried) to solve it by using only what the user sees...

Still, I managed to get the times of fincs' script down to 1 second (the absolute minimum!) for every difficulty. 8)

First, put this anywhere above the Loop
SetMouseDelay, -1
Second, comment out the sleep in the loop:
;Sleep, 10
Third, use speed 0 for MouseMove (this one seems to make only a minor difference, on Advanced, after the other two):
MouseMove, %x%, %y%, 0
:twisted:

By the way, it might be more reliable if you used the process ID and WinWaitActive. For example:
Run, winmine,,, pid ; puts the ID of the new winmine process in "pid"
WinWaitActive, ahk_pid %pid% ; wait for the new window to be active
Or to be even more reliable (in case running winmine doesn't always activate it):
Run, winmine,,, pid
WinWait, ahk_pid %pid% ; wait for the new window to exist
WinActivate, ahk_pid %pid% ; activate it

One last comment... "solve" and "complete via cheats" are two very different things. :p

rammrodd
  • Members
  • 3 posts
  • Last active: Jul 22 2007 03:42 AM
  • Joined: 22 Jul 2007
This is awesome!! I've seen times like these and knew there had to be something to do with automation.


Question, is there a way to do this with no windows popping up and then enter a name? so like i could make an auto run on my flash drive and whatever comp I stuck it into it would do this and post my name (or a message I put in) as the high score?

great work though i love cheating it intrigues me

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

so like i could make an auto run on my flash drive and whatever comp I stuck it into it would do this and post my name (or a message I put in) as the high score?

If all you want is your name in the high scores, edit
HKEY_CURRENT_USER\Software\Microsoft\winmine\Name*

HKEY_CURRENT_USER\Software\Microsoft\winmine\Time*
in the registry. Minesweeper high scores are recorded seperately for each user, though...

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007

So how does it work...? By checking some cheat pixel? It'd be a whole lot more interesting if it (tried) to solve it by using only what the user sees...

Indeed. That would be very impressive. It would be quite do-able though, just alot of work. The logic involved is fairly simple. ImageSearch for squares with numbers. Count the neighboring marked mines (ImageSearch) again. Count the neighboring unexplored squares. If marked mines = Number then explore any neighboring unexplored squares. If marked mines + unexplored squares = Number then mark all unexplored squares as mines.

Somewhat harder would be programing it to make the best guess in cases where there was no clear correct choice.

Thrillski
  • Members
  • 195 posts
  • Last active: Mar 17 2016 03:47 PM
  • Joined: 18 Jul 2007
Good morning!

If you do a search for a program called "Autosweeper" you will find a minesweeper solver that actually solves it versus using a cheat code and pixel shade tricks.

If you turn guessing on, it will attempt to solve the full puzzle and will muck up sometimes when it has to guess.

On my machine, it averages about 75-80 seconds to do the expert minefield.

1900 lines of code make it migraine-inducing but to this newbie autohotkey programmer, it definitely is interesting.

Have a nice day!
Thrillski

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

to this newbie autohotkey programmer

Who is this AHK programmer? Me? (note that I have 1 year with AHK) Or you? I don’t know.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

to this newbie autohotkey programmer, it definitely is interesting.

Who is this AHK programmer? Me? (note that I have 1 year with AHK) Or you? I don’t know.

I'm sure Thrillski wouldn't know whether you found it interesting or not...

Thrillski
  • Members
  • 195 posts
  • Last active: Mar 17 2016 03:47 PM
  • Joined: 18 Jul 2007
The poster of the Autosweeper is bhao. I figured that if you actually did the search, the author would be discovered.

Have a nice day!
Thrillski

outspoken
  • Members
  • 4 posts
  • Last active: Jan 04 2009 11:33 AM
  • Joined: 04 Jan 2009
This code or the cheat for that matter will not work on Vista :(

Ref:http://erpman1.tripod.com/current/winmine.htm

Basically the current version of Minesweeper on Vista has no known cheats.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Basically the current version of Minesweeper on Vista has no known cheats.

That would make it worth playing.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Look at this, how big boys attack the problem...