同花顺小游戏

同花顺小游戏-iOS版

本案例用OC语言编写只是简单的侧重同花顺游戏逻辑方面

要求说明

  • 游戏名称:同花顺
  • 游戏方式:电脑方和玩家双方每局各3张牌,由系统随机分配。每局开始后双方六张牌全部背面显示,玩家只能通过每张牌(自己的牌)或者点击“揭牌”按钮才能显示本张牌的正面,玩家在点击“揭牌”按钮前无法查看电脑方牌(正面),在未点击“揭牌”按钮前,玩家可以增加赌注。双方点数相同,庄家赢,
  • 裁判规则:豹子(三张牌数字相同)>同花顺(三张牌花色相同且数字连续)>同花对(三张牌花色相同且其中两张数字相同)>顺子(三张牌数字连续)>单张。

思路

  先将游戏双方的牌点数统一转化成int型变量,然后统一比较来决定出本局游戏的胜负。
  52张纸牌图片(去除大小王)按照下列命名方式:花色分组编号_牌面数字。
  花色分组情况:1---红桃,2---黑桃,3---方片,4---梅花,

核心源码

  • GameService
-(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;
    //1、先将数组中的花色和数字进行分离并格式化
    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];
    //1、先将数组中的花色和数字进行分离并格式化
    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]];
    }
    //2、数字数组由大到小进行排序
    NSMutableArray *sortNumber = [[NSMutableArray alloc] init];
    sortNumber = [self sortFromArray:number];
    //3、以数字为优先遍历
    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;
}


要编写一个模拟砸金花游戏的代码,我们首先需要创建一副扑克牌,然后定义各种牌型的判断规则和概率计算。下面是一个基础的Python代码示例,使用列表和循环来模拟发牌和统计概率: ```python import random import numpy as np # 初始化一副扑克牌(52张牌,包括大王和小王) suits = ['♠', '♥', '♦', '♣'] ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', 'W'] # W代表大小王 deck = [(rank, suit) for rank in ranks for suit in suits] # 模拟发牌函数 def deal_cards(num_deals): return random.sample(deck, num_deals) # 定义各种牌型判断和概率计算 def check_card_type(cards): card_counts = {rank: cards.count(rank) for rank in ranks} if any(count == 4 for count in card_counts.values()): return '四条', 0.01 # 四条出现概率极低,这里设置为0.1%作为示例 elif any(card_counts[rank] == 3 for rank in ranks): return '条', 1 / len(ranks)**3 # 条概率为1/(13)^3 elif sorted(card_counts.values()) == [1, 1, 1, 1]: return '炸弹', 1 / (len(ranks)**4) # 炸弹概率为1/(13)^4 else: # 排序并检查是否连续 sorted_ranks = sorted(card_counts) is_straight = True and (sorted_ranks[-1] - sorted_ranks[0]) == 4 if is_straight: return '顺子', 4/51 # 标准五十四张牌中,有4种顺子,概率为4/51 else: # 检查同花和同花顺 is_flush = len(set(suits)) == 1 and sorted(card_counts.items(), key=lambda x: x[0])[0][1] == card_counts.values()[0] if is_flush: if is_straight: return '同花顺', 1 / (len(ranks) * len(suits)) # 同花顺概率为1/52 else: return '同花', 4/51 # 同花概率为4/51,因为每种花色都有四个可能的顺子 else: return '普通', 1 - sum(prob for _, prob in locals().items() if _ != 'normal') # 计算剩余概率 # 发牌10000次并统计牌型出现次数 num_trials = 10000 results = {'pairs': 0, 'threes': 0, 'straights': 0, 'flushes': 0, 'fours': 0, 'full_houses': 0, 'straight_flushes': 0} for _ in range(num_trials): dealt_cards = deal_cards(5) # 我们通常玩的是五张牌的砸金花 card_type, _ = check_card_type(dealt_cards) results[card_type] += 1 # 输出统计结果和概率 print(f"模拟{num_trials}次砸金花,各牌型出现次数及概率分别为:") for card_type, freq in results.items(): probability = freq / num_trials print(f"{card_type}: 出现次数 {freq}, 预测概率约为 {probability:.2%}") #
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值