Blitz Basic Tutes - Minesweeper Program - 1

Graphics 640,480
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Global gn_rows=5
Global gn_cols=6
Global gx0=50
Global gy0=50
Global gw=20
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 click(1)
  If MouseHit(2) Then click(2)
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=gy0
  For r=1 To gn_rows
    x=gx0
    For c=1 To gn_cols
      Color 255,255,255
      Rect x,y,gw,gw,False
      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+gw
    Next
    y=y+gw
  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 click(n)
  y=gy0
  For r=1 To gn_rows
    x=gx0
    For c=1 To gn_cols
      If mouse_in(x,y,x+gw,y+gw)
        grid_state(r,c)=n
      End If
      x=x+gw
    Next
    y=y+gw
  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

Valid HTML 4.0 Strict Valid CSS