Jump to content

My Very Own Minesweeper Game .... Just Wanted To Share With Cyber Sangat


SarabjeetS
 Share

Recommended Posts

Ok , first of all I am sorry I could not share the actual game with you . Though I have the exe and setup , there's no way I can share it with you guyz . Atleast , I don't know how to ..

Game developed in Visual Basic 6.0

Screenshots at various phases of game :

untitled-1.jpg?t=1306154603

I developed it in some 4 days time ...

Link to comment
Share on other sites

Why are you still using VB6? Learn something like Java, C# etc VB6 is not for serious developers.

rowingmachine.gif

Actually , I can develop the same game in Java also , using Swing ... . I don't know C## . Knows somethings about VC++

The problem with Java is that I have to actually make all the GUI and give them co-ordinates . So , too much efforts goes into making GUI. , unless you are using JavaBeans ! But , still I agree that Java is too beautiful of a language !

Ok , I will share the coding of usercontrol of this game with you guyz !!


Option Explicit
Dim r, c As Integer
Dim gridlines_count As Integer
Dim mines_count As Integer
Dim labels_count As Integer
Dim grid_array() As Integer
Dim gridcheck_array() As Integer
Dim firstclick As Boolean
Dim mine_pos_array() As String

Public Sub Init(ByVal row As Integer, ByVal col As Integer, ByVal mines As Integer)
   Dim i, j As Integer
   grid_button(0).Visible = False
   grid_button(0).Enabled = True
   grid_button(0).Picture = LoadPicture("")

   For i = 0 To r - 1
       For j = 0 To c - 1
           Dim curr_button_index As Integer
           curr_button_index = i * c + j
           If curr_button_index <> 0 Then Unload grid_button(curr_button_index)
       Next
   Next

   For i = 1 To gridlines_count 'ERASE ALL GRID LINES'
       Unload grid_line(i)
   Next

   ReDim grid_array(1 To col, 1 To row)
   ReDim gridcheck_array(1 To col, 1 To row)
   ReDim mine_pos_array(1 To mines)

   For i = 1 To mines_count
       On Error Resume Next
       Unload mine(i)
   Next

   For i = 1 To labels_count - 1
       On Error Resume Next
       Unload lblNum(i)
   Next

   labels_count = 0
   r = row
   c = col
   mines_count = mines
   UserControl_Initialize

End Sub

Private Sub grid_button_MouseDown(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
   If Button = 1 Then 'left button is clicked'
       grid_button(Index).Visible = False

       Dim xpos, ypos As Integer
       Dim co_ord
       co_ord = ReturnXY(Index)
       gridcheck_array(co_ord(0), co_ord(1)) = 1

       If firstclick = True Then '////clicked first time in button grid , hence need to place mines randomly'
           PlaceMines co_ord(0), co_ord(1)
           DrawNumbers
           firstclick = False
       End If

       '///check whether clicked on a MINE'
       If grid_array(co_ord(0), co_ord(1)) = -1 Then
           mines_count = mines_count + 1
           Load mine(mines_count)
           mine(mines_count).Picture = imgRedMine.Picture
           mine(mines_count).left = (co_ord(0) - 1) * grid_button(0).Width
           mine(mines_count).top = (co_ord(1) - 1) * grid_button(0).Height
           mine(mines_count).Visible = True
           mine(mines_count).ZOrder (0)

           '////show all mines positions'
           Dim i, j As Integer
           For i = 1 To mines_count - 1
               Dim temp
               temp = Split(mine_pos_array(i), ",")
               grid_button(ReturnIndex(Val(temp(0)), Val(temp(1)))).Visible = False
           Next
           UserControl.Enabled = False
       Else
           tmrFloodFillEmpty.Enabled = True
       End If
   End If
End Sub

Private Sub tmrFloodFillEmpty_Timer()
   Dim i As Integer
   For i = 1 To 5
       f1
   Next
End Sub

Private Sub f1()
   On Error Resume Next

   Dim x, y As Integer
   For y = 1 To r
       For x = 1 To c
           If gridcheck_array(x, y) = 1 And grid_array(x, y) = 0 Then
               gridcheck_array(x - 1, y) = 1
               gridcheck_array(x + 1, y) = 1
               gridcheck_array(x, y - 1) = 1
               gridcheck_array(x, y + 1) = 1

               gridcheck_array(x + 1, y + 1) = 1
               gridcheck_array(x - 1, y - 1) = 1
               gridcheck_array(x - 1, y + 1) = 1
               gridcheck_array(x + 1, y - 1) = 1

               gridcheck_array(x, y) = -1
               grid_button(ReturnIndex(x, y)).Visible = False

           ElseIf gridcheck_array(x, y) = 1 And grid_array(x, y) > 0 Then
               grid_button(ReturnIndex(x, y)).Visible = False
               gridcheck_array(x, y) = -1
           End If
       Next
   Next

   '//// check whether player WINS game ...'
   Dim count As Integer
   For y = 1 To r
       For x = 1 To c
           If grid_button(ReturnIndex(x, y)).Visible = True Then count = count + 1
       Next
   Next
   If count = mines_count Then
       For x = 1 To mines_count
           Dim temp
           temp = Split(mine_pos_array(x), ",")
           grid_button(ReturnIndex(Val(temp(0)), Val(temp(1)))).Picture = imgRedFlag.Picture
           UserControl.Enabled = False
       Next
       tmrFloodFillEmpty.Enabled = False
   End If
End Sub

Private Sub UserControl_Initialize()
   On Error Resume Next
   If (r = 0 And c = 0) Then
       ReDim grid_array(1 To 10, 1 To 10)
       ReDim gridcheck_array(1 To 10, 1 To 10)
       ReDim mine_pos_array(1 To 5)
       r = 10
       c = 10
   End If
   firstclick = True
   UserControl.Enabled = True

   Dim x, y As Integer
   For y = 0 To r - 1
       For x = 0 To c - 1

           Dim curr_button_index As Integer
           curr_button_index = y * c + x
           If curr_button_index <> 0 Then Load grid_button(curr_button_index) 'if button is not grid_but(0)'
           grid_button(curr_button_index).left = grid_button(0).Width * x
           grid_button(curr_button_index).top = grid_button(0).Height * y
           grid_button(curr_button_index).Visible = True
       Next
   Next
   grid_button(20).Visible = True

   'draw grid lines'
   gridlines_count = r + c + 2

   Dim i As Integer
   For i = 0 To r 'horizontal lines'
       Load grid_line(i + 1)
       grid_line(i + 1).X1 = 0
       grid_line(i + 1).Y1 = i * grid_button(0).Height
       grid_line(i + 1).X2 = UserControl.Width
       grid_line(i + 1).Y2 = grid_line(i + 1).Y1
       grid_line(i + 1).Visible = True
   Next

   Dim temp As Integer
   For i = (r + 1) To gridlines_count - 1 'vertical lines'
       Load grid_line(i + 1)
       grid_line(i + 1).X1 = temp
       grid_line(i + 1).Y1 = 0
       grid_line(i + 1).X2 = grid_line(i + 1).X1
       grid_line(i + 1).Y2 = UserControl.Height
       grid_line(i + 1).Visible = True
       temp = temp + grid_button(i).Width
   Next

'    UserControl height/width is 15 times the scaleheight/scalewidth'
   UserControl.Height = 15 * (r * grid_button(0).Width)
   UserControl.Width = 15 * (c * grid_button(0).Height)
End Sub

'//////PLACE MINES RANDOMLY IN THE GRID . initX and initY hold the values of X and Y coordinate of clicked button so that mine is not placed there'
Private Sub PlaceMines(ByVal initX As Integer, ByVal initY As Integer)
   If mines_count = 0 Then mines_count = 5

   Randomize
   Dim i, j, counter As Integer
   For i = 1 To mines_count
       Dim left, top As Integer
       left = Int(Rnd * (c - 1)) + 0
       top = Int(Rnd * (r - 1)) + 0

       If (left + 1 <> initX) And (top + 1 <> initY) Then
           Dim already_mined As Boolean
           For j = 1 To counter
               If mine_pos_array(j) = Str(left + 1) + "," + Str(top + 1) Then
                   already_mined = True
                   Exit For
               End If
           Next

           If already_mined = False Then
               grid_array(left + 1, top + 1) = -1 ' /// -1 in grid_array indicates a mine'
               Load mine(i)

               mine(i).left = left * grid_button(0).Width
               mine(i).top = top * grid_button(0).Height
               mine(i).Visible = True
               counter = counter + 1
               mine_pos_array(counter) = Str(left + 1) + "," + Str(top + 1)
           Else
               i = i - 1
               already_mined = False
           End If
       Else
           i = i - 1
       End If
   Next
End Sub

Private Sub DrawNumbers() ' draws numbers around mines'
   On Error Resume Next

   Dim x, y As Integer
   For y = 1 To r
       For x = 1 To c
           If grid_array(x, y) = -1 Then
               If grid_array(x - 1, y - 1) <> -1 Then grid_array(x - 1, y - 1) = grid_array(x - 1, y - 1) + 1
               If grid_array(x + 1, y + 1) <> -1 Then grid_array(x + 1, y + 1) = grid_array(x + 1, y + 1) + 1
               If grid_array(x - 1, y + 1) <> -1 Then grid_array(x - 1, y + 1) = grid_array(x - 1, y + 1) + 1
               If grid_array(x + 1, y - 1) <> -1 Then grid_array(x + 1, y - 1) = grid_array(x + 1, y - 1) + 1

               If grid_array(x - 1, y) <> -1 Then grid_array(x - 1, y) = grid_array(x - 1, y) + 1
               If grid_array(x + 1, y) <> -1 Then grid_array(x + 1, y) = grid_array(x + 1, y) + 1
               If grid_array(x, y - 1) <> -1 Then grid_array(x, y - 1) = grid_array(x, y - 1) + 1
               If grid_array(x, y + 1) <> -1 Then grid_array(x, y + 1) = grid_array(x, y + 1) + 1
           End If
       Next
   Next


   '//////////////testing'
   Form2.Label1.Caption = ""
   For y = 1 To r
       For x = 1 To c
           Form2.Label1.Caption = Form2.Label1.Caption + Str(grid_array(x, y)) + "  "
       Next
       Form2.Label1.Caption = Form2.Label1.Caption + vbNewLine
   Next
   '////////////'

   ' //// Generate number Labels'

   For y = 1 To r
       For x = 1 To c
           If (grid_array(x, y) <> 0) Or (grid_array(x, y) <> -1) Then
               labels_count = labels_count + 1

               Load lblNum(labels_count)
               lblNum(labels_count).left = (x - 1) * grid_button(0).Width
               lblNum(labels_count).top = (y - 1) * grid_button(0).Height
               lblNum(labels_count).Visible = True
               lblNum(labels_count).Caption = grid_array(x, y)
               lblNum(labels_count).ForeColor = color_of_number(grid_array(x, y))
           End If
       Next
   Next
   'frmGame.Caption = labels_count'

   Dim i As Integer
   For i = 1 To labels_count
       If lblNum(i).Caption = "0" Then Unload lblNum(i)
   Next
End Sub

Private Function color_of_number(ByVal key As Integer) As ColorConstants
   If key = 1 Then color_of_number = vbBlue
   If key = 2 Then color_of_number = RGB(0, 150, 0)
   If key = 3 Then color_of_number = vbRed
End Function

'/////// ReturnIndex function TAKES X AND Y COORDINATES OF BUTTON AND RETURNS ITS INDEX IN CONTROL ARRAY'
Private Function ReturnIndex(ByVal x As Integer, ByVal y As Integer) As Integer
   ReturnIndex = (y - 1) * c + (x - 1)
End Function

'/////// ReturnXY function TAKES INDEX OF BUTTON IN CONTROL ARRAY AND RETURNS ITS X AND Y COORDINATES'
Private Function ReturnXY(ByVal Index As Integer) As Variant()
   Dim x, y As Integer
   Dim res As Variant

   x = (Index Mod c) + 1
   y = Int(Index / c) + 1

   res = Array(x, y)
   ReturnXY = res
End Function

I know it maybe not that efficient code ... but hey it works ! happyretard.gif

Link to comment
Share on other sites

Nice going! :trophy:

The problem with Java is that I have to actually make all the GUI and give them co-ordinates . So , too much efforts goes into making GUI. , unless you are using JavaBeans !
That is not true at all. As far as I know, you use java beans for web based projects only so you wouldn't use java beans in your desktop application. Java Swing offers many layout plugins which take care of the sizing and placement issues for you. It's quite easy actually.

Balait_da_sher is absolutely correct, you need to learn C# or Java EE, that's where the good paying jobs are when it comes to programming. Stay away from VC++ as well as it is a dying technology.

What year of your course are you in?

Link to comment
Share on other sites

Nice going! :trophy:

That is not true at all. As far as I know, you use java beans for web based projects only so you wouldn't use java beans in your desktop application. Java Swing offers many layout plugins which take care of the sizing and placement issues for you. It's quite easy actually.

Balait_da_sher is absolutely correct, you need to learn C# or Java EE, that's where the good paying jobs are when it comes to programming. Stay away from VC++ as well as it is a dying technology.

What year of your course are you in?

Not really . Who told you Java Beans is used for webbased projects and not for desktop standalone applications . Maybe , you are confusing Java Beans with NetBeans .

Java Beans provide an easy facility of dragging and dropping controls from control box and changing their properties through property box ...

So , it saves considerable amount of time .

I have completed my course in B.Sc Computer science .. Now , I am planning for M.Sc (master of science ) in my field ... and later on with kirpa I may as well go for MBA after that ! happybouncer.gif

Link to comment
Share on other sites

I stand corrected, it's been so long since I have done desktop applications in Java. Back in University when I built desktop apps in Java, we used Swing and I remember using the layouts which took care of the placement and sizing of all components for me. It was dead simple.

I'm just wondering why you are doing MSC and not MBA after BSC.

Link to comment
Share on other sites

I stand corrected, it's been so long since I have done desktop applications in Java. Back in University when I built desktop apps in Java, we used Swing and I remember using the layouts which took care of the placement and sizing of all components for me. It was dead simple.

I'm just wondering why you are doing MSC and not MBA after BSC.

yes dear I know what you are saying . Components can be placed quite easily using box layout ...

I am going for MSc as that is the only option I see now . I would have loved to go for MCA , but when I realized that prospect , I was too late to prepare for entrance exams .

Why do people frown at M.Sc ? Its not like its going to be loss for me , right ? Also , I can do MBA anytime , right ?

outofideas.gif

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • advertisement_alt
  • advertisement_alt
  • advertisement_alt


×
×
  • Create New...

Important Information

Terms of Use