View Full Version : Help with random numbers
Drenguin
02-08-2009, 09:36 AM
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.
#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:(UIIn terfaceOrientation)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.
Schenk Studios
02-08-2009, 10:40 AM
NSString *attack = arc4random() %20 + 1;
I keep getting this warning.
warning: initialization makes pointer from integer without a cast
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,
int myRandomNum = arc4random() %20 +1;
NSString* attack = [NSString stringWithFormat:@"%i", myRandomNum];
We have over an hour of free tutorials (http://www.schenkstudios.com/Downloads.html) on our website for budding developers. Check them out!
Drenguin
02-08-2009, 11:38 AM
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!
Drenguin
02-11-2009, 09:13 PM
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.
int myRandomNum = arc4random() %20 +1;
NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
Schenk Studios
02-11-2009, 09:23 PM
To convert from a string to a int, double, or float you'd do the following
NSString* yourString = @"50"; //Whatever it happens to be
int myInt = [yourString intValue];
double myDouble = [yourString doubleValue];
float myFloat = [yourString floatValue];
Drenguin
02-14-2009, 05:49 PM
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
@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;
}
Schenk Studios
02-15-2009, 03:23 PM
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
@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;
}
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
int health = 100;
and stick it right below
@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, how do I reset it to a different number simply take an int value of some kind (presumably one that you've calculated based off damage) and assign it to health.
aka
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;
int health -= damageDealt;
That is a no no!
One final note:
int myRandomNum = arc4random() %20 +1;
NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
int attackDamage = [attack intValue];
Can be abbreviated to:
int attackDamage = arc4random()%20+1;
Drenguin
03-01-2009, 03:05 PM
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.
#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;
}
Schenk Studios
03-01-2009, 03:32 PM
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.
#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;
}
Okay, so what we need to do is something like,
#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];
}
Drenguin
03-01-2009, 05:12 PM
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.
@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];
}
InsertWittyName
03-01-2009, 05:34 PM
You need to wrap if statements in parenthesis, and there is no semi-colon on the end.
if(health <= 0)
Drenguin
03-02-2009, 08:26 PM
Thank you, if I wanted an alert to pop up when that if statement is triggered how would I do that?
Schenk Studios
03-02-2009, 10:06 PM
Put some parentheses and get rid of the semi colon in the if statement.
Old ==
@implementation AttackViewController
health -= attackDamage;
if health <= 0;
{
NSString *youWin = [[NSString alloc] initWithFormat:
"You Win"];
statusText.text = youWin;
}
New ==
@implementation AttackViewController
health -= attackDamage;
if (health <= 0)
{
NSString *youWin = [[NSString alloc] initWithFormat:
"You Win"];
statusText.text = youWin;
}
Schenk Studios
03-02-2009, 11:39 PM
Thank you, if I wanted an alert to pop up when that if statement is triggered how would I do that?
Tutorial 16 (http://www.schenkstudios.com/Downloads.html#Tutorial%2016) should answer that!
Drenguin
03-07-2009, 08:21 AM
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.
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.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
health = 100;
}
else
NSLog("Shop");
}
Schenk Studios
03-07-2009, 09:51 AM
Did you declare the ViewController as a UIAlertView Delegate?
It'd look something like this in your .h file
@interface yourViewController : UIViewController<UIAlertViewDelegate>{
Drenguin
03-17-2009, 11:02 AM
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.
#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.
#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:(UIIn terfaceOrientation)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!
Hodapp
03-17-2009, 11:33 AM
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.
Drenguin
03-20-2009, 08:57 PM
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.
Schenk Studios
03-21-2009, 02:56 PM
It'd look something like
myLabel.text = myTextField.text;
Drenguin
03-22-2009, 07:56 AM
Okay I tried that and nothing happened here is the action that is called when the user hits a button.
- (IBAction)userIsDone:(id)sender
{
oneField.text=oneLabel.text;
}
Drenguin
03-22-2009, 08:36 AM
Sorry forget the last post I made I messed up, its working now.
Drenguin
03-22-2009, 09:03 AM
I have got my program changing views thanks to your tutorial! But, I've run into another problem. I am trying to take information a user typed into a text field and display it in a label in the second view. How do I move this information over to the second view?
Schenk Studios
03-22-2009, 11:51 AM
I have got my program changing views thanks to your tutorial! But, I've run into another problem. I am trying to take information a user typed into a text field and display it in a label in the second view. How do I move this information over to the second view?
If the second view is a child view of the first one. i.e. the first one calls the second one into being with
[self presentModalViewController:secondView animated:YES];
Then you could write a simple method in the second View.
- (void)switchText:text{
label.text = text;
}
You'd call that method in the first view controller
[secondViewController switchText:textField.text];
OR
You may find it easier to make an IBOutlet for that label in your first view controller, and hook it up accordingly. You always need to be careful with this though as sometimes it can lead to crash city if you go overboard with it.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.