redribben

BMGameDynamics

Nov 21st, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  BMGameDynamics.m
  3. //  tic tac toe
  4. //
  5. //  Created by Bogdan Michalchuk on 10/27/15.
  6. //  Copyright © 2015 PDXRR. All rights reserved.
  7. //
  8.  
  9. #import "BMGameDynamics.h"
  10. #import "Values.h"
  11.  
  12. @implementation BMGameDynamics
  13.  
  14. @synthesize tillCatsGame;
  15. @synthesize playersTurn;
  16.  
  17.  
  18. - (void)cleanArray {
  19.     for (int i = 0; i < 9; i++) {
  20.         xoSpots[i] = 0;
  21.     }
  22. }
  23.  
  24. - (void)modifyXOSpotsWithIndex:(int)indexOfSquare {
  25.     if (playersTurn == BMPlayerTypeX) {
  26.         xoSpots[indexOfSquare-1] = BMPlayerTypeX;
  27.         [_pDelegate updateCurrentSquare:indexOfSquare withCurrentPlayer:BMPlayerTypeX];
  28.         playersTurn = BMPlayerTypeO;
  29.     }
  30.     else if (playersTurn == BMPlayerTypeO) {
  31.         xoSpots[indexOfSquare-1] = BMPlayerTypeO;
  32.         [_pDelegate updateCurrentSquare:indexOfSquare withCurrentPlayer:BMPlayerTypeO];
  33.         playersTurn = BMPlayerTypeX;
  34.     }
  35.     [self checkForWinWithMove:YES];
  36. }
  37.  
  38. - (void)checkForWinWithMove:(BOOL)move {
  39.     NSUInteger winArray[24]={0,1,2,  3,4,5,  6,7,8,  0,3,6,  1,4,7,  2,5,8,  0,4,8,  2,4,6};
  40.     for(NSUInteger i=0;i<24; i+=3){
  41.         NSInteger totalPoint = xoSpots[winArray[i]] + xoSpots[winArray[i+1]] + xoSpots[winArray[i+2]];
  42.         switch (totalPoint) {
  43.             case BMPlayerTypeX * 3:
  44.                 [_pDelegate updateGameStatusWithWinner:BMPlayerTypeX];
  45.                 return;
  46.             case BMPlayerTypeO * 3:
  47.                 [_pDelegate updateGameStatusWithWinner:BMPlayerTypeO];
  48.                 return;
  49.             default:
  50.                 break;
  51.         }
  52.     }
  53.     if (move) {
  54.         // This checks if its a Cats Game
  55.         tillCatsGame = tillCatsGame -1;
  56.         if (tillCatsGame == 0) {
  57.             [_pDelegate updateGameStatusWithWinner:BMPlayerTypeTie];
  58.         }
  59.     }
  60. }
  61.  
  62. - (void)startAnew {
  63.     tillCatsGame = 9;
  64.     [self cleanArray];
  65. }
  66.  
  67. - (void)restoreGame {
  68.     for (int i = 0; i < 9; i++) {
  69.         switch (xoSpots[i]) {
  70.             case BMPlayerTypeX:
  71.                 [_pDelegate updateCurrentSquare:(i+1) withCurrentPlayer:BMPlayerTypeX];
  72.                 break;
  73.             case BMPlayerTypeO:
  74.                 [_pDelegate updateCurrentSquare:(i+1) withCurrentPlayer:BMPlayerTypeO];
  75.                 break;
  76.             default:
  77.                 break;
  78.         }
  79.     }
  80.     [self checkForWinWithMove:FALSE];
  81. }
  82.  
  83. - (NSArray*)returnSpotArray {
  84.     NSMutableArray* spotsArray = [[NSMutableArray alloc] initWithObjects:@0,@0,@0,@0,@0,@0,@0,@0,@0, nil ];
  85.     for (int i = 0; i < 9; ++i) {
  86.         NSNumber *value = [NSNumber numberWithInt:xoSpots[i]];
  87.         [spotsArray replaceObjectAtIndex:i withObject:value];
  88.     }
  89.     return spotsArray;
  90. }
  91.  
  92. - (void)setXOSpots:(NSArray*)incomingValues {
  93.     for (int i = 0; i < [incomingValues count]; ++i) {
  94.         xoSpots[i] = [[incomingValues objectAtIndex:i] intValue];
  95.     }
  96. }
  97.  
  98. //- (void) saveGameState {
  99. //    NSArray *spotsStateArray = [[NSArray alloc] initWithArray:[self returnSpotArray]]; // For saving each players' moves
  100. //    NSNumber *gameCount = [[NSNumber alloc] initWithInt:tillCatsGame]; // For saving the countdown to Scratch game
  101. //    NSNumber *currentTurn = [[NSNumber alloc] initWithInt:playersTurn]; // For saving the player turn
  102. //    NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys:
  103. //                              spotsStateArray, @"squares",
  104. //                              gameCount, @"count",
  105. //                              currentTurn, @"turn",
  106. //                              nil];
  107. //    [NSKeyedArchiver archiveRootObject:dataDict toFile:[self stateFileLocation]];
  108. //}
  109. //
  110. //- (BOOL)loadGameState {
  111. //    if ([[NSFileManager defaultManager] fileExistsAtPath:[self stateFileLocation]]) {
  112. //        NSData *data = [NSData dataWithContentsOfFile:[self stateFileLocation]];
  113. //        NSDictionary *savedData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  114. //
  115. //        NSArray *spotsStateArray = [[NSArray alloc] initWithArray:[savedData objectForKey:@"squares"]];
  116. //        [self setXOSpots:spotsStateArray];
  117. //        tillCatsGame = [[savedData objectForKey:@"count"] intValue];
  118. //        playersTurn = [[savedData objectForKey:@"turn"] intValue];
  119. //        return 1;
  120. //    }
  121. //    return 0;
  122. //}
  123.  
  124. - (id)initWithCoder:(NSCoder *)decoder {
  125.     self = [super init];
  126.     if (!self) {
  127.         return nil;
  128.     }
  129.     NSArray *myspots = [[NSArray alloc] initWithArray:[decoder decodeObjectForKey:@"squares"]];
  130.     [self setXOSpots:myspots];
  131. //    [self setXOSpots:[decoder decodeObjectForKey:@"squares"]];
  132.     //    [decoder decodeArrayOfObjCType:@encode(int) count:9 at:xoSpots];
  133.     int yes = [decoder decodeIntForKey:@"count"];
  134.     self.tillCatsGame = yes;
  135.     self.playersTurn = [decoder decodeIntForKey:@"turn"];
  136.     return self;
  137. }
  138.  
  139. - (void)encodeWithCoder:(NSCoder *)encoder {
  140.     NSArray *spotsStateArray = [[NSArray alloc] initWithArray:[self returnSpotArray]];
  141.     [encoder encodeObject:spotsStateArray forKey:@"squares"];
  142. //    [encoder encodeArrayOfObjCType:@encode(int) count:9 at:xoSpots];
  143.     [encoder encodeInt:self.tillCatsGame forKey:@"count"];
  144.     [encoder encodeInt:self.playersTurn forKey:@"turn"];
  145. }
  146.  
  147. @end
Advertisement
Add Comment
Please, Sign In to add comment