125 lines
3.6 KiB
Plaintext
125 lines
3.6 KiB
Plaintext
Type GameState
|
|
digitos(3) As Double
|
|
operaciones(2) As String
|
|
End Type
|
|
|
|
Function randomDigits() As String
|
|
Dim As String resultado = ""
|
|
For i As Integer = 0 To 3
|
|
resultado &= Str(Int(Rnd * 9) + 1)
|
|
Next
|
|
Return resultado
|
|
End Function
|
|
|
|
Function evaluate(digitos() As Double, operaciones() As String) As Double
|
|
Dim As Double valor = digitos(0)
|
|
|
|
For i As Integer = 0 To 2
|
|
Select Case operaciones(i)
|
|
Case "+": valor += digitos(i +1)
|
|
Case "-": valor -= digitos(i +1)
|
|
Case "*": valor *= digitos(i +1)
|
|
Case "/": If digitos(i +1) <> 0 Then valor /= digitos(i +1)
|
|
End Select
|
|
Next
|
|
|
|
Return valor
|
|
End Function
|
|
|
|
Sub permute(digitos() As Double, soluciones() As GameState, Byref solutionCnt As Integer, k As Integer)
|
|
Dim As String*1 opChars(3) = {"+", "-", "*", "/"}
|
|
Dim As String ops(2)
|
|
Dim As Integer i, j, l, m
|
|
|
|
If k = 4 Then
|
|
For i = 0 To 3
|
|
ops(0) = opChars(i)
|
|
For j = 0 To 3
|
|
ops(1) = opChars(j)
|
|
For l = 0 To 3
|
|
ops(2) = opChars(l)
|
|
If Abs(evaluate(digitos(), ops()) - 24) < 0.001 Then
|
|
With soluciones(solutionCnt)
|
|
For m = 0 To 3: .digitos(m) = digitos(m): Next
|
|
For m = 0 To 2: .operaciones(m) = ops(m): Next
|
|
End With
|
|
solutionCnt += 1
|
|
Exit For 'Stop after first solution
|
|
End If
|
|
Next
|
|
If solutionCnt Then Exit For
|
|
Next
|
|
If solutionCnt Then Exit For
|
|
Next
|
|
Else
|
|
For i = k To 3
|
|
Swap digitos(i), digitos(k)
|
|
permute(digitos(), soluciones(), solutionCnt, k +1)
|
|
If solutionCnt Then Exit For
|
|
Swap digitos(k), digitos(i)
|
|
Next
|
|
End If
|
|
End Sub
|
|
|
|
' Main program
|
|
Randomize Timer
|
|
Dim As Integer i
|
|
Dim As String cmd
|
|
Dim As Double digitos(3)
|
|
Dim As String operaciones(2)
|
|
|
|
Do
|
|
Cls
|
|
Print "24 Game"
|
|
Print "Generating 4 digitos..."
|
|
|
|
Dim As String inputDigits = randomDigits()
|
|
Print "Make 24 using these digitos: ";
|
|
For i = 1 To Len(inputDigits)
|
|
Print Mid(inputDigits, i, 1); " ";
|
|
Next
|
|
Print
|
|
|
|
Line Input "Enter your expression (e.g. 4+5*3-2): ", cmd
|
|
|
|
Dim As Integer digitCnt = 0, opCnt = 0
|
|
' Parse user input
|
|
For i = 1 To Len(cmd)
|
|
Select Case Mid(cmd, i, 1)
|
|
Case "1" To "9"
|
|
digitos(digitCnt) = Val(Mid(cmd, i, 1))
|
|
digitCnt += 1
|
|
Case "+", "-", "*", "/"
|
|
operaciones(opCnt) = Mid(cmd, i, 1)
|
|
opCnt += 1
|
|
End Select
|
|
Next
|
|
|
|
Dim As Double resultado = evaluate(digitos(), operaciones())
|
|
Print "Your resultado: "; resultado
|
|
|
|
If Abs(resultado - 24) < 0.001 Then
|
|
Print !"\nCongratulations, you found a solution!"
|
|
Else
|
|
Print !"\nThe valor of your expression is "; resultado; " instead of 24!"
|
|
|
|
Dim As GameState soluciones(1000)
|
|
Dim As Integer solucCnt = 0
|
|
|
|
permute(digitos(), soluciones(), solucCnt, 0)
|
|
|
|
If solucCnt > 0 Then
|
|
Print !"\nA possible solution could have been: ";
|
|
With soluciones(0)
|
|
Print .digitos(0) & .operaciones(0) & .digitos(1) & .operaciones(1) & .digitos(2) & .operaciones(2) & .digitos(3)
|
|
End With
|
|
Else
|
|
Print !"\nThere was no known solution for these digitos."
|
|
End If
|
|
End If
|
|
|
|
Print !"\nDo you want to try again? (press N for exit, other key to continue)"
|
|
Loop Until (Ucase(Input(1)) = "N")
|
|
|
|
Sleep
|