// minesweeper, by Rilden
dir=[-11,-10,-9,-1,1,9,10,11]
next={" ":" ",".":"M","M":"."}
board=[0]*111
status=["."]*111
for i in status.indexes
if i<10 or i>99 or i%10==0 then status[i]=" "
end for
revealed=[0]
init=function(cell)
m=0
while m<10
i=floor(rnd*111)
if i!=cell and status[i]=="." and board[i]==0 then
m=m+1
board[i]=-1
end if
end while
for i in range(11,99)
if board[i]<0 then
for d in dir
id=i+d
if board[id]>=0 then board[id]=board[id]+1
end for
end if
end for
end function
draw=function
print " A B C D E F G H I "
print " ."+"-"*17+"."
for row in range(1,9)
b=[]
for j in range(1,9)
cell=row*10+j
ch=" "
if status[cell]!=" " then
ch=status[cell]
else if board[cell]>0 then
ch=str(board[cell])
else if board[cell]<0 then
ch="X"
end if
b=b+[ch]
end for
print str(row)+"|"+b.join(" ")+"|"
end for
print " '"+"-"*17+"'"
end function
reveal=function(cell)
status[cell]=" "
revealed[0]=revealed[0]+1
if board[cell]==0 then
for d in dir
if status[cell+d]=="." then reveal cell+d
end for
end if
end function
first=true
over=false
while true
draw
if over then
print "Game over!"
break
else if revealed[0]>=71 then
print "You won!"
break
end if
choices=input("Choose a location to reveal (XY) or mark (mXY):").upper
s=char("A".code+floor(rnd*9))+str(ceil(rnd*9))
err="Error: expected an input like "+s+" or m"+s
for choice in choices.split
len=choice.len
if len<2 or len>3 then
if len>0 then print err
continue
end if
row=val(choice[-1])
column=choice[-2].code-"A".code+1
if column<1 or row<1 or column>9 or row>9 then
print err
continue
end if
cell=row*10+column
if len==3 and choice[0]=="M" then
status[cell]=next[status[cell]]
else if len==2 then
if first then
init cell
first=false
end if
if board[cell]<0 then over=true
if status[cell]=="." then reveal cell
else
print err
end if
end for
end while