Example #1
0
function new_game($session, $hand)
{
    # Make a bid, which we'll do randomly, by choosing a number between 1 and
    # 13.
    $bid = rand(1, 13);
    # Register our bid with the server.
    info("Attempting to bid " . $bid . ".");
    api("bid", ["session" => $session, "bid" => $bid]);
    info("Our bid has been accepted.");
    # Check the status repeatedly, and if it's our turn play a card, until all
    # cards have been played and the game ends.
    while (count($hand) !== 0) {
        # Always wait 1 second, it may not seem like much but it helps avoid
        # pinning the client's CPU and flooding the server.
        sleep(1);
        # Request the game's status from the server.
        info("Requesting the status of our game...");
        $json = api("status", ["session" => $session]);
        info("Status received.");
        # If the game has ended prematurely, due to a forfeit from your opponent
        # or some other reason, rejoice and find a new opponent.
        if (is_null($json["game"])) {
            info("Our game appears to have ended.");
            return;
        }
        # If we're still in the bidding process, it's nobody's turn.
        if (is_null($json["your-turn"])) {
            info("Our game is still in the bidding phase, we need to wait for our opponent.");
            next;
        }
        # If not it's not our turn yet, jump back to the top of the loop to
        # check the status again.
        if (!$json["your-turn"]) {
            info("It is currently our opponent's turn, we need to wait for our opponent.");
            continue;
        }
        # Finally, it's our turn. First, we have to determine if another card
        # was played first in this round. If so, it restricts our choices.
        $allowed_cards;
        if (is_null($json["opponent-current-card"])) {
            # We can play any card we want, since we're going first in this
            # round. So all the cards in our hand are allowed.
            $allowed_cards = $hand;
            info("We have the lead this round, so we may choose any card.");
        } else {
            # We can only play cards that match the suit of the lead card, since
            # we're going second in this round. Gather together all the cards in
            # our hand that have the appropriate suit.
            $allowed_cards = [];
            $lead_card = parse_card($json["opponent-current-card"]);
            info("Our opponent has lead this round, so we must try to play a card that matches the lead card's suit: " . $lead_card["suit"] . ".");
            foreach ($hand as $card) {
                $card = parse_card($card);
                if ($card["suit"] === $lead_card["suit"]) {
                    array_push($allowed_cards, $card["abbr"]);
                }
            }
            # Check if we actually found any cards in our hand with the
            # appropriate suit. If we don't have any, there are no restrictions
            # on the card we can then play.
            if (count($allowed_cards) === 0) {
                info("We have no " . $lead_card["suit"] . " in our hand, so we can play any suit we choose.");
                $allowed_cards = $hand;
            }
        }
        # Among the cards that we have determined are valid, according to the
        # rules, choose one to play at random.
        $idx = rand(0, count($allowed_cards) - 1);
        $card = $allowed_cards[$idx];
        info("We have randomly chosen " . $card . ".");
        # Now that the card has been chosen, play it.
        info("Attempting to play " . $card . "...");
        api("play", ["session" => $session, "card" => $card]);
        info("Card has been played.");
        # Remove the card from our hand.
        $new_hand = [];
        foreach ($hand as $card_in_hand) {
            if ($card_in_hand !== $card) {
                array_push($new_hand, $card_in_hand);
            }
        }
        $hand = $new_hand;
    }
}
Example #2
0
function card_cmp($card1, $card2)
{
    $card1 = parse_card($card1);
    $card2 = parse_card($card2);
    // The rules are:
    // 1) If the suits are the same, the highest value won.
    // 2) If the suits are different:
    // 2a) If the second suit is a trump, it won.
    // 2b) Otherwise it lost.
    if ($card1[0] === $card2[0]) {
        if ($card1[1] > $card2[1]) {
            return 1;
        }
        return 2;
    } elseif ($card2[0] === "S") {
        return 2;
    } else {
        return 1;
    }
}
Example #3
0
if ($leader) {
    play_card("card1");
    reset_timeout("game");
    reset_timeout("session");
    success([]);
}
// Check if the leader has played a card yet.
if (is_null($row["card1"])) {
    failure("The leader, which isn't you, has not yet played a card in this round.");
}
// If we're the second person to play in this round, the leading suit restricts
// our choices. We need to match the suit if we can. We need to validate that the
// bot did not have any cards of the leading suit to play, or that the bot played
// the suit.
$card1 = parse_card($row["card1"]);
$card2 = parse_card($req["card"]);
if ($card1[0] !== $card2[0]) {
    // The cards were different suits, so now check if this bot has any of the suit
    // that the leader played.
    if (preg_match("/({$card1[0]}\\d\\d)/", $bot["cards"]) === 1) {
        failure("You cannot play the card '{$req['card']}' since '{$row['card1']}' was lead and you have at least one card of that suit.");
    }
}
// We have a valid card to play, so play it.
play_card("card2");
// The round is now over, so determine who won it.
$winner = "bot" . card_cmp($row["card1"], $req["card"]);
// Update the columns for the tricks and the round.
if ($winner === "bot1") {
    // Figure out which bot is bot1 in this round.
    if ($row["bot1"] === $row["id1"]) {