2

For a school project we were asked to design an 'education game' and I have thought of an idea, but am confused as to how to code it in Visual Basic. For my game there are 10 objects that move from left to right of the playing screen one at a time at set intervals and the player has to solve one randomly generated equation for each object before it reaches the end of the playing screen. How would I go about programming this?

This is the code that I have attempted so far, having the level change once the snakecount = 0

Public Class frmGame
Dim SnakeCount As Integer
Dim Score As Integer
Dim Lives As Integer
Dim Level As Integer
Dim Objects(9) As Integer
Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    While Level = 1
        Objects(0) = Rnd() + Rnd()
        Objects(1) = Rnd() - Rnd()
        Objects(2) = Rnd() - Rnd()
        Objects(3) = Rnd() * Rnd()
        Objects(4) = Rnd() + Rnd()
        Objects(5) = Rnd() * Rnd()
        Objects(6) = Rnd() + Rnd()
        Objects(7) = Rnd() - Rnd()
        Objects(8) = Rnd() + Rnd()
        Objects(9) = Rnd() * Rnd()
    End While
End Sub

The greatest difficulty I'm having at the moment is assigning random integers to each element of the array and displaying them as an equation for the user to see and then solve

Any help or advice is greatly appreciated!! :)

Nathan S
  • 23
  • 3

1 Answers1

2

To generate (pseudo-)random integers you shouldn't rely on Rnd() to do the job, rather use the functionality in System.Random, as otherwise patterns become visible (and without setting a seed you'd always get the same sequence of numbers).

An example of how to create pseudo-random numbers can be found here.
Basically to get a random number in the range from Min (inclusive) to Max (exclusive) you can use the following function:

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer ' by making Generator static, we preserve the same instance ' ' (i.e., do not create new instances with the same seed over and over) ' ' between calls ' Static Generator As System.Random = New System.Random() Return Generator.Next(Min, Max) End Function

 
Edit in response to comment:
To generate random equations you could do something like

    Function randomEquation() As Tuple(Of Integer, String)
    Dim op As Integer = GetRandom(0, 3)
    Dim a As Integer = GetRandom(1, 10) 'Set range to your liking
    Dim b As Integer = GetRandom(1, 10)

    Select Case op
        Case 0
            Return New Tuple(Of Integer, String)(a + b, a & " + " & b)
        Case 1
            Return New Tuple(Of Integer, String)(a - b, a & " - " & b)
        Case 2
            Return New Tuple(Of Integer, String)(a * b, a & " * " & b)
        Case Else
            Throw New NotImplementedException("The value " & op & " is not assigned to an operator.")
    End Select
End Function

As the result is a tuple containing the value of your equation and the equation itself (as a string), you can use the string to display it to toe user, like Label1.Text = randomEquation().Item2.

Community
  • 1
  • 1
Zepporle
  • 413
  • 4
  • 13
  • 1
    I didn't know that, thanks! :D From there how would I then go about setting the value of each element of my Object array to an equation of two of these random numbers? – Nathan S Aug 24 '15 at 09:42
  • Just like you did before, but instead of using `Objects(0) = Rnd() + Rnd()`, use `Objects(0) = GetRandom(Min, Max) + GetRandom(Min, Max)`. – Drarig29 Aug 24 '15 at 09:52
  • Ah I see, thank you very much for your time, just one more question. As i mentioned previously for each object an equation will be displayed so that the user can solve it. How do i get the same values of each object to be displayed in a label? – Nathan S Aug 24 '15 at 09:58
  • See my edit on how to display the equation in a label. Basically the function is returning two values now; the equation to be displayed for the user and its result. – Zepporle Aug 24 '15 at 10:05
  • Ah yes I see now! Thank you very much for your time and help! :D – Nathan S Aug 24 '15 at 10:07
  • Again, sorry for the question (I'm quite new to all of this), with your edited version of the code, how would I set the values of the elements in the objects array to match this? – Nathan S Aug 24 '15 at 10:12
  • You can use the first item of the tuple: `Objects(0) = equation.Item1` (`equation` holds the result of a call to `randomEquation()`). – Zepporle Aug 24 '15 at 10:14
  • How do you mean a 'call to randomEquation())'? – Nathan S Aug 24 '15 at 11:09
  • Well, you call `randomEquation()` and save the result in a variable: `Dim equation as Tuple(Of Integer, String) = randomEquation()` – Zepporle Aug 24 '15 at 11:47