ViewController.m 6.6 KB

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