當前位置: 首頁>>代碼示例>>Golang>>正文


Golang org.Organism類代碼示例

本文整理匯總了Golang中github.com/dnesting/alife/goalife/grid2d/org.Organism的典型用法代碼示例。如果您正苦於以下問題:Golang Organism類的具體用法?Golang Organism怎麽用?Golang Organism使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Organism類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Run

// Run executes Step repeatedly, until Step returns an error, at which point this
// method will invoke o.Die and return.
func (c *Cpu) Run(o *org.Organism) error {
	Logger.Printf("%v.Run(%v)\n", c, o)
	for {
		if err := c.Step(o); err != nil {
			Logger.Printf("%v.Run: %v\n", c, err)
			o.Die()
			return err
		}
	}
}
開發者ID:dnesting,項目名稱:alife,代碼行數:12,代碼來源:cpu.go

示例2: opSenseOthers

// opSenseOthers: sense energy ahead, excluding those with the same bytecode, capped at 255
func opSenseOthers(o *org.Organism, c *Cpu) error {
	filter := func(n interface{}) float64 {
		if org, ok := n.(*org.Organism); ok {
			if nc, ok := org.Driver.(*Cpu); ok {
				if c.Hash() == nc.Hash() {
					return 0.0
				}
			}
		}
		return 1.0
	}
	c.R[0] = clip(int(o.Sense(filter)), 0, 255)
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:15,代碼來源:ops.go

示例3: Step

// Step executes one CPU operation.  Any error returned either assessing the
// operation's energy cost or executing it will be returned by this method.
// Execution is expected to cease (and the organism's Die method
// invoked) if an error is returned.
func (c *Cpu) Step(o *org.Organism) (err error) {
	op, ip := c.readOp()
	c.Ip = ip
	if op == nil {
		return unableToReadErr
	}
	Logger.Printf("%v.Step(%v): %v\n", c, o, op)

	// All operations cost at least 1 energy, to avoid infinite loops.
	if err := o.Discharge(1 + op.Cost); err != nil {
		return err
	}

	if err := op.Fn(o, c); err != nil {
		return err
	}

	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:23,代碼來源:cpu.go

示例4: opDivide

// opDivide: spawn a new organism in neighboring cell with same bytecode
// and energy fraction described by A/256.
func opDivide(o *org.Organism, c *Cpu) error {
	lenc := len(c.Code)
	if err := o.Discharge(lenc); err != nil {
		return err
	}
	nc := c.Copy()
	if rand.Float64() < MutationRate {
		nc.Mutate()
	}
	n, err := o.Divide(nc, float64(c.R[0])/256.0)
	if err == org.ErrNotEmpty {
		return nil
	}
	if err != nil {
		return err
	}
	go nc.Run(n)
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:21,代碼來源:ops.go

示例5: opSense

// opSense: sense energy ahead, capped at 255
func opSense(o *org.Organism, c *Cpu) error {
	c.R[0] = clip(int(o.Sense(nil)), 0, 255)
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:5,代碼來源:ops.go

示例6: opForward

// opForward: move forward if able
func opForward(o *org.Organism, c *Cpu) error {
	if err := o.Forward(); err != nil && err != org.ErrNotEmpty {
		return err
	}
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:7,代碼來源:ops.go

示例7: opRight

// opRight: turn right
func opRight(o *org.Organism, c *Cpu) error {
	o.Right()
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:5,代碼來源:ops.go

示例8: opLeft

// opLeft: turn left
func opLeft(o *org.Organism, c *Cpu) error {
	o.Left()
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:5,代碼來源:ops.go

示例9: opEat

// opEat: Consume A*10 energy from neighbor
func opEat(o *org.Organism, c *Cpu) error {
	if _, err := o.Eat(c.R[0] * 10); err != nil {
		return err
	}
	return nil
}
開發者ID:dnesting,項目名稱:alife,代碼行數:7,代碼來源:ops.go


注:本文中的github.com/dnesting/alife/goalife/grid2d/org.Organism類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。