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