SRL Archive - Other Scripts

Actual Minesweeper Script


masquerader

2006-12-03 21:42:35

after hearing that wallfire had made one that would actually solve minesweeper back at kaitnieks, i decided to try my hand at this puzzle

it works 100% as long as it doesnt have to guess
i coded it in under 2 hours, but i think the logic is solid
if it says guessing, thats bad, because it just picks a random unknown square

anyway, it is under 200 lines and solves the beginner level in 5 seconds on my computer :)
i have to say i am pretty proud of it

program minesweep;
//by masquerader
//built for xp minesweeper
//for best results, run after youve clicked a couple times to open the board up a bit
const
boardwidth=9;
boardheight=9;
{ Beginner: 9 × 9
Intermediate: 16 × 16
Expert: 30 × 16 }

var
board: array[0..boardwidth-1] of array[0..boardheight-1] of integer;
w,h,winbmp,losebmp:integer;

function win:boolean;
var
x,y:integer;
begin
result:=findbitmap(winbmp,x,y);
end;

function lose:boolean;
var
x,y:integer;
begin
result:=findbitmap(losebmp,x,y);
end;

procedure getboard;
var
i,j,x,y,x1,y1:integer;
begin
for i:=0 to w do
begin
for j:=0 to h do
begin
x1:=16*i+15;
y1:=16*j+105;
if(findcolor(x,y,0,x1,y1,x1+15,y1+8))then
begin
if(findcolor(x,y,255,x1,y1,x1+15,y1+8))then
board[i][j]:=99
else
board[i][j]:=7;
end else
if(findcolor(x,y,16777215,x1,y1,x1+15,y1+8))then
board[i][j]:=-1
else
if(findcolor(x,y,16711680,x1,y1,x1+15,y1+8))then
begin
board[i][j]:=1
end else
if(findcolor(x,y,32768,x1,y1,x1+15,y1+8))then
board[i][j]:=2
else
if(findcolor(x,y,255,x1,y1,x1+15,y1+8))then
board[i][j]:=3
else
if(findcolor(x,y,8388608,x1,y1,x1+15,y1+5))then
board[i][j]:=4
else
if(findcolor(x,y,128,x1,y1,x1+15,y1+5))then
board[i][j]:=5
else
if(findcolor(x,y,8421376,x1,y1,x1+15,y1+5))then
board[i][j]:=6
else
if(findcolor(x,y,8421504,x1,y1,x1+15,y1+5))then
board[i][j]:=8
else
board[i][j]:=0;
end;
end;
end;

procedure guess;
var
i,j,l:integer;
unknowncoords:tpointarray;
begin
writeln('guessing...');
for j:=0 to h do
for i:=0 to w do
begin
if(board[i][j]=-1)then
begin
l:=l+1;
setarraylength(unknowncoords,l);
unknowncoords[l-1].x:=i;
unknowncoords[l-1].y:=j;
end;
end;
i:=random(l);
clickmouse(16*unknowncoords[i].x+20,16*unknowncoords[i].y+110,true)
getboard;
end;

function check(number:integer):boolean;
var
checkx,checky,i,j,c,x,y,unknown,bombs:integer;
unknowncoords:tpointarray;
begin
for j:=0 to h do
for i:=0 to w do
begin
if(board[i][j]=number)then
begin
unknown:=0;
bombs:=0;
setarraylength(unknowncoords,0)
for x:=-1 to 1 do
for y:=-1 to 1 do
begin
checkx:=i+x;
checky:=j+y;
if((checkx>-1)and(checkx<w+1)and(checky>-1)and(checky<h+1))then
begin
if(board[checkx][checky]=99)then
bombs:=bombs+1
else
if(board[checkx][checky]=-1)then
begin
setarraylength(unknowncoords,unknown+1)
unknowncoords[unknown].x:=checkx;
unknowncoords[unknown].y:=checky;
unknown:=unknown+1;
end;
end;
end
if(unknown+bombs=number)then
begin
for c:=0 to unknown-1 do
begin
clickmouse(16*unknowncoords[c].x+20,16*unknowncoords[c].y+110,false);
board[unknowncoords[c].x][unknowncoords[c].y]:=99;
result:=true;
end;
end;
if(bombs=number)then
begin
for c:=0 to unknown-1 do
begin
clickmouse(16*unknowncoords[c].x+20,16*unknowncoords[c].y+110,true);
getboard;
result:=true;
end;
end;
end;
end;
end;

begin
activateclient;
wait(100);
winbmp:= BitmapFromString(17, 17, 'z78DA7336702608F103670A203270' +
'0302DC2479361263327E1B69673E317651CB7CFC7661DA421EC06' +
'50BA9E613AF065325B2087E738897C5A512D90D16406840203D23' +
'03887AFC6142DD78C71F62B4B3057F4A263EEF939792494D75F4C' +
'F9BB42E67C82B33695726D3A27E0100AF9A822C');
losebmp:= BitmapFromString(17, 17, 'z78DA7336702608F103670A203270' +
'0302DC2479361263327E1B69673E3176619A8F2C82CB76FC6A30E' +
'D22C61CFC2EC7EF23F2DC49AA1A6AC505312970E4D8821F109FAA' +
'699DC688C92FC4870C797973709633E49599B42B936951BF0000D' +
'9168AA4');
w:=boardwidth-1;
h:=boardheight-1;
getboard;
repeat
repeat
repeat
repeat
repeat
repeat
repeat
repeat
until(not(check(1)));
until(not(check(2)));
until(not(check(3)));
until(not(check(4)));
until(not(check(5)));
until(not(check(6)));
until(not(check(7)));
if(win)then
begin
writeln('yay, we win');
terminatescript;
end;
guess;
until(lose)
writeln('oh no, we lost');
end.

TOB

2006-12-03 22:21:30

WOW! Awesome man, just awesome.

Boreas

2006-12-03 22:23:21

Solved beginner in 8 and then 5. Guessed and lost on intermediate, then started going well but got [Runtime Error] : Out Of Range in line 95 in script.

setarraylength(unknowncoords,l); line 89

TOB

2006-12-03 22:26:25

I commented out the waits and beat beginner in 1 second.

You got the runtime error because you forgot to change boardwidth/height consts.

Boreas

2006-12-03 22:40:27

lol i didnt even see that, i assumed it got it by the client size

Cheesehunk

2006-12-03 22:42:41

From: TOB
I commented out the waits and beat beginner in 1 second.

You got the runtime error because you forgot to change boardwidth/height consts.

I commented them out and beat Intermediate in 7 seconds. Thats almost faster than my legit beginner record. Very impressive masquerader! (Why does your name have to be so hard to spell? :p) I would like to see Wallfire's script, and make a comparison.

masquerader

2006-12-03 22:43:53

haha, i guess you dont need those waits
ill take them out

edit:
sometimes it wont notice that you lost then :(

Cheesehunk

2006-12-03 22:56:13

From: masquerader
haha, i guess you dont need those waits
ill take them out

edit:
sometimes it wont notice that you lost then :(

Still notices for me.. :) Maybe just add a smaller wait instead?

masquerader

2006-12-03 23:17:32

the losing bug isnt a big deal :p

the bad guessing is annoying however
first, i didnt think the probability thing would be doable, because thats where minesweeper can get complicated,
but i started thinking to give each box a ranking, by dividing the mines left by unknowns, that would give you the best chance of winning
(not 100%, but better)

right now the guess procedure is retarded. it probably wont even open up a new area, because its just random

alas, my weekend is over, and its time to work on other things
i'll implement this whenever i can

TOB

2006-12-04 02:54:25


*******************************
** Time To End Game: 30922ms
** Board width: 30
** Board height: 16
** Times guessed: 2
** Guess 2 screwed us over
** Missed Bombs: 3
** Correct Flags: 96
** Wrong Flags: 0
*******************************

This is the script with progress report:

[php]program minesweep;
//by masquerader
//built for xp minesweeper
//for best results, run after youve clicked a couple times to open the board up a bit
const
boardwidth = 30;
boardheight = 16;
{ Beginner: 9 × 9
Intermediate: 16 × 16
Expert: 30 × 16 }
highscoresname = 'Pwn it up';



var
board: array[0..boardwidth-1] of array[0..boardheight-1] of integer;
w, h, winbmp, losebmp, starttime, guesses: Integer;
guessdeath: string;


function win:boolean;
var
x,y:integer;
begin
result:=findbitmap(winbmp,x,y);
end;

function lose:boolean;
var
x,y:integer;
begin
result:=findbitmap(losebmp,x,y);
end;

procedure getboard;
var
i,j,x,y,x1,y1:integer;
begin
for i:=0 to w do
begin
for j:=0 to h do
begin
x1:=16*i+15;
y1:=16*j+105;
if(findcolor(x,y,0,x1,y1,x1+15,y1+8))then
begin
if(findcolor(x,y,255,x1,y1,x1+15,y1+8))then
board[j]:=99
else
board[j]:=7;
end else
if(findcolor(x,y,16777215,x1,y1,x1+15,y1+8))then
board[j]:=-1
else
if(findcolor(x,y,16711680,x1,y1,x1+15,y1+8))then
begin
board[j]:=1
end else
if(findcolor(x,y,32768,x1,y1,x1+15,y1+8))then
board[j]:=2
else
if(findcolor(x,y,255,x1,y1,x1+15,y1+8))then
board[j]:=3
else
if(findcolor(x,y,8388608,x1,y1,x1+15,y1+5))then
board[j]:=4
else
if(findcolor(x,y,128,x1,y1,x1+15,y1+5))then
board[j]:=5
else
if(findcolor(x,y,8421376,x1,y1,x1+15,y1+5))then
board[j]:=6
else
if(findcolor(x,y,8421504,x1,y1,x1+15,y1+5))then
board[j]:=8
else
board[j]:=0;
end;
end;
end;

procedure guess;
var
i,j,l:integer;
unknowncoords:tpointarray;
begin
ClearDebug;
writeln('Guessing…');
for j:=0 to h do
for i:=0 to w do
begin
if(board[j]=-1)then
begin
l:=l+1;
setarraylength(unknowncoords,l);
unknowncoords[l-1].x:=i;
unknowncoords[l-1].y:=j;
end;
end;
guesses:= guesses + 1;
i:=random(l);
clickmouse(16*unknowncoords.x+20,16*unknowncoords.y+110,true)
//wait(200);
getboard;
ClearDebug;
if(lose) then
guessdeath:= 'Guess ' + IntToStr(guesses) + ' screwed us over';
end;

function check(number:integer):boolean;
var
checkx,checky,i,j,c,x,y,unknown,bombs:integer;
unknowncoords:tpointarray;
begin
for j:=0 to h do
for i:=0 to w do
begin
if(board[j]=number)then
begin
unknown:=0;
bombs:=0;
setarraylength(unknowncoords,0)
for x:=-1 to 1 do
for y:=-1 to 1 do
begin
checkx:=i+x;
checky:=j+y;
if((checkx>-1)and(checkx<w+1)and(checky>-1)and(checky<h+1))then
begin
if(board[checkx][checky]=99)then
bombs:=bombs+1
else
if(board[checkx][checky]=-1)then
begin
setarraylength(unknowncoords,unknown+1)
unknowncoords[unknown].x:=checkx;
unknowncoords[unknown].y:=checky;
unknown:=unknown+1;
end;
end;
end
if(unknown+bombs=number)then
begin
for c:=0 to unknown-1 do
begin
clickmouse(16*unknowncoords[c].x+20,16*unknowncoords[c].y+110,false);
board[unknowncoords[c].x][unknowncoords[c].y]:=99;
//wait(100);
result:=true;
end;
end;
if(bombs=number)then
begin
for c:=0 to unknown-1 do
begin
clickmouse(16*unknowncoords[c].x+20,16*unknowncoords[c].y+110,true);
//wait(100);
getboard;
result:=true;
end;
end;
end;
end;
end;

type
GameSquare = record
row, col, x, y :integer;
end;

procedure ProgressReport;
var
x, y, i, numOfSquares, flags, missedbombs, wrongflags, worktime: Integer;
Pos: array[1..(boardwidth * boardheight)] of GameSquare;
begin
worktime := GetSystemTime - starttime;
ClearDebug;
wait(100);
SendKeys(highscoresname + #13);
Wait(100);
SendKeys(#13);
wait(100);
numOfSquares:= boardwidth * boardheight;
for i:= 1 to numOfSquares do
begin
Pos.row:= ((i - 1) / boardwidth) + 1;
Pos.col:=((i + (boardwidth - 1)) mod boardwidth);
Pos.x:= 14 + (Pos.col * 16);
Pos.y:= 103 + ((Pos.row - 1) * 16);
if(GetColor(Pos.x + 7, Pos.y + 7) = 16777215) then
missedbombs := missedbombs + 1;
if((GetColor(Pos.x + 8, Pos.y + 6) = 255) and (GetColor(Pos.x + 8, Pos.y + 11) = 0)) then
flags := flags + 1;
if(GetColor(Pos.x + 10, Pos.y + 10) = 255) then
wrongflags := wrongflags + 1;
end;
Writeln('*******************************');
Writeln('** Time To End Game: ' + IntToStr(worktime) + 'ms');
Writeln('** Board width: ' + IntToStr(boardwidth));
Writeln('** Board height: ' + IntToStr(boardheight));
Writeln('** Times guessed: ' + IntToStr(guesses));
if(not(guessdeath = '')) then
Writeln('** ' + guessdeath);
Writeln('** Missed Bombs: ' + IntToStr(missedbombs));
Writeln('** Correct Flags: ' + IntToStr(flags));
Writeln('** Wrong Flags: ' + IntToStr(wrongflags));
Writeln('*******************************');
end;

begin
ClearDebug;
activateclient;
wait(100);
winbmp:= BitmapFromString(17, 17, 'z78DA7336702608F103670A203270' +
'0302DC2479361263327E1B69673E317651CB7CFC7661DA421EC06' +
'50BA9E613AF065325B2087E738897C5A512D90D16406840203D23' +
'03887AFC6142DD78C71F62B4B3057F4A263EEF939792494D75F4C' +
'F9BB42E67C82B33695726D3A27E0100AF9A822C');
losebmp:= BitmapFromString(17, 17, 'z78DA7336702608F103670A203270' +
'0302DC2479361263327E1B69673E3176619A8F2C82CB76FC6A30E' +
'D22C61CFC2EC7EF23F2DC49AA1A6AC505312970E4D8821F109FAA' +
'699DC688C92FC4870C797973709633E49599B42B936951BF0000D' +
'9168AA4');
starttime := GetSystemTime;
w:=boardwidth-1;
h:=boardheight-1;
getboard;
repeat
repeat
repeat
repeat
repeat
repeat
repeat
repeat
if(lose) then
Break;
until(not(check(1)));
if(lose) then
Break;
until(not(check(2)));
if(lose) then
Break;
until(not(check(3)));
if(lose) then
Break;
until(not(check(4)));
if(lose) then
Break;
until(not(check(5)));
if(lose) then
Break;
until(not(check(6)));
if(lose) then
Break;
until(not(check(7)));

if(win)then
begin
Writeln('yay, we win');
Break;
end;
guess;
until(lose)

writeln('oh no, we lost');

ProgressReport;
end.[/php]

_______

OMG!!!

On about the 404353453 time running this:


*******************************
** Time To End Game: 41875ms
** Board width: 30
** Board height: 16
** Times guessed: 8
** Missed Bombs: 0
** Correct Flags: 79
** Wrong Flags: 0
*******************************
^^^ That is messed up because I didn't send the name for high score before checking colors, I fixed that now.

Roberty Bob

2006-12-04 02:59:58

good job… I always appreciate non-RS apps. Went close to solving expert a few times but had to guess too often

Boreas

2006-12-05 00:21:50

*******************************
** Time To End Game: 24312ms
** Board width: 30
** Board height: 16
** Times guessed: 3
** Missed Bombs: 0
** Correct Flags: 99
** Wrong Flags: 0
*******************************

haha expert in 24!!!!!!!

phantombmx

2006-12-23 00:43:19

hmm i might have to give this a shot.. this has always been a good game to try..

addy

2007-01-16 15:48:15

ss

Starblaster100

2007-01-16 16:20:20

WOW!
Awesome Job Dude.

Just ran on Beginner and it failed on the last square lol… Thanks OK, its the Code which counts :D :D

Great job once again

Fusion89k

2007-01-18 19:33:14

Thsi is awesome man it never lost for me got awesome stats.

Beginner: 2 secs
Intermediate: 7 secs
Expert: 13 secs

Very good dude

firedemon

2007-02-27 19:55:14

lol awesome script 1 second for beginner lolz

Smartzkid

2007-02-27 20:25:53

*******************************
** Time To End Game: 38422ms
** Board width: 30
** Board height: 16
** Times guessed: 14
** Missed Bombs: 0
** Correct Flags: 99
** Wrong Flags: 0
*******************************

Awesome script!

Ron

2007-02-27 22:52:26

Cool script, but I get the same runtime error as Boreas. :(

Successfully compiled
guessing…
guessing…
guessing…
guessing…
guessing…
oh no, we lost
Successfully executed
Successfully compiled
guessing…
guessing…
guessing…
guessing…
guessing…
guessing…
guessing…
guessing…
[Runtime Error] : Out Of Range in line 95 in script

Boreas

2007-02-27 23:01:28

Ron we are nubs that forgot to change board size.

Ron

2007-02-27 23:12:59

Oh. Heh. :D

Shouldn't there be some kind of fail safe to say, "Bad width and height!" :p

Drew_Dawg

2007-03-06 17:57:41

flippin sweet dude!

der alt nite

2007-03-17 13:05:57

Wow! O_o I just beat beginner in 1 second and it rarely messes up. But like you said, the only time it messes up is when it has to guess. But that's miesweeper. :/

Although when I opened the board up a lil' bit and then did expert. I got this message:

"[Runtime Error] : Out Of Range in line 95 in script"

ranojit

2007-03-24 14:15:42

omg this is crazy. now all my friends think i got skills. lol

XcanadamanX

2007-03-24 17:12:39

i havent gotten it to work once:( oh well maybe my minesweeper is different

IEatJ00erBaybees

2007-03-24 17:24:46

Godly SCRIPT!

BEGINNER IN 1 SECOND!
Now you can be: In god Mode. :O

EotT

2007-03-26 16:48:11

doens't work in vista because of the new minesweeper :(


Smartzkid

2007-03-26 16:55:59

Wouldn't that be a little obvious?

botmaster

2007-03-26 17:27:33

Who uses vista anyways :p. IMHO vista sucks, bigtime. Any OS that sells less than 30 million copies in the first few weeks of release sucks. Vista sold only 20 mill, GLOBALLY. No way I'm catching the Vista craze.

R0b0t1

2007-03-28 20:21:20

Nice script, and Vista does suck

Rune Hacker

2007-04-02 22:57:15

[Runtime Error] : Out Of Range in line 95 in script C:\Documents and Settings\*****\Desktop\Minesweep Script.scar
Can you tell me why this pops up? I have SRL and everything set up perfectly. Once its half way solving minesweeper, this error pops up and wotn continue.

danrox2004

2007-04-03 08:58:12

From: rune hacker
[Runtime Error] : Out Of Range in line 95 in script C:\Documents and Settings\*****\Desktop\Minesweep Script.scar
Can you tell me why this pops up? I have SRL and everything set up perfectly. Once its half way solving minesweeper, this error pops up and wotn continue.


I think you have set the sizes wrong, look at top of script. Change the constants to match the board size..

By the way… I'm gonna try to write something to do minesweeper tonight… But it will work a very different way :P

Very nice code!!!!!!!!

I just wrote this up… Uses the cheat - we are at a cheating forum.
Works about 50% of the time. DO NOT DRAG CROSSHAIR OVER CLIENT WINDOW! if it doesnt work im preety sure it has to do with the sending keys…
program New;
const
Level = 3;
{1 for beginner, 2 for intermediate, 3 for expert}

var
Desktop, MineSweep: integer;
square: integer;
x, y: integer;
FirstTilex, FirstTiley: integer;
i: integer;
width, height: integer;
time: integer;
win, lose: integer;

procedure CheckWin;
begin
win := BitmapFromString(4, 4, 'z78DA333000013720809106384490C5' +
'31D998BA2C80D0C00000F82D14D1');
lose := BitmapFromString(11, 4, 'z78DA73737373333070C320F103371' +
'CBA90F592A78618BDB8DC49BC2E4C12003A013D31');
if(FindBitmap(win, x, y))then
Writeln('Yay we won');
if(FindBitmap(lose, x, y))then
Writeln('Noo! We lost');
FreeBitmap(Win);
FreeBitmap(Lose);
end;

procedure ActivateCheat;
begin
KeyDown(GetKeyCode('x'));
Wait(50)
KeyUp(GetKeyCode('x'));
Wait(20);
KeyDown(GetKeyCode('y'));
Wait(50)
KeyUp(GetKeyCode('y'));
Wait(20);
KeyDown(GetKeyCode('z'));
Wait(50)
KeyUp(GetKeyCode('z'));
Wait(20);
KeyDown(GetKeyCode('z'));
Wait(50)
KeyUp(GetKeyCode('z'));
Wait(20);
KeyDown(GetKeyCode('y'));
Wait(50)
KeyUp(GetKeyCode('y'));
Wait(20);
KeyDown(VK_Shift);
Wait(50)
KeyUp(VK_Shift)
Wait(20);
KeyDown(VK_RETURN);
Wait(50);
KeyUp(VK_RETURN);
end;

Procedure UhOh(Reason: String);
begin
Writeln('Script Terminated: ' + Reason);
end;

procedure Setup;
begin
case Level of
1: begin
Width := 9;
Height := 9;
end
2: begin
Width := 16;
Height := 16;
end
3: begin
Width := 30;
Height := 16;
end
end
Wait(100);
ActivateCheat;
Square := BitmapFromString(3, 7, 'z78DA73367026082DC8826E60003101' +
'998D2C0B00CD8C1BE5');
if(not FindBitmap(Square, x, y)) then
UhOh('Could not find minesweeper');
FirstTilex := x - 7;
FirstTiley := y + 13;
//61, 224 - 77, 224
FreeBitmap(Square); //61, 260
end;

procedure GetXY(sq : integer; var x, y: integer);
begin
x := FirstTilex + ((trunc(((sq - 1)/Height))) * 16);
y := FirstTiley + (((sq - 1) mod Height) * 16);
end;

procedure CheckPixel;
begin
if(GetColor(0, 0) = 0) then
ClickMouse(x, y, False);
if(GetColor(0, 0) = 16777215) then
ClickMouse(x, y, True);
end;

begin
Setup;
Wait(1000) ;
Time := GetSystemTime;
for i := 1 to (Width * Height) do
begin
GetXY(i, x, y);
if((GetColor((x - 6), (y - 7)) = 16777215)) then
begin
MoveMouse(x, y);
CheckPixel;
end
end
Writeln('Script took ' + IntToStr((GetSystemTime - Time)) + 'ms to complete');
CheckWin;
end.

When it works it gets under 1 second on expert =)

EDIT:
Removed a wait, now it get 70 ms

Boreas

2007-04-03 17:49:40

Yoho posted a cheat using one before masq posted this :p

danrox2004

2007-04-03 19:54:00

From: Boreas
Yoho posted a cheat using one before masq posted this :p


Heh cool, but his does expert in 20 seconds and mine does it in 1 second =)