From 3c17082bd73f3623fe0680393b1b96938d63be30 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Sat, 23 Nov 2019 23:39:12 -0500 Subject: Basic AI --- .../me/robbyzambito/othello/game/AIPlayer.scala | 12 ++++++++ .../scala/me/robbyzambito/othello/game/Game.scala | 23 +++----------- .../me/robbyzambito/othello/game/Player.scala | 15 +++------ .../me/robbyzambito/othello/game/UserPlayer.scala | 36 ++++++++++++++++++++++ 4 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 src/main/scala/me/robbyzambito/othello/game/AIPlayer.scala create mode 100644 src/main/scala/me/robbyzambito/othello/game/UserPlayer.scala diff --git a/src/main/scala/me/robbyzambito/othello/game/AIPlayer.scala b/src/main/scala/me/robbyzambito/othello/game/AIPlayer.scala new file mode 100644 index 0000000..5c3efc6 --- /dev/null +++ b/src/main/scala/me/robbyzambito/othello/game/AIPlayer.scala @@ -0,0 +1,12 @@ +package me.robbyzambito.othello.game + +case class AIPlayer(override val color: Position) extends Player(color) { + + override def nextMove(board: Board): Move = { + println(s"$this moving...") + Thread.sleep(500L) + println() + possibleMoves(board).head + } + +} diff --git a/src/main/scala/me/robbyzambito/othello/game/Game.scala b/src/main/scala/me/robbyzambito/othello/game/Game.scala index ea8c382..4c0e7a2 100644 --- a/src/main/scala/me/robbyzambito/othello/game/Game.scala +++ b/src/main/scala/me/robbyzambito/othello/game/Game.scala @@ -1,8 +1,6 @@ package me.robbyzambito.othello.game import scala.annotation.tailrec -import scala.io.StdIn -import scala.util.Try /** * Represents the state of the game. @@ -66,23 +64,8 @@ case class Game(board: Board, def takeTurn: Game = { println(s"${this}\n") - def getPos: (Int, Int) = { - val rowCount = Iterator.continually( - Try(StdIn.readLine(s"Enter the row to move for ${currentPlayer}: ").toInt) - ).dropWhile(_.isFailure).next().get - val colCount = Iterator.continually( - Try(StdIn.readLine(s"Enter the col to move for ${currentPlayer}: ").toInt) - ).dropWhile(_.isFailure).next().get + val move = currentPlayer.nextMove(board) - (rowCount, colCount) - } - - val possibleMoves = currentPlayer.possibleMoves(board) - val pos = Iterator.continually(getPos) - .dropWhile(!possibleMoves.map(m => (m.rowCount, m.colCount)).contains(_)) - .next() - - val move = possibleMoves.find(m => m.rowCount == pos._1 && m.colCount == pos._2).get this.copy(board = move(board, currentPlayer), turnCount = turnCount + 1) } @@ -93,5 +76,7 @@ case class Game(board: Board, } object Game { - def apply(): Game = new Game(Board.init(), List(Player(Position.WHITE), Player(Position.BLACK))) + def apply(players: List[Player]): Game = new Game(Board.init(), players) + + def apply(): Game = Game( List(UserPlayer(Position.WHITE), AIPlayer(Position.BLACK)) ) } \ No newline at end of file diff --git a/src/main/scala/me/robbyzambito/othello/game/Player.scala b/src/main/scala/me/robbyzambito/othello/game/Player.scala index 581d2df..7abf3d2 100644 --- a/src/main/scala/me/robbyzambito/othello/game/Player.scala +++ b/src/main/scala/me/robbyzambito/othello/game/Player.scala @@ -1,18 +1,10 @@ package me.robbyzambito.othello.game -import me.robbyzambito.othello.game.Position.Position - 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 Player(color: Position) { +abstract class Player(val color: Position) { + def nextMove(board: Board): Move + def canMove(board: Board): Boolean = possibleMoves(board).nonEmpty @@ -97,4 +89,5 @@ case class Player(color: Position) { case Position.BLACK => "Black" case _ => "Default" } + } diff --git a/src/main/scala/me/robbyzambito/othello/game/UserPlayer.scala b/src/main/scala/me/robbyzambito/othello/game/UserPlayer.scala new file mode 100644 index 0000000..dc1c84c --- /dev/null +++ b/src/main/scala/me/robbyzambito/othello/game/UserPlayer.scala @@ -0,0 +1,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 + } + +} -- cgit v1.2.3