同花顺小游戏-iOS版
本案例用OC语言编写只是简单的侧重同花顺游戏逻辑方面
要求说明
- 游戏名称:同花顺
- 游戏方式:电脑方和玩家双方每局各3张牌,由系统随机分配。每局开始后双方六张牌全部背面显示,玩家只能通过每张牌(自己的牌)或者点击“揭牌”按钮才能显示本张牌的正面,玩家在点击“揭牌”按钮前无法查看电脑方牌(正面),在未点击“揭牌”按钮前,玩家可以增加赌注。双方点数相同,庄家赢,
- 裁判规则:豹子(三张牌数字相同)>同花顺(三张牌花色相同且数字连续)>同花对(三张牌花色相同且其中两张数字相同)>顺子(三张牌数字连续)>单张。
思路
先将游戏双方的牌点数统一转化成int型变量,然后统一比较来决定出本局游戏的胜负。
52张纸牌图片(去除大小王)按照下列命名方式:花色分组编号_牌面数字。
花色分组情况:1---红桃,2---黑桃,3---方片,4---梅花,
核心源码
-(NSMutableArray *)getAllDataFromFile{
NSMutableArray *allData = [[NSMutableArray alloc] init];
NSMutableArray *data = [[NSMutableArray alloc] init];
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"allData" ofType:@"plist"];
allData = [[NSMutableArray alloc] initWithContentsOfFile:dataPath];
for (int i = 0; i < 6; i++) {
int random = arc4random() % [allData count];
[data addObject:allData[random]];
}
return data;
}
-(BOOL)compareWinWithComputer:(NSMutableArray *)computer andPlayer:(NSMutableArray *)player{
int changeComputer = 0;
int changePlayer = 0;
changeComputer = [self core:computer];
changePlayer = [self core:player];
if (changePlayer - changeComputer > 0) {
return YES;
}
return NO;
}
-(NSString *)intChangeFromString:(NSMutableArray *)array{
NSString * result = [[NSString alloc] init];
for (int i = 0; i < [array count]; i++) {
result = [NSString stringWithFormat:@"%@%@",result,array[i]];
}
return result;
}
-(NSMutableArray *)sortFromArray:(NSMutableArray *)array{
NSString *temp = [[NSString alloc] init];
for (int i = 0; i < [array count]; i++) {
for (int j = 0; j < [array count]; j++) {
if ([array[i] intValue] > [array[j] intValue]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
-(int)examineSameNumber:(NSMutableArray *)array{
int count = 0;
int temp = 0;
for (int i = 0; i < [array count]; i++) {
temp = 0;
for (int j = 0; j < [array count]; j++) {
if ([array[i] isEqualToString:array[j]]) {
temp++;
}
}
if (temp > count) {
count = temp;
}
}
return count;
}
-(int)core:(NSMutableArray *)array{
NSString *result = [[NSString alloc] init];
NSMutableArray *colour = [[NSMutableArray alloc] init];
NSMutableArray *number = [[NSMutableArray alloc] init];
NSArray *temp = [[NSArray alloc] init];
for (int i = 0; i < [array count]; i++) {
temp = [array[i] componentsSeparatedByString:@"_"];
[colour addObject:temp[0]];
[number addObject:[temp[1] componentsSeparatedByString:@"."][0]];
}
NSMutableArray *sortNumber = [[NSMutableArray alloc] init];
sortNumber = [self sortFromArray:number];
int count = [self examineSameNumber:sortNumber];
if (count == 3) {
result = [NSString stringWithFormat:@"50%@",[self intChangeFromString:sortNumber]];
}else if (count == 2){
if ([self isSameColour:colour]) {
result = [NSString stringWithFormat:@"42%@",[self intChangeFromString:sortNumber]];
}else{
result = [NSString stringWithFormat:@"20%@",[self intChangeFromString:sortNumber]];
}
}else if (count == 1){
if ([self isContinuous:sortNumber]) {
if ([self isSameColour:colour]) {
result = [NSString stringWithFormat:@"43%@",[self intChangeFromString:sortNumber]];
}else{
result = [NSString stringWithFormat:@"30%@",[self intChangeFromString:sortNumber]];
}
}else if ([self isSameColour:colour]){
result = [NSString stringWithFormat:@"40%@",[self intChangeFromString:sortNumber]];
}else{
result = [NSString stringWithFormat:@"10%@",[self intChangeFromString:sortNumber]];
}
}
return [result intValue];
}
- (BOOL)isContinuous:(NSMutableArray *)array{
int count = 0;
for (int i = 0; i < [array count]-1; i++) {
if ([array[i] intValue] - [array[i+1] intValue] == 1) {
count++;
}
}
if (count == [array count]-1) {
return YES;
}
return NO;
}
- (BOOL)isSameColour:(NSMutableArray *)array{
int count = 1;
for (int i = 0; i < [array count]-1; i++) {
if ([array[i] isEqualToString:array[i+1]]) {
count++;
}
}
if (count == [array count]) {
return YES;
}
return NO;
}