This repository is my fork of James Powell’s battlegame challenge.
About the challenge
- The explainations about the game are copied from his repository.
About my solution
- You can find my solutions here:
battleserver.py
andbattleplayer.py
. - My “server” script implements perfectly what was asked by the exercise, and my “client” script implements a quite naive heuristic: it plays randomly and only tries to keep hitting a ship when it first hits it (my experiments showed it wins in about 60% to 75% lesser number of plays in comparison to the naive purely uniform strategy).
- Both scripts stand on the shoulder on giants: the Python standard library (
sys
,string.ascii_uppercase
,collections.OrderedDict
,pprint.pprint
), the Numpy package and the :sparkles: awesome docopt package for command-line handling. (see requirements.txt).
Documentation
Server
$ ./battleserver.py --help
Battle Server.
Usage:
battleserver.py [--carrier=<x,y,dir>] [--battleship=<x,y,dir>] [--cruiser=<x,y,dir>] [--submarine=<x,y,dir>] [--destroyer=<x,y,dir>] [--random] [--size=<xy>] (--show | --play)
battleserver.py (-h | --help)
battleserver.py --version
Options:
-h --help Show this screen.
--version Show version.
--show Print the board.
--play Let you play a "one player" game interactively.
--size=<xy> Set size of the board [default: 5,5].
--random Set every ship to a random position.
--carrier=<x,y,dir> Place ship Carrier at position x,y and direction (h or v) [default: 1,1,h].
--battleship=<x,y,dir> Place ship Battleship at position x,y and direction (h or v) [default: 2,1,h].
--cruiser=<x,y,dir> Place ship cRuiser at position x,y and direction (h or v) [default: 3,1,h].
--submarine=<x,y,dir> Place ship Submarine at position x,y and direction (h or v) [default: 4,1,h].
--destroyer=<x,y,dir> Place ship Destroyer at position x,y and direction (h or v) [default: 5,1,h].
Client
$ ./battleplayer.py --help
Battle Client.
Usage:
battleplayer.py [--delay=<t>] [--smart] [--size=<xy>] [--server_command=<cmd>]
battleplayer.py (-h | --help)
battleplayer.py --version
Options:
-h --help Show this screen.
--version Show version.
--server_command=<cmd> Play against a server launched by 'cmd' [default: ./battleserver.py --random --play].
--size=<xy> Set size of the board [default: 5,5].
--delay=<t> Delay between successive plays, in seconds [default: 0.1].
--smart Try to be smart when playing. Experimental.
Examples
Server
- We can see two random 5x5 boards:
$ ./battleserver.py --random --show ccccc bbbb- srrr- s-d-- s-d-- $ ./battleserver.py --random --show ccccc -brrr sb--- sbd-- sbd--
- We can see a random 7x7 board:
$ ./battleserver.py --size=7,7 --random --show ----sss --bbbb- --ccccc ------- --rrr-- -dd---- -------
Client
$ ./battleplayer.py --smart --delay=0
bot: 1,4
board: hit carrier
bot: 0,4
board: miss
bot: 2,4
WARNING: was hitting carrier but now hitting battleship
board: hit battleship
bot: 3,4
WARNING: was hitting carrier but now hitting destroyer
board: hit destroyer
bot: 4,4
board: miss
bot: 1,0
board: hit carrier
bot: 1,1
board: hit carrier
bot: 1,2
board: hit carrier
bot: 1,3
board: sunk carrier
bot: 0,3
board: miss
bot: 3,0
WARNING: was hitting None but sunk destroyer
board: sunk destroyer
bot: 3,1
board: hit cruiser
bot: 0,1
board: hit cruiser
bot: 2,1
WARNING: was hitting cruiser but now hitting battleship
board: hit battleship
bot: 4,1
board: sunk cruiser
bot: 4,2
board: hit submarine
bot: 2,2
WARNING: was hitting submarine but now hitting battleship
board: hit battleship
bot: 3,2
board: hit submarine
bot: 4,0
board: miss
bot: 4,3
board: miss
bot: 0,0
board: miss
bot: 2,0
board: miss
bot: 3,3
board: miss
bot: 2,3
WARNING: was hitting submarine but sunk battleship
board: sunk battleship
bot: 0,2
board: sunk submarine
bot: 0,0
board: you win! in 25 moves
VICTORY!
:scroll: License ?
MIT Licensed (file LICENSE). © Lilian Besson, 2018.