-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathViewController.m
More file actions
166 lines (146 loc) · 4.71 KB
/
Copy pathMathViewController.m
File metadata and controls
166 lines (146 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// MathViewController.m
// gameBeforeDishes
//
// Created by Graeme Judkins on 2016-03-27.
// Copyright © 2016 bdong. All rights reserved.
//
#import "MathViewController.h"
#import "gameConst.h"
@interface MathViewController ()
@end
@implementation MathViewController
{
gameConst* puzzles;
}
- (void)viewDidLoad {
[super viewDidLoad];
float gameSpeedValue;
gameSpeedValue = [[NSUserDefaults standardUserDefaults] floatForKey:@"gameSpeed"];
gameSpeedValue = -4*gameSpeedValue;
super.timerValue = 5 + gameSpeedValue;
super.gameInstruction = @"Is this correct?";
super.QuestionUp.textAlignment = NSTextAlignmentCenter;
super.QuestionDown.textAlignment = NSTextAlignmentCenter;
super.AnswerUp.textAlignment = NSTextAlignmentCenter;
super.AnswerDown.textAlignment = NSTextAlignmentCenter;
[super.threadProgressView1 setTransform:CGAffineTransformMakeRotation(-M_PI)];
super.GoodTimeToPressButton = true;
[super.QuestionUp setTransform:CGAffineTransformMakeRotation(-M_PI)];
[super.AnswerUp setTransform:CGAffineTransformMakeRotation(-M_PI)];
puzzles = [[gameConst alloc] init];
[self generatePuzzle];
}
- (void)generatePuzzle
{
[self animateProgressView2];
[self createNewPuzzlewith:[self randomBoolWithYesPercentage:30]];
[super.Timer invalidate];
super.Timer = [NSTimer scheduledTimerWithTimeInterval:super.timerValue
target:self
selector:@selector(generatePuzzle)
userInfo:nil
repeats:NO];
}
- (void)createNewPuzzlewith:(bool)isCorrect
{
if (isCorrect) {
super.GoodTimeToPressButton = true;
int firstNum = (arc4random_uniform(40));
int secondNum = (arc4random_uniform(40));
int sum;
NSString* sign;
if(arc4random_uniform(100) < 50)
{
sum = firstNum + secondNum;
sign = @" + ";
}
else
{
sum = firstNum - secondNum;
sign = @" - ";
}
NSString* question = [NSString stringWithFormat:@"%d%@%d = ", firstNum, sign, secondNum];
[self setQuestion:question];
NSString* answer = [NSString stringWithFormat:@"%d", sum];
[self setAnswer:answer];
}
else
{
super.GoodTimeToPressButton = false;
int firstNum = (arc4random_uniform(40));
int secondNum = (arc4random_uniform(40));
int sum;
NSString* sign;
if(arc4random_uniform(100) < 50)
{
int randomOffset = arc4random_uniform(20)-10;
if (randomOffset == 0)
{
randomOffset ++;
}
sum = firstNum + secondNum + randomOffset;
sign = @" + ";
}
else
{
int randomOffset = arc4random_uniform(20)-10;
if (randomOffset == 0)
{
randomOffset ++;
}
sum = firstNum - secondNum + randomOffset;
sign = @" - ";
}
NSString* question = [NSString stringWithFormat:@"%d%@%d = ", firstNum, sign, secondNum];
[self setQuestion:question];
NSString* answer = [NSString stringWithFormat:@"%d", sum];
[self setAnswer:answer];
}
}
- (BOOL)isGoodTime
{
return super.GoodTimeToPressButton;
}
-(void)updateProgressBar
{
if(super.Time >= super.timerValue)
{
//Invalidate timer when time reaches 0
[super.countdownTimer invalidate];
}
else
{
super.Time += 0.05;
super.threadProgressView1.progress = (super.timerValue - super.Time)/super.timerValue;
super.threadProgressView2.progress = (super.timerValue - super.Time)/super.timerValue;
}
}
-(void)animateProgressView2
{
[super.countdownTimer invalidate];
super.threadProgressView1.progress = 1.0;
super.threadProgressView2.progress = 1.0;
super.Time = 0.1;
super.countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 0.05f
target: self
selector: @selector(updateProgressBar)
userInfo: nil
repeats: YES];
}
- (void)resetGame
{
[super.Timer invalidate];
[self generatePuzzle];
}
- (void)pauseGame
{
[super.countdownTimer invalidate];
[super.Timer invalidate];
super.Time = super.timerValue;
}
-(BOOL)randomBoolWithYesPercentage:(int) percentage
{
return arc4random_uniform(100) < percentage;
}
@end