<?php
$cluedo = new cluedo();
class cluedo {
var $murderCards = array( 'people'=> array('Mustard', 'Plum', 'Green', 'Peacock', 'Scarlett', 'White'),
'weapon' => array('Dagger', 'CandleStick', 'Revolver', 'Rope', 'Lead Piping', 'Spaer'),
'room' => array('Hall', 'Lounge', 'Dining Room', 'Kitchen', 'Ball Room', 'Conservatory', 'Billiard Room', 'Library', 'Study') );
var $cardsToDeal = array();
function __construct() {
// DATABASE
$INFO = array( 'sql_host' => "localhost", 'sql_username' => "cluedo", 'sql_password' => "cluedo", 'sql_database' => "cluedo", 'sql_charset' => "utf8");
$dsn = "mysql:host={$INFO['sql_host']};dbname={$INFO['sql_database']};charset={$INFO['sql_charset']}";
$opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ];
try {
$this->DB = new PDO($dsn, $INFO['sql_username'], $INFO['sql_password'], $opt);
}
catch (PDOException $e) {
exit($e->getMessage());
}
echo "<h1>Cluedo</h1>";
$this->generateGame();
}
// DEAL Murder Cards:
function dealMurderCards($numberOfPlayers) {
$playerId = 1;
$totalNumberOfCards = count($this->cardsToDeal);
for( $i = 1; $i <= $totalNumberOfCards; $i++ ) {
shuffle($this->cardsToDeal);
$numberOfCardsRemaining = ( count($this->cardsToDeal) - 1);
$cardId = random_int(0, $numberOfCardsRemaining);
$this->playerCards[$playerId]['cards'][] = "{$this->cardsToDeal[$cardId]}";
unset($this->cardsToDeal[$cardId]);
if( $playerId < $numberOfPlayers) { $playerId++; } else {$playerId = 1; }
}
print_r($this->playerCards);
}
// GENERATE NEW GAME:
function generateGame() {
$start = 0;
$gameId = uniqid();
$gameElements = count($this->murderCards);
$query = "";
$i = 1;
echo "<div>Your Game Id: " . $gameId . "</div>n";
foreach( $this->murderCards as $k => $v ) {
$max = ( count($v) - 1 );
$int = random_int($start, $max);
$query .= "'$int'";
if( $i < $gameElements) { $query .= ", "; }
$i++;
// Add 'Murder' Card to Envelope
$games[$gameId][$k] = "$v[$int]";
// Remove the selected Murder Card from the deck.
unset($v[$int]);
// Add the unused 'Murder' cards to the $cardsToDeal array
$this->cardsToDeal = array_merge($this->cardsToDeal, $v);
}
echo "<h1>The game has been game been generated and the cards have been dealt - but has <b>NOT</b> been SAVED as of yet!</h1>";
echo "<div>INSERT cluedo_games (`cluedo_gameid`, `cluedo_person`, `cluedo_weapon`, `cluedo_room`) VALUES ('$gameId', $query)</div>n";
/*$stmt = $this->DB->prepare("INSERT (`cluedo_gameid`, `cluedo_person`, `cluedo_weapon`, `cluedo_room`) VALUES ('$gameId', $query)) = ?");
$stmt->execute([$cat]);*/
print_r($games);
$this->dealMurderCards(3);
}
}
برچسب:
نویسنده: آیلین رضوانی