Title4
#import "Round.h"
加入這段程式碼
做出比對
NSInteger incorrect= 4-answerSet.count;
NSInteger correct = 0;
for(NSNumber * number in answerSet){
NSUInteger indexOfUserGuess = [self.guessArray indexOfObject:number];
NSInteger indexofAnswer = [self.answer indexOfObject:number];
if (indexOfUserGuess == indexofAnswer){
correct += 1;
}
}
Round * round =[[Round alloc] init];
round.userGuess = self.playTextField.text;
round.result=[NSString stringWithFormat:@"%liA@%liB",correct,4-incorrect-correct];
[self.guessHistory addObject:round];
#import "GuessViewController.h"
#import "Round.h"
@interface GuessViewController ()
@property (weak, nonatomic) IBOutlet UITextField *playTextField;
@property (strong, nonatomic)NSArray*guessArray;
@property (strong, nonatomic)NSArray*answer;
@property (strong, nonatomic)NSMutableArray*guessHistory;
@end
@implementation GuessViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self randomNumber];
// Do any additional setup after loading the view.
}
- (IBAction)getResult:(UIButton *)sender {
// [self randomNumber];
[self getUserGuess];
NSMutableSet * answerSet = [NSMutableSet setWithArray:self.answer];
NSMutableSet * userSet = [NSMutableSet setWithArray:self.guessArray];
[answerSet intersectSet:userSet];
NSInteger incorrect= 4-answerSet.count;
NSInteger correct = 0;
for(NSNumber * number in answerSet){
NSUInteger indexOfUserGuess = [self.guessArray indexOfObject:number];
NSInteger indexofAnswer = [self.answer indexOfObject:number];
if (indexOfUserGuess == indexofAnswer){
correct += 1;
}
}
Round * round =[[Round alloc] init];
round.userGuess = self.playTextField.text;
round.result=[NSString stringWithFormat:@"%liA@%liB",correct,4-incorrect-correct];
[self.guessHistory addObject:round];
NSLog(@"%liA%liB",correct, 4-incorrect-correct);
}
-(void)randomNumber{
//產生隨機數字
NSArray *array=@[@0,@1,@2,@3,@4,@5,@6,@7,@8,@9];
NSMutableSet * picked = [[NSMutableSet alloc] init];
while(picked.count !=4){
NSInteger index = arc4random() %10;
NSNumber*unit = array[index];
[picked addObject:unit];
}
self.answer = [picked allObjects];
NSLog(@"%@",self.answer);
}
//取得文字框的字
-(void)getUserGuess{
NSString * userTyped=self.playTextField.text;
NSInteger guessNumber= userTyped.integerValue;
NSNumber *thousand=[NSNumber numberWithInteger:(guessNumber/1000)] ;
guessNumber %=1000;
NSNumber *hundred =[NSNumber numberWithInteger:(guessNumber/100)] ;
guessNumber %=100;
NSNumber *ten = [NSNumber numberWithInteger:(guessNumber/10)] ;
guessNumber %=10;
NSNumber *unit =[NSNumber numberWithInteger:(guessNumber%10)] ;
self.guessArray = @[thousand,hundred,ten,unit];
NSLog(@"%@",self.guessArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Comments
Post a Comment