Previous chapter ⋅ Full source code
First of all, we need to define available fleet - both players should know it:
submarine = Nonempty @List
`ha` Item Unit `ha` Next
`ha` Item Unit `ha` Next
`ha` Item Unit `ha` Last
destroyer = Nonempty @List
`ha` Item Unit `ha` Next
`ha` Item Unit `ha` Last
fleet = Nonempty @List @Ship
`ha_` Item `hv` by submarine `ha_` Next
`ha_` Item `hv` by destroyer `ha_` Last `hv_` Unit
Desperately trying to eliminate your opponent fleet we shouldn’t forget to stop by these reasons:
- Reach the board edge - so there are no adjacent tiles
- Fleet is defeated and you won already - congratulations!
- Internal error - the ship you bomb is not in the fleet
type Result = Unit `S` Unit `S` Ship
pattern Verge e = This (This e)
pattern Smash e = This (That e)
pattern Fault e = That e
As before, for the sake ob simplicity in this chapter we will not choose where to shoot first - we already have probability distribution though, but it’s better to check if basic agorithm is working: by having stateful fleet, bombing target and a playing board we should try to frame an enemy ship by extending a sliding window until we meet a board edge:
process = enter @(State `WR_` Target `P` Fleet `P` Board Cell `JNT_` Reach Result)
`yuk__` New `ha` State `hv__` Event `ha` extend `hv` by Fore `ha_` Scope `hv` at @(Board Cell)
`yok__` Try `ha__` Reach @Result `ha` Verge `la` Continue `ha` this @Tile
So, a tile could be either contain Idle
or Ship
, let’s focus on the latter first since it seems easier - we don’t need to change the fleet content:
pursuit = enter @(State `WR_` Target `P` Fleet `P` Board Cell `JNT_` Reach Result)
`yuk___` State `ho` New `hv___` Event `hv_` hit `ha_` Scope `hv` at @Target
`yuk___` State `ho` Old `hv___` Event `hv` auto `ha_` Scope `hv` at @Fleet
- If there is
Ship
tile and curret target is empty - initialize a new bombing ship (intro) - If there is
Ship
tile and there is a ship we are bombing - pushing a ship tile to the target
hit = auto `ha` Some @Ship `ha__` None @Ship `hu_` intro `hv` Unit `la` push `hv` Unit `ho` that
- If there is
Idle
tile and curret target is empty: do nothing - If there is
Idle
tile and there is a ship we are bombing - unstock it from a shared fleet
Work in progress… come back later, I’ll finish this chapter soon.