Skip to content

Commit

Permalink
switched to use singleton factory, added better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
noitsjocelyn committed Nov 4, 2013
1 parent 2fbf270 commit bddcac9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 89 deletions.
3 changes: 0 additions & 3 deletions Sudoku Teacher/Sudoku Teacher/PPMainMenuController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
#import <UIKit/UIKit.h>

@class Puzzle;
@class SudokuBoard;

@protocol PPMainMenuProtocol

- (void)setGameInProgress:(Puzzle *)thePuzzle;
- (void)preGeneratePuzzle;

@end

Expand All @@ -24,7 +22,6 @@
CGRect moderateCheckPosition;
BOOL newGameConfirmed;
UIAlertView *newGameAlert;
SudokuBoard *preGeneratedPuzzle;
}

@property (assign) NSUInteger difficulty;
Expand Down
49 changes: 3 additions & 46 deletions Sudoku Teacher/Sudoku Teacher/PPMainMenuController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#import "PPMainMenuController.h"
#import "PPSudokuGameViewController.h"
#import "Puzzle.h"
#import "SudokuBoard.h"
#import "SudokuBoardGenerator.h"
#import "PuzzleMakerFactory.h"

#define CHECK_FADE_TIME 0.4

Expand All @@ -20,8 +19,6 @@ @interface PPMainMenuController ()

@implementation PPMainMenuController

//@synthesize preGeneratedPuzzle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
Expand All @@ -34,7 +31,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
// Default to no game in progress. This may change with resuming from
// the closed app later, but should be good for now.
self.puzzleInProgress = Nil;
preGeneratedPuzzle = NULL;
}
return self;
}
Expand All @@ -44,7 +40,8 @@ - (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setupCheckPositions];
[self preGeneratePuzzle];
// Get the shared factory so we start generating puzzles
[PuzzleMakerFactory sharedInstance];
}

// viewDidLayoutSubviews is the last place to modify UI elements before they appear
Expand Down Expand Up @@ -79,45 +76,6 @@ - (void)didReceiveMemoryWarning
// Dispose of any resources that can be recreated.
}

- (void)preGeneratePuzzle
{
// Spin off the puzzle pre-generation to its own thread
[NSThread detachNewThreadSelector:@selector(makePuzzle)
toTarget:self
withObject:Nil];
}

- (void)makePuzzle
{
preGeneratedPuzzle = nil;
#ifdef DEBUG
NSUInteger attempts = 0;
#endif
// The generate method returns nil if there is a failure, so loop it
while (!preGeneratedPuzzle)
{
#ifdef DEBUG
++attempts;
NSLog(@"Pre-generating puzzle, attempt %d...", attempts);
#endif
preGeneratedPuzzle = [SudokuBoardGenerator generate];
}
#ifdef DEBUG
NSLog(@"Pre-generated.");
#endif
UIAlertView *preGenAlert = [[UIAlertView alloc] initWithTitle:@"Pre-generation complete"
message:nil
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[preGenAlert show];
// Exit the thread
if (![NSThread isMainThread])
{
[NSThread exit];
}
}

// Do stuff that needs to be done before a segue, like sending values ahead.
// Fires only if shouldPerformSegueWithIdentifier:sender: returned YES.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Expand All @@ -131,7 +89,6 @@ - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[controller setPuzzleData:nil];
self.puzzleInProgress = nil;
[controller setPreGeneratedPuzzle:preGeneratedPuzzle];
}
else
{
Expand Down
2 changes: 0 additions & 2 deletions Sudoku Teacher/Sudoku Teacher/PPSudokuGameViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

@class PPSudokuView;
@class Puzzle;
@class SudokuBoard;

@interface PPSudokuGameViewController : UIViewController
{
Expand All @@ -24,7 +23,6 @@
@property (assign) NSUInteger difficulty;
@property (assign) NSUInteger buttonSelected;
@property (assign) Puzzle *puzzleData;
@property (assign) SudokuBoard *preGeneratedPuzzle;

@property (weak, nonatomic) id<PPMainMenuProtocol> delegate;
@property (weak, nonatomic) IBOutlet PPSudokuView *boardBackground;
Expand Down
38 changes: 5 additions & 33 deletions Sudoku Teacher/Sudoku Teacher/PPSudokuGameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#import "PPSudokuGameViewController.h"
#import "Puzzle.h"
#import "PuzzleMaker.h"
#import "SudokuBoard.h"
#import "SudokuBoardGenerator.h"
#import "PuzzleMakerFactory.h"

@interface PPSudokuGameViewController ()

Expand Down Expand Up @@ -73,7 +72,6 @@ - (void)didReceiveMemoryWarning
- (void)viewWillDisappear:(BOOL)animated
{
[self.delegate setGameInProgress:self.puzzleData];
[self.delegate preGeneratePuzzle];
}

- (IBAction)setValue:(id)sender
Expand Down Expand Up @@ -180,40 +178,14 @@ - (void)setupProcessingView

- (void)generateAndDisplayBoard:(id)sender
{
SudokuBoard *aBoard;
// If we have a pre-generated board, use it
if (self.preGeneratedPuzzle)
{
aBoard = self.preGeneratedPuzzle;
#ifdef DEBUG
NSLog(@"Loaded pre-generated puzzle.");
#endif
}
// Otherwise, make one now
else
{
// The generate method returns nil if there is a failure, so loop it
#ifdef DEBUG
NSUInteger attempts = 0;
#endif
while (!aBoard)
{
#ifdef DEBUG
++attempts;
NSLog(@"Generating puzzle, attempt %d...", attempts);
#endif
aBoard = [SudokuBoardGenerator generate];
}
#ifdef DEBUG
NSLog(@"Generated.");
#endif
}
// Get an instance of the puzzle factory
PuzzleMakerFactory *factory = [PuzzleMakerFactory sharedInstance];
short *fullPuzzleArray = calloc(81, sizeof(short));
fullPuzzleArray = [aBoard boardAsShortArray:fullPuzzleArray];
// Generate the puzzle
[factory getBoard:fullPuzzleArray];
PuzzleMaker *aMaker = [[PuzzleMaker alloc] init];
[aMaker givePuzzle:fullPuzzleArray];
// Make our Puzzle data
// Make our puzzle data
short *puzzleArray = calloc(81, sizeof(short));
if (self.difficulty == 0)
{
Expand Down
2 changes: 0 additions & 2 deletions Sudoku Teacher/Sudoku Teacher/PuzzleMakerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

@interface PuzzleMakerFactory : NSObject
{
// dispatch_semaphore_t genSemaphore;
// __strong SudokuBoard *currentBoard;
short *currentBoard;
}

Expand Down
11 changes: 8 additions & 3 deletions Sudoku Teacher/Sudoku Teacher/PuzzleMakerFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

#import "PuzzleMakerFactory.h"
#import "../SudokuGenerator/SudokuBoard.h"
#import "../SudokuGenerator/SudokuBoardGenerator.h"
#import "SudokuBoard.h"
#import "SudokuBoardGenerator.h"

@implementation PuzzleMakerFactory

Expand All @@ -24,6 +24,7 @@ - (id)init

+ (id)sharedInstance
{
// Use the standard singleton creation pattern
static dispatch_once_t pred;
static PuzzleMakerFactory *sharedInstance = nil;
dispatch_once(&pred, ^{
Expand All @@ -33,13 +34,17 @@ + (id)sharedInstance
}

- (void)getBoard:(short *)outputArray
{ @synchronized (self)
{
@synchronized (self)
{
// Wait for there to be a puzzle
dispatch_semaphore_wait(self.genSemaphore, DISPATCH_TIME_FOREVER);
// Hand it off
for (NSUInteger i = 0; i < 81; ++i)
{
outputArray[i] = currentBoard[i];
}
// Start the next generation
[self generateBoard];
}
}
Expand Down

0 comments on commit bddcac9

Please sign in to comment.