[+] Implement regular math expressions
This commit is contained in:
@@ -102,6 +102,11 @@ class AlarmActivationViewController: EndEditingOnReturn
|
|||||||
// Initialize alarm
|
// Initialize alarm
|
||||||
switch wvm
|
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":
|
case "Factor":
|
||||||
initFactorProblem()
|
initFactorProblem()
|
||||||
puzzleView.show()
|
puzzleView.show()
|
||||||
|
|||||||
@@ -677,10 +677,10 @@
|
|||||||
<nil key="textColor"/>
|
<nil key="textColor"/>
|
||||||
<nil key="highlightedColor"/>
|
<nil key="highlightedColor"/>
|
||||||
</label>
|
</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"/>
|
<rect key="frame" x="20" y="358" width="374" height="126.5"/>
|
||||||
<subviews>
|
<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"/>
|
<rect key="frame" x="20" y="20" width="334" height="32.5"/>
|
||||||
<fontDescription key="fontDescription" type="system" pointSize="27"/>
|
<fontDescription key="fontDescription" type="system" pointSize="27"/>
|
||||||
<nil key="textColor"/>
|
<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"/>
|
<constraint firstItem="eTb-g7-8x3" firstAttribute="top" secondItem="CZ0-R6-FDq" secondAttribute="bottom" constant="20" id="yME-j6-vSk"/>
|
||||||
</constraints>
|
</constraints>
|
||||||
</view>
|
</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"/>
|
<rect key="frame" x="20" y="391" width="374" height="60.5"/>
|
||||||
<subviews>
|
<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">
|
<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"/>
|
<constraint firstItem="nK2-jK-GsP" firstAttribute="top" secondItem="Zph-rd-rPI" secondAttribute="top" constant="20" id="yVq-ID-HNc"/>
|
||||||
</constraints>
|
</constraints>
|
||||||
</view>
|
</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"/>
|
<rect key="frame" x="20" y="338.5" width="374" height="165"/>
|
||||||
<subviews>
|
<subviews>
|
||||||
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="213-rS-oYj">
|
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" translatesAutoresizingMaskIntoConstraints="NO" id="213-rS-oYj">
|
||||||
|
|||||||
@@ -40,11 +40,10 @@ enum MathOperator : String {
|
|||||||
case plus = "+"
|
case plus = "+"
|
||||||
case minus = "-"
|
case minus = "-"
|
||||||
case multiply = "*"
|
case multiply = "*"
|
||||||
case divide = "/"
|
|
||||||
case power = "**"
|
case power = "**"
|
||||||
|
|
||||||
static func random() -> MathOperator {
|
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)))
|
let index = Int(arc4random_uniform(UInt32(allMathOperators.count)))
|
||||||
|
|
||||||
return allMathOperators[index]
|
return allMathOperators[index]
|
||||||
@@ -83,50 +82,42 @@ class MathExpression : CustomStringConvertible {
|
|||||||
return "\(leftString) \(self.op.rawValue) \(rightString)"
|
return "\(leftString) \(self.op.rawValue) \(rightString)"
|
||||||
}
|
}
|
||||||
|
|
||||||
var result : Int? {
|
var result: Int {
|
||||||
let format = "\(lhs.nsExpressionFormatString) \(op.rawValue) \(rhs.nsExpressionFormatString)"
|
let format = "\(lhs.nsExpressionFormatString) \(op.rawValue) \(rhs.nsExpressionFormatString)"
|
||||||
let expr = NSExpression(format: format)
|
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 {
|
static func random() -> MathExpression {
|
||||||
|
let op: MathOperator = .random()
|
||||||
let lhs = MathElement.Integer(value: Int(arc4random_uniform(10)))
|
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
|
Generate simple problem - 2 expressions
|
||||||
*/
|
*/
|
||||||
class AlgProb2 : MathExpression {
|
class MathExpProblem
|
||||||
let a = MathExpression.random()
|
{
|
||||||
let b = MathExpression.random()
|
let prob: String
|
||||||
|
let ans: Int
|
||||||
|
|
||||||
func getProblem() -> String {
|
init(size: Int)
|
||||||
return "\(a) + \(b)"
|
{
|
||||||
}
|
var expressions: [String] = []
|
||||||
|
var answer = 0
|
||||||
func getAnswer() -> String {
|
for _ in 1...size
|
||||||
return "\(a.result! + b.result!)"
|
{
|
||||||
}
|
let exp = MathExpression.random()
|
||||||
}
|
expressions.append(exp.description)
|
||||||
|
answer += exp.result
|
||||||
/**
|
}
|
||||||
Generate simple problem - 3 expressions
|
prob = expressions.joined(separator: " + ")
|
||||||
*/
|
ans = answer
|
||||||
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!)"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,9 +39,12 @@ struct WVM: Codable
|
|||||||
|
|
||||||
|
|
||||||
let wvms = [
|
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: "Factor", desc: "Factor a binomial"),
|
||||||
WVM(name: "RPS", desc: "Win a game of rock paper scissors"),
|
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: "Smash", desc: "It'll never turn off"),
|
||||||
//WVM(name: "Walk", desc: "Walk a few steps"),
|
//WVM(name: "Walk", desc: "Walk a few steps"),
|
||||||
//WVM(name: "Jump", desc: "Make a few jumps")
|
//WVM(name: "Jump", desc: "Make a few jumps")
|
||||||
|
|||||||
Reference in New Issue
Block a user