Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating logic of marking cells after the ship was killed #5

Merged
merged 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added app/release/app-release.aab
Binary file not shown.
94 changes: 83 additions & 11 deletions app/src/main/java/com/avs/sea/battle/battle_field/BattleField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,101 @@ class BattleField : BaseBattleField() {
printBattleField()
}

fun handleShot(coordinate: Coordinate?): Boolean {
fun handleShot(coordinate: Coordinate?): Pair<Boolean, ArrayList<Coordinate>> {
var isShipHit = false
var killedShipCoordinates: ArrayList<Coordinate> = ArrayList()
if (coordinate != null && coordinate.x in 0 until SQUARES_COUNT && coordinate.y in 0 until SQUARES_COUNT) {
if (battleField[coordinate.x][coordinate.y]?.getCellState() == CellState.EMPTY) {
battleField[coordinate.x][coordinate.y]?.setCellState(CellState.SHOT_FAILURE)
} else {
battleField[coordinate.x][coordinate.y]?.setCellState(CellState.SHOT_SUCCESS)
defineShipByCoordinate(coordinate)
isShipHit = true
val ship = getShipByCoordinate(coordinate)
ship?.let {
it.setShotSuccessState(coordinate)
if (it.isDead()) {
markNeighbours(ship)
killedShipCoordinates = getShipCoordinates(ship)
}
}
}
}
return isShipHit to killedShipCoordinates
}

private fun markNeighbours(ship: Ship) {
if (ship.getShipOrientation() == Orientation.VERTICAL) {
markVerticalNeighbours(ship)
} else {
markHorizontalNeighbours(ship)
}
}

private fun markHorizontalNeighbours(ship: Ship) {
for (cell in ship.getShipCells()) {
if (cell.getX() != 0) battleField[cell.getX() - 1][cell.getY()]?.setCellState(CellState.SHOT_FAILURE)
if (cell.getX() != battleField.size - 1) battleField[cell.getX() + 1][cell.getY()]?.setCellState(
CellState.SHOT_FAILURE
)
}
val fistCell = ship.getShipCells().first()
if (fistCell.getY() != 0) {
battleField[fistCell.getX()][fistCell.getY() - 1]?.setCellState(CellState.SHOT_FAILURE)
if (fistCell.getX() != 0) {
battleField[fistCell.getX() - 1][fistCell.getY() - 1]?.setCellState(CellState.SHOT_FAILURE)
}
if (fistCell.getX() != battleField.size - 1) {
battleField[fistCell.getX() + 1][fistCell.getY() - 1]?.setCellState(CellState.SHOT_FAILURE)
}
}
val lastCell = ship.getShipCells().last()
if (lastCell.getY() != battleField.size - 1) {
battleField[lastCell.getX()][lastCell.getY() + 1]?.setCellState(CellState.SHOT_FAILURE)
if (lastCell.getX() != 0) {
battleField[lastCell.getX() - 1][lastCell.getY() + 1]?.setCellState(CellState.SHOT_FAILURE)
}
if (lastCell.getX() != battleField.size - 1) {
battleField[lastCell.getX() + 1][lastCell.getY() + 1]?.setCellState(CellState.SHOT_FAILURE)
}
}
return isShipHit
}

private fun defineShipByCoordinate(coordinate: Coordinate) {
private fun markVerticalNeighbours(ship: Ship) {
for (cell in ship.getShipCells()) {
if (cell.getY() != 0) battleField[cell.getX()][cell.getY() - 1]?.setCellState(CellState.SHOT_FAILURE)
if (cell.getY() != battleField.size - 1) battleField[cell.getX()][cell.getY() + 1]?.setCellState(
CellState.SHOT_FAILURE
)
}
val fistCell = ship.getShipCells().first()
if (fistCell.getX() != 0) {
battleField[fistCell.getX() - 1][fistCell.getY()]?.setCellState(CellState.SHOT_FAILURE)
if (fistCell.getY() != 0) {
battleField[fistCell.getX() - 1][fistCell.getY() - 1]?.setCellState(CellState.SHOT_FAILURE)
}
if (fistCell.getY() != battleField.size - 1) {
battleField[fistCell.getX() - 1][fistCell.getY() + 1]?.setCellState(CellState.SHOT_FAILURE)
}
}
val lastCell = ship.getShipCells().last()
if (lastCell.getX() != battleField.size - 1) {
battleField[lastCell.getX() + 1][lastCell.getY()]?.setCellState(CellState.SHOT_FAILURE)
if (lastCell.getY() != 0) {
battleField[lastCell.getX() + 1][lastCell.getY() - 1]?.setCellState(CellState.SHOT_FAILURE)
}
if (lastCell.getY() != battleField.size - 1) {
battleField[lastCell.getX() + 1][lastCell.getY() + 1]?.setCellState(CellState.SHOT_FAILURE)
}
}
}

private fun getShipByCoordinate(coordinate: Coordinate): Ship? {
for (ship in ships) {
if (coordinate.x in ship.getRowCoordinates() && coordinate.y in ship.getColumnCoordinates()) {
ship.setShotSuccessState(coordinate)
break
return ship
}
}
return null
}

fun isGameOver(): Boolean {
Expand All @@ -94,12 +168,10 @@ class BattleField : BaseBattleField() {
return shipsCoordinates
}

fun getAllShipsCoordinates(): ArrayList<Coordinate> {
private fun getShipCoordinates(ship: Ship): ArrayList<Coordinate> {
val shipsCoordinates = arrayListOf<Coordinate>()
ships.forEach { ship ->
ship.getShipCells().forEach { cell ->
shipsCoordinates.add(cell.getCoordinate())
}
ship.getShipCells().forEach { cell ->
shipsCoordinates.add(cell.getCoordinate())
}
return shipsCoordinates
}
Expand Down
7 changes: 0 additions & 7 deletions app/src/main/java/com/avs/sea/battle/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.util.Log
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ContextThemeWrapper
Expand Down Expand Up @@ -118,12 +117,6 @@ class MainActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
}
}

private fun setTextColor(v: View, color: Int) {
if (v is TextView) v.setTextColor(
ContextCompat.getColor(this, color)
)
}

private fun showPopup(v: View?) {
val wrapper: Context = ContextThemeWrapper(this, R.style.PopupStyle)
if (v != null) {
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/com/avs/sea/battle/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ class MainViewModel : ViewModel() {

fun makeFireAsPerson() {
if (activePlayer == Player.PERSON) {
val isShipHit = computerBattleField.handleShot(_selectedByPersonCoordinate.value)
val shipState = computerBattleField.handleShot(_selectedByPersonCoordinate.value)
_selectedByPersonCoordinate.value = null
if (isShipHit) {
if (shipState.first) {
if (shipState.second.isNotEmpty()) {
_personFailShots.value = computerBattleField.getDotsCoordinates()
}
_status.value = R.string.status_shot_ship_again_text
_personSuccessfulShots.value = computerBattleField.getCrossesCoordinates()
if (computerBattleField.isGameOver()) {
Expand All @@ -127,12 +130,15 @@ class MainViewModel : ViewModel() {
private fun playAsComputer() {
val coordinate: Coordinate = shotManager.getCoordinateToShot()
_selectedByComputerCoordinate.value = coordinate
val isShipHit = personBattleField.handleShot(coordinate)
shotManager.handleShot(isShipHit)
if (isShipHit) {
val shipState = personBattleField.handleShot(coordinate)
shotManager.handleShot(shipState)
if (shipState.first) {
viewModelScope.launch {
delay(SECOND_IN_MILLIS)
_computerSuccessfulShots.value = personBattleField.getCrossesCoordinates()
if (shipState.second.isNotEmpty()) {
_computerFailShots.value = personBattleField.getDotsCoordinates()
}
if (personBattleField.isGameOver()) {
endGame(false)
} else {
Expand Down Expand Up @@ -161,7 +167,7 @@ class MainViewModel : ViewModel() {

private fun endGame(isPersonWon: Boolean) {
activePlayer = Player.NONE
_computerShips.value = computerBattleField.getAllShipsCoordinates()
_computerShips.value = computerBattleField.getShipsCoordinates()
if (isPersonWon) {
_endGameEvent.value = true to Player.PERSON
_status.value = R.string.status_game_over_you_win_text
Expand Down
38 changes: 15 additions & 23 deletions app/src/main/java/com/avs/sea/battle/main/ShotManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,17 @@ class ShotManager {
ONE_DECK_SHIP_SIZE, ONE_DECK_SHIP_SIZE, ONE_DECK_SHIP_SIZE, ONE_DECK_SHIP_SIZE
)

fun getShipsSize(): Int {
return ships.size
}
fun getShipsSize(): Int = ships.size

fun getBattleField(): BaseBattleField {
return battleField
}
fun getBattleField(): BaseBattleField = battleField

fun getFirstCell(): Cell {
return firstCell
}
fun getFirstCell(): Cell = firstCell

fun getSecondCell(): Cell {
return secondCell
}
fun getSecondCell(): Cell = secondCell

fun getThirdCell(): Cell {
return thirdCell
}
fun getThirdCell(): Cell = thirdCell

fun getFourthCell(): Cell {
return fourthCell
}
fun getFourthCell(): Cell = fourthCell

fun getCoordinateToShot(): Coordinate {
var coordinate = Coordinate()
Expand Down Expand Up @@ -162,17 +150,17 @@ class ShotManager {

fun getMaxCoordinate(list: MutableList<Coordinate>, orientation: Orientation): Coordinate {
return if (orientation == Orientation.VERTICAL) {
list.maxByOrNull { it.x }!!
list.maxBy { it.x }
} else {
list.maxByOrNull { it.y }!!
list.maxBy { it.y }
}
}

fun getMinCoordinate(list: MutableList<Coordinate>, orientation: Orientation): Coordinate {
return if (orientation == Orientation.VERTICAL) {
list.minByOrNull { it.x }!!
list.minBy { it.x }
} else {
list.minByOrNull { it.y }!!
list.minBy { it.y }
}
}

Expand Down Expand Up @@ -267,7 +255,8 @@ class ShotManager {
return firstCell.getCoordinate()
}

fun handleShot(shipHit: Boolean) {
fun handleShot(shipState: Pair<Boolean, ArrayList<Coordinate>>) {
val shipHit = shipState.first
if (firstCell.isState(EMPTY) || firstCell.isState(SHOT_FAILURE)) {
updateBattleField(shipHit, firstCell)
} else if (firstCell.isState(SHOT_SUCCESS)
Expand All @@ -294,6 +283,9 @@ class ShotManager {
markNeighbours(fourthCell)
}
}
if (shipState.second.isNotEmpty()) {
resetValuesAfterShipIsDead(shipState.second.size, shipState.second)
}
}

private fun markNeighbours(cell: Cell) {
Expand Down
Loading