GKTurnBasedMatch: Trouble Getting/Setting the matchOutcome - ios

I am working on a Game Center based board game. All seems to be running grew with a few bugs and quirks here and there. The most annoying of those quirks is that when viewing a match that has ended, it always seems to think that the viewing player has lost.
When I end a match with a game winning move, I run this:
// Act if the game is over because of that move
if ([board playerHasWon:activePlayer]) {
board.canMove = NO;
match = [CNFTurnBasedMatchHelper sharedInstance].currentMatch;
NSUInteger currentIndex = [match.participants indexOfObject:match.currentParticipant];
NSUInteger nextIndex = (currentIndex == 0 ? 1 : 0);
GKTurnBasedParticipant *nextParticipant = [match.participants objectAtIndex:nextIndex];
match.currentParticipant.matchOutcome = GKTurnBasedMatchOutcomeWon;
nextParticipant.matchOutcome = GKTurnBasedMatchOutcomeLost;
[match endMatchInTurnWithMatchData:[[board stringValue] dataUsingEncoding:NSUTF8StringEncoding] completionHandler:nil];
}
However, I suspect the real problem lies in my retrieval of that matchOutcome.
When loading a match I run this if the it's not the current players turn:
if (match.status == GKTurnBasedMatchStatusEnded) {
// These lines get the board to show the winning line
[board playerHasWon:1];
[board playerHasWon:2];
board.canMove = NO;
if (match.currentParticipant.matchOutcome == GKTurnBasedMatchOutcomeWon) statusLabel.text = #"You won!";
else statusLabel.text = #"You lost!";
}

Well, I solved my own problem just by really laying it out.
if ([((GKTurnBasedParticipant*)[match.participants objectAtIndex:0]).playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
if ([board playerHasWon:1]) statusLabel.text = #"You won!";
else statusLabel.text = #"You lost!";
}
else {
if ([board playerHasWon:2]) statusLabel.text = #"You won!";
else statusLabel.text = #"You lost!";
}

Related

How to get native keyboard autosuggestion string value

I made a page which contained a UITextFiled and a view that presented search result and I want to change the triggering conditions of the search. The original triggering conditions was changing the input text of textField and I want to make some change. I hope the search is triggered only when user touch the keyboard autocorrection. Could someone give me some advance, thank you!
Try implementing UITextChecker like this. The last two lines are the print statement for two types. One is for guesses words when misspelled occurred and the other is for dictionary words. The arrPredectiveText array contains words sequentially like dictionary. You can get suggestions from this.
Call the suggestionsForCustomKeyboard method with some string that you are typing.
-(void)suggestionsForCustomKeyboard:(NSString *)word{
if ([currentString length] >= 1) {
UITextChecker *textChecker = [[UITextChecker alloc] init];
NSRange misspelledRange = [textChecker
rangeOfMisspelledWordInString:currentString
range:NSMakeRange(0, [currentString length])
startingAt:0
wrap:NO
language:#"en_US"];
if (misspelledRange.location != NSNotFound) {
guesses = [textChecker guessesForWordRange:misspelledRange
inString:currentString
language:#"en_US"];
NSLog(#"First guess: %#", [guesses firstObject]);
} else {
NSLog(#"Textchecker Not found");
autocorrectedText = #"";
}
if (arrPredectiveText.count >= 2){
suggestionOne = [arrPredectiveText objectAtIndex:0];
suggestionTwo = [arrPredectiveText objectAtIndex:1];
}
else if (arrPredectiveText.count == 1 && guesses.count >= 1) {
suggestionOne = [arrPredectiveText firstObject];
suggestionTwo = [guesses firstObject];
}else if (arrPredectiveText.count == 0 && guesses.count > 0){
suggestionOne = [guesses firstObject];
if (guesses.count > 1) {
suggestionTwo = [guesses objectAtIndex:1];
}
}
NSLog(#"Textchecker all guess: %#", guesses);
NSLog(#"Prediction: %#",arrPredectiveText);
}

Score label node disappears after first round

I don't know why, but score label node dissapears after first round.I have two scores, one for human ,other for computer :
-(void)scoreCount{
if(scoreLabel == nil){
NSString* scoretxt =[NSString stringWithFormat:#"0"];
[scoreLabel setText:scoretxt];
scoreLabel = [SKLabelNode labelNodeWithFontNamed:#"ROTORcapExtendedBold"];
scoreLabel.fontSize = 65.f;
scoreLabel.fontColor = [UIColor grayColor];
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame)/2,CGRectGetMaxY(self.frame)-70);
scoreLabel.zPosition = -1;
[self addChild:scoreLabel];
}
scoreLabel.text = [NSString stringWithFormat:#"%ld",(long)score];
if(scoreLabelCom == nil){
NSString* scoretxtcom =[NSString stringWithFormat:#"0"];
[scoreLabelCom setText:scoretxtcom];
scoreLabelCom = [SKLabelNode labelNodeWithFontNamed:#"ROTORcapExtendedBold"];
scoreLabelCom.fontSize = 65.f;
scoreLabelCom.fontColor = [UIColor grayColor];
scoreLabelCom.position = CGPointMake(CGRectGetMidX(self.frame)+(CGRectGetMidX(self.frame)/2),CGRectGetMaxY(self.frame)-70);
scoreLabelCom.zPosition = -1;
[self addChild:scoreLabelCom];
}
scoreLabelCom.text = [NSString stringWithFormat:#"%ld",(long)scoreCom];
}
this method is called every time somebody is getting a point, and I put in
-(void)update:(CFTimeInterval)currentTime {
[self scoreCount];
}
because, without it scoreCount wont show 0 points, but only show up after first point, but, when new round starts, ScoreCout wont show up at all.
how can I correct it? And why it is happening?
This (long)score and (long)scoreCom
You can add value to it by now.
score = score + 1; //Before add to nsstring
scoreLabel.text = [NSString stringWithFormat:#"%ld",(long)score];
And
scoreCom = scoreCom + 1;//Before add to nsstring
scoreLabelCom.text = [NSString stringWithFormat:#"%ld",(long)scoreCom];
Well, I don't know how good it is but I add scoreLabel = nil and scoreLabelCom = nil at didBeginContactwhen game ends, and it works now.

whats the proper way to add up coins and scores? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Whats the proper way, I've been using if statements but the way everything is set up is wrong I believe because the scores and coins are not being added up as a whole number they are being replaced by the if statements...
Example:
- (IBAction)btncheck:(id)sender {
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:#" " withString:#""];
// to block user from getting incorrect answer with no text in text field
if([answer isEqualToString:#""]){
}
else
if ([answer isEqualToString:#"q"]) {
// String is correct, resign keyboard
_keyboard.hidden = YES;
_textXclear.hidden = YES;
//Perfect button
[_closeone setHidden:NO];
[_wrongone setHidden:YES];
score = MAX (score +100, 0);
[scoreLabel setText:[NSString stringWithFormat:#"score: %d", score]];
coins = coins +5;
if (score == 100) {coins = 8;}
if (score == 0) {coins = 0;}
if (score == 4) {coins = 4;}
if (score == 3) {coins = 3;}
if (score == 2) {coins = 2;}
if (score == 1) {coins = 1;}
[coinsLabel setText:[NSString stringWithFormat:#"%d", coins]];
}
else {
// Was not correct. Notify user, or just don't do anything
[_wrongone setHidden:NO];
score = MIN(score -5, 0);
[scoreLabel setText:[NSString stringWithFormat:#"score: %d", score]];
}
if (score < 100) {
closeonechange.text = #"Correct!";
_imageCorP.image = [UIImage imageNamed:#"boxg_25.png"];
} else {
closeonechange.text = #"Perfect!";
_imageCorP.image = [UIImage imageNamed:#"telioo.png"];
}
//correct button - perfect button
}
//Perfect & Correct button sets it hidden
- (IBAction)closeone:(id)sender {
[_closeone setHidden:YES];
}
Oh and I'd like the user to start with at least 50 coins before the game starts, I'm having trouble figuring out how to do that as well..
By setting
coins = 8;
etc. in your if statements you replace the value of coins with the new value (here: 8) - if you want to add it up you have to write
coins = coins + 8;
, like you also did for counting up the coins in the line
coins = coins + 5;
Some more tips:
You should use if (...) { } else if (...) { } statements instead of only using if's - by that once an if statement is true, all others won't be executed anymore.
If you want to get rid of leading and trailing spaces of the user input you should use
NSString* inputText = [_textBox.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceANdNewlineCharacterSet];

When i click on map its get crashed at the initial time in iOS5?

for (int i = 0; i< [delarsInfoArray count] ; i++)
{
NSString *lattitudeValue;
NSString *longitudeValue;
if ([[delarsInfoArray objectAtIndex:i]count]>1) {
lattitudeValue = [[[delarsInfoArray objectAtIndex:i]valueForKey:#"LATITUDE"]objectAtIndex:1];
longitudeValue = [[[delarsInfoArray objectAtIndex:i]valueForKey:#"LONGITUDE"]objectAtIndex:0];
}
else
{
lattitudeValue = #"";
longitudeValue = #"";
}
CLLocationCoordinate2D pinLocation;
if(([lattitudeValue floatValue] != 0) && ([longitudeValue floatValue] != 0) ) {
mapRegion.center.latitude = [lattitudeValue floatValue];
mapRegion.center.longitude = [longitudeValue floatValue];
if(pinLocation.latitude !=0 && pinLocation.longitude !=0) {
myAnnotation1 = [[MyAnnotation alloc] init];
if ([[delarsInfoArray objectAtIndex:i] count] == 0) {
myAnnotation1.title = #"";
myAnnotation1.subtitle = #"";
}
else
{
// NSLog(#"====== delears array is===%#",delarsInfoArray);
NSLog(#"===== delears array count is %d",[delarsInfoArray count]);
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:#"Address"]objectAtIndex:2] !=nil)
{
myAnnotation1.title = [[[delarsInfoArray objectAtIndex:i]valueForKey:#"Address"]objectAtIndex:2];
}
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:#"City"]objectAtIndex:3]!= nil) {
myAnnotation1.subtitle = [[[delarsInfoArray objectAtIndex:i]valueForKey:#"City"]objectAtIndex:3];
}
NSLog(#"%#",[[[delarsInfoArray objectAtIndex:i]valueForKey:#"City"]objectAtIndex:3]);
}
[dealerMapView setRegion:mapRegion animated:YES];
[dealerMapView addAnnotation:myAnnotation1];
myAnnotation1.coordinate = mapRegion.center;
[myAnnotation1 release];
}
}
}
The above code is written in the viewWillAppear.After loading the map in to the view,when i clicked on the map.app gets crashed.How can solve this crash?
There are a lot of issues here, but the one that leaps out to the top of the list are the lines that read:
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:#"Address"]objectAtIndex:2] !=nil)
...
and
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:#"City"]objectAtIndex:3]!= nil) {
...
The problem is that objectAtIndex of a valueForKey of an array will never be nil. You can't store a nil in an array, so what valueForKey does, if it doesn't find a value, is it uses a NSNull object, [NSNull null]. That designates that there was no value found, but uses NSNull (which can be added to the array) instead of nil (which can't).
The problem is likely that there is some subsequent code (for example, the code that tries to figure out the size of the callout bubble) which tries to get the length of the string, but since you stored a NSNull, it's trying to call the length method and it's failing.
You could fix this a number of ways, such as:
if ([[[delarsInfoArray objectAtIndex:i]valueForKey:#"Address"]objectAtIndex:2] != [NSNull null])
...

Programmatically and automatically adding an integer into a UILabel? Possible?

I've set up a simple if statement that says if the input length is equal to 3 make the next character a "-".
It works well, but I'd like the "-" to automatically be put there after I press my button for the 3rd time. So I press, "1", "2", then when I press the "3" it automatically puts a "-" directly afterwards. Currently the "-" only gets placed when I hit the button for the 4th time?
-(IBAction)buttonDigitPressed:(id)sender {
NSString *val = phoneNumberLabel.text;
int length = [val length];
} else {
NSString *tagValue = [NSString stringWithFormat:#"%d", [sender tag]];
phoneNumberLabel.text = [val stringByAppendingString: tagValue];
if (length == 3) {
phoneNumberLabel.text = [val stringByAppendingString:#"-"];
}
if (length == 7) {
phoneNumberLabel.text = [val stringByAppendingString:#"-"];
}
}
}
Any help would be appreciated, greatly! Thanks!
-(IBAction)buttonDigitPressed:(id)sender {
NSString *val = phoneNumberLabel.text;
NSString *newValue = #"";
NSString *dash = #"";
int length = [val length];
if ( ((length == 3) || (length == 7)) ) {
dash = #"-";
}
newValue = [NSString stringWithFormat:#"%#%#%d", val, dash, [sender tag]];
phoneNumberLabel.text = newValue;
}
Add the following code in buttonClicked method
if ([myLabel.text length] == 3)
{
myLabel.text = [val stringByAppendingString:#"-"];
}
Just add that code in the same method after you update the label with the integer.
Try this method,hope it will help you.
Keep UILabel object blank in xib.
- (IBAction)pressAction:(UIButton*)sender
{
if (lbl.text.length<=2) {
lbl.text=[lbl.text stringByAppendingFormat:#"%i",sender.tag];
}
if (lbl.text.length>=3){
lbl.text=[lbl.text stringByAppendingString:#"-"];
}
}
You can send the button the events. like [button sendActionsForControlEvents:UIControlEventTouchUpInside]
Or even perform selector after delay may do the trick for you.
both the cases you need not press a button
For the addition of "-" just check how many characters have been entered and when the third character has been entered add the "-".

Resources