Newer
Older
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package battleship.core
import battleship.core.models._
import battleship.utils.io._
import scala.annotation.tailrec
import scala.util.Random
object Main extends App {
val gameConfig = GameConfig(GameConfig.DEFAULT)
if (gameConfig.gridSize > 10) {
GameDisplay.gridTooBig()
System.exit(1)
}
GameDisplay.clear()
GameDisplay.introduction()
val randoms = Seq[Random](new Random(), new Random())
/**
*
* @param gameType
* @param numberOfGames
* @param randoms
* @param shipConfig
* @return
*/
def initGameStates(numberOfGames: Int, randoms: Seq[Random], gameConfig: GameConfig): Set[GameState] = {
GameDisplay.choseYourName(1)
val namePlayer: String = PlayerInputs.choseName()
val player: HumanPlayer = HumanPlayer.createPlayer(namePlayer, randoms(0), gameConfig.shipsConfig, gameConfig.gridSize)
val ia: WeakIAPlayer = WeakIAPlayer.generateIA(1, randoms(1), gameConfig.shipsConfig, gameConfig.gridSize)
Set(GameState(player, ia, numberOfGames, 1))
}
/**
*
* @param gameState
* @param shipsConfig
* @return
*/
@tailrec
def mainLoop(gameState: GameState, gameConfig: GameConfig): (Player, Player) = {
val currentPlayer = gameState.currentPlayer
val opponent = gameState.opponent
val isCurrentPlayerHuman: Boolean = currentPlayer.isInstanceOf[HumanPlayer]
val winner = gameState.isThereAWinner()
if (winner.isEmpty) {
if (isCurrentPlayerHuman) {
GameDisplay.clear()
PlayerDisplay.show(currentPlayer, opponent)
GridDisplay.showPlayerGrid(currentPlayer.ships, opponent.shots.keys.toSeq, gameConfig.gridSize)
GridDisplay.showOpponentGrid(currentPlayer.shots, gameConfig.gridSize)
PlayerDisplay.shoot()
}
val target: (Int, Int) = currentPlayer.shoot(gameConfig.gridSize)
val (newOpponent, touched, shipSunk): (Player, Boolean, Option[Ship]) = opponent.receiveShoot(target)
if (isCurrentPlayerHuman) {
if (shipSunk.isDefined) PlayerDisplay.sunk(shipSunk.get.name) else if (touched) PlayerDisplay.touched() else PlayerDisplay.notTouched()
GameDisplay.endOfTurn()
PlayerInputs.pressAKey()
}
val newCurrentPlayer = currentPlayer.didShoot(target, didTouch = touched)
mainLoop(GameState(newOpponent, newCurrentPlayer, gameState.numberOfGames, gameState.gameCount), gameConfig)
} else {
val addedVictoryWinner = winner.get.addVictory()
val continue: Boolean = if (currentPlayer.isInstanceOf[HumanPlayer] || opponent.isInstanceOf[HumanPlayer]) {
GameDisplay.winner(addedVictoryWinner.name)
GameDisplay.continue()
PlayerInputs.continue() != "q"
} else {
gameState.gameCount < gameState.numberOfGames
}
if (continue) {
GameDisplay.clear()
GameDisplay.gameNumber(gameState.gameCount + 1, gameState.numberOfGames)
mainLoop(GameState(currentPlayer.reset(gameConfig.shipsConfig, gameConfig.gridSize), addedVictoryWinner.reset(gameConfig.shipsConfig, gameConfig.gridSize), gameState.numberOfGames, gameState.gameCount + 1), gameConfig)
} else {
(currentPlayer, addedVictoryWinner)
}
}
}
val gameStates = initGameStates(100, randoms, gameConfig)
val results = gameStates.map(gameState => {
GameDisplay.gameNumber(gameState.gameCount, gameState.numberOfGames)
mainLoop(gameState, gameConfig)
})
GameDisplay.end(results)
}