Summing Character Numbers

Hello all, i´m new to the scripting but I´m working now on a project for children and we have to make sums of graphic character numbers
ex: 4+7=?.
What do you think is the best approach for programming sums of characters and validate results?

Thanks!

Rob.

Well, you have there answer and then you go,

if (thereAnswer == correctAnswer)
{
//What ever happens if the answer is correct.
}

It depends on how your thing works really.

Sorry, i think i explained poorly.
I need to assing values to objects ( An 3Dobject with the shape of FOUR has value 4) for doing all kind of math operations with these graphic objects.
I was wondering the best approach for assigning these values in JScript and making math operations between them and then compare to the correct answer.

Thank you,

Are you doing this in 3D, or are you doing this in 2D with the GUI interface?

In 3D you need the Text Mesh:

This displays the text you want.

This should be used to SHOW the text you want. You don’t use the ‘display’ to store information, you use it to display information.

Next you have local variables in your script for the values you want stored. 4 and 7 specifically. Getting the result of that in code is trivial:

var result = 4 + 7;

If it’s in the GUI. Same goes, but instead of using Text Mesh you use the GUI:

But again, the GUI is your display. You don’t store the values in the GUI.

Thanks lordofduct!

Yes we was considering TextMesh, but now we are thinking on imported 3D objetcs of each number (0-9) for achieving the look of the game and mechanics we want to have.
About yor answer, i can´t create vars with the value of the predefined operation (like 4+7). I need to do the operations on the run because it can be between random numbers or choosen by the user. So i need to do real math operations between the values of each 3D object.

so your player is arranging values on screen, and you have to calculate from said values.

All right, first off your gameobjects in scene that get to be moved around. They need to have the numbers they represent stored in them some how (they SHOULD in the first place… less they know what number to display!)

now, reading the numbers from left to right is going to be interesting. You’re going to have to find all the gameobjects in the region you’re reading from, sort them from left to right (this depends on the direction your camera is facing… you can just project their positions [dot product] onto a vector pointing in the direction of right, and sort).

Now that you have that, each object becomes a char value. Strung together you get a string.

Now if they just have a bunch of numbers, said string value is just a number and can be parsed to a number.

But if it’s an equation… well you’re going to need some sort of math parsing tool. There are tons out there. Just google for a math string parsing tool and you’ll get a bunch of results.

Here’s a very basic one I have written in VB.Net (I’m at work, this is the best I have on my person):

Namespace Utils
    ''' <summary>
    ''' Deals with the basic arithmetic operations and standard function calculations
    ''' </summary>
    ''' <remarks></remarks>
    Public Class MathParser

#Region "Special Types"

        Public Enum Parameters
            A
            B
            C
            D
            E
            F
            G
            H
            I
            J
            K
            L
            M
            N
            O
            P
            Q
            R
            S
            T
            U
            V
            W
            X
            Y
            Z
        End Enum

#End Region

#Region "Declarations"

        Private _params As New Dictionary(Of Parameters, Decimal)
        Private _opOrder As String() = New String() {"/", "*", "-", "+"}

#End Region

#Region "CONSTRUCTOR"
        ''' <summary>
        ''' Initiates a new instance of MathParser
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()

        End Sub

#End Region

#Region "Properties"
        ''' <summary>
        ''' Returns an array list of Parameters in type Decimal
        ''' </summary>
        ''' <value></value>
        ''' <returns>List of Parameters</returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Params() As Dictionary(Of Parameters, Decimal)
            Get
                Return _params
            End Get
        End Property

#End Region


#Region "Calculate"
        ''' <summary>
        ''' Calculates a formulated string
        ''' </summary>
        ''' <param name="sFormula">Formulated string to be calculated</param>
        ''' <returns>A decimal value as a result of the calculation</returns>
        ''' <remarks></remarks>
        Public Function Calculate(ByVal sFormula As String) As Decimal

            Try

                Dim arr() As String = sFormula.Split("/+-*()".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                For Each de As KeyValuePair(Of Parameters, Decimal) In _params
                    For Each s As String In arr
                        If s <> de.Key.ToString() AndAlso s.EndsWith(de.Key.ToString()) Then
                            sFormula = sFormula.Replace(s, (Convert.ToDecimal(s.Replace(de.Key.ToString(), "")) * de.Value).ToString())
                        End If
                    Next
                    sFormula = sFormula.Replace(de.Key.ToString(), de.Value.ToString())
                Next

                While sFormula.LastIndexOf("(") > -1
                    Dim lastOpenIndex = sFormula.LastIndexOf("(")
                    Dim firstCloseIndexAfterLastOpen = sFormula.IndexOf(")", lastOpenIndex)
                    Dim result As Decimal = ProcessOperation(sFormula.Substring(lastOpenIndex + 1, firstCloseIndexAfterLastOpen - lastOpenIndex - 1))
                    Dim bAppendAsterix = False
                    If lastOpenIndex > 0 Then
                        If sFormula.Substring(lastOpenIndex - 1, 1) <> "(" AndAlso Not _opOrder.Contains(sFormula.Substring(lastOpenIndex - 1, 1)) Then
                            bAppendAsterix = True
                        End If
                    End If
                    sFormula = sFormula.Substring(0, lastOpenIndex)  If(bAppendAsterix, "*", "")  result.ToString()  sFormula.Substring(firstCloseIndexAfterLastOpen + 1)
                End While

                Return ProcessOperation(sFormula)

            Catch ex As Exception
                Throw New Exception("Error Occured While Calculating. Check Syntax", ex)
            End Try

        End Function
        ''' <summary>
        ''' A generic Fucntion calculator
        ''' </summary>
        ''' <param name="operation">The complete operation string</param>
        ''' <returns>The resulted calculation based on the operation passed</returns>
        ''' <remarks></remarks>
        Private Function ProcessOperation(ByVal operation As String) As Decimal

            Dim arr As New ArrayList()
            Dim s As String = ""
            Dim c As String

            For i As Integer = 0 To operation.Length - 1
                c = operation(i)
                If Array.IndexOf(_opOrder, c) > -1 Then
                    If s <> "" Then
                        arr.Add(s)
                    End If
                    arr.Add(c)
                    s = ""
                Else
                    s = c
                End If
            Next
            arr.Add(s)
            s = ""

            For Each op As String In _opOrder
                While arr.IndexOf(op) > -1
                    Dim index = arr.IndexOf(op)
                    Dim dBefore As Decimal = Convert.ToDecimal(arr(index - 1))
                    Dim dAfter As Decimal = 0D
                    If arr(index + 1).ToString() = "-" Then
                        arr.RemoveAt(index + 1)
                        dAfter = Convert.ToDecimal(arr(index + 1)) * -1
                    Else
                        dAfter = Convert.ToDecimal(arr(index + 1))
                    End If
                    arr(index) = CalculateByOperator(dBefore, dAfter, op)
                    arr.RemoveAt(index - 1)
                    arr.RemoveAt(index)
                End While
            Next

            Return If(arr.Count > 0, Convert.ToDecimal(arr(0)), 0D)

        End Function
        ''' <summary>
        ''' Does a standard arithmetic calculation based on the operator passed
        ''' </summary>
        ''' <param name="number1">1st Operand</param>
        ''' <param name="number2">2nd Operand</param>
        ''' <param name="op">Operation</param>
        ''' <returns>The resulted calculation</returns>
        ''' <remarks></remarks>
        Private Function CalculateByOperator(ByVal number1 As Decimal, ByVal number2 As Decimal, ByVal op As String) As Decimal

            Select Case op
                Case "/"
                    Return number1 / number2
                Case "*"
                    Return number1 * number2
                Case "-"
                    Return number1 * number2
                Case "+"
                    Return number1 + number2
                Case Else
                    Return 0
            End Select

        End Function

#End Region


    End Class
End Namespace

Hi lord

Yes you understand me well. Maybe we need a parsing tool, although the maths operations involved in the game are not very advanced. I will look for something at the unity ecosytem.
We are thinking also in doing that project with a visual scripting tool like Playmaker. Do you think it will be easier.?

See the thing is… you’re going to have to parse it. May you be parsing strings, or parsing visual objects. Either way… it’s parsing.

I just personally use strings to parse because they’re faster to parse then objects/tokens that represent chars, that are strung along, that… wait, that sounds like a string!

Thanks !