aboutsummaryrefslogtreecommitdiff
path: root/go/game/pos.go
blob: bf35ca3b20a5536f586b77e0d9fdd9aa44ffdee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package game

import (
	"fmt"
)

// Position represents a zero-based position on the map.
// TODO: Change to a 1-based coordinate system.
//
//	This allows to use the zero value as invalid Position.
//
// The origin is the upper left corner of the map.
type Position struct {
	X int
	Y int
}

const POSITION_FMT = "(%d, %d)"

func (p Position) String() string {
	if p == INVALID_POSITION() {
		return "INVALID_POSITION"
	}

	return fmt.Sprintf(POSITION_FMT, p.X, p.Y)
}

func INVALID_POSITION() Position {
	return Position{-1, -1}
}

// Valid reports if a coordinate is smaller than 0.
func (p Position) Valid() bool {
	return p.X >= 0 && p.Y >= 0
}