summaryrefslogtreecommitdiff
path: root/src/main/scala/me/robbyzambito/othello/game/UserPlayer.scala
blob: dc1c84c4f498a667a7d815aeade1bf142dc09d00 (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
package me.robbyzambito.othello.game

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

/**
 * Represents a party which is partaking in the game.
 * Both the AI and the end user are instances of Player.
 *
 * Written by Robby Zambito
 * Written on 11/20/2019
 * Targeting Scala 2.13.1
 */
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

      (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
  }

}