ng

package
v0.0.0-...-6e7763a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Example (ParseRing)
tests := []string{
	"3+2",
	"0-1",
}
for _, test := range tests {
	ring, err := parseRing(test)
	if err != nil {
		panic(err)
	}
	fmt.Printf("location: %d, speed: %+d\n", ring.Location, ring.Speed)
}
Output:

location: 3, speed: +2
location: 0, speed: -1
Example (ParseRingGroup)
rg, err := parseRingGroup("o")
if err != nil {
	panic(err)
}
fmt.Printf("%s, %s", rg.Name(), rg.ShortName())
Output:

Outer, o
Example (ParseRingGroups)
rgs, err := parseRingGroups("mo,io,i")
if err != nil {
	panic(err)
}
for _, rg := range rgs {
	fmt.Printf("%s, %s\n", rg.Name(), rg.ShortName())
}
Output:

OuterMiddle, om
OuterInner, oi
Inner, i

Index

Examples

Constants

View Source
const (
	Outer       RingGroup = 0b100
	Middle      RingGroup = 0b010
	Inner       RingGroup = 0b001
	OuterMiddle           = Outer | Middle
	OuterInner            = Outer | Inner
	MiddleInner           = Middle | Inner
)

RingGroup 的合法值

View Source
const SCALES = 6

SCALES 总刻度值

Variables

This section is empty.

Functions

func CheckSolution

func CheckSolution(compass Compass, solution Steps) (bool, error)

CheckSolution 检查罗盘解谜步骤 对穷举的解法试错,尝试捕获最小能够解谜的转动操作方案

func Mod

func Mod(x, mod int) int

Mod 取模运算

Types

type Compass

type Compass struct {
	OuterRing  Ring        // 外圈
	MiddleRing Ring        // 中圈
	InnerRing  Ring        // 内圈
	RingGroups []RingGroup // 方案,可以同时旋转的一个或多个圈组成的一个分组
}

Compass 引航罗盘

func ParseCompass

func ParseCompass(expression string) (Compass, error)

ParseCompass 解析罗盘信息表达式 罗盘信息表达式满足如下格式:

{oLoc}{oSpeed},{mLoc}{mSpeed},{iLoc}{iSpeed}/{rg1},{rg2},{rg3}
{oLoc}, {mLoc}, {iLoc}:分别表示外圈、中圈、内圈的初始刻度
{oSpeed}, {mSpeed}, {iSpeed}:分别表示外圈、中圈、内圈的旋转速度
{rg1}, {rg2}, {rg3}:分别表示三种转动方案,可选值如下
	o
	m
	i
	om 或 mo
	oi 或 io
	im 或 mi

func (*Compass) IsRingGroupSupported

func (c *Compass) IsRingGroupSupported(ringGroup RingGroup) bool

IsRingGroupSupported 判断指定方案是否受当前罗盘支持

func (*Compass) Standardize

func (c *Compass) Standardize() *Compass

Standardize 标准化罗盘

func (*Compass) String

func (c *Compass) String() string

String 转为字符串表述

Example
compass := &Compass{
	InnerRing:  Ring{Location: 0, Speed: 1},
	MiddleRing: Ring{Location: 4, Speed: -4},
	OuterRing:  Ring{Location: 0, Speed: 2},
	RingGroups: []RingGroup{
		OuterInner,
		OuterMiddle,
		MiddleInner,
	},
}
fmt.Println(compass.String())
Output:

0+2,4-4,0+1/mi,oi,om

func (*Compass) Validate

func (c *Compass) Validate() error

Validate 合法化

type Ring

type Ring struct {
	// 位置
	// 指针从罗盘正左方∠0°沿顺时针方向旋转至当前位置所需的刻度
	// 刻度以∠60°为一度
	// 例如:0 表示正左方∠0°位置,3 表示正右方∠180°位置
	// 有效范围是 0-6
	Location int
	// 旋转速度
	// 单位为刻度,符号表示旋转方向,正数是顺时针,负数是逆时针
	// 旋转速度不会是 0(目前游戏内没有发现不旋转的圈)
	// 例如:-1 表示每次逆时针旋转 1 个刻度;2 表示每次顺时针旋转 2 个刻度
	// 有效范围是 1-4
	Speed int
}

Ring 定义引航罗盘中的一圈

func (*Ring) String

func (r *Ring) String() string

String 转为字符串表述

Example
ring := &Ring{Location: 1, Speed: 3}
fmt.Println(ring.String())
ring = &Ring{Location: 0, Speed: -2}
fmt.Println(ring.String())
Output:

1+3
0-2

type RingGroup

type RingGroup uint8

RingGroup 引航罗盘方案分组

func (RingGroup) Name

func (rg RingGroup) Name() string

Name 返回方案名称

func (RingGroup) ShortName

func (rg RingGroup) ShortName() string

ShortName 返回缩写

func (RingGroup) String

func (rg RingGroup) String() string

String 转为字符串表述

type Solver

type Solver interface {
	// Solve 求解引航罗盘
	Solve(ctx context.Context, compass Compass) (Steps, error)
}

Solver 引航罗盘求解器

func NewHungerSolver

func NewHungerSolver(opts SolverOptions) (Solver, error)

NewHungerSolver 创建穷举求解器

type SolverOptions

type SolverOptions struct {
	Logger logr.Logger
}

SolverOptions 求解器的选项

type Step

type Step struct {
	// 转动方案
	RingGroup RingGroup
	// 转动次数
	Count int
}

Step 声明了转动方案的操作次数

func (*Step) String

func (s *Step) String() string

String 转为字符串表述

func (*Step) Validate

func (s *Step) Validate() error

Validate 合法化

type Steps

type Steps []Step

Steps 引航罗盘解谜步骤组合

func (Steps) Standardize

func (s Steps) Standardize() Steps

Standardize 标准化

func (Steps) String

func (s Steps) String() string

String 转为字符串表述

Example
steps := Steps{
	{RingGroup: Inner, Count: 1},
	{RingGroup: OuterMiddle, Count: 3},
	{RingGroup: MiddleInner, Count: 2},
	{RingGroup: Outer, Count: 1},
	{RingGroup: Inner, Count: 1},
	{RingGroup: Middle, Count: 0},
}
fmt.Println(steps.String())
Output:

i2,mi2,o1,om3

func (Steps) Validate

func (s Steps) Validate() error

Validate 合法化

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL