Slava0135 / nanoservice

Service for "battleship" puzzle game

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nanoservice

Run Tests (develop) (develop)

Run Tests (master) (master)

About

Small service resembling "battleship" puzzle game that:

  • Can generate ship layout as plain text or image
  • Can validate ship layout
  • Can replay game given layout and shots made

Game variation:

  • Game field is 10 by 10 squares
  • There are:
    • 4 ships of size 1
    • 3 ships of size 2
    • 2 ships of size 3
    • 1 ship of size 4

Building

Docker image is provided:

docker build -t nanoservice .
docker run -dp 8080:8080 nanoservice:latest

You can also run it with:

go run main.go

Or build and run:

go build -o app
./app

Features

Generate ship layout

Example request:

curl -i http://localhost:8080/generate

Output:

{{7 4} {7 4}}
{{5 4} {5 4}}
{{6 1} {6 1}}
{{4 0} {4 0}}
{{1 3} {2 3}}
{{9 1} {9 2}}
{{2 6} {3 6}}
{{0 8} {2 8}}
{{6 6} {8 6}}
{{5 8} {8 8}}

Those are ship coordinates, e.g. last ship is on squares {5 8}, {6 8}, {7 8}, {8 8}

Generate ship layout as grid

Example request:

curl -i http://localhost:8080/generate/grid

Output:

oo#oo###o#
ooooooooo#
#oo#o#oooo
ooo#o#oo#o
ooo#oooooo
o#o#oooooo
o#oooooooo
o#ooo##ooo
oooooooooo
ooo#oooooo

o means the square is empty, # means there is a ship

Generate ship layout as image

Example request:

curl -i http://localhost:8080/generate/image

Output:

example

Validate ship layout

Example request:

curl -i http://localhost:8080/validate -d "{{7 5} {7 5}}
{{2 7} {2 7}}
{{2 3} {2 3}}
{{0 5} {0 5}}
{{6 2} {7 2}}
{{3 9} {4 9}}
{{9 3} {9 4}}
{{4 1} {4 3}}
{{6 8} {8 8}}
{{2 5} {5 5}}"

Output:

true

If layout is not valid (try to remove 1 ship) it would output false

You can use different format:

curl -i http://localhost:8080/validate -d "#ooooooooo
#o#oooo#oo
oooo#oo#o#
ooooooo#o#
#oooo#oooo
#ooooooo#o
#ooooooo#o
#o#ooooo#o
oo#oo#oooo
oooooooooo"

Output:

true

Replay game

Example request:

curl -i http://localhost:8080/replay -d "{{7 5} {7 5}}
{{2 7} {2 7}}
{{2 3} {2 3}}
{{0 5} {0 5}}
{{6 2} {7 2}}
{{3 9} {4 9}}
{{9 3} {9 4}}
{{4 1} {4 3}}
{{6 8} {8 8}}
{{2 5} {5 5}}
+
{0 0}
{9 4}
{9 3}
{5 5}"

Output:

Untouched:
{{7 5} {7 5}}
{{2 7} {2 7}}
{{2 3} {2 3}}
{{0 5} {0 5}}
{{6 2} {7 2}}
{{3 9} {4 9}}
{{4 1} {4 3}}
{{6 8} {8 8}}
Damaged:
{{2 5} {5 5}}
Destroyed:
{{9 3} {9 4}}

You can use different format:

curl -i http://localhost:8080/replay -d "#ooooooooo
#o#oooo#oo
oooo#oo#o#
ooooooo#o#
#oooo#oooo
#ooooooo#o
#ooooooo#o
#o#ooooo#o
oo#oo#oooo
oooooooooo
+
{0 0}
{7 0}
{2 1}
{0 7}"

Output:

Untouched:
{{2 7} {2 8}}
{{4 2} {4 2}}
{{5 4} {5 4}}
{{5 8} {5 8}}
{{7 1} {7 3}}
{{8 5} {8 7}}
{{9 2} {9 3}}
Damaged:
{{0 0} {0 1}}
{{0 4} {0 7}}
Destroyed:
{{2 1} {2 1}}

About

Service for "battleship" puzzle game

License:MIT License


Languages

Language:Go 100.0%