How to Write your own Minesweeper AI

luckytoilet's avatarLucky's Notes

A while ago, I wrote a minesweeper AI. I intended to publish a writeup, but due to university and life and exams, I never got around to writing it. But having just finished my Fall term, I have some time to write a decent overview of what I did.

Short 30 second video of the AI in action here:

How to Play Minesweeper

If you’re an experienced minesweeper player, you can probably skip this section. Otherwise, I’ll just give a quick overview of some basic strategies that we can use to solve an easy minesweeper game.

We start with a 10×10 Beginner’s grid, and click on a square in the middle:

We can quickly identify some of the mines. When the number 1 has exactly one empty square around it, then we know there’s a mine there.

Let’s go ahead and mark the mines:

Now the next strategy: if a…

View original post 1,572 more words

2012: Looking Back

2012 is almost over. It was an awesome year. Thanks to everyone that made it that way!

1.) The year began by entering a very intense second semester of my freshman year at the University of Michigan. I learned a ton about how to handle a tough schedule and balance my time wisely.

2.) After applying to many various internships, I found out that I had an offer from Path in San Francisco, a company that I had dreamed of working at.

3.) Besides just talking the normal workload, my friend Chris Wendel and I taught an iOS development class under the guidance of Prof. Ringenberg at U of M. We had the opportunity to build and give lectures. We made new friends, learned a ton, and gave a few students new skills to get internships.

4.) I made some great new friends through school. I can’t even name all of the great people I met.

5.) I worked the few month or two of the summer at Detroit Labs, learning again from Henry Balanon, Dan Ward, Nathan Hughes, and Paul Glomski.

6.) I made the trip out to California. I began working at Path, exploring the city, and learning true SF/silicon valley startup culture. It was amazing. I lived with Chris Wendel in the heart of downtown. Living on my own that far away from Michigan was a true adventure, and I am so glad I decided to make the jump this year. Least enjoyable part of this summer: sleeping on an air mattress for 2+ months.

7.) Since I was finally out in California, I was able to spend more time with Stacey Ferreira, cofounder of MySocialCloud. Besides explore the city, and the California coast, I was able to spend a lot of time with her and her brother. Midnight walks around San Francisco were awesome.

8.) I was able to speak at the Teens in Tech Conference in August! Thanks to Daniel Brusilovsky for that amazing opportunity!

9.) I came right back to Ann Arbor for school in September.

10.) I just finished a semester filled with much caffeine, great friends, and much math and code.

11.) I was able to head back out to the Bay for interviews in Palo Alto and the city.

12.) I was fortunate enough to receive offers from Path, Flipboard, Twitter, Detroit Labs, and Apple. I accepted an Apple internship for the summer.

 

Looking forward to next year, I have a few goals for myself. I want to become a better software engineer. One of the main reasons I chose to go to Apple was simply that I wanted a place where I could learn the most. By expanding my knowledge from simply iOS to Mac development, I will learn a ton more about Object Oriented Design, deeper components of the Objective-C language, and the Cocoa Framework. Regardless of where I choose to go in the future, I feel that Apple will give me a solid background.

Next, I want to learn to even better manage my time, and be more productive. Throughout the course of the past few months I have tried to work in freelance projects. Although the projects do get accomplished, I want to improve the way I schedule my time and mangage school, projects, and everything else going on in my life.

Finally,  I will be turning 20, and I know (mostly) what I want to do with my life. I want to continue to maintain and seek out friendships and relationships that help me accomplish my goals and push me beyond them. I want to make sure I take the time to do what it takes to keep these connections in my life strong.

 

Making Network Calls in iOS

A very common task of any developer is to write a web services class. In this post I will present a very common pattern for organizing and incorporating network calls into iOS apps.

To give a quick overview, we will be creating a singleton web services class. This basically means we will make a new class that stores all the methods for making network requests, and that the same allocation of this class in memory will be accessible across all views in the app. If you want to learn more about this design pattern, look at my other post.

So, lets create this class. In Xcode, create a new objective c file that subclasses from NSObject.

The header file will look like this:


// WebServices.h
// DC_magazine
//
// Created by Andrew Rauh on 6/4/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface WebServices : NSObject

@property (strong, nonatomic) NSMutableArray *blogPosts;
@property (strong, nonatomic) NSMutableArray *titles;
@property (strong, nonatomic) NSMutableDictionary *selectedPost;

- (void) grabDataAndParse;
- (void) buildTitleArray;
+(id) sharedInstance;

And here is the implementation file. (.m)

</pre>
//
// WebServices.m
// DC_magazine
//
// Created by Andrew Rauh on 6/4/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "WebServices.h"

@implementation WebServices
@synthesize blogPosts, titles,selectedPost, ;

+(id)sharedInstance
{
 static id sharedInstance = nil;
 if (sharedInstance == nil) {
 sharedInstance = [[self alloc] init];
 }
 return sharedInstance;
}

-(void)grabDataAndParse
{
 selectedPost = [[NSMutableDictionary alloc]init];
 NSURLRequest *main = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://dcfaithinaction.org/?json=get_recent_posts"]]];

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 dispatch_async(queue, ^{
 NSURLResponse *response = nil;
 NSError *error = nil;

 NSData *data = [NSURLConnection sendSynchronousRequest:main
 returningResponse:&response
 error:&error];
 NSString *json = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
 id parsedObject = nil;
 NSError *parseError = nil;
 if (json != nil && [json isKindOfClass:[NSString class]] && [json length] > 0) {
 parsedObject = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSASCIIStringEncoding]
 options:0
 error:&parseError];
 }
 if ([parsedObject isKindOfClass:[NSDictionary class]]) {
 NSDictionary *parsedDictionary = (NSDictionary *)parsedObject;

 NSArray *posts = [parsedDictionary objectForKey:@"posts"];
 blogPosts =[NSMutableArray arrayWithArray:posts];
 [self buildTitleArray];
 }
 });
}
- (void)buildTitleArray
{
 titles = [[NSMutableArray alloc] init ];

 for (NSDictionary *dict in blogPosts) {
 [titles addObject:[dict objectForKey:@"title"]];
 //NSLog(@"%@", [dict objectForKey:@"title"]);
 }
}

@end
<pre>

In this case, I used NSURLConnection with an asynchronous block to make the actual network call. After you make the call, you get NSData back. You then treat this as a generic objective-c object, and see if it is either a dictionary or an array. Once you figure out what data type the object you got back from the server is, cast it to that type of object and start iterating over its elements, and extract the needed data.

The reason this network call must be made asynchronously is pretty simple. On iOS, there is a main thread, where all of the UI code is run. If we put this networking code on that same thread, then all of the UI on the devices would completely freeze until we got the needed data back from the server.