I have a tap counter and this is the "Who Wins" code. The app is crashing whenever Flipped (int timer) is equal to 30 and it is deciding who had the most taps. It always says "Player 2 Wins" and freezes. Please help. Number is the number value of one tapper, Number1 is the value of the second tapper. How do I fix this?
- (void)countup
{
if (Fliped == 30)
{
//error message
if (Number < Number1)
{
myAlertView = [[UIAlertView alloc] initWithTitle:#"Stop!" message:#"Player 2 Wins!" delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
timer = [NSTimer scheduledTimerWithTimeInterval:0.0
target:self selector:#selector(countup)userInfo:nil repeats:YES];
[myAlertView show];
}
if (Number > Number1)
{
myAlertView = [[UIAlertView alloc] initWithTitle:#"Stop!" message:#"Player 1 Wins!" delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
timer = [NSTimer scheduledTimerWithTimeInterval:0.0
target:self selector:#selector(countup)userInfo:nil repeats:YES];
[myAlertView show];
}
else
{
MainInt += 1;
seconds.text = [NSString stringWithFormat:#"%d", MainInt];
Fliped += 1;
secondsFlip.text = [NSString stringWithFormat:#"%d", Fliped];
}
}
Thanks in advance.
what could be the cause of this crash???
You're calling your own method countup recursively an infinite number of times when the variable Fliped is 30. That's why you're crashing. Don't do that.
Related
I am getting an error message on my Xcode - Objective C.
Arithmetic on pointer to interface "UILabel", which is not constant size for this architecture and platform.
if([self checkforwin]){
NSString*winner = nil;
if (playertoken==1)
winner =#"Player 2 Wins";
_result1 = _result1+1
else if (playertoken==2)
_result2 = _result2 +1
winner =#"Player 1 Wins";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Result"
message: winner
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[self resetboard];
[_audioPlayer play];
[alert show];
Any Ideas how to get rid of them??
The error shows on both the result1 and result2. Trying to add a score to my game.
Use parenthesis while using condition. Also don't set string to nil, set it with empty string #""
if([self checkforwin]){
NSString *winner = #"";
if (playertoken==1) {
winner =#"Player 2 Wins";
_result1 = _result1 + 1;
}
else if (playertoken==2) {
winner =#"Player 1 Wins";
_result2 = _result2 + 1;
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Result"
message: winner
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
[self resetboard];
[_audioPlayer play];
[alert show];
}
You are trying to add 1 to the UILabel. You need an integer variable to do the increment. Either declare the _result 1 and 2 as int type variable or declare a int variable and increment it, and pass it to UILabel .text property
_result1 = _result1+1
here _result1 must be a integer variable.
You need to something like:
Declare the result1 as int,
#property(assign) int result1;
and this line should be fine
_result1=_result1+1;
I'm trying to create a function in order to save some code, however I have no idea how I should do it.
if (count > 100 && count < 120)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert show];
}
else if(count > 120)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert2 show];
}
else
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"Not very good, you scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert3 show];
}
As you can see the code is very repetitive and I would like to create a function where I can change the value of the "alert" variable and the message string. So when I call the function I only enter the functions name and the value the alert variable should have, in this case; 1, 2 and 3 and the message text depending on what condition we are looking at.
So is there some way of doing this? There has to be, because repetitive code like this is never good looking! I would appreciate if someone could help me out, I've tried looking at some references but none of them made me get any closer to solving the problem.
Simply look at the duplicated functionality and examine the values that are different. In this case it's just the message:
NSString *message = nil;
if (count > 100 && count < 120)
{
message = [NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1];
}
else if(count >= 120)
{
message = [NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1];
}
else
{
message = [NSString stringWithFormat:#"Not very good, you scored %i points", count - 1];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:message
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert show];
(Edit: You missed the value 120 which would have slipped through the cracks).
I am create a new function that creates a new alert with given message :
-(void)showAlertWithMsg:(NSString *)msg tag:(NSInteger)tag
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:msg
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
alert.tag = tag;
[alert show];
}
To call this function write the below code:
if (count > 100 && count < 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1] tag:1];
}
else if(count > 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1] tag:2];
}
else
{
[self showAlertWithMsg:[NSString stringWithFormat:#"Not very good, you scored %i points", count - 1] tag:3];
}
One more thing I am also pass the tag.So if you want to perform different operation for different alert.
You can set "title" property exactly for existing UIAlertView instance:
UIAlertView * alertView = [[UIAlertView allow] init...];
alertView.title = #"Some message here.";
if (stash != 0) {
for (i=1; i<=6; i++) {
a[1][i]=a[1][i]/stash;
}
}
else
{
NSLog (#"Matrix is Not Invertible");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Matrix is not invertible!" delegate:nil cancelButtonTitle:#"Review Input" otherButtonTitles:nil, nil];
[alert show];
}
I Want to stop the program if the variable "stash" is zero, but i can't use break since it's not in a loop, i wanted to use return but it says that void should not return any value... what should i do to get this working? thanks for all your help....
I don't know if you need to exit the method only but you could use:
return;
or redefine the method to return an integer -(int)myMethod; and then return 0;
//Add return statement, returning nothing
return;
- (void)yourMethod {
//your code
//your declerations
if (stash != 0) {
for (i=1; i<=6; i++) {
a[1][i]=a[1][i]/stash;
}
}
else
{
NSLog (#"Matrix is Not Invertible");
UI *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Matrix is not invertible!" delegate:nil cancelButtonTitle:#"Review Input" otherButtonTitles:nil, nil];
[alert show];
//Add return statement here, returning nothing
return;
}
//other code in your method
//your code
}
This is not all the code. I have made sure that I have declared everything correctly however the label is not changing as 'seconds' is decreasing.
I'm not sure why as in 'subtractTime' I have made timerLabel.text equal to the string with format using seconds which "should" and is counting down as I use an alert to reset the game and so even though the label isn't changing, I know it is counting down otherwise the alert wouldn't be triggered from 'seconds' equalling 0.
- (void)setupGame;
{
seconds = 30;
count = 0;
timerLabel.text = [NSString stringWithFormat: #"Time: %i", seconds];
scoreLabel.text = [NSString stringWithFormat: #"Score: %i", count];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(subtractTime)
userInfo:nil
repeats:YES];
}
- (void)subtractTime;
{
seconds--;
timerLabel.text = [NSString stringWithFormat:#"Time: %i", seconds];
if (seconds == 0)
{
[timer invalidate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Time is up !"
message: [NSString stringWithFormat: #"You Scored %i points", count]
delegate:self
cancelButtonTitle: #"Play Again"
otherButtonTitles:nil];
[alert show];
}
}
#end
Make sure in Storyboard/Interface builder that your label is wired properly to the viewXontroller's .h file. A good way to test this is to "prime" your label on the XIB with something other than "Time:" but instead with something nonsensical like "foo". That way you'll be able to accurately tell if the label is changing at all versus troubleshooting whether or not your Int value isn't showing properly.
I am new to programming. I am having trouble finding out what all is wrong with this. It is an alert view that i am trying to randomize the text displayed in the message.
-(void)alert:(id)sender{
int randomNumber;
randomNumber = (randomNumber() %3 + 1);
NSLog(#"%i", randomNumber);
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello" message:[NSString stringWithFormat:#"%#",YouWin] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
button.hidden = YES;
Try this one:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
You needed text-value stored in YouWin, but you passed the YouWin object itself.
*Note: you can use arc4random() for generating random numbers.
There are many good suggestions; I agree with AKB8085.
Replacing the randomNumber() with arc4random() will help at compile time.
But you might want to re-think implementing a random number generator. The reason is in fairness to your user. I pose the question, “Is it fair to assume you want your user to guess a number with this large of a number range?”
Did you Know?
Using the arc4random(3), rand(3), or random(3) you are using a C function.
You are asking the user to guess a number with the following ranges:
arc4random(3) = 0 to 4294967296
rand(3) and random(3) that top out at a RAND_MAX of 0x7fffffff (214748647)
To help in answering your question, answer the following requirement questions:
Is there a min/max range restraint?
What type of compelling delays will happen
by using arc4random(3), rand(3), or random(3)?
Is using NSArray like in the Fisher–Yates_shuffle a better answer?
SUGGESTION:
Read an article on random numbers and NSArray.
NOTE:
Random numbers tend to task the compiler and your user experience will be hindered.
As Anoop noted you are using stringWithFormat but you're not providing a format string at all.
You should do
[NSString stringWithFormat:#"%#", #"You Win"];
but that's extremely redundant, although correct, and it's totally equivalent to just using #"You Win".
Also an advice for the general approach on the problem. Instead of having a big if-else statement, it's better to store all your string into a data structure and then randomly access to it.
In code this would translate to
NSArray * outcomes = #[ #"You Win", #"You lose", #"Tie" ];
int randomIndex = arc4random_uniform(outcomes.count);
NSString * randomOutcome = outcomes[randomIndex];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:randomOutcome
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;
Note the usage of arc4random_uniform() that gives you back a random number between 0 and the argument provided, excluded.
Replace
randomNumber = (randomNumber() %3 + 1);
with
randomNumber = arc4random() %3 + 1;
Also use this...
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;