Skip to content

Instantly share code, notes, and snippets.

@zodiki
Created April 30, 2011 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zodiki/950076 to your computer and use it in GitHub Desktop.
Save zodiki/950076 to your computer and use it in GitHub Desktop.
Final Project Scala Meetup (demonstrates equipment slots)
trait Described {
def description: String
}
trait Item {
def name: String
def encumberance: Int
def durability: Int
def cost: Int
def slots: Double
}
trait Weapon {
def damage : Int
}
trait Cover {
def defense : Int
}
trait Potion {
def power: Int
}
sealed trait Equippable
sealed trait DollSlot
trait ChestSlot extends DollSlot
trait ArmSlot extends DollSlot
trait LeftArmSlot extends ArmSlot
trait RightArmSlot extends ArmSlot
trait BothArmSlot extends DollSlot
case class TwoHandedSword(name:String, encumberance:Int=40, durability:Int=100,cost:Int=100, slots:Double=3, damage:Int=2) extends Item with Weapon with BothArmSlot{
}
case class Armor(name:String, encumberance:Int=50, durability:Int=100, cost:Int=100,slots:Double=2, defense:Int=50) extends Item with Cover with ChestSlot {
}
case class Sword(name:String, encumberance:Int=20, durability:Int=100,cost:Int=50, slots:Double=2, damage:Int=1) extends Item with Weapon with RightArmSlot{
}
case class Shield (name:String, encumberance:Int=20, durability:Int=200, cost:Int=50, slots:Double=4, defense:Int=25) extends Item with Cover with LeftArmSlot {
}
case class HealingPotion ( name:String, encumberance:Int=1, durability:Int=1, cost:Int=10, slots:Double=0.2, power:Int=10)extends Item with Potion {
}
case class Inventory (items: List[Item] = Nil){
def addItem(item: Item, qty: Int = 1) : Inventory = qty match {
case 0 => this
case 1 => this.copy(item::items)
case num: Int if num > 1 => this.copy(items.padTo(qty + items.size, item))
case _ => this
}
def removeItem(item:Item): Inventory = {
this
}
}
case class PaperDoll (chest: Option[ChestSlot] = None,rightArm: Option[RightArmSlot] = None, leftArm: Option[LeftArmSlot] = None, bothArm: Option[BothArmSlot] = None){
def equip(item: DollSlot) : PaperDoll = item match {
case c:ChestSlot => this.copy(Some(c), rightArm, leftArm, bothArm)
case ra:RightArmSlot => this.copy(chest, Some(ra),leftArm, None)
case la:LeftArmSlot => this.copy(chest, rightArm, Some(la), None)
case ba:BothArmSlot => this.copy(chest, None, None, Some(ba))
case _ => this
}
}
trait Creature {
def name : String
def description: String
def health: Int
def killable: Boolean
def currentHealth: Int
}
case class Player( name:String, inventory:Inventory = Inventory(), paperDoll:PaperDoll = PaperDoll()) extends Creature {
val description = "PLAYER"
val health = 200
val currentHealth = 200
val killable = true
def dropItem(item:Item):Player = {
this.copy (inventory = inventory.removeItem(item))
}
def equipWeapon(weapon: DollSlot): Player = weapon match {
case w:Weapon => this.copy(paperDoll = paperDoll.equip(weapon))
case _ => {
println("That's not a weapon")
this
}
}
def equipArmor(armor: DollSlot): Player = armor match {
case a:Armor => this.copy(paperDoll = paperDoll.equip(armor))
case _ => {
println("That's not armor")
this
}
}
def equipShield(shield: DollSlot): Player = shield match {
case a:Shield => this.copy(paperDoll = paperDoll.equip(shield))
case _ => {
println("That's not a shield")
this
}
}
}
sealed trait Action extends (World => World) with Described
case object Exit extends Action {
def description = "Exit the game"
def apply(old: World): World = {
println("Are you sure you want to exit the game? (y/n)")
readChar().toLower match {
case 'y' => System.exit(0)
}
old
}
}
case object EquipArmor extends Action {
def description = "Equip Armor "
def apply(old: World): World = {
import old.player
old.copy(
player = player.equipArmor(Armor("Leather"))
)
}
}
case object EquipSword extends Action {
def description = "Equip Sword "
def apply(old: World): World = {
import old.player
old.copy(
player = player.equipWeapon(Sword("Rapier"))
)
}
}
case object Equip2HSword extends Action {
def description = "Equip 2H Sword "
def apply(old: World): World = {
import old.player
old.copy(
player = player.equipWeapon(TwoHandedSword("Bastard Sword"))
)
}
}
case object Strip extends Action {
def description = "Strip"
def apply(old: World): World = {
import old.player
old.copy(
player = player.copy(paperDoll= new PaperDoll())
)
}
}
case object EquipShield extends Action {
def description = "Equip Shield "
def apply(old: World): World = {
import old.player
old.copy(
player = player.equipShield(Shield("Steel Shield"))
)
}
}
case class World(
player: Player
) {
def actions: List[Action] = Exit :: EquipArmor :: EquipSword :: EquipShield :: Equip2HSword :: Strip :: Nil
}
object World {
def apply(name: String): World =
World(Player(
name = name
//health = Health.Max,
//location = Locations.GrassyFields,
//inventory = Container.empty
))
}
object Dungeon {
def main(args: Array[String]) {
println("Welcome to Armor Equipping")
println("What is your name? ")
val name = readLine()
println("Welcome, " + name)
val world = World(name)
run(world)
}
def run(world: World): World = {
println("What would you like to do now? ")
val actions = world.actions
actions.zipWithIndex.foreach {
case (action, index) =>
println(index + ". " + action.description)
}
val newWorld = (readInt() match {
case choice if (choice < 0 || choice >= actions.length) =>
println("That is not a choice you have, " + world.player.name)
world
case choice =>
val chosenAction = actions(choice)
println("You have chosen to: " + chosenAction.description)
chosenAction(world)
})
println(newWorld.player.paperDoll)
run(newWorld)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment