0

我很难找到有关此的任何信息,并且我遇到的所有代码示例都是基于所有玩家都以平局结束的比赛。在我的 2 人回合制游戏中,我希望能够以赢家和输家结束比赛。使用下面的代码,我编写的比赛总是以两个玩家的相同结果结束,如果赢了,那么玩家 1 和 2 都赢了,如果输了,玩家 1 和 2 都松了……有什么帮助吗?谢谢你。

    if (gameOver == true) {
    if (GameWinner == 0) {
        GKTurnBasedParticipant *player0 = [currentMatch.participants objectAtIndex:0];
        player0.matchOutcome = GKTurnBasedMatchOutcomeWon;
        GKTurnBasedParticipant *player1 = [currentMatch.participants objectAtIndex:1];
        player1.matchOutcome = GKTurnBasedMatchOutcomeLost;

        [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }
        }];  
        testlabel.text = @"Player 1 Wins!";
    } else if (GameWinner == 1) {
        GKTurnBasedParticipant *player0 = [currentMatch.participants objectAtIndex:0];
        player0.matchOutcome = GKTurnBasedMatchOutcomeLost;
        GKTurnBasedParticipant *player1 = [currentMatch.participants objectAtIndex:1];
        player1.matchOutcome = GKTurnBasedMatchOutcomeWon;

        [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }
        }];
        testlabel.text = @"Player 2 Wins!";
    } else if (GameWinner == 2) {
        for (GKTurnBasedParticipant *part in currentMatch.participants) {
            part.matchOutcome = GKTurnBasedMatchOutcomeTied;
        }
        [currentMatch endMatchInTurnWithMatchData:data completionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"%@", error);
            }
        }];
        testlabel.text = @"Tie Game!";
    } else {
        testlabel.text = @"Your turn is over.";
    }
4

1 回答 1

1

这听起来类似于这个SO Question,试试:

GKTurnBasedParticipant *curr = currentMatch.currentParticipant;
    NSUInteger currentIndex = [currentMatch.participants indexOfObject:currentMatch.currentParticipant];
    NSUInteger nextIndex = (currentIndex + 1) % [currentMatch.participants count];
    GKTurnBasedParticipant *next = [currentMatch.participants objectAtIndex:nextIndex];

    if (currScore < otherScore)
    {
        // Curr player lost
        curr.matchOutcome = GKTurnBasedMatchOutcomeLost;
        next.matchOutcome = GKTurnBasedMatchOutcomeWon;
    }
    else if (currScore == otherScore)
    {
        // Tied
        curr.matchOutcome = GKTurnBasedMatchOutcomeTied;
        next.matchOutcome = GKTurnBasedMatchOutcomeTied;
    }
    else 
    {
        // Won
        curr.matchOutcome = GKTurnBasedMatchOutcomeWon;
        next.matchOutcome = GKTurnBasedMatchOutcomeLost;
    }
于 2012-06-19T06:02:20.437 回答