[+] Implement regular math expressions
This commit is contained in:
@@ -102,6 +102,11 @@ class AlarmActivationViewController: EndEditingOnReturn
|
||||
// Initialize alarm
|
||||
switch wvm
|
||||
{
|
||||
case "Math 1", "Math 2", "Math 3":
|
||||
let q = MathExpProblem(size: Int(wvm[5...5])!)
|
||||
puzzleQuestionLabel.text = "Solve: \(q.prob.replacingOccurrences(of: "**", with: "^"))"
|
||||
puzzleAnswers = [q.ans]
|
||||
puzzleView.show()
|
||||
case "Factor":
|
||||
initFactorProblem()
|
||||
puzzleView.show()
|
||||
|
||||
@@ -677,10 +677,10 @@
|
||||
<nil key="textColor"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
<view hidden="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="OMQ-qK-fej" userLabel="factorView">
|
||||
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="OMQ-qK-fej" userLabel="factorView">
|
||||
<rect key="frame" x="20" y="358" width="374" height="126.5"/>
|
||||
<subviews>
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Solve: " lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="CZ0-R6-FDq">
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Solve: " lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="CZ0-R6-FDq">
|
||||
<rect key="frame" x="20" y="20" width="334" height="32.5"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="27"/>
|
||||
<nil key="textColor"/>
|
||||
@@ -705,7 +705,7 @@
|
||||
<constraint firstItem="eTb-g7-8x3" firstAttribute="top" secondItem="CZ0-R6-FDq" secondAttribute="bottom" constant="20" id="yME-j6-vSk"/>
|
||||
</constraints>
|
||||
</view>
|
||||
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Zph-rd-rPI" userLabel="shakeView">
|
||||
<view hidden="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Zph-rd-rPI" userLabel="shakeView">
|
||||
<rect key="frame" x="20" y="391" width="374" height="60.5"/>
|
||||
<subviews>
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Shake your phone to dismiss" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="nK2-jK-GsP">
|
||||
@@ -723,7 +723,7 @@
|
||||
<constraint firstItem="nK2-jK-GsP" firstAttribute="top" secondItem="Zph-rd-rPI" secondAttribute="top" constant="20" id="yVq-ID-HNc"/>
|
||||
</constraints>
|
||||
</view>
|
||||
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NmX-iJ-1D0" userLabel="rpsView">
|
||||
<view hidden="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NmX-iJ-1D0" userLabel="rpsView">
|
||||
<rect key="frame" x="20" y="338.5" width="374" height="165"/>
|
||||
<subviews>
|
||||
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="213-rS-oYj">
|
||||
|
||||
@@ -40,11 +40,10 @@ enum MathOperator : String {
|
||||
case plus = "+"
|
||||
case minus = "-"
|
||||
case multiply = "*"
|
||||
case divide = "/"
|
||||
case power = "**"
|
||||
|
||||
static func random() -> MathOperator {
|
||||
let allMathOperators: [MathOperator] = [.plus, .minus, .multiply, .divide, .power]
|
||||
let allMathOperators: [MathOperator] = [.plus, .minus, .multiply, .power]
|
||||
let index = Int(arc4random_uniform(UInt32(allMathOperators.count)))
|
||||
|
||||
return allMathOperators[index]
|
||||
@@ -83,50 +82,42 @@ class MathExpression : CustomStringConvertible {
|
||||
return "\(leftString) \(self.op.rawValue) \(rightString)"
|
||||
}
|
||||
|
||||
var result : Int? {
|
||||
var result: Int {
|
||||
let format = "\(lhs.nsExpressionFormatString) \(op.rawValue) \(rhs.nsExpressionFormatString)"
|
||||
let expr = NSExpression(format: format)
|
||||
return expr.expressionValue(with: nil, context: nil) as? Int
|
||||
let result = expr.expressionValue(with: nil, context: nil)
|
||||
return Int(round(result as! Double))
|
||||
}
|
||||
|
||||
static func random() -> MathExpression {
|
||||
let op: MathOperator = .random()
|
||||
let lhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
||||
let rhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
||||
let rhs = MathElement.Integer(value: Int(arc4random_uniform(op == .power ? 3 : 10)))
|
||||
|
||||
return MathExpression(lhs: lhs, rhs: rhs, op: .random())
|
||||
return MathExpression(lhs: lhs, rhs: rhs, op: op)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Generate simple problem - 2 expressions
|
||||
*/
|
||||
class AlgProb2 : MathExpression {
|
||||
let a = MathExpression.random()
|
||||
let b = MathExpression.random()
|
||||
class MathExpProblem
|
||||
{
|
||||
let prob: String
|
||||
let ans: Int
|
||||
|
||||
func getProblem() -> String {
|
||||
return "\(a) + \(b)"
|
||||
}
|
||||
|
||||
func getAnswer() -> String {
|
||||
return "\(a.result! + b.result!)"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Generate simple problem - 3 expressions
|
||||
*/
|
||||
class AlgProb3 : MathExpression {
|
||||
let a = MathExpression.random()
|
||||
let b = MathExpression.random()
|
||||
let c = MathExpression.random()
|
||||
|
||||
func getProblem() -> String {
|
||||
return "\(a) + \(b) + \(c)"
|
||||
}
|
||||
|
||||
func getAnswer() -> String {
|
||||
return "\(a.result! + b.result! + c.result!)"
|
||||
init(size: Int)
|
||||
{
|
||||
var expressions: [String] = []
|
||||
var answer = 0
|
||||
for _ in 1...size
|
||||
{
|
||||
let exp = MathExpression.random()
|
||||
expressions.append(exp.description)
|
||||
answer += exp.result
|
||||
}
|
||||
prob = expressions.joined(separator: " + ")
|
||||
ans = answer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,9 +39,12 @@ struct WVM: Codable
|
||||
|
||||
|
||||
let wvms = [
|
||||
WVM(name: "Shake", desc: "Shake your phone... aggresively!"),
|
||||
WVM(name: "Math 1", desc: "Easy math expression"),
|
||||
WVM(name: "Math 2", desc: "Medium math expression"),
|
||||
WVM(name: "Math 3", desc: "Hard math expression"),
|
||||
WVM(name: "Factor", desc: "Factor a binomial"),
|
||||
WVM(name: "RPS", desc: "Win a game of rock paper scissors"),
|
||||
WVM(name: "Shake", desc: "Shake your phone... aggresively!"),
|
||||
//WVM(name: "Smash", desc: "It'll never turn off"),
|
||||
//WVM(name: "Walk", desc: "Walk a few steps"),
|
||||
//WVM(name: "Jump", desc: "Make a few jumps")
|
||||
|
||||
Reference in New Issue
Block a user