the implementation carries on

This commit is contained in:
Enrico Fasoli 2015-10-03 00:08:36 +02:00
parent 0004fa2acf
commit 5005bc17cf
3 changed files with 56 additions and 21 deletions

View File

@ -1,38 +1,45 @@
use action::Action; use action::Action;
use std::vec; use structure::Structure;
fn make_connections() -> Vec<f64> { pub enum Blocks {
vec![0 as f64;9] Neuron,
Leg { dir: i32 }
} }
trait Block { pub struct Block {
fn get_connections(&self) -> &Vec<f64>; structure: Structure,
fn get_value(&self) -> &f64;
fn get_name(&self) -> &'static str;
fn compute(&mut self) -> Vec<Action>;
fn clean(&mut self);
}
struct NeuronBlock {
connections: Vec<f64>, connections: Vec<f64>,
current_value: f64, current_value: f64,
new_value: f64, new_value: f64,
block_type: Blocks
} }
impl Block for NeuronBlock { impl Block {
fn get_connections(&self) -> &Vec<f64> { pub fn new(structure: Structure, btype: Blocks) -> Block {
Block {
structure: structure,
connections: vec![0 as f64;9],
current_value: 0 as f64,
new_value: 0 as f64,
block_type: btype
}
}
pub fn get_connections(&self) -> &Vec<f64> {
&self.connections &self.connections
} }
fn get_value(&self) -> &f64 { pub fn get_value(&self) -> &f64 {
&self.current_value &self.current_value
} }
fn get_name(&self) -> &'static str { pub fn get_name(&self) -> &'static str {
"Neuron" match self.block_type {
Blocks::Neuron => "Neuron",
Blocks::Leg { dir } => "Leg"
} }
fn compute(&mut self) -> Vec<Action> {
Vec::new()
} }
fn clean(&mut self) { pub fn clean(&mut self) {
self.current_value = self.new_value self.current_value = self.new_value;
}
pub fn compute(&mut self) -> Option<Vec<Action>> {
None
} }
} }

View File

@ -1,6 +1,7 @@
mod util; mod util;
mod action; mod action;
mod block; mod block;
mod structure;
fn main() { fn main() {
println!("Hello, world!"); println!("Hello, world!");

View File

@ -0,0 +1,27 @@
use util::Point;
use action::Action;
use block::Block;
pub struct Structure {
pub blocks: Vec<Block>,
pub position: Point
}
impl Structure {
fn compute(&mut self) -> Vec<Action> {
let mut v = Vec::new() as Vec<Action>;
// Iterate blocks
for b in &mut self.blocks {
// Compute and save result
let ret = match b.compute() {
Some(x) => x,
None => Vec::new() as Vec<Action>
};
// push actions to the structure's array
for i in ret {
v.push(i)
}
}
v
}
}