Skip to content

Instantly share code, notes, and snippets.

@gurzgri
Last active February 8, 2022 22:03
Show Gist options
  • Save gurzgri/f09b8b78e96cd1d876d38ae7a64b8c96 to your computer and use it in GitHub Desktop.
Save gurzgri/f09b8b78e96cd1d876d38ae7a64b8c96 to your computer and use it in GitHub Desktop.
Red Playground
Red [
Title: "Go Fish"
Author: "Christian Ensel"
Date: 08-Feb-2022
]
ranks: [2 3 4 5 6 7 8 9 10 J Q K A]
suits: [♠ ♣ ♥ ♦]
rank-of: func ["Returns rank of card" card] [trim/with copy card "♠♣♥♦"]
start-game: func ["Initialise a new game"] [
context [
players: random [Human Computer] ;-- capitals come in handy when printing
pool: collect [foreach rank ranks [foreach suit suits [keep/only rejoin [rank suit]]]]
;-- deck of 52 cards
human: copy/deep [hand [] books []]
computer: copy/deep [hand [] books []]
books: copy pool
books: collect [loop length? ranks [keep/only take/part books 4]] ;-- books to be collected
random pool ;-- shuffle
loop 7 [ ;-- 7 cards for each player
move pool human/hand
move pool computer/hand
]
]
]
play-book?: function ["Plays out book from player's hand" player] [
hand: sort game/:player/hand
forall hand [
all [
books: find/only game/books copy/part hand 4 ;-- finding sorted 4 cards means we have a book
append/only game/:player/books book: take/part hand 4
remove books
print rejoin [player " completes book " book "."]
]
]
if empty? hand: game/:player/hand [move game/pool hand] ;-- restock empty hand
]
ask-rank: function ["Asks player to choose a rank"] [
ranks: unique collect [foreach card game/human/hand [keep rank-of card]]
until [
rank: ask "Wish rank: "
if "quit" = rank [halt]
find ranks rank
]
rank
]
holds-rank?: function ["Returns cards of requested rank, NONE otherwise" player rank] [
all [
not empty? cards: collect [foreach card hand: game/:player/hand [
if rank = rank-of card [keep card]
]]
cards
]
]
go-fish: function ["Send player to go fishing" player rank] [
until [
all [
card: take game/pool
append game/:player/hand card
if player = 'human [
print ["Fished card:" card]
]
]
any [
none? card
not equal? rank rank-of card
]
]
]
pick-rank: function ["Deep Blue grade computers rank choice calculation"] [
rank-of random/only game/computer/hand
]
play-game: function ["Main game loop" game] [
while [
set [other: player:] reverse game/players
play-book? player
not empty? hand: game/:player/hand
][
either player = 'human [
print [lf "Your hand:" sort hand]
rank: ask-rank
][
rank: pick-rank
print [lf "Computer's wish:" rank]
]
either cards: holds-rank? other rank [
print rejoin [other "gives " cards "."]
game/:other/hand: exclude game/:other/hand cards
append game/:player/hand cards
][
print [other "says: Go fish."]
go-fish player rank
]
ask "<ENTER>"
]
print [
"Computer's Books: " computer: game/computer/books lf
"Human's Books: " human: game/human/books lf lf
"You" pick ["win!" "loose."] greater? length? human length? computer
]
]
play-game game: start-game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment