ну что, новая проблема. программа запускается, работает, но в определенный момент виснет. без ошибок и всякого такого. мне хотелось бы узнать. почему? как удалось выяснить?
если кто захочет по мучаться, то опишу константы. HEIGHT,WIDTH- высота и ширина соответственно, N= колличество ячеек по ширине и высоте, должно быть равно константе в inf, RANDEA- шанс появления еды, RENDMU
да-да, знаю что неправильно, исправлять лень,- шанс мутаций. Vtp- схема отображения 0- пищевая, 1- защитная, 2- энергетическая. с остольным разбобраться можно, благо я даже названя нормальные давал
а если не удастся то не судьбаextern crate sdl2;
extern crate rand;
//use sdl2::pixels::PixelFormatEnum;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::gfx::primitives::DrawRenderer;
use sdl2::render::Canvas;
use sdl2::video::Window;
use std::f64;
//use std::mem;
//use std::ptr;
use rand::{Rng,thread_rng};
use std::time::Duration;
const Vtp:i16 = 2;
static HEIGHT:u32 = 600;
static WIDTH:u32 = 800;
const N:u32 = 40;
const RANDEA:u32 = 1;
pub fn main() {
let context = sdl2::init().unwrap();
let video = context.video().unwrap();
let mut rng = rand::thread_rng();
let window = video.window("rust-sdl2 demo: Video", WIDTH, HEIGHT)
.position_centered()
.opengl()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
let tc = canvas.texture_creator();
let mut tex = tc.create
texturestatic(tc.default
pixelformat(), 150,
150).unwrap();
canvas.clear();
canvas.set
drawcolor(Color::RGB(255, 210, 0));
let mut a:[[inf::Obj;N as usize];N as usize] =
unsafe{std::mem::uninitialized()};
for i in a.iter_mut(){
for j in i.iter_mut(){
unsafe{::std::ptr::write(j,inf::Obj::newNu())}
}
}
a[10][10]=inf::Obj::newPl(100,0,0);
let mut event
pump = context.eventpump().unwrap();
let mut run = true;
while run {
for event in event
pump.polliter() {
match event {
Event::Quit {..} | Event::KeyDown { keycode:
Some(Keycode::Escape), .. } => {
run = false;
},
_ => {}
}
}
for i in a.iter_mut(){
for j in i.iter_mut(){
if j.otp() == 2 {
*j= j.mkcour();
} else if (j.otp() == 0) &(rng.gen_range(0,100) < RANDEA) {*j =
inf::Obj::newEa();}
}
}
for i in 0..((N) as usize){
for j in 0..((N) as usize){
let a0 = a[i][j].copy();
if (a[i][j].otp() == 2) & (a0.getcour()){a[i][j]= a0.itr(&mut
a,i,j);}
}
}
fild(&canvas,&a);
canvas.present();
// std::thread::sleep(Duration::from_millis(2000));
}
// let mut event
pump = context.eventpump().unwrap();
// 'running: loop {
// for event in event
pump.polliter() {
// match event {
// Event::Quit {..}
// | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
// break 'running
// },
// _ => {}
// }
// }
// }
}
fn fild(canvas:&Canvas,arr:&[[inf::Obj;N as usize];N as usize]){
let r = (if HEIGHT/(2 + 3
(N-1)/2) < (((WIDTH as f64)/(((2N+1) as
f64)
(( f64::consts::PI/3.0).sin()))) as u32) {HEIGHT/(2+3(N-1)/2)} else
{((WIDTH as f64)/(((2
N+1) as f64)(( f64::consts::PI/3.0).sin()))) as u32}) as
i16;
let a =(( f64::consts::PI/3.0).sin() * (r as f64)) as i16;
let mut x;
let mut c;
let mut y = r;
let mut f = true;
for i in (&arr).iter(){
if f {
x=a;
f=false;
} else {
x=2*a;
f=true;
};
for j in i.iter(){
c=j.col(Vtp);
hex(&canvas,x,y,r,Color::RGB(c.0,c.1,c.2));
x+=2*a;
}
y+=(3*r/2);
}
}
fn hex(c:&Canvas,x:i16,y:i16,r:i16,color:Color){
let a =(( f64::consts::PI/3.0).sin() * (r as f64)) as i16;
&c.filled_polygon(&[x,x+a,x+a,x,x-a,x-a],&[y-r,y-r/2,y+r/2,y+r,y+r/2,y-
r/2],color);
}
mod inf {
extern crate rand;
use rand::{Rng,thread_rng};
const N:usize = 40;
const RENDMU:i32 = 0;
pub struct Obj {
course:bool,
cond:i16,
side:i16,
tp:i16,
hp:i16,
def:i16,
off:i16,
liveiter:i16,
livebreed:i16,
liveancestor:i16,
table:Vec< [(i16,i16);4]>
}
impl Obj{
pub fn newPl(hp:i16,lb:i16,la:i16) -> Obj {
Obj
{course:false,cond:0,side:0,tp:2,hp:hp,def:0,off:0,liveiter:0,livebreed:lb,livea
ncestor:la,table:vec![Obj::vecstr()]}
}
pub fn newEa() -> Obj{
Obj
{course:false,cond:0,side:0,tp:1,hp:10,def:0,off:0,liveiter:0,livebreed:0,livean
cestor:0,table:vec![]}
}
pub fn newNu() -> Obj{
Obj
{course:false,cond:0,side:0,tp:0,hp:0,def:0,off:0,liveiter:0,livebreed:0,liveanc
estor:0,table:vec![]}
}
pub fn lock() -> Obj{
Obj
{course:false,cond:0,side:0,tp:3,hp:0,def:0,off:0,liveiter:0,livebreed:0,liveanc
estor:0,table:vec![]}
}
pub fn col(&self,t:i16) -> (u8,u8,u8){
match t {
0 => if self.tp == 0 {(255,255,255)} else if self.tp == 1
{(0,255,0)}else if self.tp==2 {((self.off
255/2) as u8,0,((2-self.off)255/2) as
u8)} else {(0,0,0)},
1 => if self.tp == 0 {(255,255,255)} else if self.tp == 1
{(0,255,0)}else if self.tp==2 {if self.def == 0 {(244,205,138)} else if self.def
== 1 {(114,130,87)} else {(66,13,74)} } else {(0,0,0)},
2 => if self.tp == 0 {(255,255,255)} else if self.tp == 1
{(0,255,0)}else if self.tp==2 {((self.hp*255/100) as u8,0,0)} else {(0,0,0)}
_ => (0,0,0),
}
}
fn vecstr() -> [(i16,i16);4] {
[(0,0),(0,0),(1,0),(1,0)]
}
pub fn otp(&self) -> i16{
self.tp
}
fn war(&self,a:&mut Obj) -> bool{
((self.off == 0) & ((a.tp == 0)|(a.tp == 1))) | ((a.tp == 2) &
(self.off > a.def))
}
pub fn mkcour(&self ) -> Obj {
Obj
{course:true,cond:self.cond,side:self.side,tp:2,hp:self.hp,def:self.def,off:self
.off,liveiter:self.liveiter,livebreed:self.livebreed,liveancestor:self.liveances
tor,table:self.table.clone()}
}
pub fn getcour(&self) -> bool {
self.course
}
pub fn itr(&self,a1:&mut [[Obj;N];N],i:usize,j:usize) -> Obj{
println!("{},{} - {} -{}",i,j,self.hp,self.side);
for i0 in self.table.iter(){
print!("({},{}),({},{}),({},{}),({},{})",
i0[0].0,i0[0].1,i0[1].0,i0[1].1,i0[2].0,i0[2].1,i0[3].0,i0[3].1);
}
println!("");
if self.hp > 100{
self.division(a1,i,j)
} else {
let mut lock = Obj::lock();
let a =match (self.side) {
0 => if j+1 < N {&mut a1[i][j+1]} else {&mut lock},
1 => if i%2 == 0 {
if i+1 < N {&mut a1[i+1][j]} else {&mut lock}
}else{
if (i+1 < N) & (j+1 < N) {&mut a1[i+1][j+1]} else
{&mut lock}
},
2 => if i%2 == 0 {
if (i+1 < N) & (j >0) {&mut a1[i+1][j-1]} else {&mut
lock}
} else {
if i+1
},
3 => if j > 0 {&mut a1[i][j-1]} else {&mut lock},
4 => if i%2 == 0 {
if (i > 0) & (j >0) {&mut a1[i-1][j-1]} else {&mut
lock}
} else {
if i > 0 {&mut a1[i-1][j]} else {&mut lock}
},
5 => if i%2 == 0 {
if i > 0 {&mut a1[i-1][j]} else {&mut lock}
} else {
if (i > 0) & (j+1 < N) {&mut a1[i-1][j+1]} else
{&mut lock}
},
_ => &mut lock,
};
let (l,ncond) = self.table[self.cond as usize][a.tp as usize];
let nhp = self.hp - (1 + (self.table.len() as i16)/5) - (if l ==
3 {0} else {1})*(self.def +1)/(if (l==1) |(l==2){2} else {1});
if nhp <= 0{
Obj::newNu()
} else {
match l {
0 => {
if self.war(a){
*a = Obj
{course:false,cond:ncond,side:self.side,tp:2,hp:(nhp + a.hp -
3*self.off),def:self.def,off:self.off,liveiter:
(self.liveiter+1),livebreed:self.livebreed,liveancestor:self.liveancestor,table:
self.table.clone()};
Obj::newNu()
} else {
Obj
{course:false,cond:ncond,side:self.side,tp:2,hp:nhp,def:self.def,off:self.off,li
veiter:
(self.liveiter+1),livebreed:self.livebreed,liveancestor:self.liveancestor,table:
self.table.clone()}
}
},
1 => Obj {course:false,cond:ncond,side:(if self.side ==
5 {0} else {self.side +1}),tp:2,hp:nhp,def:self.def,off:self.off,liveiter:
(self.liveiter+1),livebreed:self.livebreed,liveancestor:self.liveancestor,table:
self.table.clone()},
2 => Obj {course:false,cond:ncond,side:(if self.side ==
0 {5} else {self.side -1}),tp:2,hp:nhp,def:self.def,off:self.off,liveiter:
(self.liveiter+1),livebreed:self.livebreed,liveancestor:self.liveancestor,table:
self.table.clone()},
3 => Obj
{course:false,cond:ncond,side:self.side,tp:2,hp:nhp,def:self.def,off:self.off,li
veiter:
(self.liveiter+1),livebreed:self.livebreed,liveancestor:self.liveancestor,table:
self.table.clone()},
_ => Obj::lock(),
}
}
}
}
fn division(&self,a1:&mut [[Obj;N];N],i:usize,j:usize) -> Obj{
let hp = (self.hp -20)/2;
let inc:[(i16,i16);6] = [(i as i16,(j as i16)+1),if i%2 == 0 {((i as
i16)+1,j as i16)} else{((i as i16)+1,(j as i16)+1)},if i%2 == 0 {((i as i16)+1,
(j as i16)-1)} else {((i as i16)+1,j as i16)},(i as i16,(j as i16)-1),if i%2 ==
0 {((i as i16)-1,(j as i16)-1)}else {((i as i16)-1,j as i16)},if i%2 == 0 {((i
as i16)-1,j as i16)} else {((i as i16)-1,(j as i16)+1)}];
let mut i1=self.side as usize;
if ((inc[i1].0 >=0) & (inc[i1].1>=0) & (inc[i1].0 <(N as i16)) &
(inc[i1].1 < (N as i16))){
if (a1[inc[i1].0 as usize][inc[i1].1 as usize].tp == 0) {
a1[inc[i1].0 as usize][inc[i1].1 as usize] = self.baby((i1
as i16),hp);
}
} else {
while {
if i1 == 0 {i1 = 5} else {i1-=1};
if (inc[i1].0 >=0) & (inc[i1].1>=0) & (inc[i1].0 < N as
i16 ) & (inc[i1].1 < N as i16){
a1[inc[i1].0 as usize][inc[i1].1 as usize].tp == 0
} else {i1 == (self.side as usize)}
} {}
if((i1==(self.side as usize)) & (inc[i1].0 >=0) & (inc[i1].1>=0)
& (inc[i1].0
if (a1[inc[i1].0 as usize][inc[i1].1 as usize].tp == 0) {
a1[inc[i1].0 as usize][inc[i1].1 as usize] =
self.baby((i1 as i16),hp);
}
}
}
self.baby((i1 as i16),hp)
}
fn baby (&self,side:i16,nhp:i16) -> Obj{
let mut rng = rand::thread_rng();
if (rng.gen_range(0,100) < RENDMU){
println!("mutation!");
let mut ndef= self.def;
let mut noff= self.off;
let mut ntable = self.table.clone();
if rng.gen() {
if rng.gen() {
ndef = match ndef {
0 => 1,
1 => if rng.gen() {0} else {2},
2 => 1,
_ => 0,
};
} else {
noff = match noff {
0 => 1,
1 => if rng.gen() {0} else {2},
2 => 1,
_ => 0,
};
}
} else {
let i = rng.gen_range(0,self.table.len());
let j = rng.gen_range(0,4);
if rng.gen() {
ntable[i][j] = (rng.gen_range(0,4),ntable[i][j].1);
} else {
let k = ntable[i][j].1;
let nk = rng.gen_range(0,ntable.len()+1);
ntable[i][j] = (ntable[i][j].0,nk as i16);
if nk == ntable.len(){ntable.push(Obj::vecstr());}
let mut f =true;
for i1 in 0..(ntable.len()){
for j1 in 0..4 {
if ((ntable[i1][j1].1 == k) & (i1 != (k as
usize))) {f=false;}
}
}
if f & (k != 0){
for i1 in 0..(ntable.len() -1){
if (i1 >= (k as usize)){
ntable[i1] = ntable[i1+1];}
for j1 in 0..4{
if ntable[i1][j1].1 > k {
ntable[i1][j1] = (ntable[i1]
[j1].0,ntable[i1][j1].1-1);
}
}
}
ntable.pop();
}
}
}
Obj {course:false,cond:0,side:rng.gen_range(0,
6),tp:self.tp,hp:nhp,def:ndef,off:noff,liveiter:0,livebreed:0,liveancestor:self.
liveiter,table:ntable.clone()}
} else {
Obj {course:self.course,cond:self.cond,side:rng.gen_range(0,
6),tp:self.tp,hp:nhp,def:self.def,off:self.off,liveiter:0,livebreed:self.livebre
ed+1,liveancestor:self.liveiter,table:self.table.clone()}
}
}
pub fn copy(&self) ->Obj {
Obj
{course:self.course,cond:self.cond,side:self.side,tp:self.tp,hp:self.hp,def:self
.def,off:self.off,liveiter:self.liveiter,livebreed:self.livebreed,liveancestor:s
elf.liveancestor,table:self.table.clone()}
}
}
}
>>48694> Форматируй код нормально
> Так ещё хуже, чем скриншотами
братишка, я тебе покушать принес