本文整理匯總了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
}