Зануда с интернетом
Регистрация: 04.09.2005
Сообщений: 14,014
Написано 6,798 полезных сообщений (для 20,935 пользователей)
|
Ответ: Задачка...
Алгоритм реализации практически однозначен. Затруднения могут быть вызваны лишь выбором организации обработки\хранения данных, что коррелирует с исходными данными (размером острова и скоростью воспроизведения популяции)
Const PopulationTypeWolf%=1
Const PopulationTypeRabbit%=2
Const Male%=1
Const Female%=0
;=================================
Type T2Dpoint
Field x%,y%
End Type
Function Scalar2Vector(scalar%,vector.T2Dpoint)
Select scalar
Case 1
vector\x=-1
vector\y=1
Case 2
vector\x=0
vector\y=1
Case 3
vector\x=1
vector\y=1
Case 4
vector\x=-1
vector\y=0
Case 5
vector\x=1
vector\y=0
Case 6
vector\x=-1
vector\y=-1
Case 7
vector\x=0
vector\y=-1
Case 8
vector\x=1
vector\y=-1
Default
vector\x=0
vector\y=0
End Select
End Function
Function MovePoint(point.t2dpoint,vector.t2dpoint)
point\x=point\x+vector\x
point\y=point\y+vector\y
End Function
Function StabliPoint(point.t2dpoint,size.t2dpoint)
If point\x>size\x
point\x=size\x
ElseIf point\x<1
point\x=1
EndIf
If point\y>size\y
point\y=size\y
ElseIf point\y<1
point\y=1
EndIf
End Function
Function EqualPoint(a.t2dpoint,b.t2dpoint)
If a\x=b\x And a\y=b\y
Return True
EndIf
Return False
End Function
;=================================
Type TIsland
Field SIZE.T2Dpoint
Field Populations%
End Type
Function CreateIsland.TIsland(x,y)
I.TIsland=New TIsland
I\SIZE=New T2Dpoint
I\SIZE\x=x
I\SIZE\y=y
I\Populations=CreatePivot()
HideEntity I\Populations
Return I
End Function
Function AddAnimal(I.TIsland,HANDLE_,PosNeedSet%,CODE%)
;DebugLog "animal with code "+code
Local PPivot%=0
If CountChildren(I\Populations)
For j=1 To CountChildren(I\Populations)
If EntityName(GetChild(I\Populations,j))=Str(CODE)
PPivot=GetChild(I\Populations,j)
Exit
EndIf
Next
EndIf
;DebugLog "root for population "+PPivot
If PPivot=0
PPivot=CreatePivot(I\Populations)
NameEntity PPivot,Str(CODE)
;DebugLog "new population with code "+code
EndIf
Local Obj=CreatePivot(PPivot)
NameEntity Obj,Str(HANDLE_)
Select CODE
Case PopulationTypeWolf
Local Wolf.TWolf=Object.TWolf(HANDLE_)
Wolf\IslandHandle=Handle(I)
Wolf\PopPiv=Obj
If PosNeedSet
Wolf\Position\X=Rand(1,I\SIZE\x)
Wolf\Position\Y=Rand(1,I\SIZE\y)
EndIf
Case PopulationTypeRabbit
Local Rabbit.TRabbit=Object.TRabbit(HANDLE_)
Rabbit\IslandHandle=Handle(I)
Rabbit\PopPiv=Obj
If PosNeedSet
Rabbit\Position\X=Rand(1,I\SIZE\x)
Rabbit\Position\Y=Rand(1,I\SIZE\y)
EndIf
End Select
End Function
Function GetObjectsInCoord%(coord.t2dpoint,I.TIsland)
Pivot=CreatePivot()
HideEntity Pivot
Local SubPivot
Local Popul%
If CountChildren(I\Populations)
For k=1 To CountChildren(I\Populations)
popul=GetChild(I\Populations,k)
For j=1 To CountChildren(popul)
Select Int(EntityName(popul))
Case PopulationTypeWolf
Local Wolf.TWolf=Object.TWolf(EntityName(GetChild(popul,j)))
If EqualPoint(Wolf\Position,coord)
SubPivot=CreatePivot(Pivot)
NameEntity SubPivot,EntityName(popul)
EntityType SubPivot,Handle(Wolf)
EndIf
Case PopulationTypeRabbit
Local Rabbit.TRabbit=Object.TRabbit(EntityName(GetChild(popul,j)))
If EqualPoint(Rabbit\Position,coord)
SubPivot=CreatePivot(Pivot)
NameEntity SubPivot,EntityName(popul)
EntityType SubPivot,Handle(Rabbit)
EndIf
End Select
Next
Next
EndIf
Return Pivot
End Function
Function UpdateIsland(I.TIsland)
If CountChildren(I\Populations)
For k=1 To CountChildren(I\Populations)
popul=GetChild(I\Populations,k)
For j=1 To CountChildren(popul)
Select Int(EntityName(popul))
Case PopulationTypeWolf
Local Wolf.TWolf=Object.TWolf(EntityName(GetChild(popul,j)))
UpdateWolf(Wolf)
Case PopulationTypeRabbit
Local Rabbit.TRabbit=Object.TRabbit(EntityName(GetChild(popul,j)))
UpdateRabbit(Rabbit)
End Select
Next
Next
EndIf
End Function
Function DrawIsland(I.TIsland,x%,y%)
Local MapCellSize%=25
Color 0,200,0
For ix=1 To I\SIZE\x
For iy=1 To I\SIZE\y
Rect x+(ix-1)*MapCellSize+1,y+(iy-1)*MapCellSize+1,MapCellSize-2,MapCellSize-2
Next
Next
If CountChildren(I\Populations)
For k=1 To CountChildren(I\Populations)
popul=GetChild(I\Populations,k)
For j=1 To CountChildren(popul)
Select Int(EntityName(popul))
Case PopulationTypeWolf
Local Wolf.TWolf=Object.TWolf(EntityName(GetChild(popul,j)))
DrawWolf(Wolf,x,y,MapCellSize)
Case PopulationTypeRabbit
Local Rabbit.TRabbit=Object.TRabbit(EntityName(GetChild(popul,j)))
DrawRabbit(Rabbit,x,y,MapCellSize)
End Select
Next
Next
EndIf
End Function
;=================================
Type TWolf
Field Gendor%
Field Score#
Field Position.T2Dpoint
Field IslandHandle%
Field PopPiv%
End Type
Function CreateWolf.TWolf(Gendor%)
W.TWolf=New TWolf
W\GENDOR=Gendor
W\SCORE=1
W\Position=New T2Dpoint
Return W
End Function
Function UpdateWolf(W.Twolf)
Local ISL.TIsland=Object.TIsland(W\IslandHandle)
Local Move.T2Dpoint=New T2Dpoint
Scalar2Vector(Rand(0,9),Move)
MovePoint(W\Position,Move)
StabliPoint(W\Position,ISL\SIZE)
Delete MOVE
Local List=GetObjectsInCoord(W\position,ISL)
Local hav4eg=False
Local Wolf.TWolf=Null
If CountChildren(List)
For i=1 To CountChildren(List)
Select Int(EntityName(GetChild(List,i)))
Case PopulationTypeWolf
If GetEntityType(GetChild(List,i))<>Handle(W)
If W\GENDOR=Male
Wolf.TWolf=Object.TWolf(GetEntityType(GetChild(List,i)))
If Wolf\GENDOR<>Female
Wolf=Null
EndIf
EndIf
EndIf
Case PopulationTypeRabbit
Local Rabbit.TRabbit=Object.TRabbit(GetEntityType(GetChild(List,i)))
FreeEntity Rabbit\PopPiv
Delete Rabbit
W\SCORE=W\SCORE+1
hav4eg=True
Exit
End Select
Next
EndIf
FreeEntity List
If Not hav4eg
W\SCORE=W\SCORE-0.1
If Wolf<>Null
Local gendor=Rand(0,1)
Local ChildWolf.TWolf=CreateWolf(gendor)
ChildWolf\Position\X=Wolf\Position\X
ChildWolf\Position\Y=Wolf\Position\Y
AddAnimal(ISL,Handle(ChildWolf),False,PopulationTypeWolf)
EndIf
EndIf
If W\SCORE=0
FreeEntity W\PopPiv
Delete W
EndIf
End Function
Function DrawWolf(W.TWolf,x,y,mcs)
Color 0,0,0
Local ix=W\Position\x
Local iy=W\Position\y
Oval x+(ix-1)*mcs+1,y+(iy-1)*mcs+1,mcs-2,mcs-2
Color 255,255,255
Select W\gendor
Case male
Text x+(ix-1)*mcs+1,y+(iy-1)*mcs+1,"M"
Case female
Text x+(ix-1)*mcs+1,y+(iy-1)*mcs+1,"}|{"
End Select
End Function
;=================================
Type TRabbit
Field Position.T2Dpoint
Field IslandHandle%
Field PopPiv%
End Type
Function CreateRabbit.TRabbit()
R.TRabbit=New TRabbit
R\Position=New T2Dpoint
Return R
End Function
Function UpdateRabbit(R.TRabbit)
Local ISL.TIsland=Object.TIsland(R\IslandHandle)
Local Move.T2Dpoint=New T2Dpoint
Scalar2Vector(Rand(0,9),Move)
MovePoint(R\Position,Move)
StabliPoint(R\Position,ISL\SIZE)
Delete MOVE
If Rnd(0,1)<=RabbitProb
Local ChildRabbit.TRabbit=CreateRabbit()
ChildRabbit\Position\X=R\Position\X
ChildRabbit\Position\Y=R\Position\Y
AddAnimal(ISL,Handle(ChildRabbit),False,PopulationTypeRabbit)
EndIf
End Function
Function DrawRabbit(R.TRabbit,x,y,mcs)
Color 100,100,100
Local ix=R\Position\x
Local iy=R\Position\y
Oval x+(ix-1)*mcs+1,y+(iy-1)*mcs+1,mcs-2,mcs-2
End Function
;=================================
;=================================
Const TimeMoment=1000;ms
Const RabbitProb#=0.2
Graphics3D 800,600,32
SetBuffer BackBuffer()
SetFont(LoadFont("Arial cyr",20))
SeedRnd(MilliSecs())
Island.TIsland=CreateIsland(20,20)
For i=1 To 1
Rabbit.TRabbit=CreateRabbit()
AddAnimal(Island,Handle(Rabbit),True,PopulationTypeRabbit)
Next
For i=1 To 10
Wolf.TWolf=CreateWolf(Male)
AddAnimal(Island,Handle(Wolf),True,PopulationTypeWolf)
Next
For i=1 To 10
Wolf.TWolf=CreateWolf(Female)
AddAnimal(Island,Handle(Wolf),True,PopulationTypeWolf)
Next
Local time=MilliSecs()
While Not KeyHit(1)
DrawIsland(Island,5,5)
If MilliSecs()-time>=TimeMoment
time=MilliSecs()
UpdateIsland(Island)
EndIf
Flip
Wend
End
Занятно, уонечно:
то кролики делают, сами знаете что, как кролики,
то группа волчиц, забитая случайность в угол, становится интерсной целью для самцов..
__________________
http://nabatchikov.com
Мир нужно делать лучше и чище. Иначе, зачем мы живем? tormoz
А я растила сына на преданьях
о принцах, троллях, потайных свиданьях,
погонях, похищениях невест.
Да кто же знал, что сказка душу съест?
|