Help with random numbers

Discussion in 'Public Game Developers Forum' started by Drenguin, Feb 8, 2009.

  1. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Hello I am trying to make a random number generator that will display the random number in a text box but xcode is not cooperating with me so I you could help me that would be great, here is my code.

    Code:
    #import "UIKit/UIKit.h"
    #import "AttackViewController.h"
    
    @implementation AttackViewController
    @synthesize statusText;
    
    -(IBAction)buttonPressed:(id)sender;
    {
    	
    	NSString *attack = arc4random() %20 + 1;
    	NSString *newText = [[NSString alloc] initWithFormat:
    		@"Your attack is %@.", attack];
    	statusText.text = newText;
    	[newText release];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }
    
    
    - (void)dealloc {
    	[statusText release];
        [super dealloc];
    }
    
    @end
    This is from the AttackViewController.m file

    I keep getting this warning.
    warning: initialization makes pointer from integer without a cast


    Thanks in advance.
     
  2. Schenk Studios

    Schenk Studios Well-Known Member

    That particular warning is because of that line of code. You are generating a random number with arc4random, but then you are assigning that number directly to a string. Try instead something like,

    Code:
    int myRandomNum = arc4random() %20 +1;
    NSString* attack = [NSString stringWithFormat:@"%i", myRandomNum];
    We have over an hour of free tutorials on our website for budding developers. Check them out!
     
  3. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Thank you so much I will try this out.

    Also, I have been on your website for the past couple weeks and it is so helpful so thank you for that too!
     
  4. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    How to convert strings to numbers

    I am trying to do math with strings but I realize that I have to make the string a number, how would I do that?

    Here is one of the strings I am trying to convert.

    Code:
    int myRandomNum = arc4random() %20 +1;
    	NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
     
  5. Schenk Studios

    Schenk Studios Well-Known Member

    To convert from a string to a int, double, or float you'd do the following

    Code:
    NSString* yourString = @"50"; //Whatever it happens to be
    
    int myInt = [yourString intValue];
    double myDouble = [yourString doubleValue];
    float myFloat = [yourString floatValue];
    
     
  6. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Thank you so much for what you have helped me with so far but I have another question. I am trying to return health but it is already set to 100, how do I reset it to a different number.

    Here is my code

    Code:
    @implementation AttackViewController
    @synthesize statusText;
    
    
    
    -(IBAction)buttonPressed:(id)sender;
    {
    	int health = 100;
    	int myRandomNum = arc4random() %20 +1;
    	NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
    	int attackDamage =  [attack intValue];
    	int damageDealt = health - attackDamage;
    	NSString *damage = [NSString stringWithFormat:@"%i", damageDealt];
    
    	NSString *newText = [[NSString alloc] initWithFormat:
    		@"%@.", damage];
    	
    	statusText.text = newText;
    	[newText release];
    	return health;
    }
    
     
  7. Schenk Studios

    Schenk Studios Well-Known Member

    Without knowing what you're wanting to do with health it is somewhat difficult to give you the best most succinct answer. Judging from what I can see of your code, it looks like you have a UILabel statusText that shows the changes each time a button is pressed to represent the latest amount of damage dealt. I'm assuming that since you don't have health set up with a @property & @synthesize that you intend to keep that information in the background, a.k.a. you won't be presenting it to the user.

    Again, assuming, that you wish to keep track of health over the course of the game, you'll need to declare it outside of that method. Otherwise each time you run through that method it'll get reset back to 100. Thankfully, this is incredibly easy to do. Simply take

    Code:
    int health = 100;
    and stick it right below

    Code:
    @synthesize statusText;
    We've just changed health from being a local variable (only visible within that one method, to being an instance variable (visible within the entire class, so now other methods inside AttackViewController.m can use it).

    Now each time we run through that method, health doesn't get reset back to 100. To get to your question,
    simply take an int value of some kind (presumably one that you've calculated based off damage) and assign it to health.

    aka

    Code:
    int damageDealt = 50; //Or whatever # you desire, including randoms
    health -= damageDealt;
    "-=" is legit coding shorthand for, health = health - damageDealt; Also note that we didn't redeclare health;

    Code:
    int health -= damageDealt;
    That is a no no!

    One final note:

    Code:
    int myRandomNum = arc4random() %20 +1;
    NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
    int attackDamage =  [attack intValue];
    Can be abbreviated to:

    Code:
    int attackDamage = arc4random()%20+1;
     
  8. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Sorry that I didn't specify what I wanted, I want health to begin at 100 and decrease by a random number every time you click the button. However it keeps resetting health back to 100, am I returning it wrong? I keep getting the error "'return' with a value, in function returning void."

    Here is my code again, Thank you in advance.
    Code:
    #import "UIKit/UIKit.h"
    #import "AttackViewController.h"
    
    @implementation AttackViewController
    @synthesize statusText;
    int health = 100;
    
    
    -(IBAction)buttonPressed:(id)sender;
    {
    
    	int attackDamage = arc4random() %20 +1;
    
    	int damageDealt = health - attackDamage;
    	NSString *damage = [NSString stringWithFormat:@"%i", damageDealt];
    
    	NSString *newText = [[NSString alloc] initWithFormat:
    		@"%@.", damage];
    	statusText.text = newText;
    	[newText release];
    	return health;
    }
    
     
  9. Schenk Studios

    Schenk Studios Well-Known Member

    Okay, so what we need to do is something like,


    Code:
    #import "UIKit/UIKit.h"
    #import "AttackViewController.h"
    
    @implementation AttackViewController
    @synthesize statusText;
    int health = 100;
    
    
    -(IBAction)buttonPressed:(id)sender;
    {
    	int attackDamage = arc4random() %20 +1;
    
    	health -= attackDamage;
    	NSString *damage = [NSString stringWithFormat:@"%i",      attackDamage]; 
    //This puts the random number into a string
    
    	NSString *newText = [[NSString alloc] initWithFormat:
    		@"%@.", damage];
    	statusText.text = newText;
    	[newText release];
    
    NSString *newHealth = [[NSString alloc] initWithFormat:
    		@"%i.", health];
    	//If You want to do something with the health as a string, do it here
    	[newHealth release];
    }
    
     
  10. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Thank you so much you are amazing, but I have another problem. I got it to work and now I am trying to tell it to display "You Win" if health is less than or equal to 0 but I keep getting the error "syntax error before health" again here is my code. Thank You.

    Code:
    @implementation AttackViewController
    @synthesize statusText;
    int health = 100;
    
    
    -(IBAction)buttonPressed:(id)sender;
    {
    	int attackDamage = arc4random() %20 +1;
    	
    	health -= attackDamage;
    	if health <= 0;
    	{
    		NSString *youWin = [[NSString alloc] initWithFormat:
    							 "You Win"];
    		statusText.text = youWin;
    	}
    	NSString *damage = [NSString stringWithFormat:@"%i",      health]; 
    	//This puts the random number into a string
    	
    	NSString *newText = [[NSString alloc] initWithFormat:
    						 @"%@.", damage];
    	statusText.text = newText;
    	[newText release];
    	NSString *newHealth = [[NSString alloc] initWithFormat:
    						   @"%i.", health];
    	//If You want to do something with the health as a string, do it here
    	[newHealth release];
    }
    
     
  11. InsertWittyName

    InsertWittyName Well-Known Member

    Nov 26, 2008
    202
    0
    0
    You need to wrap if statements in parenthesis, and there is no semi-colon on the end.

    Code:
    if(health <= 0)
     
  12. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Thank you, if I wanted an alert to pop up when that if statement is triggered how would I do that?
     
  13. Schenk Studios

    Schenk Studios Well-Known Member

    Put some parentheses and get rid of the semi colon in the if statement.

    Old ==

    New ==

    Code:
    @implementation AttackViewController
    
    	health -= attackDamage;
    	if (health <= 0)
    	{
    		NSString *youWin = [[NSString alloc] initWithFormat:
    							 "You Win"];
    		statusText.text = youWin;
    	}
    
    
     
  14. Schenk Studios

    Schenk Studios Well-Known Member

    Tutorial 16 should answer that!
     
  15. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Okay I've looked at your tutorial and I tried to incorporate your code with mine but it didn't work.
    Here is where I want the alert to get triggered.
    Code:
    if (health <= 0){
    	NSString* message = @"You beat the slime";
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Won!"
    						message:message 
    						delegate:self
    						cancelButtonTitle:@"Fight slime"
    										  otherButtonTitles:@"Shop", nil];
    	[alert show];
    	[alert release];	
    	}
    Here is where I am getting two error messages: error: 'alertView' undeclared (first use in this function), and error: syntax error before ':' token

    These errors are both on the first line.
    Code:
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    		if(buttonIndex == 0){
    			health = 100;
    		}
    		else
    			NSLog("Shop");
    	}
     
  16. Schenk Studios

    Schenk Studios Well-Known Member

    Did you declare the ViewController as a UIAlertView Delegate?

    It'd look something like this in your .h file

    @interface yourViewController : UIViewController<UIAlertViewDelegate>{
     
  17. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    I decided to stop working on the alert for now but now I am trying to make a second label that displays text when I want it to. Here is my .h file.
    Code:
    #import <UIKit/UIKit.h>
    
    @interface AttackViewController : UIViewController {
    	IBOutlet	UILabel	*statusText;
    	IBOutlet	UILabel *goldText;
    }
    @property (retain, nonatomic) UILabel *statusText, *goldText;
    -(IBAction)buttonPressed:(id)sender;
    
    @end
    I have linked goldText with the label but whenever I open up the file it unexpectedly quits. Here is my .m file too.
    Code:
    #import "UIKit/UIKit.h"
    #import "AttackViewController.h"
    
    @implementation AttackViewController
    @synthesize statusText, goldText;
    
    int health = 100;
    int gold = 0;
    
    -(IBAction)buttonPressed:(id)sender;
    {
    	int attackDamage = arc4random() %20 +1;
    	
    	health -= attackDamage;
    	if (health <= 0){
    		health = 100;
    		gold += 5;
    		}
    	NSString *yourGold = [NSString stringWithFormat:@"%i",      gold]; 
    	NSString *goldTextDisplay = [[NSString alloc] initWithFormat:
    								  @"%@.", yourGold];
    	goldText.text = goldTextDisplay;
    	[goldTextDisplay release];
    	NSString *damage = [NSString stringWithFormat:@"%i",      health]; 
    	//This puts the random number into a string
    	
    	NSString *newText = [[NSString alloc] initWithFormat:
    						 @"%@.", damage];
    	statusText.text = newText;
    	[newText release];
    	NSString *newHealth = [[NSString alloc] initWithFormat:
    						   @"%i.", health];
    
    	//If You want to do something with the health as a string, do it here
    	[newHealth release];
    	
    	//- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    	//	if(buttonIndex == 0){
    	//		health = 100;
    	//	}
    	//	else
    	//		NSLog("Shop");
    	//}
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
        // Release anything that's not essential, such as cached data
    }
    
    
    - (void)dealloc {
    	[statusText release];
    	[goldText release];
        [super dealloc];
    }
    
    @end
    
    Thank you in advance!
     
  18. Eli

    Eli ᕕ┌◕ᗜ◕┐ᕗ
    Staff Member

    You should seed your RNG from random.org... Otherwise it's not really random. ;)

    I'm surprised there is only one app on the App Store currently that utilizes true random numbers.
     
  19. Drenguin

    Drenguin Well-Known Member

    Jan 10, 2009
    87
    0
    0
    Haha sorry probably not going to do that, anyway another question. How do you take text from a Text Field and display it in a label. I know this is a really silly question to ask but I keep coming up with numbers.
     
  20. Schenk Studios

    Schenk Studios Well-Known Member

    It'd look something like

    myLabel.text = myTextField.text;
     

Share This Page