Matching The Value Of User Input To Picturebox Value In An Array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Matching The Value Of User Input To Picturebox Value In An Array

Hybrid View

  1. #1
    Join Date
    Sep 2013
    Posts
    2

    Exclamation Matching The Value Of User Input To Picturebox Value In An Array

    *Program is attached as well, due to there being pictures of the ships that are required to get the full experience of the program*

    I am recreating battleship in Visual Basic 6, and I am almost complete. The one part that I am unable to complete, is matching an input value to the value of the picturebox. To better explain...

    I have 100 Pictureboxes in a control array, and the ships are placed among the 100 spots based off of a generated number. Now, the program is singleplayer instead of multiplayer, so the objective of the game is to find the ships. In order to find the ships, the user is required to input the column value and a row value, which is added together to represent a picturebox in the array. For example, Column 4, row six would be i=53. From there, I wanted the program to use that to check the value of the picture1(i) to see if there was a small, medium, or large ship within that picturebox.

    My problem is that I am having trouble matching the value that is inputted from the user to the picturebox in the array to check if any ship or part of a ship is in that picturebox. Also, if the user input value matches a picturebox that contains a piece of a ship, how do I eliminate all of the pieces of that ship?

    *Code Below*
    Code:
    Dim placesmall, placemed, placebig As Integer 'ship placement
    Dim row, column, spot As Integer 'Ship Guess
    Dim i As Integer 'ship random spot
    
    Private Sub Locate_Click()
    r = 0 'Zero Values
    column = 0 'Zero Values
    column = InputBox("Guess the Column")
    row = InputBox("Guess the Row")
    'Choose the row for the ship location guess
        row = (row * 10) - 10
        column = column - 1
    i = row + column
    
    
    End Sub
    
    Private Sub placeship_Click()
    'clear the pictureboxes
    For i = 0 To 99
    Picture1(i).Cls
    Next i
    
    'Small ship
    placesmall = Int(99 * Rnd + 1)
    i = placesmall
        i = 0
        i = Int(99 * Rnd + 1)
    Picture1(i) = LoadPicture(App.Path & "/ss1.jpg")
        i = i + 1
    Picture1(i) = LoadPicture(App.Path & "/ss2.jpg")
    
    'medium ship
    placemed = Int(99 * Rnd + 1)
    If placemed = placesmall Or placemed = placesmall + 1 Then 'If the ship is placed over another ship, then it must respot itself
        placemed = 0
        placemed = Int(99 * Rnd + 1)
    End If
    
    
    'Test if ship is going to overlap to the next line
    
    i = placemed
    If i = 8 Or 18 Or 28 Or 38 Or 48 Or 58 Or 68 Or 78 Or 88 Or 98 Then
            i = 0
                i = Int(99 * Rnd + 1)
    End If
    If i = 9 Or 19 Or 29 Or 39 Or 49 Or 59 Or 69 Or 79 Or 89 Or 99 Then
        i = 0
            i = Int(99 * Rnd + 1)
    End If
    
    
    'Print The Ship
    
    Picture1(i) = LoadPicture(App.Path & "/ms1.jpg")
        i = i + 1
    Picture1(i) = LoadPicture(App.Path & "/ms2.jpg")
        i = i + 1
    Picture1(i) = LoadPicture(App.Path & "/ms3.jpg")
    
    'large ship
    placebig = Int(99 * Rnd + 1)
    If placebig = placesmall Or placebig = placemed Or placebig = placesmall + 1 Or placebig = placemed + 1 Or placebig = placemed + 2 Then 'If the ship is placed over another ship, then it must respot itself
        placebig = Int(99 * Rnd + 1)
    End If
    i = placebig
    
    'Stop ship from printing to next line
    
    If i = 7 Or 17 Or 27 Or 37 Or 47 Or 57 Or 67 Or 77 Or 87 Or 97 Then
        i = 0
            i = Int(99 * Rnd + 1)
    End If
    If i = 8 Or 18 Or 28 Or 38 Or 48 Or 58 Or 68 Or 78 Or 88 Or 98 Then
            i = 0
                i = Int(99 * Rnd + 1)
    End If
    If i = 9 Or 19 Or 29 Or 39 Or 49 Or 59 Or 69 Or 79 Or 89 Or 99 Then
        i = 0
            i = Int(99 * Rnd + 1)
    End If
    
    
    
    'Print The Ship
    
    Picture1(i) = LoadPicture(App.Path & "/bs1.jpg")
        i = i + 1
    Picture1(i) = LoadPicture(App.Path & "/bs2.jpg")
        i = i + 1
    Picture1(i) = LoadPicture(App.Path & "/bs3.jpg")
        i = i + 1
    Picture1(i) = LoadPicture(App.Path & "/bs4.jpg")
    
    
    'For i = 0 To 99
    'Picture1(i) = Nothing
    'Next i
    
    End Sub
    
    
    Private Sub quit_Click()
    End
    End Sub
    battleship.zip

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,875

    Re: Matching The Value Of User Input To Picturebox Value In An Array

    First problem:
    Code:
    Dim row, column, spot As Integer
    That is wrong. Only spot is an Integer, Everything else in the line is an OBJECT type.

    Code:
    Dim row As Integer, column As Integer, spot As Integer
    is the proper way to declare three Integer types in one line.

    You just MIGHT want to use String values, for the next part

    Take a look at InStr$().
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center