/BlackJack/BlackJackViewController.m

https://github.com/chao87/BlackJack · Objective C · 584 lines · 416 code · 114 blank · 54 comment · 58 complexity · e0dd5c3f63cb2b0923da3796b144697a MD5 · raw file

  1. //
  2. // BlackJackViewController.m
  3. // BlackJack
  4. //
  5. // Created by Yachao Liu on 7/27/11.
  6. // Copyright 2011 Intercall. All rights reserved.
  7. //
  8. #import "BlackJackViewController.h"
  9. #import "CardDeck.h"
  10. @implementation BlackJackViewController
  11. @synthesize cardDeckImage, score, deal, hit, stand;
  12. @synthesize cardDeck, dealer, guest, gameStarted, gameEnded;
  13. @synthesize staticPos, staticPos2;
  14. //@synthesize frontCardImage, containerView;
  15. //- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  16. //{
  17. // UITouch *touch = [touches anyObject];
  18. // CGPoint touchLocation = [touch locationInView:self.view];
  19. //
  20. // CGRect cardRect = [image1 frame];
  21. // if(CGRectContainsPoint(cardRect, touchLocation))
  22. // {
  23. // NSLog(@"Card tapped");
  24. // }
  25. // else
  26. // {
  27. // NSLog(@"Card not tapped");
  28. // return;
  29. // }
  30. //
  31. //}
  32. - (IBAction)hitMe:(id)sender
  33. {
  34. if (gameStarted)
  35. {
  36. //score.text = @"You hit!";
  37. if([guest handTotal] <= 21)
  38. {
  39. //get new card
  40. Card *dealtCard = [cardDeck dealCard];
  41. [guest.hand addObject:dealtCard];
  42. UIImageView *frontImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
  43. frontImage.tag = 1;
  44. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 73, 100, 100)];
  45. containerView.tag = 1;
  46. UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dealtCard.imageURL]];
  47. [self.view addSubview:containerView];
  48. [containerView addSubview:frontImage];
  49. [UIView animateWithDuration:1.0
  50. delay:0.0
  51. options: UIViewAnimationCurveEaseInOut
  52. animations:^{
  53. }
  54. completion:^(BOOL finished) {
  55. [UIView beginAnimations:nil context:NULL];
  56. [UIView setAnimationDuration:1.0];
  57. [UIView setAnimationDelay:.5];
  58. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES];
  59. [frontImage removeFromSuperview];
  60. [containerView addSubview:backImage];
  61. [UIView commitAnimations];
  62. [NSThread sleepForTimeInterval:1];
  63. [UIView animateWithDuration:1.0
  64. delay:1.0
  65. options: UIViewAnimationCurveEaseInOut
  66. animations:^{
  67. backImage.center = CGPointMake(staticPos, 167);
  68. }
  69. completion:^(BOOL finished) {
  70. [self checkGame:YES];
  71. }];
  72. }];
  73. //[self showScore];
  74. staticPos += 17;
  75. }
  76. }
  77. }
  78. - (void) hitDealer
  79. {
  80. //get new card
  81. Card *dealtCard2 = [cardDeck dealCard];
  82. [dealer.hand addObject:dealtCard2];
  83. UIImageView *frontImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
  84. frontImage.tag = 1;
  85. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 73, 100, 100)];
  86. containerView.tag = 1;
  87. UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dealtCard2.imageURL]];
  88. [self.view addSubview:containerView];
  89. [containerView addSubview:frontImage];
  90. [UIView animateWithDuration:1.0
  91. delay:0.0
  92. options: UIViewAnimationCurveEaseInOut
  93. animations:^{
  94. }
  95. completion:^(BOOL finished) {
  96. [UIView beginAnimations:nil context:NULL];
  97. [UIView setAnimationDuration:1.0];
  98. [UIView setAnimationDelay:.5];
  99. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES];
  100. [frontImage removeFromSuperview];
  101. [containerView addSubview:backImage];
  102. [UIView commitAnimations];
  103. [NSThread sleepForTimeInterval:1];
  104. [UIView animateWithDuration:1.0
  105. delay:1.0
  106. options: UIViewAnimationCurveEaseInOut
  107. animations:^{
  108. backImage.center = CGPointMake(staticPos2, -13);
  109. }
  110. completion:^(BOOL finished) {
  111. //[self checkGame:YES];
  112. }];
  113. }];
  114. staticPos2 += 17;
  115. }
  116. - (IBAction)stand:(id)sender
  117. {
  118. if(gameStarted)
  119. {
  120. score.text = @"You hit stand!";
  121. if([guest handTotal] <= 21)
  122. {
  123. [self hitDealer];
  124. }
  125. //[self resetGame];
  126. }
  127. }
  128. - (void) checkGame:(bool) checkDealer
  129. {
  130. if(checkDealer)
  131. {
  132. if([guest handTotal] > 21)
  133. {
  134. score.text = @"You Busted!";
  135. [self endGame];
  136. }
  137. }
  138. else
  139. {
  140. if([dealer handTotal] > 21)
  141. {
  142. score.text = @"Dealer Busted! You Win!";
  143. [self endGame];
  144. }
  145. }
  146. }
  147. - (void) resetGame
  148. {
  149. //remove card image views
  150. for(UIView* subview in [self.view subviews])
  151. {
  152. if(subview.tag == 1)
  153. {
  154. [subview removeFromSuperview];
  155. }
  156. }
  157. //set player to null
  158. [guest.hand removeAllObjects];
  159. [dealer.hand removeAllObjects];
  160. guest = nil;
  161. dealer = nil;
  162. //set carddeck null
  163. [cardDeck.deck removeAllObjects];
  164. cardDeck = nil;
  165. //set game state
  166. gameStarted = NO;
  167. gameEnded = YES;
  168. //reset score text
  169. score.text = @"";
  170. }
  171. - (void) showScore
  172. {
  173. //score.text = [score.text stringByAppendingString: [NSString stringWithFormat: @"%i", [guest handTotal]]];
  174. NSString* guestTotal = [NSString stringWithFormat: @"%i", [guest handTotal]];
  175. NSString* dealerTotal = [NSString stringWithFormat: @"%i", [dealer handTotal]];
  176. NSString* result = [[NSString alloc] initWithString:@" Guest Total is: "];
  177. result = [result stringByAppendingString:guestTotal];
  178. result = [result stringByAppendingString:@" - Dealer total is: "];
  179. result = [result stringByAppendingString:dealerTotal];
  180. score.text = result;
  181. //score.text = [score.text stringByAppendingString:result];
  182. }
  183. - (void) checkIfBlackjack
  184. {
  185. bool dealerBlackJack = NO;
  186. bool guestBlackJack = NO;
  187. if( ([[dealer.hand objectAtIndex:0] cardRank] == ONE) &&
  188. ([[dealer.hand objectAtIndex:1] cardRank] == TEN ||
  189. [[dealer.hand objectAtIndex:1] cardRank] == JACK ||
  190. [[dealer.hand objectAtIndex:1] cardRank] == QUEEN ||
  191. [[dealer.hand objectAtIndex:1] cardRank] == KING))
  192. {
  193. dealerBlackJack = YES;
  194. }
  195. else if( ([[dealer.hand objectAtIndex:1] cardRank] == ONE) &&
  196. ([[dealer.hand objectAtIndex:0] cardRank] == TEN ||
  197. [[dealer.hand objectAtIndex:0] cardRank] == JACK ||
  198. [[dealer.hand objectAtIndex:0] cardRank] == QUEEN ||
  199. [[dealer.hand objectAtIndex:0] cardRank] == KING))
  200. {
  201. dealerBlackJack = YES;
  202. }
  203. if( ([[guest.hand objectAtIndex:0] cardRank] == ONE) &&
  204. ([[guest.hand objectAtIndex:1] cardRank] == TEN ||
  205. [[guest.hand objectAtIndex:1] cardRank] == JACK ||
  206. [[guest.hand objectAtIndex:1] cardRank] == QUEEN ||
  207. [[guest.hand objectAtIndex:1] cardRank] == KING))
  208. {
  209. guestBlackJack = YES;
  210. }
  211. else if( ([[guest.hand objectAtIndex:1] cardRank] == ONE) &&
  212. ([[guest.hand objectAtIndex:0] cardRank] == TEN ||
  213. [[guest.hand objectAtIndex:0] cardRank] == JACK ||
  214. [[guest.hand objectAtIndex:0] cardRank] == QUEEN ||
  215. [[guest.hand objectAtIndex:0] cardRank] == KING))
  216. {
  217. guestBlackJack = YES;
  218. }
  219. if (dealerBlackJack && guestBlackJack)
  220. {
  221. score.text = @"Congrats on dealer and you for getting BlackJack!";
  222. [self showScore];
  223. }
  224. else if (dealerBlackJack)
  225. {
  226. score.text = @"Dealer got BlackJack!";
  227. [self showScore];
  228. }
  229. else if (guestBlackJack)
  230. {
  231. score.text = @"You got BlackJack!";
  232. [self showScore];
  233. }
  234. else
  235. {
  236. score.text = @"No one got Blackjack!";
  237. [self showScore];
  238. }
  239. if(guestBlackJack || dealerBlackJack)
  240. {
  241. [self endGame];
  242. }
  243. }
  244. - (void) endGame
  245. {
  246. gameEnded = YES;
  247. }
  248. - (void) dealSecondCards
  249. {
  250. //deal dealer second card
  251. Card *dealerSecondCard = [cardDeck dealCard];
  252. [dealer.hand addObject:dealerSecondCard];
  253. //deal player second card
  254. Card *playerSecondCard = [cardDeck dealCard];
  255. [guest.hand addObject:playerSecondCard];
  256. UIImageView *frontImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
  257. frontImage.tag = 1;
  258. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 73, 100, 100)];
  259. containerView.tag = 1;
  260. UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dealerSecondCard.imageURL]];
  261. backImage.tag = 1;
  262. [self.view addSubview:containerView];
  263. [containerView addSubview:frontImage];
  264. [UIView animateWithDuration:1.0
  265. delay:0.0
  266. options: UIViewAnimationCurveEaseInOut
  267. animations:^{
  268. }
  269. completion:^(BOOL finished) {
  270. [UIView beginAnimations:nil context:NULL];
  271. [UIView setAnimationDuration:1.0];
  272. [UIView setAnimationDelay:.5];
  273. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES];
  274. [frontImage removeFromSuperview];
  275. [containerView addSubview:backImage];
  276. [UIView commitAnimations];
  277. [NSThread sleepForTimeInterval:1];
  278. [UIView animateWithDuration:1.0
  279. delay:1.0
  280. options: UIViewAnimationCurveEaseInOut
  281. animations:^{
  282. backImage.center = CGPointMake(170, -13);
  283. }
  284. completion:^(BOOL finished) {
  285. [NSThread sleepForTimeInterval:1];
  286. //second
  287. UIImageView *frontImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
  288. frontImage2.tag = 1;
  289. UIView *containerView2 = [[UIView alloc] initWithFrame:CGRectMake(100, 73, 100, 100)];
  290. containerView2.tag = 1;
  291. UIImageView *backImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:playerSecondCard.imageURL]];
  292. backImage2.tag = 1;
  293. [self.view addSubview:containerView2];
  294. [containerView2 addSubview:frontImage2];
  295. [UIView animateWithDuration:1.0
  296. delay:0.0
  297. options: UIViewAnimationCurveEaseInOut
  298. animations:^{}
  299. completion:^(BOOL finished) {
  300. [UIView beginAnimations:nil context:NULL];
  301. [UIView setAnimationDuration:1.0];
  302. [UIView setAnimationDelay:.5];
  303. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView2 cache:YES];
  304. [frontImage2 removeFromSuperview];
  305. [containerView2 addSubview:backImage2];
  306. [UIView commitAnimations];
  307. [NSThread sleepForTimeInterval:1];
  308. [UIView animateWithDuration:1.0
  309. delay:1.0
  310. options: UIViewAnimationCurveEaseInOut
  311. animations:^{
  312. backImage2.center = CGPointMake(170, 167);
  313. }
  314. completion:^(BOOL finished) {
  315. [self checkIfBlackjack];
  316. }];
  317. }];
  318. }];
  319. }];
  320. }
  321. - (void) dealFirstCards
  322. {
  323. //deal dealer first card
  324. Card *dealerFirstCard = [cardDeck dealCard];
  325. [dealer.hand addObject:dealerFirstCard];
  326. UIImageView *dealerCardView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100, 90, 64, 64)];
  327. dealerCardView1.image = [UIImage imageNamed:@"back.png"];
  328. dealerCardView1.contentMode = UIViewContentModeCenter;
  329. //set tag value so we can identify it when we need to remove it for reset game
  330. dealerCardView1.tag = 1;
  331. [self.view addSubview:dealerCardView1];
  332. [UIView animateWithDuration:1.0
  333. delay:1.0
  334. options: UIViewAnimationCurveEaseInOut
  335. animations:^{
  336. dealerCardView1.center = CGPointMake(250, 60);
  337. }
  338. completion:^(BOOL finished) {
  339. [NSThread sleepForTimeInterval:1];
  340. //deal player first card
  341. Card *playerFirstCard = [cardDeck dealCard];
  342. [guest.hand addObject:playerFirstCard];
  343. UIImageView *frontImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
  344. frontImage.tag = 1;
  345. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 73, 100, 100)];
  346. containerView.tag = 1;
  347. UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:playerFirstCard.imageURL]];
  348. [self.view addSubview:containerView];
  349. [containerView addSubview:frontImage];
  350. [UIView animateWithDuration:1.0
  351. delay:0.0
  352. options: UIViewAnimationCurveEaseInOut
  353. animations:^{
  354. }
  355. completion:^(BOOL finished) {
  356. [UIView beginAnimations:nil context:NULL];
  357. [UIView setAnimationDuration:1.0];
  358. [UIView setAnimationDelay:.5];
  359. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES];
  360. [frontImage removeFromSuperview];
  361. [containerView addSubview:backImage];
  362. [UIView commitAnimations];
  363. [NSThread sleepForTimeInterval:1];
  364. [UIView animateWithDuration:1.0
  365. delay:1.0
  366. options: UIViewAnimationCurveEaseInOut
  367. animations:^{
  368. backImage.center = CGPointMake(150, 167);
  369. }
  370. completion:^(BOOL finished) {
  371. [self dealSecondCards];
  372. }];
  373. }];
  374. }];
  375. }
  376. - (IBAction)dealCards:(id)sender
  377. {
  378. if(!gameStarted && gameEnded)
  379. {
  380. gameStarted = YES;
  381. gameEnded = NO;
  382. staticPos = 170;
  383. staticPos2 = 170;
  384. //init card deck
  385. cardDeck = [[CardDeck alloc] init];
  386. [cardDeck shuffleDeck];
  387. //init players
  388. dealer = [[Player alloc] initWithName:@"Dealer"];
  389. guest = [[Player alloc] initWithName:@"Chao"];
  390. [self dealFirstCards];
  391. }
  392. else if(gameStarted && gameEnded)
  393. {
  394. [self resetGame];
  395. [self dealCards:self];
  396. }
  397. }
  398. - (void) moveDealerCard1:(UIImageView *)imageName
  399. {
  400. [UIView animateWithDuration:1.0
  401. delay:1.0
  402. options: UIViewAnimationCurveEaseInOut
  403. animations:^{
  404. imageName.center = CGPointMake(250, 200);
  405. }
  406. completion:^(BOOL finished) {
  407. }];
  408. }
  409. - (void) movePlayerCard1:(UIImageView *)imageName
  410. {
  411. [UIView animateWithDuration:1.0
  412. delay:1.0
  413. options: UIViewAnimationCurveEaseInOut
  414. animations:^{
  415. imageName.center = CGPointMake(50, 200);
  416. }
  417. completion:^(BOOL finished) {
  418. }];
  419. }
  420. - (void)didReceiveMemoryWarning
  421. {
  422. [super didReceiveMemoryWarning];
  423. // Release any cached data, images, etc that aren't in use.
  424. }
  425. #pragma mark - View lifecycle
  426. - (void)viewDidLoad
  427. {
  428. [super viewDidLoad];
  429. // Do any additional setup after loading the view, typically from a nib.
  430. //set default to landscape
  431. [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  432. CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
  433. [self.view setTransform:rotate];
  434. //load card deck image
  435. cardDeckImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 90, 64, 64)];
  436. cardDeckImage.image = [UIImage imageNamed:@"back.png"];
  437. cardDeckImage.contentMode = UIViewContentModeCenter;
  438. [self.view addSubview:cardDeckImage];
  439. //set game state
  440. gameStarted = NO;
  441. gameEnded = YES;
  442. }
  443. - (void)viewDidUnload
  444. {
  445. [super viewDidUnload];
  446. // Release any retained subviews of the main view.
  447. // e.g. self.myOutlet = nil;
  448. }
  449. - (void)viewWillAppear:(BOOL)animated
  450. {
  451. [super viewWillAppear:animated];
  452. }
  453. - (void)viewDidAppear:(BOOL)animated
  454. {
  455. [super viewDidAppear:animated];
  456. }
  457. - (void)viewWillDisappear:(BOOL)animated
  458. {
  459. [super viewWillDisappear:animated];
  460. }
  461. - (void)viewDidDisappear:(BOOL)animated
  462. {
  463. [super viewDidDisappear:animated];
  464. }
  465. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  466. {
  467. // Return YES for supported orientations
  468. return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
  469. }
  470. @end