Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Created October 27, 2012 12:57
Show Gist options
  • Save volgar1x/3964548 to your computer and use it in GitHub Desktop.
Save volgar1x/3964548 to your computer and use it in GitHub Desktop.
package hello
import java.util.Scanner
import java.util.Random
import com.sun.javaws.exceptions.InvalidArgumentException
import java.util.HashMap
/**
* Created with IntelliJ IDEA.
* User: Blackrush
* Date: 27/10/12
* Time: 12:43
*/
///////
// UTILS
///////
val random = Random(System.nanoTime())
val stdin = Scanner(System.`in`)
fun IntRange.rand(): Int = random.nextInt(this.end - this.start) + this.start
fun prompt(msg : String? = null): String {
if (msg != null) {
print(msg + " ")
}
return stdin.nextLine() ?: ""
}
fun String?.int(): Int = Integer.parseInt(this)
fun String?.bool(): Boolean = this?.equalsIgnoreCase("oui") ?: false
///////
// GAMES
///////
trait Game {
fun run()
}
abstract class GameFactory {
abstract val name : String
abstract fun create(): Game
class object {
private val games = HashMap<String, GameFactory>()
fun <T : Game> register() where class object T : GameFactory {
games[T.name] = T
}
fun byName(name : String): GameFactory? = games[name]
}
}
class PlusOuMoins(val min : Int, val max : Int) : Game {
class object : GameFactory() {
override val name: String = "plus ou moins"
override fun create(): Game = PlusOuMoins(
prompt("entrez le minimum").int(),
prompt("entrez le maximum").int()
)
}
override fun run() {
val num = (min..max).rand()
var input = 0
var rounds = 0
while (input != num) {
rounds++
input = prompt("entrez un nombre").int()
if (input < num) {
println("c'est plus !")
} else if (input > num) {
println("c'est moins !")
} else {
println("bien joué ! tu as gagné en $rounds coups !")
}
}
}
}
class PlusOuMoins2(var min : Int, var max : Int) : Game {
class object : GameFactory() {
override val name : String = "plus ou moins2"
override fun create(): Game = PlusOuMoins2(
prompt("entrez le minimum").int(),
prompt("entrez le maximum").int()
)
}
var success : Boolean = false
fun input(num : Int) {
var valid = true
do {
when (prompt("est-ce \"$num\" ?")) {
"oui" -> success = true
"plus" -> min = num + 1
"moins" -> max = num
else -> {
println("vous devez entrer oui/plus/moins")
valid = false
}
}
} while (!valid)
}
fun algo(): Int = (min + max) / 2
override fun run() {
success = false // if player re-run
var rounds = 0
while (!success) {
rounds++
val num = algo()
input(num)
}
println("j'ai gagné ! en $rounds coups !")
}
}
///////
// EXECUTION
///////
fun String?.game(): GameFactory? = if (this != null) GameFactory.byName(this) else null
fun main(args: Array<String>) {
GameFactory.register<PlusOuMoins>()
GameFactory.register<PlusOuMoins2>()
var factory : GameFactory? = null
var game : Game? = null
do {
if (factory == null || prompt("souhaitez-vous changer de jeu ?").bool()) {
factory = prompt("choisissez votre jeu").game()
game = factory?.create()
} else if (prompt("souhaitez-vous garder les mêmes paramètres ?").bool()) {
game = factory!!.create()
}
game!!.run()
} while (prompt("souhaitez-vous continuer ?").bool())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment