ViewController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #import "ViewController.h"
  2. @interface ViewController ()
  3. @end
  4. @implementation ViewController
  5. NSMutableArray *buttonsArray;
  6. UIButton *newButton;
  7. int player=1;
  8. int jugada=0;
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. [self addButtons];
  12. }
  13. - (void) addButtons{
  14. float buttonWidth=self.view.bounds.size.width/3;
  15. float buttonHeight=self.view.bounds.size.height/3;
  16. buttonsArray = [[NSMutableArray alloc] init];
  17. int x=0;
  18. int y=0;
  19. for (int i=0; i<9; i++) {
  20. newButton = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth*x+1, buttonHeight*y+1, buttonWidth, buttonHeight)];
  21. [newButton setBackgroundColor:[UIColor blueColor]];
  22. [newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  23. [newButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
  24. // [newButton setTitle:@"" forState:UIControlStateNormal];
  25. [newButton setAccessibilityLabel:@"Casilla vacía"];
  26. [newButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
  27. newButton.tag = i;
  28. newButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  29. newButton.layer.masksToBounds = YES;
  30. newButton.layer.cornerRadius = 5;
  31. newButton.layer.borderWidth = 1;
  32. newButton.layer.borderColor = [[UIColor blackColor] CGColor];
  33. [buttonsArray addObject:newButton];
  34. if(x==2){
  35. x=0;
  36. y=y+1;
  37. }else{
  38. x=x+1;
  39. }
  40. }
  41. for(UIButton *button in buttonsArray){
  42. [self.view addSubview:button];
  43. }
  44. }
  45. -(void) buttonTouched:(UIButton *) sender {
  46. if([self buttonTitle:sender.tag]==nil){
  47. switch(player){
  48. case 1:
  49. [sender setAccessibilityLabel:@"X"];
  50. [sender setTitle:@"X" forState:UIControlStateNormal];
  51. break;
  52. case 2:
  53. [sender setAccessibilityLabel:@"O"];
  54. [sender setTitle:@"O" forState:UIControlStateNormal];
  55. break;
  56. }
  57. jugada=jugada+1;
  58. int resultado=[self checkPartida];
  59. if(resultado>0) {
  60. [self finPartida:resultado];
  61. }else{
  62. [self nextTurn];
  63. }
  64. }
  65. }
  66. -(void) nextTurn{
  67. switch(player){
  68. case 1:
  69. player=2;
  70. break;
  71. case 2:
  72. player=1;
  73. break;
  74. }
  75. }
  76. - (int) checkPartida{
  77. if (([[self buttonTitle:0] isEqual:[self buttonTitle:1]])&&([[self buttonTitle:1] isEqual:[self buttonTitle:2]])) return 1;
  78. if (([[self buttonTitle:3] isEqual:[self buttonTitle:4]])&&([[self buttonTitle:4] isEqual:[self buttonTitle:5]])) return 1;
  79. if (([[self buttonTitle:6] isEqual:[self buttonTitle:7]])&&([[self buttonTitle:7] isEqual:[self buttonTitle:8]])) return 1;
  80. if (([[self buttonTitle:0] isEqual:[self buttonTitle:3]])&&([[self buttonTitle:3] isEqual:[self buttonTitle:6]])) return 1;
  81. if (([[self buttonTitle:1] isEqual:[self buttonTitle:4]])&&([[self buttonTitle:4] isEqual:[self buttonTitle:7]])) return 1;
  82. if (([[self buttonTitle:2] isEqual:[self buttonTitle:5]])&&([[self buttonTitle:5] isEqual:[self buttonTitle:8]])) return 1;
  83. if (([[self buttonTitle:0] isEqual:[self buttonTitle:4]])&&([[self buttonTitle:4] isEqual:[self buttonTitle:8]])) return 1;
  84. if (([[self buttonTitle:2] isEqual:[self buttonTitle:4]])&&([[self buttonTitle:4] isEqual:[self buttonTitle:6]])) return 1;
  85. if(jugada==9) return 2;
  86. return 0;
  87. }
  88. -(NSString*) buttonTitle:(int) nButton{
  89. for(UIButton *button in buttonsArray){
  90. if(button.tag==nButton) return button.currentTitle;
  91. }
  92. return NULL;
  93. }
  94. -(void) finPartida:(int) resultado{
  95. NSString *message;
  96. if(resultado==1) message=[NSString stringWithFormat:@"Ha ganado el jugador %i",player];
  97. if (resultado==2) message=@"La partida ha finalizado con un empate";
  98. UIAlertController *alerta = [UIAlertController
  99. alertControllerWithTitle:@"¡Partida terminada!"
  100. message:message
  101. preferredStyle:UIAlertControllerStyleAlert];
  102. UIAlertAction *ok = [UIAlertAction
  103. actionWithTitle:@"OK"
  104. style:UIAlertActionStyleDefault
  105. handler:^(UIAlertAction *action)
  106. {
  107. [alerta dismissViewControllerAnimated:YES completion:nil];
  108. player=0;
  109. }];
  110. [alerta addAction:ok];
  111. [self presentViewController:alerta animated:YES completion:nil];
  112. }
  113. - (void)didReceiveMemoryWarning {
  114. [super didReceiveMemoryWarning];
  115. // Dispose of any resources that can be recreated.
  116. }
  117. @end