raku

My Raku Scripts Collection
git clone https://fab.ddns.me.uk/stagit/raku
Log | Files | Refs | README

commit c67523bbd9dceb321cfc81853168228b20bda046
parent 039099acf09c1375b6e462aaaa0ffd33ac849bf7
Author: masayoshi <masayoshi@example.com>
Date:   Mon, 23 Feb 2026 18:13:53 +0900

Upload

Diffstat:
Ablackjack.raku | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+), 0 deletions(-)

diff --git a/blackjack.raku b/blackjack.raku @@ -0,0 +1,61 @@ +#!/usr/bin/env raku +# 1. 山札(デッキ)の作成 +my @base = <1 2 3 4 5 6 7 8 9 10 J Q K A>; +my @deck = (@base, @base, @base, @base).flat; + +# 2. 手札の合計を計算する関数 +sub count-score(@hand) { + my $total = 0; + my $aces = 0; + + for @hand -> $card { + if $card ~~ Int { $total += $card } + elsif $card eq 'A' { $aces += 1; $total += 11 } + else { $total += 10 } # J, Q, K は 10 + } + + # Aの調整(21を超えたら11から1に変換) + while $total > 21 && $aces > 0 { + $total -= 10; + $aces -= 1; + } + return $total; +} + +# 3. ゲーム開始 +my @cards = @deck.pick(*); # 山札をシャッフル +my @player = @cards.pop, @cards.pop; +my @dealer = @cards.pop, @cards.pop; + +loop { + my $p-score = count-score(@player); + say "Your hand: {@player.join(', ')} (Score: $p-score)"; + + if $p-score > 21 { say "Bust! You lose. 🇬🇧 Dreadful luck!"; exit; } + if $p-score == 21 { say "Blackjack! 🇬🇧 Top-hole!"; last; } + + my $choice = prompt "Do you want to (H)it or (S)tand? "; + if $choice.lc.starts-with('h') { + @player.push: @cards.pop; + } else { + last; + } +} + +# 4. ディーラーのターン +say "\nDealer's hand: {@dealer.join(', ')}"; +while count-score(@dealer) < 17 { + @dealer.push: @cards.pop; + say "Dealer hits: {@dealer.join(', ')}"; +} + +# 5. 結果判定 +my $p-total = count-score(@player); +my $d-total = count-score(@dealer); + +say "\nFinal Scores - You: $p-total, Dealer: $d-total"; + +if $d-total > 21 { say "Dealer busts! You win! 🇬🇧 Brilliant!"; } +elsif $p-total > $d-total { say "You win! 🇬🇧 Jolly good!"; } +elsif $p-total < $d-total { say "Dealer wins. 🇬🇧 Better luck next time!"; } +else { say "It's a push (Tie)."; } +\ No newline at end of file