Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/ru/neolab/forest/fauna/WildlifeSanctuary.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public void addListener(final WildlifeSanctuaryListener listener) {
listeners.add(listener);
}

//-------------------------------------- ������ � ��������� � ���������� ������������
//-------------------------------------- ðàáîòà ñ äâèæåíèåì è âîçìîæíûìè êîîðäèíàòàìè

/**
* �������� ������� ������������ �� ����. ����� ����� �������� �����������, ������� � �� � �� � ��
* Çàäàþòñÿ ïðàâèëà ïåðåäâèæåíèÿ ïî ïîëþ. Ìîæíî çäåñü íàêèäàòü ïðåïÿòñòâèé, îâðàãîâ è ïð è äð è òä
*/
private boolean possibleMove(final Coordinates from, final Coordinates to, final double speed) throws SanctuaryException {
if (!checkIsPossible(from)) {
Expand All @@ -53,7 +53,7 @@ private boolean possibleMove(final Coordinates from, final Coordinates to, final
}

/**
* �������� ��������� ����, ��� ����� ���� ���� ����������
* Çàäàåòñÿ ãåîìåòðèÿ ïîëÿ, òóò ìîæíî õîòü êðóã íàðèñîâàòü
*/
boolean checkIsPossible(final Coordinates coordinates) {
return coordinates.x >= 0
Expand Down Expand Up @@ -88,19 +88,20 @@ public final List<Coordinates> getPossibleMoves(final Coordinates from, final do
return coordinates;
}

//-------------------------------------- ��������
//-------------------------------------- èòåðàöèè
public void addBeast(final Beast beast, final Coordinates coordinates) {
beasts.put(beast, new BeastSanctuaryState(beast, coordinates));
}

/**
* �������� �������� ��������� ����. ���� ��� ��� �����, ��� ���������, ��� �������� ��� �� ����� ������, �����
* ��� ����������.
* Ïðîâîäèì èòåðàöèþ èçìåíåíèÿ ìèðà. Ïîêà ìèð íàø ïðîñò, áåç ýêñöåññîâ, âñå âûáèðàþò ÷òî îí áóäóò äåëàòü, ïîòîì
* ýòî ïðîèñõîäèò.
* <p>
* � ������� ���� ������ - ���� ��������� ����, ���� - ���� ������������, � ������ - ��� ��� ��������, � ������ ���� -
* ��� ���� ����� ��� �� � ���� � ������������ ������.
* Ê ïðèìåðó çàÿö ñêàçàë - áóäó äâèãàòüñÿ òóäà, âîëê - áóäó ðàçìíîæàòüñÿ, à òðàâêà - ÷òî îíà îòðàñòåò, à âòîðîé çàÿö -
* ÷òî áóäó æðàòü êàê íå â ñåáÿ è íàñëàæäàòüñÿ æèçíüþ.
*/
private void iteration() throws SanctuaryException {
List<Hare> ateHares = new ArrayList<Hare>();
if (!events.isEmpty()) {
throw new SanctuaryException("Not empty state");
}
Expand All @@ -114,6 +115,7 @@ private void iteration() throws SanctuaryException {
final List<Hare> beasts = getAliveBeast(whereBeast(wolf), Hare.class);
if (beasts.size() == 0) continue;
final Hare ateHare = beasts.get((int) (beasts.size() * Math.random()));
ateHares.add(ateHare);
events.add(new Event.BeastAte(wolf, ateHare.getKilocalories()));
beasts.remove(ateHare);
}
Expand All @@ -138,7 +140,7 @@ private void iteration() throws SanctuaryException {
grass.stepDone(0);
}
}
// ��������������� ����� ����� ���� ��� �� ��� �����
// âîñïðîèçâîäñòâî òðàâû ïîñëå òîãî êàê åå âñþ ñúåëè
for (final Coordinates coordinate : grasses.keySet()) {
final Grass grass = grasses.get(coordinate);
if (grass.getKilocalories() <= 0) {
Expand All @@ -154,12 +156,18 @@ private void iteration() throws SanctuaryException {
grass.growGrass(numPossibleParents);
}
}
// ������ ���� ���....
// ìåíÿåì ýòîò ìèð....
for (final Event event : events) {
if (event.beast.isDead()) continue;
event.apply(this);
}
events.clear();

if (!ateHares.isEmpty()) {
for (Hare ateHare : ateHares) {
this.beasts.remove(ateHare);
}
}
}

public void addEvent(final Event event) {
Expand All @@ -174,7 +182,7 @@ public double getDistance(final Coordinates c1, final Coordinates c2) {
return Math.sqrt(Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2));
}

//----------------------------------- ���������� ���������� ����������� ������� ��������� ����
//----------------------------------- ðåàëèçàöèÿ ïðèìåíåíèÿ íàêîïëåííûõ ðåøåíèé èçìåíåíèé ìèðà

void moveBeast(final Beast beast, final Coordinates to) throws SanctuaryException {
final BeastSanctuaryState beastSanctuaryState = beasts.get(beast);
Expand All @@ -192,10 +200,10 @@ void beastAte(final Beast beast, final double kilocalories) {
beast.kilocaloriesAte(kilocalories);
}

//----------------------------------- ��� ���������� ����
//----------------------------------- äëÿ ïðîðèñîâêè ìèðà

/**
* �������� �������� � ���� ������ ����
* Ïîëó÷èòü æèâîòíûõ â ýòîé ÿ÷åéêå ìèðà
*/
public List<Beast> getBeasts(final Coordinates coordinate) {
final ArrayList<Beast> out = new ArrayList<>();
Expand Down