本文整理汇总了Golang中github.com/dnesting/alife/goalife/grid2d/org.Organism.Discharge方法的典型用法代码示例。如果您正苦于以下问题:Golang Organism.Discharge方法的具体用法?Golang Organism.Discharge怎么用?Golang Organism.Discharge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/dnesting/alife/goalife/grid2d/org.Organism
的用法示例。
在下文中一共展示了Organism.Discharge方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}