Game specifications

Specifications for data used to represent players and the overall card game.

Specification definition so far

(ns practicalli.card-game
  (:require [clojure.spec.alpha :as spec]
            [clojure.spec.gen.alpha :as spec-gen]
            [clojure.spec.test.alpha :as spec-test]))

(spec/def ::suit #{:clubs :diamonds :hearts :spades})
(spec/def ::rank (into #{:jack :queen :king :ace} (range 2 11)))
(spec/def ::playing-card (spec/tuple ::rank ::suit))
(spec/def ::dealt-hand (spec/* ::playing-card))

Player specifications

The player name is a very simple spec.

(spec/def ::name string?)

Score will keep a running total of a player score across games, again a simple integer value.

(spec/def ::score int?)

A player is represented by a hash-map that contains their name, score and the hand they are currently dealt. The hand is a collection of tuples representing a playing card.

(spec/def ::player
            :req [::name ::score ::dealt-hand]))

Game specifications

A card game has a deck of 52 cards, one card for each combination of suit and rank.

The size of the card deck changes over the course of a game, so the deck can contain any number of cards. The deck must contain only cards to be valid.

(spec/def ::card-deck (spec/* ::playing-card))

At this stage in the design, a card game can have any number of players

(spec/def ::players (spec/* ::player))

A game is represented by a hash-map with a collection of players and a card deck

(spec/def ::game (spec/keys :req [::players ::card-deck]))

Generating random player data

(spec-gen/generate (spec/gen ::player))

Generating random game data

(spec-gen/generate (spec/gen ::game))

results matching ""

    No results matching ""