summaryrefslogtreecommitdiff
path: root/src/main/scala/me/robbyzambito/othello/game/UserPlayer.scala
blob: 4ecf1e18ee4e171ccf793a0e6a122f8aba315319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package me.robbyzambito.othello.game

import scala.io.StdIn
import scala.util.Try

/**
 * Allows for the User to input their moves.
 *
 * Written by Robby Zambito
 * Written on 11/20/2019
 * Targeting Scala 2.13.1
 *
 * See [[Player]]
 */
case class UserPlayer(override val color: Position) extends Player(color) {

  def nextMove(board: Board): Move = {
    def getPos: (Int, Int) = {
      val rowCount = Iterator.continually(
        Try(StdIn.readLine(s"Enter the row to move for ${this}: ").toInt)
      ).dropWhile(_.isFailure).next().get
      val colCount = Iterator.continually(
        Try(StdIn.readLine(s"Enter the col to move for ${this}: ").toInt)
      ).dropWhile(_.isFailure).next().get

      println()

      (rowCount, colCount)
    }

    val mvs = possibleMoves(board)
    val pos = Iterator.continually(getPos)
      .dropWhile(!mvs.map(m => (m.rowCount, m.colCount)).contains(_))
      .next()

    mvs.find(m => m.rowCount == pos._1 && m.colCount == pos._2).get
  }

}