Tracks
/
Factor
Factor
/
Exercises
/
Role Playing Game
Role Playing Game

Role Playing Game

Learning Exercise

Introduction

A tuple bundles several named values together into one object — what other languages call a record, struct, or class. Each tuple class has a fixed set of named slots and Factor automatically generates getters and setters for them.

Defining a tuple

TUPLE: point
    { x initial: 0 }
    { y initial: 0 } ;

Each slot is declared { name initial: default-value }. Without an initial: clause the default is f.

Constructing a tuple

The simplest constructor is new:

point new .
! => T{ point }

boa ("by order of arguments") fills slots in declaration order from the data stack:

3 4 point boa .
! => T{ point { x 3 } { y 4 } }

The literal syntax T{ class { slot value } … } lets you construct a tuple with a subset of slots set, and prints the same way:

T{ point { x 3 } } .
! => T{ point { x 3 } }

When a slot's value matches its default, the printer omits it.

Reading a slot

Factor generates a getter slot>> for each slot. Getters live in accessors:

T{ point { x 3 } { y 4 } } x>> .
! => 3

Updating a slot

Setters are written >>slot and mutate the tuple in place:

T{ point { x 3 } { y 4 } } 10 >>x .
! => T{ point { x 10 } { y 4 } }

To leave the original alone, clone first:

: shifted ( pt -- pt' )
    clone 10 >>x ;

change-slot takes a quotation that maps the current value to the new one:

: shift-x ( pt dx -- pt )
    [ + ] curry change-x ;

change-x is also mutating, so clone before calling it if you want to preserve the original.

Instructions

Sarah is working on a new role-playing game and needs your help with the player mechanics.

1. Define the player tuple

Define a tuple class player with four slots and default values:

slot default
name f
level 0
health 100
mana f

Once defined, you can build a player as a literal:

T{ player { name "Merlin" } { level 5 } } .
! => T{ player { name "Merlin" } { level 5 } }

2. Introduce the player

Define introduce to return the player's name. Stealthy players hide their name (name is f) and should be introduced as "Mighty Magician".

T{ player { name "Merlin" } } introduce .
! => "Merlin"

T{ player { health 8 } } introduce .
! => "Mighty Magician"

3. Revive a fallen player

Define revive to return a new player with health set to 100 and mana set to 100, but only if the player's health is 0. If the player is still alive, return f.

T{ player { name "Merlin" } { health 0 } } revive .
! => T{ player { name "Merlin" } { health 100 } { mana 100 } }

T{ player { health 50 } } revive .
! => f

4. Take damage

Define take-damage to return a new player whose health has been reduced by the given amount, never falling below 0.

T{ player { health 100 } } 30 take-damage .
! => T{ player { health 70 } }

T{ player { health 5 } } 30 take-damage .
! => T{ player { health 0 } }
Edit via GitHub The link opens in a new window or tab
Factor Exercism

Ready to start Role Playing Game?

Sign up to Exercism to learn and master Factor with 47 concepts163 exercises, and real human mentoring, all for free.