1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rustとscalaとkotilnの勉強 数当てゲーム(scala) 2回目

Last updated at Posted at 2018-06-26

TRPLの数当てゲーム
scalaがあんまりだったので書き直した

とりあえずBreaksは使わなくなったが・・・
scalaの省略ルールが難しすぎて覚えられない

import scala.util.Random
import java.security.SecureRandom
import scala.util.{Try, Success, Failure}
object Main{
	def main(args:Array[String]){
		val secret_number = new Random(new SecureRandom()).nextInt(99)+1
		println(s"秘密の数字は次の通り:$secret_number")
		println("数をあててごらん")
		println("ほら、予想を入力してね")
		gen().map(t=>t.map(s=>s.toInt))
			.filter(_.isSuccess)
			.map(s=>s.get)
			.map(i=>
				i match{
					case i if i == secret_number => i
					case i if i < secret_number => println("ちいさすぎる"); i
					case i if i > secret_number => println("おおきすぎる"); i
					})
			.filter(_==secret_number)
			.head
		println("正解")
	}

	def gen():Stream[Try[String]]={
		import scala.io.StdIn
		import scala.collection.immutable.Stream
		return Stream.cons(Try(StdIn.readLine),gen)
	}
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?