| summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'data/examples/blackjack')
| -rw-r--r-- | data/examples/blackjack/cards.fate | 116 | ||||
| -rw-r--r-- | data/examples/blackjack/global.fate | 36 | ||||
| -rw-r--r-- | data/examples/blackjack/main.fate | 77 | ||||
| -rw-r--r-- | data/examples/blackjack/play.fate | 293 | ||||
| -rw-r--r-- | data/examples/blackjack/player.fate | 9 | ||||
| -rw-r--r-- | data/examples/blackjack/rules.fate | 5 |
6 files changed, 536 insertions, 0 deletions
diff --git a/data/examples/blackjack/cards.fate b/data/examples/blackjack/cards.fate new file mode 100644 index 0000000..2312f03 --- /dev/null +++ b/data/examples/blackjack/cards.fate @@ -0,0 +1,116 @@ +(fate_version 1) + +(declare_structure card + ( text name ) + ( int number ) + ( int score ) +) + +(local (list card) heart_cards) +(local (list card) spade_cards) +(local (list card) club_cards) +(local (list card) diamond_cards) + +(define_sequence generate_card_familly ((string name) ((ptr (list card)) deck)) + (local int i) + + (for (set i 1) (=< (var i) 13) (set i (+ (var i) 1)) + (local card c) + + (set c.number (var i)) + + (set c.name + (text + (switch (var i) + (1 Ace) + (11 Jack) + (12 Queen) + (13 Kind) + (cast string (var i)) + ) + of (var name) + ) + ) + + (set c.score + (switch (var i) + (11 10) + (12 10) + (13 10) + (var i) + ) + ) + + (add (var c) (at deck)) + ) +) + +(visit generate_card_familly Hearts (ptr heart_cards)) +(visit generate_card_familly Spades (ptr spade_cards)) +(visit generate_card_familly Diamonds (ptr diamond_cards)) +(visit generate_card_familly Clubs (ptr club_cards)) + +(global (list card) deck_template) + +(add_all heart_cards deck_template) +(add_all spade_cards deck_template) +(add_all diamond_cards deck_template) +(add_all club_cards deck_template) + +(define_sequence shuffle_into_deck + ( + ((ptr (list card)) cards) + ((ptr (list card)) deck) + ) + + (while (! (is_empty (at cards))) + (local int o) + (local int d) + + (set o (rand 0 (- (size (at cards)) 1))) + + (if_else (is_empty (at deck)) + (set d 0) + (set d (rand 0 (- (size (at deck)) 1))) + ) + + (add_at (var d) (access (at cards) (var o)) (at deck)) + (remove_at (var o) (at cards)) + ) +) + +(define_sequence generate_shuffled_deck (((ptr (list card)) result)) + (local (list card) initial_deck) + + (set initial_deck (var deck_template)) + + (clear (at result)) + (visit shuffle_into_deck (ptr initial_deck) (var result)) +) + +(define_sequence draw_a_card (((ptr (list card)) deck) ((ptr card) result)) + (set (at result) (access (at deck) 0)) + (remove_at 0 (at deck)) +) + +(define_sequence compute_score (((ptr (list card)) deck) ((ptr int) result)) + (local int aces_count) + (local int maybe_better_score) + + (set (at result) 0) + + (foreach (at deck) card + (set (at result) (+ (var card.score) (at result))) + (if (= (var card.number) 1) + (set aces_count (+ (var aces_count) 1)) + ) + ) + + (while (> (var aces_count) 0) + (set maybe_better_score (+ (at result) 10)) + (if (=< (var maybe_better_score) 21) + (set (at result) (var maybe_better_score)) + ) + (set aces_count (- (var aces_count) 1)) + ) +) diff --git a/data/examples/blackjack/global.fate b/data/examples/blackjack/global.fate new file mode 100644 index 0000000..9c10c9f --- /dev/null +++ b/data/examples/blackjack/global.fate @@ -0,0 +1,36 @@ +(fate_version 1) + +(require player.fate) + +(global player player) + +(global (lambda text (int)) coins_word) + +(set coins_word + (lambda ((int i)) + (if_else (= (var i) 1) + (text a single coin ) + (text (var i) coins ) + ) + ) +) + +(declare_text_effect action_description) + +(define_sequence money_acquisition ((int amount)) + (set player.money (+ (var player.money) (var amount))) + (if (> (var amount) 0) + (text_effect action_description + You acquired (eval coins_word (var amount))! + ) + ) +) + +(define_sequence money_loss ((int amount)) + (set player.money (- (var player.money) (var amount))) + (if (> (var amount) 0) + (text_effect action_description + You lost (eval coins_word (var amount)). + ) + ) +) diff --git a/data/examples/blackjack/main.fate b/data/examples/blackjack/main.fate new file mode 100644 index 0000000..0b5acf8 --- /dev/null +++ b/data/examples/blackjack/main.fate @@ -0,0 +1,77 @@ +(fate_version 1) + +(require player.fate) +(require global.fate) +(require cards.fate) +(require rules.fate) +(require play.fate) + +(local int original_amount) + +(set player.money 0) + +Welcome to Tonkadur's wonderful and totally original blackjack! +Say, I don't think I've seen you before... +No, no, I am sure I haven't, actually. +(prompt_string player.name 1 64 What's your name, then?) +Alright, (var player.name), well, since it's your first time here, let me give +you some coins. +Just between you and me, someone left those laying around, they aren't mine. + +(visit money_acquisition (+ 100 (rand 0 100))) +(set original_amount (var player.money)) + +Now, you're all set to go... unless you don't know how to play? + +(player_choice + ( + ( As it happens, I do not. ) + (visit rules_of_blackjack) + (text_effect action_description + You leave the counter and approach one of the tables. + ) + (visit play_a_game) + ) + ( + ( I am familiar with BlackJack. ) + (text_effect action_description + You leave the counter and approach one of the tables. + ) + (visit play_a_game) + ) +) +(text_effect action_description + As you leave the establishment, the receptionist notices you. +) + +(cond + ( + (=< (var player.money) (/ (var original_amount) 2)) + Outch. I suppose all your luck was spent by getting these free coins. + ) + ( + (< (var player.money) (var original_amount)) + Well, that's not too bad for your first time. Come back tomorrow, I am + sure you'll be getting it all back. + ) + ( + (< (var player.money) (* (var original_amount) 2)) + Had a good day, I hope? + ) + ( + (< (var player.money) (* (var original_amount) 4)) + Well! That was some fine play, if I do say so myself! + ) + ( + (true) + (var player.name)! How did you do that?! You have to teach me! I did give + you the tools of your success, so please? + ) +) + +(text_effect action_description + You walk out, having turned (eval coins_word (var original_amount)) into + (eval coins_word (var player.money)). +) + +(end) diff --git a/data/examples/blackjack/play.fate b/data/examples/blackjack/play.fate new file mode 100644 index 0000000..4c2913f --- /dev/null +++ b/data/examples/blackjack/play.fate @@ -0,0 +1,293 @@ +(fate_version 1) + +(require player.fate) +(require global.fate) +(require cards.fate) + +(global bool has_played) +(global (list card) current_deck) +(global (list card) dealer_hand) +(global int bet) +(global bool has_doubled) + +(set has_played (false)) + +(define_sequence play_a_game () + (local card new_card) + + (if (not (var has_played)) + (text_effect action_description + Sitting yourself at a table, you see someone rushing to set up the + cards and manage the game. + ) + ) + (if (= (var player.money) 0) + ( + I am sorry, (var player.name), but you appear to have ran out of coins. + (done) + ) + ) + (prompt_integer bet 1 (var player.money) How much would you like to bet?) + (text_effect action_description + The dealer shuffles the cards and starts dealing. + ) + + (newline) + + (visit generate_shuffled_deck (ptr current_deck)) + + (visit draw_a_card (ptr current_deck) (ptr new_card)) + (add (var new_card) player.hand) + + (text_effect action_description + You have been dealt the (var new_card.name). + (newline) + ) + + + (visit draw_a_card (ptr current_deck) (ptr new_card)) + (add (var new_card) dealer_hand) + + (text_effect action_description + The dealer has drawn the (var new_card.name). + (newline) + ) + + (visit draw_a_card (ptr current_deck) (ptr new_card)) + (add (var new_card) player.hand) + + (text_effect action_description + You have been dealt the (var new_card.name). + (newline) + ) + + (newline) + + (visit draw_a_card (ptr current_deck) (ptr new_card)) + (add (var new_card) dealer_hand) + + (text_effect action_description + The dealer has drawn a card, face down. + (newline) + ) + + (visit initial_draw) + + (clear dealer_hand) + + (newline) + Interesting. Would you like to go again? + + (player_choice + ( + ( Yes, please. ) + Very well. + (newline) + (jump_to play_a_game) + ) + ( + ( No, thank you. ) + It was a pleasure to play with you. Farewell. + (newline) + (done) + ) + ) +) + +(define_sequence initial_draw () + (local int dealer_score) + (local int player_score) + + (visit compute_score (ptr dealer_hand) (ptr dealer_score)) + (visit compute_score (ptr player.hand) (ptr player_score)) + + (if (= (var dealer_score) 21) + (ifelse (= (var player_score) 21) + ( + (text_effect action_description + The dealer looks very surprised. + ) + (newline) + Double Blackjack! A tie, then. + (done) + ) + ( + (text_effect action_description + The dealer looks surprised, but happy. + ) + (newline) + Blackjack! Looks like I win, this time. + (visit money_loss (var bet)) + (done) + ) + ) + ) + + (if (= (var player_score) 21) + ( + (text_effect action_description + The dealer looks surprised. + ) + (newline) + Blackjack! Looks like you win, this time. + (newline) + (visit money_acquisition (cast int (* (cast float (var bet)) 1.5))) + (done) + ) + ) + + (player_choice + ( + ( Another card, please. ) + (set has_doubled (false)) + (jump_to acquire_card) + ) + ( + ( I will stand. ) + (jump_to resolve_dealer) + ) + (if (and (>= (var player_score) 9) (<= (var player_score) 11)) + ( + ( Double my bet, I'll only take one card. ) + (set bet (* (var bet) 2)) + (set has_doubled (true)) + (jump_to acquire_card) + ) + ) + ) +) + +(define_sequence acquire_card () + (local card new_card) + (local int player_score) + + (visit draw_a_card (ptr current_deck) (ptr new_card)) + (add (var new_card) player.hand) + + (visit compute_score (ptr player.hand) (ptr player_score)) + + (text_effect action_description + You have been dealt the (var new_card.name). + (newline) + Your hand is currently: + ) + (foreach player.hand card + (text_effect action_description + (newline) + * The (var card.name), worth + (if_else (= (var card.number) 1) + (text 1 or 11 points. ) + (text (var card.score) points. ) + ) + ) + ) + (newline) + + (if (> (var player_score) 21) + ( + (text_effect action_description + The dealer looks disappointed. + ) + (newline) + A bust! What a shame... + (newline) + (visit money_loss (var bet)) + (done) + ) + ) + + (if (var has_doubled) + (jump_to resolve_dealer) + ) + + (player_choice + ( + ( Another card, please. ) + (jump_to acquire_card) + ) + ( + ( This will do. I stand. ) + (jump_to resolve_dealer) + ) + ) +) + +(define_sequence resolve_dealer () + (local int dealer_score) + (local int player_score) + (visit compute_score (ptr dealer_hand) (ptr dealer_score)) + + Very well. I shall now play my turn. + (newline) + + (text_effect action_description + My current hand is: + (newline) + ) + (foreach dealer_hand card + (text_effect action_description + (newline) + * The (var card.name), worth + (if_else (= (var card.number) 1) + (text 1 or 11 points. ) + (text (var card.score) points. ) + ) + ) + ) + (newline) + + (while (< (var dealer_score) 17) + (local card new_card) + + (visit draw_a_card (ptr current_deck) (ptr new_card)) + (add (var new_card) dealer_hand) + + (text_effect action_description + The dealer has drawn the (var new_card.name). + (newline) + ) + + (visit compute_score (ptr dealer_hand) (ptr dealer_score)) + ) + + (if (> (var dealer_score) 21) + ( + (text_effect action_description + The dealer looks disappointed. + ) + (newline) + Ah. It would appear I have gone above the limit. + (newline) + (visit money_acquisition (var bet)) + (done) + ) + ) + + (visit compute_score (ptr player.hand) (ptr player_score)) + + (var player_score) for you, against (var dealer_score) for me. + + (newline) + + (if (= (var player_score) (var dealer_score)) + ( + A tie, then. + (done) + ) + ) + + (if_else (> (var player_score) (var dealer_score)) + ( + Congratulation, you won. + (newline) + (visit money_acquisition (var bet)) + (done) + ) + ( + It would appear I have won this game. + (newline) + (visit money_loss (var bet)) + (done) + ) + ) +) diff --git a/data/examples/blackjack/player.fate b/data/examples/blackjack/player.fate new file mode 100644 index 0000000..7d8b947 --- /dev/null +++ b/data/examples/blackjack/player.fate @@ -0,0 +1,9 @@ +(fate_version 1) + +(require cards.fate) + +(define_structure player + (string name) + (int money) + ((list card) hand) +) diff --git a/data/examples/blackjack/rules.fate b/data/examples/blackjack/rules.fate new file mode 100644 index 0000000..3e4c378 --- /dev/null +++ b/data/examples/blackjack/rules.fate @@ -0,0 +1,5 @@ +(fate_version 1) + +(define_sequence rules_of_blackjack () + TODO +) |


