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

lab4 #251

Open
wants to merge 2 commits into
base: Petrov_Leonid_Alekseevich
Choose a base branch
from
Open

lab4 #251

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
79 changes: 79 additions & 0 deletions golang/lab4/ship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package lab4

import "errors"

const fieldWidth = 8
const fieldHeight = 8
const maxShipLength = 4

type ship struct {
width uint8
height uint8
x uint8
y uint8
}

func NewShip(width, height, x, y uint8) (*ship, error) {
if width > 1 && height > 1 {
return nil, errors.New("Unsupported ship sizes.")
}

if width == 0 || height == 0 {
return nil, errors.New("Unsupported ship sizes.")
}

if width > maxShipLength || height > maxShipLength {
return nil, errors.New("Unsupported ship sizes.")
}

ship := ship{
width: width,
height: height,
}

error := ship.SetX(x)
if error != nil {
return nil, error
}

error = ship.SetY(y)
if error != nil {
return nil, error
}

return &ship, nil
}

func (instance *ship) Width() uint8 {
return instance.width
}

func (instance *ship) Height() uint8 {
return instance.height
}

func (instance *ship) X() uint8 {
return instance.x
}

func (instance *ship) Y() uint8 {
return instance.y
}

func (instance *ship) SetX(x uint8) error {
if x+instance.width > fieldWidth {
return errors.New("Ship go out of field bounds.")
}

instance.x = x
return nil
}

func (instance *ship) SetY(y uint8) error {
if y+instance.height > fieldHeight {
return errors.New("Ship go out of field bounds.")
}

instance.y = y
return nil
}