Skip to content

Commit 3d9e31b

Browse files
authored
Merge pull request #13 from scala-academy/refactoring
some refactoring (mostly renaming)
2 parents 61401ca + 7b90310 commit 3d9e31b

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/main/scala/battleship/game/BoardState.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ case class BoardState(boats: Set[(Boat, BoatLocation)], history: Seq[Shot]) {
1919
BoardState(boats + location, history)
2020
}
2121

22-
def shoot(coordinate: Coordinate): BoardState = {
22+
def processShot(coordinate: Coordinate): BoardState = {
2323
val coordinateInHistory = history.exists{case (shotLocation, _) => shotLocation == coordinate}
2424
require(!coordinateInHistory, "Can't shoot at the same coordinate twice")
2525

src/main/scala/battleship/game/Game.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class Game(player1: Player, player2: Player, boardSize: Int, allBoats: Seq[Boat]
2727
def play: Player = {
2828
def playRecursively(players: (PlayerState, PlayerState)): Player = {
2929
val (currentPlayer, nextPlayer) = players
30-
if (currentPlayer.won) {
30+
if (currentPlayer.hasWon) {
3131
currentPlayer.player
3232
} else {
3333
val nextShot = currentPlayer.getNextShot(boardSize, currentPlayer.history)
34-
val newBoard = currentPlayer.shoot(nextShot)
34+
val newBoard = currentPlayer.processShot(nextShot)
3535
val updatedPlayers = (nextPlayer, (currentPlayer.player, newBoard))
3636
playRecursively(updatedPlayers)
3737
}
@@ -63,12 +63,12 @@ class Game(player1: Player, player2: Player, boardSize: Int, allBoats: Seq[Boat]
6363
* tuple (Player, BoardState) can be automatically converted to PlayerRepr. As a result, the below defined functions
6464
* are added to the scope of the tuple (Player, BoardState)
6565
*/
66-
implicit class PlayerRepr(playerAndBoard: (Player, BoardState)) {
66+
implicit class PlayerStateOps(playerAndBoard: (Player, BoardState)) {
6767
val (player, board) = playerAndBoard
6868

69-
def won: Boolean = board.allShipsSunk
69+
def hasWon: Boolean = board.allShipsSunk
7070

71-
def shoot(coordinate: Coordinate): BoardState = board.shoot(coordinate)
71+
def processShot(coordinate: Coordinate): BoardState = board.processShot(coordinate)
7272

7373
def history: Seq[((Int, Int), ShotResult)] = board.history
7474

src/test/scala/battleship/game/BoardStateSpec.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class BoardStateSpec extends WordSpec with Matchers {
2323

2424
"shoot" should {
2525
"throw an exception if a shot is made to a coordinate for the second time" in {
26-
val state = BoardState.empty.shoot(0, 0)
26+
val state = BoardState.empty.processShot(0, 0)
2727

2828
assertThrows[IllegalArgumentException] {
29-
state.shoot(0, 0)
29+
state.processShot(0, 0)
3030
}
3131
}
3232
"return an updated instance of BoardState with the shot included in the history" in {
3333
val state = BoardState.empty
3434

35-
val result = state.shoot(1, 1).shoot(3, 4)
35+
val result = state.processShot(1, 1).processShot(3, 4)
3636

3737
result should be(BoardState(Set.empty, Seq(((3, 4), Miss), ((1, 1), Miss))))
3838
}
@@ -57,10 +57,10 @@ class BoardStateSpec extends WordSpec with Matchers {
5757
val placement = BoatLocation(0, 0, true)
5858
val state = BoardState.empty
5959
.placeBoat(boat, placement)
60-
.shoot(0,0)
61-
.shoot(1,1)
62-
.shoot(1,0)
63-
.shoot(0,2)
60+
.processShot(0,0)
61+
.processShot(1,1)
62+
.processShot(1,0)
63+
.processShot(0,2)
6464

6565
state.shotsOnBoatSoFar(boat, placement) should be(2)
6666
}
@@ -80,14 +80,14 @@ class BoardStateSpec extends WordSpec with Matchers {
8080
"return Hit when a shot hits but does not sink for 2nd shot on size 3 boat" in {
8181
val state = BoardState.empty
8282
.placeBoat(Boat(3), BoatLocation(0, 0, true))
83-
.shoot(2,0)
83+
.processShot(2,0)
8484

8585
state.calculateShotResult(0,0) should be(Hit)
8686
}
8787
"return Sink when a shot hits for the x'th time on a x-size boat" in {
8888
val state = BoardState.empty
8989
.placeBoat(Boat(2), BoatLocation(0, 0, true))
90-
.shoot(1,0)
90+
.processShot(1,0)
9191

9292
state.calculateShotResult(0,0) should be(Sink(Boat(2)))
9393
}

0 commit comments

Comments
 (0)