} while (($line = fgets($file)) !== false) { $line = str_replace("\n", "", $line); $hands[] = $line; } fclose($file); // calculate $now = microtime(true); $player1WinCount = 0; $player2WinCount = 0; foreach ($hands as $hand) { // get player1 and 2 hands $player1 = substr($hand, 0, 14); $player2 = substr($hand, 15, 14); // get winner $winners = PokerHand::getWinnerHands(array(PokerHand::getInstance($player1)->getHandInfo(), PokerHand::getInstance($player2)->getHandInfo())); // tie if (!$winners) { continue; } // count if (array_keys($winners)[0] == 0) { $player1WinCount++; continue; } $player2WinCount++; } $tookTime = microtime(true) - $now; echo "Player1 won times: " . $player1WinCount . "\n"; echo "Player2 won times: " . $player2WinCount . "\n"; echo "time: " . $tookTime . " secs.\n";
} return $winHands; } } ////////////////////////// MAIN ////////////////////////////// if (isset($_GET['hands']) && isset($_GET['is_compare_suit'])) { // get parameters $hands = $_GET['hands']; $isCompareSuit = $_GET['is_compare_suit'] == 1 ? true : false; // calculate $now = microtime(true); $handsInfo = array(); foreach ($hands as $hand) { $handsInfo[] = PokerHand::getInstance($hand, $isCompareSuit)->getHandInfo(); } $winners = PokerHand::getWinnerHands($handsInfo); $tookTime = microtime(true) - $now; // tie if (false === $winners) { echo json_encode(array('msg' => 'Tie!', 'took_time' => $tookTime)); exit; } // get win message key(end($winners)); $lastWinnerKey = key($winners); $msg = 'Player '; foreach ($winners as $winnerKey => $winner) { $msg .= ++$winnerKey; if (count($winners) >= 2 && $lastWinnerKey + 1 != $winnerKey) { $msg .= ', '; }