Scalaでじゃんけんのコンソールアプリをつくってみた。
Scalaは半年ぐらいまえからコップ本などを(途中まで)読んだりして勉強していたのだが、Windows8 + IntelliJ Idea + Scalaの環境がどうにもうまいこと動作せずやる気をなくして放り出していた。
Windows7でアレば難なく動作する環境が用意できるので、それで今回は久しぶりにScalaで書いてみた。とってもひどいコードだと思うけどあえて晒してみる。
ではぽろんちょ。
import scala.util.Random object HelloWorld { def main(args: Array[String]) { println("Hello Scala World!") val input = readLine("Input>") println( "input str is -> "+input ) val r = new Random val cpuHand = r.nextInt( 2 ) val handStr = Array( "ぐー", "ちょき", "ぱー" ) var yourHand = 0; input match { case "g" => { yourHand = 0; } case "c" => { yourHand = 1; } case "p" => { yourHand = 2; } case _ => println( "INVALID input" ) } println( "You:"+handStr(yourHand)+" CPU:"+handStr(cpuHand) ) if( yourHand==cpuHand ) println("Even...") else if( (yourHand+1)%3==cpuHand ) println("You WIN!"); else if( (cpuHand+1)%3==yourHand ) println("CPU WIN!"); else println("??????"); } }