src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

position.go (1045B)


      1 package position
      2 
      3 import (
      4 	"fmt"
      5 	"math/rand"
      6 	"time"
      7 
      8 	"code.dwrz.net/src/pkg/minotaur/maze/direction"
      9 )
     10 
     11 type Position struct {
     12 	X, Y int
     13 }
     14 
     15 func (p Position) Equal(v Position) bool {
     16 	return p.X == v.X && p.Y == v.Y
     17 }
     18 
     19 func (p Position) Down() Position {
     20 	return Position{X: p.X, Y: p.Y + 1}
     21 }
     22 
     23 func (p Position) Left() Position {
     24 	return Position{X: p.X - 1, Y: p.Y}
     25 }
     26 
     27 func (p Position) Right() Position {
     28 	return Position{X: p.X + 1, Y: p.Y}
     29 }
     30 
     31 func (p Position) Up() Position {
     32 	return Position{X: p.X, Y: p.Y - 1}
     33 }
     34 
     35 func (p Position) String() string {
     36 	return fmt.Sprintf("%d,%d", p.X, p.Y)
     37 }
     38 
     39 func (p Position) WithDirection(d direction.Direction) Position {
     40 	switch d {
     41 	case direction.Down:
     42 		return p.Down()
     43 	case direction.Left:
     44 		return p.Left()
     45 	case direction.Right:
     46 		return p.Right()
     47 	case direction.Up:
     48 		return p.Up()
     49 	default:
     50 		return p
     51 	}
     52 }
     53 
     54 var random = rand.New(rand.NewSource(time.Now().UnixNano()))
     55 
     56 func Random(width, height int) Position {
     57 	return Position{
     58 		X: random.Intn(width),
     59 		Y: random.Intn(height),
     60 	}
     61 }