Created
April 10, 2023 20:11
-
-
Save yusufusta/963bc00a86d145aeccb35ab8be011426 to your computer and use it in GitHub Desktop.
ChatGPT's PHP CLI Blackjack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Kullanıcı ve kurpiyerin ellerini temsil eden sınıf. | |
class Hand | |
{ | |
public $cards = array(); | |
// Eldeki kartların toplam değerini hesapla. | |
public function getTotal() | |
{ | |
$total = 0; | |
$aces = 0; | |
foreach ($this->cards as $card) { | |
if ($card['value'] == 'A') { | |
$aces++; | |
} else if (is_numeric($card['value'])) { | |
$total += $card['value']; | |
} else { | |
$total += 10; | |
} | |
} | |
for ($i = 0; $i < $aces; $i++) { | |
if ($total + 11 <= 21) { | |
$total += 11; | |
} else { | |
$total += 1; | |
} | |
} | |
return $total; | |
} | |
// Eldeki kartları ekrana yazdır. | |
public function showCards() | |
{ | |
foreach ($this->cards as $card) { | |
echo $card['value'] . $card['suit'] . ' '; | |
} | |
echo "\n"; | |
} | |
} | |
// Kart destesi sınıfı. | |
class Deck | |
{ | |
public $cards = array(); | |
// Kartları karıştır. | |
public function shuffle() | |
{ | |
shuffle($this->cards); | |
} | |
// Bir kart çek. | |
public function drawCard() | |
{ | |
return array_pop($this->cards); | |
} | |
// Kart destesini oluştur. | |
public function createDeck() | |
{ | |
$suits = array('H', 'D', 'C', 'S'); | |
$values = array('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'); | |
foreach ($suits as $suit) { | |
foreach ($values as $value) { | |
$this->cards[] = array('value' => $value, 'suit' => $suit); | |
} | |
} | |
} | |
} | |
// Blackjack oyunu sınıfı. | |
class Blackjack | |
{ | |
public $playerHand; | |
public $dealerHand; | |
public $deck; | |
public $playerMoney = 100; | |
public $bet; | |
// Yeni bir oyun başlat. | |
public function newGame() | |
{ | |
echo "Welcome to Blackjack!\n"; | |
echo "You have $" . $this->playerMoney . ".\n"; | |
while (true) { | |
echo "How much would you like to bet? "; | |
$this->bet = (int)readline(); | |
if ($this->bet <= $this->playerMoney) { | |
break; | |
} else { | |
echo "You don't have that much money. Please bet again.\n"; | |
} | |
} | |
// Kart destesi oluştur ve karıştır. | |
$this->deck = new Deck(); | |
$this->deck->createDeck(); | |
$this->deck->shuffle(); | |
// İlk eli dağıt. | |
$this->playerHand = new Hand(); | |
$this->dealerHand = new Hand(); | |
for ($i = 0; $i < 2; $i++) { | |
$this->playerHand->cards[] = $this->deck->drawCard(); | |
$this->dealerHand->cards[] = $this->deck->drawCard(); | |
} | |
// Kurpiyerin açık kartını göster. | |
echo "Dealer's hand: " . $this->dealerHand->cards[0]['value'] . "\n"; | |
// Oyuncunun elini göster. | |
echo "Your hand: "; | |
$this->playerHand->showCards(); | |
// Oyuncunun elinin toplam değerini kontrol et. | |
$total = $this->playerHand->getTotal(); | |
if ($total == 21) { | |
echo "Blackjack! You win $" . ($this->bet * 1.5) . "!\n"; | |
$this->playerMoney += ($this->bet * 1.5); | |
return; | |
} | |
// Oyuncu aşması durumunda kaybeder. | |
if ($total > 21) { | |
echo "Bust! You lose $" . $this->bet . ".\n"; | |
$this->playerMoney -= $this->bet; | |
return; | |
} | |
// Oyun devam ederse, oyuncuya hit veya stand seçenekleri sunulur. | |
while (true) { | |
echo "Would you like to hit or stand? "; | |
$choice = readline(); | |
if ($choice == 'hit') { | |
$this->playerHand->cards[] = $this->deck->drawCard(); | |
$this->playerHand->showCards(); | |
$total = $this->playerHand->getTotal(); | |
if ($total > 21) { | |
echo "Bust! You lose $" . $this->bet . ".\n"; | |
$this->playerMoney -= $this->bet; | |
return; | |
} | |
} else { | |
break; | |
} | |
} | |
// Kurpiyerin eli açılır ve 17 veya daha fazla değere sahip olana kadar kart çeker. | |
echo "Dealer's hand: "; | |
$this->dealerHand->showCards(); | |
while ($this->dealerHand->getTotal() < 17) { | |
$this->dealerHand->cards[] = $this->deck->drawCard(); | |
echo "Dealer hits.\n"; | |
$this->dealerHand->showCards(); | |
} | |
// Kurpiyer kaybederse, oyuncu kazanır. | |
if ($this->dealerHand->getTotal() > 21) { | |
echo "Dealer busts! You win $" . $this->bet . "!\n"; | |
$this->playerMoney += $this->bet; | |
return; | |
} | |
// En yüksek eli olan kazanır. | |
if ($this->playerHand->getTotal() > $this->dealerHand->getTotal()) { | |
echo "You win $" . $this->bet . "!\n"; | |
$this->playerMoney += $this->bet; | |
} else if ($this->playerHand->getTotal() == $this->dealerHand->getTotal()) { | |
echo "Push!\n"; | |
} else { | |
echo "You lose $" . $this->bet . ".\n"; | |
$this->playerMoney -= $this->bet; | |
} | |
} | |
} | |
// Oyunu başlat. | |
$blackjack = new Blackjack(); | |
$blackjack->newGame(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment