Previous part: Battleship player agent - 2

So, we need to reconsider our probability distribution calculation so it should be aware of surroundings:

out = None `hu` by Continue
 `la__` Nail `hu` by Interrupt
   `la` Miss `hu` by Continue
   `la` Mist `hu` by Continue

If a neighbor cell was already hit (so we know there is a ship there), we should stop doing any calculations since we know it’s impossible to have adjusted ship cells. Since we still operate in one dimensional, we should check both left and right adjusted cells:

 `yuk____` State `ho` Old
 `hv_____` Event `hv` pop @List
 `ha___'he` Scope `hv` at @(Shafted List Mark)
   `ho_'he` Scope `hv` at @(Reverse List Mark)
   `ho_'he` Scope `hv` it @(List Mark)
 `yok____` Check `ha` out
 `yuk____` State `ho` Old
 `hv_____` Event `hv` pop @List
 `ha___'he` Scope `hv` at @(Shafted List Mark)
   `ho_'he` Scope `hv` at @(Forward List Mark)
   `ho_'he` Scope `hv` it @(List Mark)
 `yok____` Check `ha` out

After checking surroundings, we need to check the sliding window itself:

 `yuk____` State `ho` Old
 `hv_____` Event `hv` auto
 `ha___'he` Scope `hv` at @(List Mark)
 `yok____` Check `ha` inner
 `yok____` State `ho` New
 `ha_____` Event `ha` switch
 `ho_'ha'he` Scope `hv` at @(List Mark)

The algorithm hasn’t been changed this the previous part of this tutorial:

inner ship = ship
 `yokl` Run `ho` Forth
 `ha__` Bang `ho` Nail `ho` Shot `ho` Valid
   `la` Sunk `ho` Nail `hu` Error Unit
   `la` Miss `ho` Shot `hu` Error Unit
   `la` (+1) `ho` Mist `ho` Valid
mount board = Same `hu` board 
 `la` is `ho'he` that @Opponent
 `li` match `he'he'hv` board

We just need to slightly change chance expression:

chance = enter @(State `WR` Sliding List Mark)
 `yuk___` State `ho` New `hv____` Event `hv_` auto `ho'ho` mount
 `yuk___` State `ho` New `hv____` Event `ha` slide `hv` by Future
 `yok___` Retry `ha` Perhaps `ha` not

Finally, let’s check if it works correctly.

We need to designate our shooting results somehow:

[-]: Miss
[+]: Bang
[#]: Sunk

If we hit some ship, there is still a chance to sink it by hitting surroundings:

2 4 5 4 2 1 + + 1 0

But if there is a sunk ship it means there could be no ships arround:

2 4 5 4 2 0 # # 0 0

It seems it’s finally time to shoot!

Full source code is available here.

Continue this tutorial