Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <UIKit/UIKit.h>
- @interface LogicaViewController : UIViewController<UIGestureRecognizerDelegate> {
- IBOutlet UILabel *numero1;
- IBOutlet UILabel *numero2;
- IBOutlet UILabel *numero3;
- IBOutlet UILabel *numero4;
- IBOutlet UIButton *operador1;
- IBOutlet UIButton *operador2;
- IBOutlet UIButton *operador3;
- IBOutlet UIButton *reiniciar;
- IBOutlet UIImageView *imagenCorrecto;
- NSString *operadorReal1;
- NSString *operadorReal2;
- NSString *operadorReal3;
- }
- @property (nonatomic, retain) NSString *operadorReal1;
- @property (nonatomic, retain) NSString *operadorReal2;
- @property (nonatomic, retain) NSString *operadorReal3;
- - (IBAction)cambiarOperador1:(id)sender;
- - (IBAction)cambiarOperador2:(id)sender;
- - (IBAction)cambiarOperador3:(id)sender;
- - (IBAction)reiniciar:(id)sender;
- - (void)comprobarOperadores;
- @end
- #import "LogicaViewController.h"
- @implementation LogicaViewController
- @synthesize operadorReal1, operadorReal2,operadorReal3;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
- NSLog(@"%@", "gestureRecognizer");
- return YES;
- }
- - (IBAction)cambiarOperador1:(id)sender{
- NSLog(@"cambiarOperador1");
- [operador1 setTitle:[self recuperarNuevo:[[operador1 titleLabel]text]] forState:UIControlStateNormal];
- [self comprobarOperadores];
- }
- - (IBAction)cambiarOperador2:(id)sender{
- NSLog(@"cambiarOperador2");
- [operador2 setTitle:[self recuperarNuevo:[[operador2 titleLabel]text]] forState:UIControlStateNormal];
- [self comprobarOperadores];
- }
- - (IBAction)cambiarOperador3:(id)sender{
- NSLog(@"cambiarOperador3");
- [operador3 setTitle:[self recuperarNuevo:[[operador3 titleLabel]text]] forState:UIControlStateNormal];
- [self comprobarOperadores];
- }
- - (IBAction)reiniciar:(id)sender{
- [self crearFormulaAleatoria];
- NSArray *randomListaOperadores = [NSArray arrayWithObjects:
- @"+",
- @"-",
- @"*",
- @"=",
- @"/",nil];
- int operadorAleatorio = arc4random() % [randomListaOperadores count];
- int operadorAleatorio2 = arc4random() % [randomListaOperadores count];
- int operadorAleatorio3 = arc4random() % [randomListaOperadores count];
- NSString *randomOperador = [NSString stringWithFormat:@"%@",
- [randomListaOperadores objectAtIndex:operadorAleatorio]];
- NSString *randomOperador2 = [NSString stringWithFormat:@"%@",
- [randomListaOperadores objectAtIndex:operadorAleatorio2]];
- NSString *randomOperador3 = [NSString stringWithFormat:@"%@",
- [randomListaOperadores objectAtIndex:operadorAleatorio3]];
- [operador1 setTitle:randomOperador forState:UIControlStateNormal];
- [operador2 setTitle:randomOperador2 forState:UIControlStateNormal];
- [operador3 setTitle:randomOperador3 forState:UIControlStateNormal];
- //ocultar imagen correcto
- [imagenCorrecto setHidden:true];
- }
- - (NSString*)recuperarNuevo:(NSString*)actual{
- if([actual isEqualToString:@"+"]){
- return @"-";
- }else if([actual isEqualToString:@"-"]){
- return @"*";
- }else if([actual isEqualToString:@"*"]){
- return @"/";
- }else if([actual isEqualToString:@"/"]){
- return @"=";
- }else if([actual isEqualToString:@"="]){
- return @"+";
- }
- return @" ";
- }
- - (void)dealloc
- {
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self reiniciar:nil];
- }
- - (void)crearFormulaAleatoria{
- //nivel de dificultad2
- int numeroAle1 = arc4random() % 100; //entre 0 y 99
- int numeroAle2 = arc4random() % 100; //entre 0 y 99
- int numeroAle3 = arc4random() % 100; //entre 0 y 99
- [numero1 setText:[NSString stringWithFormat:@"%d", numeroAle1]];
- [numero2 setText:[NSString stringWithFormat:@"%d", numeroAle2]];
- [numero3 setText:[NSString stringWithFormat:@"%d", numeroAle3]];
- NSArray *randomListaOperadores = [NSArray arrayWithObjects:
- @"+",
- @"-",
- @"*",
- @"/",nil];
- int operadorAleatorio = arc4random() % [randomListaOperadores count];
- int operadorAleatorio2 = arc4random() % [randomListaOperadores count];
- int operadorAleatorio3 = arc4random() % [randomListaOperadores count];
- operadorReal1 = [NSString stringWithFormat:@"%@",
- [randomListaOperadores objectAtIndex:operadorAleatorio]];
- operadorReal2 = [NSString stringWithFormat:@"%@",
- [randomListaOperadores objectAtIndex:operadorAleatorio2]];
- operadorReal3 = @"=";
- //calcular numero4
- int numero12 = [self calcular:operadorReal1 numero1:numeroAle1 numero2:numeroAle2];
- int numero23 = [self calcular:operadorReal2 numero1:numero12 numero2:numeroAle3];
- [numero4 setText:[NSString stringWithFormat:@"%d", numero23]];
- }
- - (int)calcular:(NSString*)operador numero1:(int)numero1 numero2:(int)numero2{
- if([operador isEqualToString:@"+"]){
- return numero1+numero2;
- }else if([operador isEqualToString:@"-"]){
- return numero1-numero2;
- }else if([operador isEqualToString:@"*"]){
- return numero1*numero2;
- }else if([operador isEqualToString:@"/"]){
- return numero1/numero2;
- }
- }
- - (void)comprobarOperadores{
- /*NSLog(@"%@", [NSString stringWithFormat:@"%@", [[operador1 titleLabel]text]]);
- NSLog(@"%@", [NSString stringWithFormat:@"%@", operadorReal1]);
- NSLog(@"%@", [NSString stringWithFormat:@"%@", [[operador2 titleLabel]text]]);
- NSLog(@"%@", [NSString stringWithFormat:@"%@", operadorReal2]);
- NSLog(@"%@", [NSString stringWithFormat:@"%@", [[operador3 titleLabel]text]]);
- NSLog(@"%@", [NSString stringWithFormat:@"%@", operadorReal3]);*/
- //NSLog(operadorReal3);
- if([operadorReal1 isEqualToString:operador1.titleLabel.text] &&
- [operadorReal2 isEqualToString:operador2.titleLabel.text] &&
- [operadorReal3 isEqualToString:operador3.titleLabel.text]){
- //ocultar imagen correcto
- [imagenCorrecto setHidden:false];
- }
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- //return (interfaceOrientation == UIInterfaceOrientationPortrait);
- return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
- }
- @end
Add Comment
Please, Sign In to add comment