ViewController.m 6.5 KB

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