borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (How to Cancel menu (in systray) when app is lost forcus)Next Topic (Getting Summary Info from a File) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Minesweeper Project: 1. MinePlacement 2. Best score
Poster Message
mpetersen
Level: Guest


icon Minesweeper Project: 1. MinePlacement 2. Best score  Archived to Disk

This is actually a followup to a previous posting about the use of randomization/rnd() used to place mines in an array for minesweeper.  I was able to insert a dowhile loop which limits the placement to 10, but after a few game plays you get an error due to the array exceeding its limits/boundary.

I think I need to clear it out and how can I do this and will I clear it at the beginning of the mineplacement procedure
Im a novice at this language to bear with me.

Also looking for a good approach to track best score/time and player in a file for game winners??  Any good ideas on how to go about this?? I have a timer which displays time so could write this to the file and can use a dialog box to get the players name.  The big question is the sorting of best score ea. time.  Only need to keep the file for the term of the game session till exit the game.

Thanks for any tips or information

Mp

16-04-2002 at 04:34 AM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1617
icon Re: Minesweeper Project: 1. MinePlacement 2. Best score  Archived to Disk

1)
ReDim arrayname(x) ' where x is any number, it'll erase all contents
'ReDim Preserve arrayname(x), Preserve keeps all values previously entered
- or -
Erase arrayname()

2) (scores)
easiest: text file
next easiest: registry or ini file
easiest for sorting: Access DB

as far as sorting, in a database, just run a query. For a text file or registry type of entries, any method would work, including storing the scores and names into a list box, with a sorted property=true.



16-04-2002 at 04:56 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: Minesweeper Project: 1. MinePlacement 2. Best score  Archived to Disk

Hello,

This is my MineSweep Example...Hope you like it...

Create a Form with a little PictureBox Control Named pctCell and index it (0)

Add this code to the form:

Option Explicit

Dim XMaze As Long
Dim YMaze As Long
Dim Bomb As Long
Dim CellsLeft As Long

Private Sub InitMaze()
    Dim i As Integer
    Dim x As Long

    Randomize Timer
    For i = 0 To XMaze * YMaze - 1
        With pctCell(i)
            If .BorderStyle = 0 Then
                .BorderStyle = 1
                .Cls
                .Tag = ""
            End If
            If .BackColor <> vb3DLight Then
                .BackColor = vb3DLight
            End If
            .ZOrder 0
        End With
    Next
    For i = 1 To Bomb
        Do
            x = CInt(Rnd * XMaze * YMaze)
        Loop Until pctCell(x).Tag = ""
        pctCell(x).Tag = "B"
    Next
    CellsLeft = (XMaze * YMaze) - Bomb
End Sub

Private Sub Form_Load()
    Dim i As Integer

    pctCell(0).AutoRedraw = True
    pctCell(0).Font = "Verdana"
    pctCell(0).FontSize = 8
    pctCell(0).FontBold = True
    XMaze = 15
    YMaze = 15
    Bomb = 45
    For i = 1 To XMaze * YMaze - 1
        Load pctCell(i)
        pctCell(i).Move pctCell(0).Left + pctCell(0).Width * (i Mod XMaze), pctCell(0).Top + pctCell(0).Height * (i  XMaze), pctCell(0).Width, pctCell(0).Height
        pctCell(i).Visible = True
    Next
    InitMaze
End Sub

Private Sub BombCount(ByVal Index As Long)
    Dim x As Integer
    Dim y As Integer
    Dim Count As Integer
    
    Count = 0
    pctCell(Index).Tag = "X"
    For x = (Index Mod XMaze) - 1 To (Index Mod XMaze) + 1
        For y = (Index  XMaze) - 1 To (Index  XMaze) + 1
            If x >= 0 And y >= 0 And x < XMaze And y < YMaze Then
                If pctCell(x + y * XMaze).Tag = "B" Then
                    Count = Count + 1
                End If
            End If
        Next
    Next
    If Count = 0 Then
        For x = (Index Mod XMaze) - 1 To (Index Mod XMaze) + 1
            For y = (Index  XMaze) - 1 To (Index  XMaze) + 1
                If x >= 0 And y >= 0 And x < XMaze And y < YMaze Then
                    If pctCell(x + y * XMaze).Tag = "" And pctCell(x + y * XMaze).BorderStyle = 1 Then
                        BombCount x + y * XMaze
                    End If
                End If
            Next
        Next
    End If
    pctCell(Index).ZOrder 1
    pctCell(Index).BorderStyle = 0
    pctCell(Index).BackColor = vb3DLight
    If Count > 0 Then
        pctCell(Index).CurrentX = 0
        pctCell(Index).CurrentY = 0
        pctCell(Index).ForeColor = QBColor(Count)
        pctCell(Index).Print Count
    End If
    CellsLeft = CellsLeft - 1
    If CellsLeft = 0 Then
        ShowMaze
        MsgBox "You Win!", vbInformation, "You Win"
        InitMaze
    End If
End Sub

Private Sub ClearXTag()
    Dim i As Integer
    
    For i = 0 To XMaze * YMaze - 1
        If pctCell(i).Tag = "X" Then pctCell(i).Tag = ""
    Next
End Sub

Private Sub ShowMaze()
    Dim i As Integer
    
    For i = 0 To XMaze * YMaze - 1
        If pctCell(i).Tag = "B" Then
            pctCell(i).BackColor = vbYellow
            pctCell(i).BorderStyle = 0
            pctCell(i).ForeColor = vbRed
            pctCell(i).CurrentX = 0
            pctCell(i).CurrentY = 0
            pctCell(i).Print "B"
        End If
    Next
End Sub

Private Sub pctCell_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
    If pctCell(Index).BorderStyle = 1 Then
        If Button = vbLeftButton Then
            If pctCell(Index).Tag = "B" Then
                pctCell(Index).BackColor = vbYellow
                pctCell(Index).BorderStyle = 0
                pctCell(Index).ForeColor = vbRed
                pctCell(Index).CurrentX = 0
                pctCell(Index).CurrentY = 0
                pctCell(Index).Print "B"
                ShowMaze
                MsgBox "Bomb!", vbCritical, "Bomb"
                InitMaze
            Else
                BombCount Index
                ClearXTag
            End If
        ElseIf Button = vbRightButton Then
            If pctCell(Index).BackColor = vb3DLight Then
                pctCell(Index).BackColor = vbRed
            ElseIf pctCell(Index).BackColor = vbRed Then
                pctCell(Index).BackColor = vbBlue
            Else
                pctCell(Index).BackColor = vb3DLight
            End If
        End If
    End If
End Sub


Enjoy with it!!

Have a nice day!

____________________________
AndreaVB

16-04-2002 at 06:08 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: Minesweeper Project: 1. MinePlacement 2. Best score  Archived to Disk

Hello,

If you like, once your game is finished, I can post it in my AndreaVB's games section...

...if you want it to be open source...

good luck, anyway...    

____________________________
AndreaVB

16-04-2002 at 06:33 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
mpetersen
Level: Guest

icon Re: Minesweeper Project: 1. MinePlacement 2. Best score  Archived to Disk

Thanks to all for giving me some direction!!

Im a grad student, actually from biology/microbiology doing a cross into
computer sciences!!  & mangmt.  Worked 10 plus years in healthcare/pharmaceutical/biotech industry and wanted to take the cross into computers as worked with them so much anyway.

Lot of extra time since Im having to take all the undergrad and grad level at the same time!!

Appreciate all of your help and guidance on these problems.

MP

16-04-2002 at 04:13 PM
| Quote Reply
AndreaVB Forum : VB General : Minesweeper Project: 1. MinePlacement 2. Best score
Previous Topic (How to Cancel menu (in systray) when app is lost forcus)Next Topic (Getting Summary Info from a File) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder