Created
March 28, 2013 20:35
-
-
Save miere/5266554 to your computer and use it in GitHub Desktop.
A Small game used to illustrate basics of Scala Language.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.contaazul.api.pub.client | |
import scala.util.Random | |
class Character( | |
var name: String, | |
var lastName: String = "No Name") { | |
val location = new Location | |
var health = 100 | |
} | |
class Location { | |
var x: Int = 0 | |
var y: Int = 0 | |
} | |
trait Runner { | |
self: Character => | |
def walk(x: Int) = { | |
this.location.x += x | |
} | |
} | |
trait Jumper { | |
self: Character => | |
def jump(height: Int) = { | |
this.location.y += height | |
} | |
} | |
trait Warrior { | |
self: Hero => | |
def hit(enemy: Hero) = { | |
var damage = measureDamage(enemy) | |
enemy.health -= damage | |
printDamage(enemy, damage) | |
printHealth(enemy) | |
} | |
def measureDamage(enemy: Hero) = | |
weapon.streight | |
def printHealth(enemy: Hero) = | |
if (enemy.health <= 0) | |
println(s"${enemy.name} was killed") | |
def printDamage(enemy: Hero, damage: Int) = | |
println(s"${enemy.name} was hurted in $damage") | |
} | |
abstract class Hero(name: String) extends Character(name) | |
with Runner with Jumper with Warrior { | |
var weapon: Weapon | |
def whoAmI() = | |
s"Hello, I'm $name." + | |
s"I'm using the weapon ${weapon.name}" | |
def whereIAm() = s"I'm ${location.x} from the start point,\n" + | |
s"but I'm ${location.y} meters height." | |
} | |
class Weapon( | |
var name: String, | |
var streight: Int = 1) {} | |
class MoneyWeapon extends Weapon("Dinheiro", 25) | |
class AK47Weapon extends Weapon("AK-47", 75) | |
class TW(name: String = "TW") extends Hero(name) { | |
var weapon:Weapon = new AK47Weapon | |
} | |
class Laercio extends Hero("Laercio") { | |
var weapon:Weapon = new MoneyWeapon | |
} | |
class Game { | |
var tw: Hero = new TW | |
var laercio = new Laercio | |
def main(args: Array[String]) { | |
while ( !weHaveAWinner ) | |
doAttack | |
showTheWinner | |
} | |
def weHaveAWinner:Boolean = | |
loose( tw ) || loose( laercio ) | |
def loose( player:Hero ):Boolean | |
= player.health <= 0 | |
def doAttack() = { | |
var r = new Random | |
if ( r.nextInt(100) < 50 ) | |
tw hit laercio | |
else | |
laercio hit tw | |
} | |
def showTheWinner() = { | |
var player = laercio.health <=0 match { | |
case true => tw | |
case false => laercio | |
} | |
println( s"${player.name} Win!" ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@thiagolenz, pega este joguinho...