Ex.8
Graphics 640,480
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Global gn_rows=5
Global gn_cols=6
Dim grid(gn_rows+1,gn_cols+1)
Dim grid_state(gn_rows+1,gn_cols+1)
init()
Repeat
display()
Flip
If MouseHit(1) Then left_click()
If MouseHit(2) Then right_click()
Until KeyHit(1)
End
Function init()
For r=0 To gn_rows+1
For c=0 To gn_cols+1
grid(r,c)=-1
grid_state(r,c)=-1
Next
Next
For r=1 To gn_rows
For c=1 To gn_cols
grid(r,c)=0
grid_state(r,c)=0
If Rand(5)=1 Then grid(r,c)=1
Next
Next
End Function
Function display()
Cls
y=50
For r=1 To gn_rows
x=50
For c=1 To gn_cols
If grid_state(r,c)=0 Then
Color 255,255,255
Text x,y,"x"
End If
If grid_state(r,c)=1 Then
If grid(r,c)=0 Then
Color 255,255,255
Text x,y,mines(r,c)
Else
Color 255,0,0
Text x,y,"M"
End If
End If
If grid_state(r,c)=2 Then
Color 0,255,0
Text x,y,"M"
End If
x=x+20
Next
y=y+20
Next
End Function
Function mines(r0,c0)
n=0
For r=r0-1 To r0+1
For c=c0-1 To c0+1
If grid(r,c)=1 Then n=n+1
Next
Next
Return n
End Function
Function left_click()
y=50
For r=1 To gn_rows
x=50
For c=1 To gn_cols
If mouse_in(x,y,x+20,y+20)
grid_state(r,c)=1
End If
x=x+20
Next
y=y+20
Next
End Function
Function right_click()
y=50
For r=1 To gn_rows
x=50
For c=1 To gn_cols
If mouse_in(x,y,x+20,y+20)
grid_state(r,c)=2
End If
x=x+20
Next
y=y+20
Next
End Function
Function mouse_in(x1,y1,x2,y2)
x=MouseX(): y=MouseY()
m=True
If x<x1 Then m=False
If x>x2 Then m=False
If y<y1 Then m=False
If y>y2 Then m=False
Return m
End Function