// // Puzzle.m // Demine // // Created by Michael Heyeck on 4/8/10. // Copyright 2010 Fair Oaks Labs, Inc.. All rights reserved. // #import "Puzzle.h" const static NSUInteger MAX_EDGE_SIZE = 500; const static char BIT_REVEALED = (1<<5); const static char BIT_MINED = (1<<4); const static char NEIGHBOR_MASK = 0xf; static inline char get_record(char* d, int w, int h, int x, int y) { return ((x>=0)&&(x=0)&&(y size/2) { // More than 50% mined: remove mines memset(d, BIT_MINED, size); for (int nmines=size; nmines>m;) { int idx = rand()%size; if (d[idx] & BIT_MINED) { nmines--; d[idx] &= ~BIT_MINED; } } } else { // Less than 50% mined: add mines memset(d, 0, size); for (int nmines=0; nmines= 0; x--) { for (int y = h-1; y >= 0; y--) { d[w*y+x] |= (is_mined(d,w,h,x-1,y-1) + is_mined(d,w,h,x+0,y-1) + is_mined(d,w,h,x+1,y-1) + is_mined(d,w,h,x-1,y+0) + is_mined(d,w,h,x+1,y+0) + is_mined(d,w,h,x-1,y+1) + is_mined(d,w,h,x+0,y+1) + is_mined(d,w,h,x+1,y+1) ); } } } - (BOOL)isMinedAtX:(NSUInteger)x andY:(NSUInteger)y { return is_mined(self.cells, w, h, x, y); } - (NSUInteger)neighborsAtX:(NSUInteger)x andY:(NSUInteger)y { return get_record(self.cells, w, h, x, y)&NEIGHBOR_MASK; } - (void)dealloc { if (cells) free(cells); [super dealloc]; } @end