Guest User

Untitled

a guest
Aug 10th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. #import "ViewController.h"
  2. #import "SoundManager.h"
  3. #import "UIView+Toast.h"
  4.  
  5. @interface ViewController (){
  6. NSMutableArray *arrayDeBotones;
  7. UIButton *nuevoBoton;
  8. UIView *menu;
  9. float anchoVentana;
  10. float altoVentana;
  11. int jugador;
  12. int jugada;
  13. }@end
  14.  
  15. @implementation ViewController
  16.  
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. anchoVentana=self.view.bounds.size.width;
  20. altoVentana=self.view.bounds.size.height;
  21. jugador=1;
  22. jugada=0;
  23. [self agregarBotones];
  24. [[SoundManager sharedManager] setMusicVolume:0.2];
  25. [[SoundManager sharedManager] playMusic:@"intro.mp3" looping:YES];
  26. }
  27.  
  28. - (void) agregarBotones{
  29. float anchoBoton=anchoVentana/3;
  30. float altoBoton=altoVentana/3;
  31. arrayDeBotones = [[NSMutableArray alloc] init];
  32. int x=0;
  33. int y=0;
  34. for (int i=0; i<9; i++) {
  35. nuevoBoton = [[UIButton alloc] initWithFrame:CGRectMake(anchoBoton*x+1, altoBoton*y+1, anchoBoton, altoBoton)];
  36. [nuevoBoton setBackgroundColor:[UIColor blueColor]];
  37. [nuevoBoton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  38. [nuevoBoton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  39. [nuevoBoton setAccessibilityLabel:@"Casilla vacía"];
  40. [nuevoBoton addTarget:self action:@selector(botonPulsado:) forControlEvents:UIControlEventTouchUpInside];
  41. nuevoBoton.tag = i;
  42. nuevoBoton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  43. nuevoBoton.layer.masksToBounds = YES;
  44. nuevoBoton.layer.cornerRadius = 5;
  45. nuevoBoton.layer.borderWidth = 1;
  46. nuevoBoton.layer.borderColor = [[UIColor blackColor] CGColor];
  47. [arrayDeBotones addObject:nuevoBoton];
  48. if(x==2){
  49. x=0;
  50. y=y+1;
  51. }else{
  52. x=x+1;
  53. }
  54. }
  55. for(UIButton *boton in arrayDeBotones){
  56. [self.view addSubview:boton];
  57. }
  58. }
  59.  
  60. -(void) botonPulsado:(UIButton *) sender {
  61. if((jugador>0)&&([self tituloBoton:sender.tag]==nil)){
  62. switch(jugador){
  63. case 1:
  64. [sender setAccessibilityLabel:@"X"];
  65. [sender setTitle:@"X" forState:UIControlStateNormal];
  66. [[SoundManager sharedManager] playSound:@"gato.mp3" looping:NO];
  67. break;
  68. case 2:
  69. [sender setAccessibilityLabel:@"O"];
  70. [sender setTitle:@"O" forState:UIControlStateNormal];
  71. [[SoundManager sharedManager] playSound:@"perro.mp3" looping:NO];
  72. break;
  73. }
  74. jugada=jugada+1;
  75. int resultado=[self ComprobarPartida];
  76. if(resultado>0) {
  77. [self finPartida:resultado];
  78. }else{
  79. [self turnoSiguiente];
  80. }
  81. }else{
  82. [[SoundManager sharedManager] playSound:@"vaca.mp3" looping:NO];
  83. }
  84. }
  85.  
  86. -(void) turnoSiguiente{
  87. switch(jugador){
  88. case 1:
  89. jugador=2;
  90. break;
  91. case 2:
  92. jugador=1;
  93. break;
  94. }
  95. [self.view makeToast:[NSString stringWithFormat:@"Turno del jugador %i", jugador]
  96. duration:3.0
  97. position:CSToastPositionCenter];
  98. }
  99.  
  100. - (int) ComprobarPartida{
  101. if (([[self tituloBoton:0] isEqual:[self tituloBoton:1]])&&([[self tituloBoton:1] isEqual:[self tituloBoton:2]])) return 1;
  102. if (([[self tituloBoton:3] isEqual:[self tituloBoton:4]])&&([[self tituloBoton:4] isEqual:[self tituloBoton:5]])) return 1;
  103. if (([[self tituloBoton:6] isEqual:[self tituloBoton:7]])&&([[self tituloBoton:7] isEqual:[self tituloBoton:8]])) return 1;
  104. if (([[self tituloBoton:0] isEqual:[self tituloBoton:3]])&&([[self tituloBoton:3] isEqual:[self tituloBoton:6]])) return 1;
  105. if (([[self tituloBoton:1] isEqual:[self tituloBoton:4]])&&([[self tituloBoton:4] isEqual:[self tituloBoton:7]])) return 1;
  106. if (([[self tituloBoton:2] isEqual:[self tituloBoton:5]])&&([[self tituloBoton:5] isEqual:[self tituloBoton:8]])) return 1;
  107. if (([[self tituloBoton:0] isEqual:[self tituloBoton:4]])&&([[self tituloBoton:4] isEqual:[self tituloBoton:8]])) return 1;
  108. if (([[self tituloBoton:2] isEqual:[self tituloBoton:4]])&&([[self tituloBoton:4] isEqual:[self tituloBoton:6]])) return 1;
  109. if(jugada==9) return 2;
  110. return 0;
  111. }
  112.  
  113. -(NSString*) tituloBoton:(int) tagBoton{
  114. for(UIButton *boton in arrayDeBotones){
  115. if(boton.tag==tagBoton) return boton.currentTitle;
  116. }
  117. return NULL;
  118. }
  119.  
  120. -(void) finPartida:(int) resultado{
  121. NSString *mensajeAlerta;
  122. switch(resultado){
  123. case 1:
  124. mensajeAlerta=[NSString stringWithFormat:@"Ha ganado el jugador %i",jugador];
  125. [[SoundManager sharedManager] playSound:@"aplausos.mp3" looping:NO];
  126. break;
  127. case 2:
  128. mensajeAlerta=@"La partida ha finalizado con un empate";
  129. [[SoundManager sharedManager] playSound:@"abucheo.mp3" looping:NO];
  130. break;
  131. }
  132. UIAlertController *alerta = [UIAlertController
  133. alertControllerWithTitle:@"¡Partida terminada!"
  134. message:mensajeAlerta
  135. preferredStyle:UIAlertControllerStyleAlert];
  136. UIAlertAction *ok=[UIAlertAction
  137. actionWithTitle:@"Aceptar"
  138. style:UIAlertActionStyleDefault
  139. handler:^(UIAlertAction *action)
  140. {
  141. [alerta dismissViewControllerAnimated:YES completion:nil];
  142. [self menuPartida:mensajeAlerta];
  143. }];
  144. [alerta addAction:ok];
  145. [self presentViewController:alerta animated:YES completion:nil];
  146. }
  147.  
  148. -(void) menuPartida:(NSString*) mensaje{
  149. jugador=0;
  150. jugada=0;
  151. [[SoundManager sharedManager] stopMusic:NO];
  152. [[SoundManager sharedManager] stopAllSounds:NO];
  153. for(UIButton *boton in arrayDeBotones){
  154. [boton removeFromSuperview];
  155. }
  156. [arrayDeBotones removeAllObjects];
  157. menu = [[UIView alloc]
  158. initWithFrame:CGRectMake(20,20,anchoVentana-40,altoVentana-40)];
  159. float anchoVistaMenu=menu.bounds.size.width;
  160. float altoVistaMenu=menu.bounds.size.height;
  161. UILabel *MenuEtiqueta = [[UILabel alloc]
  162. initWithFrame:CGRectMake(0,0,anchoVistaMenu,altoVistaMenu/4*3)];
  163. MenuEtiqueta.text=[NSString stringWithFormat:@"Partida terminada. %@", mensaje];
  164. [menu addSubview:MenuEtiqueta];
  165. UIButton *menuBoton = [[UIButton alloc]
  166. initWithFrame:CGRectMake(0,altoVistaMenu/4*3+1,anchoVistaMenu,altoVistaMenu/4)];
  167. [menuBoton setBackgroundColor:[UIColor blueColor]];
  168. [menuBoton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  169. [menuBoton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  170. [menuBoton setTitle:@"Jugar de nuevo" forState:UIControlStateNormal];
  171. [menuBoton addTarget:self action:@selector(jugarDeNuevo:) forControlEvents:UIControlEventTouchUpInside];
  172. menuBoton.tag = 9;
  173. menuBoton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  174. menuBoton.layer.masksToBounds = YES;
  175. menuBoton.layer.cornerRadius = 5;
  176. menuBoton.layer.borderWidth = 1;
  177. menuBoton.layer.borderColor = [[UIColor blackColor] CGColor];
  178. [menu addSubview:menuBoton];
  179. [self.view addSubview:menu];
  180. }
  181.  
  182. -(void) jugarDeNuevo:(UIButton *) sender {
  183. [menu removeFromSuperview];
  184. [self agregarBotones];
  185. jugador=1;
  186. [[SoundManager sharedManager] playMusic:@"intro.mp3" looping:YES];
  187. }
  188.  
  189. - (void)didReceiveMemoryWarning {
  190. [super didReceiveMemoryWarning];
  191. }
  192.  
  193. @end
Add Comment
Please, Sign In to add comment