-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.m
executable file
·204 lines (154 loc) · 5.46 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// ViewController.m
// BlackJackGame
//
// Created by Yaroslav Dukal on 09.03.15.
// Copyright (c) 2015 Yaroslav Dukal. All rights reserved.
//
#import "ViewController.h"
#import "blackjack.h"
#import "Menu.h"
@implementation ViewController
@synthesize button1, button2, button3, exitbutton;
@synthesize theBlackJack;
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad
{
autoPlay = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstTwoDealt) name:@"FirstTwoCardsReady" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(donePlaying) name:@"dealerDonePlaying" object:nil];
button2.title = @"";
button2.enabled = NO;
[super viewDidLoad];
}
-(void) donePlaying {
button1.enabled = YES;
button1.title = @"PLAY";
button2.enabled = NO;
button2.title = @"";
button3.enabled = NO;
button3.title = @"";
if (autoPlay == YES) {
if (theBlackJack.totalDealerVallue > 21) {
NSString* dealerValueText = [[NSString alloc ] initWithFormat:@"Dealer has %i ", theBlackJack.totalDealerVallue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Dealer Busts!" message:dealerValueText delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: @"Continue", nil];
[alert setDelegate:self];
[alert show];
[alert release];
[dealerValueText release];
button2.enabled = NO;
} else {
NSString* dealerValueText = [[NSString alloc ] initWithFormat:@"Dealer stands %i ", theBlackJack.totalDealerVallue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:dealerValueText delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: @"Continue", nil];
[alert setDelegate:self];
[alert show];
[alert release];
[dealerValueText release];
}
}
if (autoPlay == NO) {
if (theBlackJack.totalDealerVallue > 21) {
NSString* dealerValueText = [[NSString alloc ] initWithFormat:@"Player has %i ", theBlackJack.totalDealerVallue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Player Busts!" message:dealerValueText delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: @"Continue", nil];
[alert setDelegate:self];
[alert show];
[alert release];
[dealerValueText release];
button2.enabled = NO;
} else {
NSString* dealerValueText = [[NSString alloc ] initWithFormat:@"Player stands %i ", theBlackJack.totalDealerVallue];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:dealerValueText delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: @"Continue", nil];
[alert setDelegate:self];
[alert show];
[alert release];
[dealerValueText release];
button2.enabled = NO;
}
}
}
//Обработчик нажатий
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//NSLog(@"OK button pressed");
}
if (buttonIndex == 1)
{
[self playBlackJack:self];
}
}
-(void) firstTwoDealt
{
if (autoPlay == YES) {
button1.title = @"";
button2.title = @"Make Dealer Play";
button2.enabled = YES;
} else {
button1.title = @"";
button2.title = @"Hit Me!";
button2.enabled = YES;
button3.title = @"STAY";
button3.enabled = YES;
}
}
//go back to main screen
-(IBAction) exitfromGame:(id)sender {
[UIView beginAnimations:@"Flipping back" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
}
-(IBAction) playBlackJack:(id)sender
{
if (autoPlay == YES) {
autoPlay = NO;
}
else
{
autoPlay = YES;
}
button1.title = @"";
button1.enabled = NO;
button2.enabled = NO;
button3.enabled = NO;
button2.title = @"";
button3.title = @"";
if (theBlackJack.view != nil) {
[theBlackJack.view removeFromSuperview];
}
blackjack *newBlackJack = [[blackjack alloc] initWithNibName:@"blackjack" bundle:nil];
self.theBlackJack = newBlackJack;
[self.view insertSubview:theBlackJack.view atIndex:1];
[newBlackJack release];
theBlackJack.autoPlay = autoPlay;
}
-(IBAction) showDealerHand:(id)sender
{
if (autoPlay == YES)
{
button1.enabled = NO;
button1.title = @"";
button2.enabled = NO;
button2.title = @"Dealer Playing...";
[theBlackJack delearMightHit]; // NSLog(@"show hand");
}
else
{
button1.enabled = NO;
button2.enabled = YES;
button2.title = @"HIT!";
[theBlackJack delearMightHit];
}
}
-(IBAction) stay:(id)sender {
[self donePlaying];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end