Skip to content

Instantly share code, notes, and snippets.

@World2LiveBy
Created December 12, 2019 05:53
Show Gist options
  • Save World2LiveBy/f6a7aaecd7728ecfea5daa03159eb30d to your computer and use it in GitHub Desktop.
Save World2LiveBy/f6a7aaecd7728ecfea5daa03159eb30d to your computer and use it in GitHub Desktop.
#lang racket
;;small helper func
(define I
(λ (x) x))
(define succ
(λ (n f x) (f ((n f) x))))
(define empty? null?)
;; doNumberOfTimes, where n is the number, and f(x) is the function
;;ex: (doN 3 f x) = (f (f (f x)))
(define (doN n f x)
(if (equal? n 1) (f x)
(doN (- n 1) f (f x))))
;;basic data structures
;; (card name, cost . (effect list))
(define (card name cost effect_list)
(list name cost effect_list))
(define (print_name card)
(if (null? card) (void) (displayln (car card))))
(define (hand card_list)
(card_list))
(define (deck gamestate)
(car gamestate))
(define (move_card x from to)
;;where x is selector function to seperate the from list (moving . staying)
;;returns both from and to as the pair (from . to)
(cons (cdr (x from))
(cons (car (x from)) to)))
(define (movetopcard card_list to) ;;(topcard . restoflist)
(move_card I card_list to))
(define draw ;;draws a card from deck to hand where (deck.hand) is a pair
(λ (g) (if (null? (car g)) (gameover) (movetopcard (car g) (cdr g)))))
(define (print_card_list card_list)
(if (null? card_list) (void)
(begin
(print_name (car card_list))
(print_card_list (cdr card_list)))))
(define print_gamestate
(λ (x) (begin
(displayln "Deck:")
(if (null? (car x)) (displayln "Empty")
(print_card_list (car x)))
(displayln "Hand:")
(if (null? (cdr x)) (displayln "Empty")
(print_card_list (cdr x))))))
(define (gamestate deck hand)
(cons deck hand))
(define playcard
(λ (card gamestate) ((caddr card) gamestate)))
;;heres the good bit, iterate through the hand list,
;;passing the envirement, gamestate, through every card
(define (playhand gamestate)
(if (empty? (cdr gamestate)) gamestate
(playhand (playcard (cadr gamestate) (cons (car gamestate) (cddr gamestate))))))
(define gameover
(λ () (begin
(displayln "GAME OVER")
(error "overdrawn"))))
;;repl
(define gameloop
(λ (w) (playhand(draw w))))
;;----------------------------------------------------------------------
(define bolt (card "lightning bolt" 2 (λ (x) (and (displayln "zap!") x))))
(define divination (card "divination" 2 (λ (x) (doN 2 draw x)))) ;;draw 2 card
(define decklist (list divination
bolt
divination
bolt
bolt))
(define player1hand '())
(define test (gamestate decklist player1hand))
(print_gamestate test)
(displayln "--------------")
;;(print_gamestate (draw test))
;;(displayln "--------------")
;; ------------
;; |Lightning |
;; |Dolt |
;; |Deal 2 to |
;; |Random @ |
;; ------------
;;
;; repl print_gamestate(test)
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment