Guest User

Untitled

a guest
Jan 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <UIKit/UIKit.h>
  2.  
  3.  
  4. @interface LogicaViewController : UIViewController<UIGestureRecognizerDelegate> {
  5.     IBOutlet UILabel *numero1;
  6.     IBOutlet UILabel *numero2;
  7.     IBOutlet UILabel *numero3;
  8.     IBOutlet UILabel *numero4;
  9.     IBOutlet UIButton *operador1;
  10.     IBOutlet UIButton *operador2;
  11.     IBOutlet UIButton *operador3;
  12.     IBOutlet UIButton *reiniciar;
  13.     IBOutlet UIImageView *imagenCorrecto;
  14.     NSString *operadorReal1;
  15.     NSString *operadorReal2;
  16.     NSString *operadorReal3;
  17. }
  18.  
  19. @property (nonatomic, retain) NSString *operadorReal1;
  20. @property (nonatomic, retain) NSString *operadorReal2;
  21. @property (nonatomic, retain) NSString *operadorReal3;
  22.  
  23.  
  24. - (IBAction)cambiarOperador1:(id)sender;
  25. - (IBAction)cambiarOperador2:(id)sender;
  26. - (IBAction)cambiarOperador3:(id)sender;
  27. - (IBAction)reiniciar:(id)sender;
  28.  
  29. - (void)comprobarOperadores;
  30.  
  31. @end
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. #import "LogicaViewController.h"
  39.  
  40.  
  41. @implementation LogicaViewController
  42.  
  43. @synthesize operadorReal1, operadorReal2,operadorReal3;
  44.  
  45.  
  46.  
  47. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  48. {
  49.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  50.     if (self) {
  51.         // Custom initialization
  52.        
  53.        
  54.        
  55.     }
  56.     return self;
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  64.     NSLog(@"%@", "gestureRecognizer");
  65.     return YES;
  66. }
  67.  
  68.  
  69.  
  70. - (IBAction)cambiarOperador1:(id)sender{
  71.     NSLog(@"cambiarOperador1");
  72.    
  73.     [operador1 setTitle:[self recuperarNuevo:[[operador1 titleLabel]text]] forState:UIControlStateNormal];
  74.     [self comprobarOperadores];
  75.    
  76. }
  77.  
  78. - (IBAction)cambiarOperador2:(id)sender{
  79.     NSLog(@"cambiarOperador2");
  80.     [operador2 setTitle:[self recuperarNuevo:[[operador2 titleLabel]text]] forState:UIControlStateNormal];
  81.     [self comprobarOperadores];
  82. }
  83. - (IBAction)cambiarOperador3:(id)sender{
  84.     NSLog(@"cambiarOperador3");
  85.     [operador3 setTitle:[self recuperarNuevo:[[operador3 titleLabel]text]] forState:UIControlStateNormal];
  86.     [self comprobarOperadores];
  87. }
  88.  
  89. - (IBAction)reiniciar:(id)sender{
  90.     [self crearFormulaAleatoria];
  91.     NSArray *randomListaOperadores = [NSArray arrayWithObjects:
  92.                                       @"+",
  93.                                       @"-",
  94.                                       @"*",
  95.                                       @"=",
  96.                                       @"/",nil];
  97.     int operadorAleatorio = arc4random() % [randomListaOperadores count];
  98.     int operadorAleatorio2 = arc4random() % [randomListaOperadores count];
  99.     int operadorAleatorio3 = arc4random() % [randomListaOperadores count];
  100.     NSString *randomOperador = [NSString stringWithFormat:@"%@",
  101.                                 [randomListaOperadores objectAtIndex:operadorAleatorio]];
  102.     NSString *randomOperador2 = [NSString stringWithFormat:@"%@",
  103.                                  [randomListaOperadores objectAtIndex:operadorAleatorio2]];
  104.     NSString *randomOperador3 = [NSString stringWithFormat:@"%@",
  105.                                  [randomListaOperadores objectAtIndex:operadorAleatorio3]];
  106.     [operador1 setTitle:randomOperador forState:UIControlStateNormal];
  107.     [operador2 setTitle:randomOperador2 forState:UIControlStateNormal];
  108.     [operador3 setTitle:randomOperador3 forState:UIControlStateNormal];
  109.    
  110.     //ocultar imagen correcto
  111.     [imagenCorrecto setHidden:true];
  112. }
  113.  
  114.  
  115. - (NSString*)recuperarNuevo:(NSString*)actual{
  116.     if([actual isEqualToString:@"+"]){
  117.         return @"-";
  118.     }else if([actual isEqualToString:@"-"]){
  119.         return @"*";
  120.     }else if([actual isEqualToString:@"*"]){
  121.         return @"/";
  122.     }else if([actual isEqualToString:@"/"]){
  123.         return @"=";
  124.     }else if([actual isEqualToString:@"="]){
  125.         return @"+";
  126.     }
  127.     return @" ";
  128. }
  129.  
  130.  
  131.  
  132. - (void)dealloc
  133. {
  134.     [super dealloc];
  135. }
  136.  
  137. - (void)didReceiveMemoryWarning
  138. {
  139.     // Releases the view if it doesn't have a superview.
  140.     [super didReceiveMemoryWarning];
  141.    
  142.     // Release any cached data, images, etc that aren't in use.
  143. }
  144.  
  145. #pragma mark - View lifecycle
  146.  
  147. - (void)viewDidLoad
  148. {
  149.     [super viewDidLoad];
  150.     [self reiniciar:nil];
  151.  
  152. }
  153.  
  154.  
  155. - (void)crearFormulaAleatoria{
  156.     //nivel de dificultad2
  157.     int numeroAle1 = arc4random() % 100; //entre 0 y 99
  158.     int numeroAle2 = arc4random() % 100; //entre 0 y 99
  159.     int numeroAle3 = arc4random() % 100; //entre 0 y 99
  160.     [numero1 setText:[NSString stringWithFormat:@"%d", numeroAle1]];
  161.     [numero2 setText:[NSString stringWithFormat:@"%d", numeroAle2]];
  162.     [numero3 setText:[NSString stringWithFormat:@"%d", numeroAle3]];
  163.    
  164.     NSArray *randomListaOperadores = [NSArray arrayWithObjects:
  165.                                       @"+",
  166.                                       @"-",
  167.                                       @"*",
  168.                                       @"/",nil];
  169.     int operadorAleatorio = arc4random() % [randomListaOperadores count];
  170.     int operadorAleatorio2 = arc4random() % [randomListaOperadores count];
  171.     int operadorAleatorio3 = arc4random() % [randomListaOperadores count];
  172.     operadorReal1 = [NSString stringWithFormat:@"%@",
  173.                                 [randomListaOperadores objectAtIndex:operadorAleatorio]];
  174.     operadorReal2 = [NSString stringWithFormat:@"%@",
  175.                                  [randomListaOperadores objectAtIndex:operadorAleatorio2]];
  176.     operadorReal3 = @"=";
  177.    
  178.     //calcular numero4
  179.     int numero12 = [self calcular:operadorReal1 numero1:numeroAle1 numero2:numeroAle2];
  180.     int numero23 = [self calcular:operadorReal2 numero1:numero12 numero2:numeroAle3];
  181.    
  182.      [numero4 setText:[NSString stringWithFormat:@"%d", numero23]];
  183. }
  184.  
  185. - (int)calcular:(NSString*)operador numero1:(int)numero1 numero2:(int)numero2{
  186.     if([operador isEqualToString:@"+"]){
  187.         return numero1+numero2;
  188.     }else if([operador isEqualToString:@"-"]){
  189.         return numero1-numero2;
  190.     }else if([operador isEqualToString:@"*"]){
  191.         return numero1*numero2;
  192.     }else if([operador isEqualToString:@"/"]){
  193.         return numero1/numero2;
  194.     }
  195. }
  196.  
  197. - (void)comprobarOperadores{
  198.     /*NSLog(@"%@", [NSString stringWithFormat:@"%@", [[operador1 titleLabel]text]]);
  199.     NSLog(@"%@", [NSString stringWithFormat:@"%@", operadorReal1]);
  200.     NSLog(@"%@", [NSString stringWithFormat:@"%@", [[operador2 titleLabel]text]]);
  201.     NSLog(@"%@", [NSString stringWithFormat:@"%@", operadorReal2]);
  202.     NSLog(@"%@", [NSString stringWithFormat:@"%@", [[operador3 titleLabel]text]]);
  203.     NSLog(@"%@", [NSString stringWithFormat:@"%@", operadorReal3]);*/
  204.     //NSLog(operadorReal3);
  205.    
  206.     if([operadorReal1 isEqualToString:operador1.titleLabel.text] &&
  207.        [operadorReal2 isEqualToString:operador2.titleLabel.text] &&
  208.        [operadorReal3 isEqualToString:operador3.titleLabel.text]){
  209.        
  210.         //ocultar imagen correcto
  211.         [imagenCorrecto setHidden:false];
  212.     }
  213.  
  214. }
  215.  
  216. - (void)viewDidUnload
  217. {
  218.     [super viewDidUnload];
  219.     // Release any retained subviews of the main view.
  220.     // e.g. self.myOutlet = nil;
  221. }
  222.  
  223. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  224. {
  225.     // Return YES for supported orientations
  226.     //return (interfaceOrientation == UIInterfaceOrientationPortrait);
  227.     return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
  228. }
  229.  
  230. @end
Add Comment
Please, Sign In to add comment