TVC Code

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
//
//  NewGameTableViewController.m
//  FourScore
//
//  Created by Denis Lachance on 12-04-23.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NewGameTableViewController.h"

@interface NewGameTableViewController ()

@end

@implementation NewGameTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
    NSMutableDictionary* settings = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    games = [settings objectForKey:@"Game Types"];
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [[self tableView] setRowHeight:150];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [games count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    UILabel* title = (UILabel*)[cell viewWithTag:100];
    title.text = [games objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    NSString* a = (NSString*)[games objectAtIndex:indexPath.row];
    a = [a stringByReplacingOccurrencesOfString:@" " withString:@""];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSegueWithIdentifier:a sender:self];
}

- (IBAction)onExit:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) viewWillAppear:(BOOL)animated{
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
}

@end

Game Code

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

//
//  testViewController.m
//  test
//
//  Created by Default on 11-07-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GameViewController.h"

@implementation GameViewController
@synthesize pointsLabel, secondaryLabel;


- (void)dealloc
{
    //[pointsLabel release];
    //[secondaryLabel release];
    //[super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadSkin];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:skinPath]];
    srandom(time(NULL));
    pointsLabel.text = @"Points: 0";
    roll = -1;
    for (NSInteger i = 0; i < 16; i++){
        grid[i] = 0;
        eks[i] = false;
    }
    NSInteger tagint;
    for (NSInteger i = 0; i < 16; i++){
        tagint = 10*(i/4+1)+i%4+1;
        UIImageView *tempView = [[UIImageView alloc] initWithFrame:CGRectMake(x_scale*(i%4)+x_off, y_scale*(i/4)+y_off, button_scale, button_scale)];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(onHit:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:tagint];
        [tempView setTag:tagint];
        button.frame = CGRectMake(76*(i%4)+8, 76*(i/4)+0, 75, 75);
        button.titleLabel.font = [UIFont systemFontOfSize:30];
        [self.view addSubview:tempView];
        [self.view addSubview:button];
        views[i] = tempView;
        buttons[i] = button;
    }
    [self startTimer];
}

- (void)viewDidUnload
{
    [self setPointsLabel:nil];
    [self setSecondaryLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) loadSkin{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
    NSDictionary* plistDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSString *name = [plistDictionary objectForKey:@"Selected Skin"];
    NSDictionary* skin = [[plistDictionary objectForKey:@"Skins"] objectForKey:name];
    skinPath = [skin objectForKey:@"path"];
    x_scale = (int)[[skin objectForKey:@"x_scale"] intValue];
    y_scale = (int)[[skin objectForKey:@"y_scale"] intValue];
    x_off = (int)[[skin objectForKey:@"x_offset"] intValue];
    y_off = (int)[[skin objectForKey:@"y_offset"] intValue];
    button_scale = (int)[[skin objectForKey:@"button_scale"] intValue];
}

- (IBAction) onReset{
    srandom(time(NULL));
    [resetButton setBackgroundImage:nil forState:UIControlStateNormal];
    pointsLabel.text = @"Points: 0";
    secondaryLabel.text = @"Can Place Die";
    roll = -1;
    for (NSInteger i = 0; i < 16; i++){
        grid[i] = 0;
        eks[i] = false;
        [buttons[i] setTitle:NULL forState:UIControlStateNormal];
        [(UIImageView*)views[i] setImage:nil];
    }
}

- (IBAction)onExit:(id)sender {
    //[self.navigationController popViewControllerAnimated:YES];
    [self isDone];
}

- (IBAction) onRoll:(id) sender{
    resetButton = (UIButton*) sender;
    if(roll == -1){
        roll = arc4random()%6 + 1;
        UIButton* temp = (UIButton*) sender;
        [temp setBackgroundImage:[UIImage imageNamed:[self diceImage:roll]] forState:UIControlStateNormal];
        [temp setTitle:@"" forState:UIControlStateNormal];
        if([self isAllGood]){
            secondaryLabel.text = @"Can Place Die";
        }else{
            secondaryLabel.text = @"Can't Place Die";
        }
    }
}

- (IBAction) onHit:(id) sender{
    if(roll > -1){
        UIButton *temp = (UIButton*)sender;
        NSLog(@"Button title: %@", temp.currentTitle);
        NSUInteger index1 = [sender tag];
        NSUInteger index2 = index1%10-1;
        index1 = index1/10 - 1;
        if(grid[4*index1+index2] == 0){
            if([self isGood:roll XLocation:index2 YLocation:index1]){
                grid[4*index1+index2] = roll;
                //[temp setTitle:[NSString stringWithFormat:@"%i",roll] forState:UIControlStateNormal];
                //[temp setBackgroundImage:[UIImage imageNamed:[self diceImage:roll]] forState:UIControlStateNormal];
                [(UIImageView*)views[4*index1+index2] setImage:[UIImage imageNamed:[self diceImage:roll]]];
                [resetButton setBackgroundImage:nil forState:UIControlStateNormal];
                roll = -1;
                for(NSInteger i = 0; i < 16; i++){
                    if(eks[i]){
                        [(UIImageView*)views[i] setImage:nil];
                        eks[i] = false;
                    }
                }
                pointsLabel.text = [NSString stringWithFormat:@"Points: %i",[self getPoints]];
            }else{
                NSLog(@"Can't Place Here");
                //[temp setTitle:@"X" forState:UIControlStateNormal];
                [(UIImageView*)views[4*index1+index2] setImage:[UIImage imageNamed:@"Redx.png"]];
                eks[4*index1+index2] = true;
            }
        }
    }
}

- (NSString*) diceImage:(int) a{
    NSString *name = [NSString stringWithFormat:@"Dice-%i.png",a]; 
    return name;
}

-(void) startTimer{
    startDate = [NSDate date];
}

-(int) endTimer{
    NSTimeInterval a;
    a = [[NSDate date] timeIntervalSinceDate:startDate];
    return a;
}

-(void) isDone{
    int full = 0;
    for(NSInteger i = 0; i < 4; i++){
        if([self rowIsComplete:i])
            full += 1;
        if([self colIsComplete:i])
            full += 1;
    }
    if(![self isAllGood] || full == 8){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
        NSMutableDictionary* plistDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
        NSArray* stats = [NSArray arrayWithObjects: [[NSNumber alloc] initWithInt:80], [[NSNumber alloc] initWithInt:[self getPoints]], [[NSNumber alloc] initWithInt:[self endTimer]], [[NSNumber alloc] initWithInt:[self countFullTiles]], nil];
        [plistDictionary setObject:stats forKey:@"Stats"];
        [plistDictionary writeToFile:path atomically:YES];
        [self performSegueWithIdentifier:@"Stats" sender:self];
    }
}

-(int) countFullTiles{
    int full = 0;
    for (int i = 0; i < 16; i++) {
        if(grid[i] != 0)
            full++;
    }
    return full;
}

-(BOOL) isAllGood{
    BOOL good = false;
    for(NSInteger i = 0; i < 4; i++){
        for (NSInteger j = 0; j < 4; j++){
            if(grid[4*j+i] == 0 && [self isGood:roll XLocation:i YLocation:j])
                good = true;
        }
    }
    return good;
}

- (BOOL) isGood:(int) Nroll XLocation:(int) x YLocation:(int) y{
    BOOL good = true;
    if (x > 0 && [self consecutive:Nroll SecondNumber:grid[4*y+x-1]]) {
        good = false;
    }
    if (x < 3 && [self consecutive:Nroll SecondNumber:grid[4*y+x+1]]) {
        good = false;
    }
    if (y > 0 && [self consecutive:Nroll SecondNumber:grid[4*y+x-4]]) {
        good = false;
    }
    if (y < 3 && [self consecutive:Nroll SecondNumber:grid[4*y+x+4]]) {
        good = false;
    }
    return good;
}

- (BOOL) consecutive:(int) a SecondNumber:(int) b{
    if(a == 0 || b == 0)
        return false;
    if(a+1 == b)
        return true;
    if(a-1 == b)
        return true;
    if(a == b)
        return true;
    return false;
}

-(int) getPoints{
    int points = 0;
    for(NSInteger i = 0; i < 4; i++){
        if([self rowIsComplete:i])
            points += 10;
        if([self colIsComplete:i])
            points += 10;
    }
    return points;
}

-(BOOL) rowIsComplete:(int) row{
    BOOL complete = true;
    for(NSInteger i = 0; i < 4; i++){
        if(grid[4*row+i] == 0)
            complete = false;
    }
    return complete;
}

-(BOOL) colIsComplete:(int) col{
    BOOL complete = true;
    for(NSInteger i = 0; i < 4; i++){
        if(grid[4*i+col] == 0)
            complete = false;
    }
    return complete;
}

- (void) viewWillAppear:(BOOL)animated{
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
}

@end

Stats Menu Code

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

//
//  StatsViewController.m
//  FourScore
//
//  Created by Denis Lachance on 12-05-31.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "StatsViewController.h"

@interface StatsViewController ()

@end

@implementation StatsViewController
@synthesize gradeLabel;
@synthesize commentString, pointsLabel, timeLabel, completionLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
    NSDictionary* plistDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSArray* stats = [plistDictionary objectForKey:@"Stats"];
    NSArray* comments = [plistDictionary objectForKey:@"Comments"];
    maxPoints = (int)[[stats objectAtIndex:0] intValue];
    points = (int)[[stats objectAtIndex:1] intValue];
    time = (int)[[stats objectAtIndex:2] intValue];
    completion = (int)100*[[stats objectAtIndex:3] intValue]/16;
    int grade = (int)(points*100/maxPoints);
    NSArray* comments2;
    if(grade < 20){
        comments2 = [comments objectAtIndex:0];
    }else if (grade < 40) {
        comments2 = [comments objectAtIndex:1];
    }else if (grade < 60) {
        comments2 = [comments objectAtIndex:2];
    }else if (grade < 80) {
        comments2 = [comments objectAtIndex:3];
    }else{
        comments2 = [comments objectAtIndex:4];
    }
    int index = arc4random()%[comments2 count];
    gradeLabel.text = [NSString stringWithFormat:@"%i%%", grade];
    commentString.text = [comments2 objectAtIndex:index];
    pointsLabel.text = [NSString stringWithFormat:@"%i pts",points];
    if(time >= 60)
        timeLabel.text = [NSString stringWithFormat:@"%i min, %i sec",time/60,time%60];
    else
        timeLabel.text = [NSString stringWithFormat:@"%i sec",time%60];
    completionLabel.text = [NSString stringWithFormat:@"%i %%",completion];
}

- (void)viewDidUnload
{
    [self setCommentString:nil];
    [self setPointsLabel:nil];
    [self setTimeLabel:nil];
    [self setCompletionLabel:nil];
    [self setGradeLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    //[commentString release];
    //[pointsLabel release];
    //[timeLabel release];
    //[completionLabel release];
    //[gradeLabel release];
    //[super dealloc];
}
- (IBAction)reset:(id)sender {
    NSArray* controllers = self.navigationController.viewControllers;
    GameViewController* parent = (GameViewController*)[controllers objectAtIndex:[controllers count] - 2];
    [parent onReset];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)exit:(id)sender {
    int a = [self.navigationController.viewControllers count] - 1;
    [self.navigationController 
     popToViewController:[[self.navigationController viewControllers] objectAtIndex:a-2]
     animated:YES];
}
@end