Chắc mọi người bít bài này rồi, mình share code theo cách làm lấy mod 52 % 13 như hồi sáng có bạn đã gợi ý^^.
Cách làm này có 1 vấn đề lớn là ko thể xóa phần tử( lá bài) rút ra khỏi mảng(bộ bài) được bởi vì:
- Theo cách làm bình thường đầu tiên random 1 số từ 1 - 52, xong thì lần tiếp theo ta random 1 số từ 1 - 51, vì đã rút ra 1 lá, điều này đồng nghĩa với chỉ số index của các phần tử thay đổi.
- Theo cách nêu trên vì ta lấy 52 % 13, kết quả của phép tính này cho ra chỉ số index theo thứ tự từ 1-13 của bộ bài( chỉ tính từ 1->13 thôi 11 là bồi 12 là đầm 13 là già, và cứ 4 bộ từ 1->13 ta có đầy đủ bộ 52 lá). Nếu thực hiện tiếp bước remove lá bài ra khỏi bộ bài để ko bị trùng lặp thì sẽ làm thay đổi chỉ số index và như thế sẽ dẫn tới kết quả của phép tính 52 % 13 ko đúng với thực tế của bộ bài lúc này.
Cách giải quyết như bên dưới, mọi người góp ý nhá:
PHP Code:
//
// NhatNgheViewController.m
// Buoi7_bai2
//
// Created by Duy Lee on 9/15/12.
// Copyright (c) 2012 Duy Lee. All rights reserved.
//
#import "NhatNgheViewController.h"
@interface NhatNgheViewController ()
@end
@implementation NhatNgheViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrCardComp = [[NSArray alloc]initWithObjects:crdComp1,crdComp2,crdComp3, nil ];
arrCardYou = [[NSArray alloc]initWithObjects:crdYou1,crdYou2,crdYou3, nil ];
}
- (void)viewDidUnload
{
[crdComp1 release];
crdComp1 = nil;
[crdComp2 release];
crdComp2 = nil;
[crdComp3 release];
crdComp3 = nil;
[crdYou1 release];
crdYou1 = nil;
[crdYou2 release];
crdYou2 = nil;
[crdYou3 release];
crdYou3 = nil;
[lblComp release];
lblComp = nil;
[lblYou release];
lblYou = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)newGame:(id)sender {
NSMutableArray *arr = [[NSMutableArray alloc] init];
arrCard = [[NSMutableArray alloc]initWithObjects:
@"c1.gif",@"c2.gif",@"c3.gif",@"c4.gif",@"c5.gif",@"c6.gif",@"c7.gif",@"c8.gif",@"c9.gif",@"c10.gif",@"cj.gif",@"cq.gif",@"ck.gif",
@"d1.gif",@"d2.gif",@"d3.gif",@"d4.gif",@"d5.gif",@"d6.gif",@"d7.gif",@"d8.gif",@"d9.gif",@"d10.gif",@"dj.gif",@"dq.gif",@"dk.gif",
@"h1.gif",@"h2.gif",@"h3.gif",@"h4.gif",@"h5.gif",@"h6.gif",@"h7.gif",@"h8.gif",@"h9.gif",@"h10.gif",@"hj.gif",@"hq.gif",@"hk.gif",
@"s1.gif",@"s2.gif",@"s3.gif",@"s4.gif",@"s5.gif",@"s6.gif",@"s7.gif",@"s8.gif",@"s9.gif",@"s10.gif",@"sj.gif",@"sq.gif",@"sk.gif",nil ];
///////////////////////////////////////////////////
int scoreComp = 0;
int specComp = 0;
for (UIImageView *card in arrCardComp) {
int randIndex = arc4random() % arrCard.count;
while ([arr containsObject:[NSString stringWithFormat:@"%i",randIndex]]) {
randIndex = arc4random() % arrCard.count;
}
card.image = [UIImage imageNamed:[arrCard objectAtIndex:randIndex]];
[arr addObject:[NSString stringWithFormat:@"%i",randIndex]];
int score = (randIndex + 1) % 13;
if (score > 10) {
specComp++;
score = 10;
}
scoreComp += score;
}
///////////////////////////////////////////////////
int scoreYou = 0;
int specYou = 0;
for (UIImageView *card in arrCardYou) {
int randIndex = arc4random() % arrCard.count;
while ([arr containsObject:[NSString stringWithFormat:@"%i",randIndex]]) {
randIndex = arc4random() % arrCard.count;
}
card.image = [UIImage imageNamed:[arrCard objectAtIndex:randIndex]];
[arr addObject:[NSString stringWithFormat:@"%i",randIndex]];
int score = (randIndex + 1) % 13;
if (score > 10) {
specYou++;
score = 10;
}
scoreYou += score;
}
///////////////////////////////////////////////////
if (specComp == 3 && specYou == 3 ) {
lblComp.text = @"Draw";
lblYou.text = @"Draw";
return;
}
else if (specComp == 3){
lblComp.text = @"Win";
lblYou.text = @"Lose";
return;
}
else if (specYou == 3){
lblComp.text = @"Lose";
lblYou.text = @"Win";
return;
}
scoreComp = scoreComp % 10;
scoreYou = scoreYou % 10 ;
if (scoreComp > scoreYou) {
lblComp.text = @"Win";
lblYou.text = @"Lose";
}
else if(scoreComp == scoreYou) {
lblComp.text = @"Draw";
lblYou.text = @"Draw";
}
else {
lblComp.text = @"Lose";
lblYou.text = @"Win";
}
}
- (void)dealloc {
[crdComp1 release];
[crdComp2 release];
[crdComp3 release];
[crdYou1 release];
[crdYou2 release];
[crdYou3 release];
[lblComp release];
[lblYou release];
[super dealloc];
}
@end