Skip to content

Commit

Permalink
Cleanup files
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Jul 12, 2023
1 parent bebe020 commit 7084aa5
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 300 deletions.
6 changes: 3 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"semi": false, // Specify if you want to print semicolons at the end of statements
"singleQuote": true, // If you want to use single quotes
"arrowParens": "avoid", // Include parenthesis around a sole arrow function parameter
"semi": false,
"singleQuote": true,
"arrowParens": "avoid",
"trailingComma": "es5"
}
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# 🐰 The Rabbits, Wolves, and Carrots' World 🐺

*The world is inhabited by carrots, rabbits, and wolves!* 🌎
_The world is inhabited by carrots, rabbits, and wolves!_ 🌎

## World Problem

Rabbits would roam this world eating car- rots that grew in random patches. When they had eaten enough carrots, the rabbits would get fat and split in two. Wolves ran around eating up the rabbits; if they managed to catch and eat enough rabbits, they would also get fat and split. Rabbits and wolves within a certain distance of each other would broadcast information on food and predators. If a rabbit found a carrot patch, other rabbits would quickly join him. If a wolf found a rabbit, the pack would start chasing it.


## Specification / Requirement

- You will need to install NodeJS (`v14` or newer) and npm on your machine for testing it (if it isn't the case).
- If needed, download them both here: https://nodejs.org/en/download


## How to run the project

- At the location to which the project has been unzipped,
Expand All @@ -21,11 +19,10 @@ Rabbits would roam this world eating car- rots that grew in random patches. When
- See the results shown in the command line.
- See the results in `results.txt`


## Running unit tests

```console
npm test
npm run test
```

Or with `npx`
Expand All @@ -34,7 +31,6 @@ Or with `npx`
npx ava
```


### Simulation Results

Available in both `results.txt` and `world-simulation-results.png` files.
Expand All @@ -43,10 +39,8 @@ Available in both `results.txt` and `world-simulation-results.png` files.

![Later Simulation Graphic Results](./late-world-simulation-results.png)


Happy Hunting! 🥕🐇🐺


## License

Distributed under _[MIT](https://opensource.org/licenses/MIT)_ license.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"npm": ">=6.14.4"
},
"scripts": {
"check:prettier": "npx prettier . --check \"src/**/*.{ts,js,json}\" \"!package.json\"",
"fix:prettier": "npx prettier --write \"**/*.{ts,js,json}\" \"!package.json\"",
"test": "npm run build && ava --verbose"
},
"keywords": [
Expand Down
34 changes: 17 additions & 17 deletions src/__tests__/worldSimulation.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import WorldSimulation from "../worldSimulation.js";
import test from "ava";
import WorldSimulation from '../worldSimulation.js'
import test from 'ava'

test.beforeEach((t) => {
t.context.worldSimulation = new WorldSimulation();
});
test.beforeEach(t => {
t.context.worldSimulation = new WorldSimulation()
})

test("must be able to get carrots", (t) => {
const actual = t.context.worldSimulation.getCarrots();
test('must be able to get carrots', t => {
const actual = t.context.worldSimulation.getCarrots()

t.true(actual.length > 0);
});
t.true(actual.length > 0)
})

test("must be able to get animals", (t) => {
const actual = t.context.worldSimulation.getAnimals();
test('must be able to get animals', t => {
const actual = t.context.worldSimulation.getAnimals()

t.true(actual.length > 0);
});
t.true(actual.length > 0)
})

test("must generate the current living world", (t) => {
const actual = t.context.worldSimulation.generateEmojiGraph();
test('must generate the current living world', t => {
const actual = t.context.worldSimulation.generateEmojiGraph()

t.not(actual, "");
});
t.not(actual, '')
})
58 changes: 29 additions & 29 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,66 @@ import {
SCRIPT_CHARSET,
LIVING_THINGS,
INTERVAL_TIMEOUT_MS,
} from "./constants.js";
import WorldSimulation from "./worldSimulation.js";
import fs from "fs";
import os from "os";
import path from "path";
} from './constants.js'
import WorldSimulation from './worldSimulation.js'
import fs from 'fs'
import os from 'os'
import path from 'path'

export const RESULT_FILENAME = "results.txt";
export const RESULT_FILENAME = 'results.txt'

const worldSimulation = new WorldSimulation();
const worldSimulation = new WorldSimulation()

const updateFile = (pathFile, data) => {
try {
fs.appendFileSync(pathFile, data + os.EOL, { encoding: SCRIPT_CHARSET });
fs.appendFileSync(pathFile, data + os.EOL, { encoding: SCRIPT_CHARSET })
} catch (err) {
new Error(
`An error while updating ${pathFile} has occurred. ${err.message}`
);
)
}
};
}

const generateTxtStats = (world, pathFile) => {
const initialValues = { wolves: 0, rabbits: 0, carrots: 0 };
const initialValues = { wolves: 0, rabbits: 0, carrots: 0 }

const reducer = (stats, livingThing) => {
const { name } = livingThing.constructor;
const { name } = livingThing.constructor

if (name === LIVING_THINGS.wolf) {
stats.wolves++;
stats.wolves++
} else if (name === LIVING_THINGS.rabbit) {
stats.rabbits++;
stats.rabbits++
} else if (name === LIVING_THINGS.carrot) {
stats.carrots++;
stats.carrots++
}

return stats;
};
const results = world.getLivingThings().reduce(reducer, initialValues);
return stats
}
const results = world.getLivingThings().reduce(reducer, initialValues)

const testResult = JSON.stringify(results);
updateFile(pathFile, testResult);
};
const testResult = JSON.stringify(results)
updateFile(pathFile, testResult)
}

// Starting the simulation
const pathFile = path.resolve(path.dirname("")) + "/" + RESULT_FILENAME;
console.info(`Simulation results will be generated in ${pathFile}`);
const pathFile = path.resolve(path.dirname('')) + '/' + RESULT_FILENAME
console.info(`Simulation results will be generated in ${pathFile}`)

// Wait for one second before starting the world simulation
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 1000))

try {
setInterval(() => {
worldSimulation.update();
worldSimulation.update()

// Generate the txt simulation-results file
generateTxtStats(worldSimulation, pathFile);
generateTxtStats(worldSimulation, pathFile)

// Output the result
console.log(worldSimulation.generateEmojiGraph());
}, INTERVAL_TIMEOUT_MS);
console.log(worldSimulation.generateEmojiGraph())
}, INTERVAL_TIMEOUT_MS)
} catch (err) {
// Console log exception error messages
console.error(err.message);
console.error(err.message)
}
18 changes: 9 additions & 9 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const SCRIPT_CHARSET = "utf-8";
export const INTERVAL_TIMEOUT_MS = 1000;
export const SCRIPT_CHARSET = 'utf-8'
export const INTERVAL_TIMEOUT_MS = 1000

// World definition
export const WORLD = {
Expand All @@ -8,18 +8,18 @@ export const WORLD = {
height: 80,
width: 80,
foodDensity: 20,
};
}

export const ANIMALS = {
maxHealthScore: 20
maxHealthScore: 20,
}

export const LIVING_THINGS = {
// Those values are the class names
rabbit: "Rabbit",
wolf: "Wolf",
carrot: "Carrot",
};
rabbit: 'Rabbit',
wolf: 'Wolf',
carrot: 'Carrot',
}

// Food
export const CARROTS_QUANTITY = 1000;
export const CARROTS_QUANTITY = 1000
22 changes: 11 additions & 11 deletions src/models/__tests__/carrot.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CARROTS_QUANTITY } from "../../constants.js";
import Carrot from "../carrot.js";
import test from "ava";
import { CARROTS_QUANTITY } from '../../constants.js'
import Carrot from '../carrot.js'
import test from 'ava'

test.beforeEach((t) => {
t.context.carrot = new Carrot();
});
test.beforeEach(t => {
t.context.carrot = new Carrot()
})

test("carrot must decrease by 1 when a carrot is eaten", (t) => {
t.context.carrot.eaten();
test('carrot must decrease by 1 when a carrot is eaten', t => {
t.context.carrot.eaten()

const actual = t.context.carrot.getRemainingQuantity();
t.is(actual, CARROTS_QUANTITY - 1);
});
const actual = t.context.carrot.getRemainingQuantity()
t.is(actual, CARROTS_QUANTITY - 1)
})
44 changes: 22 additions & 22 deletions src/models/__tests__/rabbit.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import Rabbit from "../rabbit.js";
import World from "../world.js";
import test from "ava";
import Rabbit from '../rabbit.js'
import World from '../world.js'
import test from 'ava'

test.beforeEach((t) => {
const world = new World();
t.context.rabbit = new Rabbit(world);
});
test.beforeEach(t => {
const world = new World()
t.context.rabbit = new Rabbit(world)
})

test("rabbit must be able to reproduce themself by default", (t) => {
t.true(t.context.rabbit.canBeReproduced());
});
test('rabbit must be able to reproduce themself by default', t => {
t.true(t.context.rabbit.canBeReproduced())
})

test("rabbit doesn't have barriers in front of them", (t) => {
const size = { horizontal: 30, vertical: 40 };
t.true(t.context.rabbit.noBarriers(size, size));
});
test("rabbit doesn't have barriers in front of them", t => {
const size = { horizontal: 30, vertical: 40 }
t.true(t.context.rabbit.noBarriers(size, size))
})

test("set to new home-place must changes the position", (t) => {
const currentHorizontalPosition = t.context.rabbit.position.horizontal;
const currentVerticalPosition = t.context.rabbit.position.vertical;
test('set to new home-place must changes the position', t => {
const currentHorizontalPosition = t.context.rabbit.position.horizontal
const currentVerticalPosition = t.context.rabbit.position.vertical

t.context.rabbit.chase();
t.context.rabbit.setToNewPlace();
t.context.rabbit.chase()
t.context.rabbit.setToNewPlace()

t.is(currentHorizontalPosition, t.context.rabbit.position.horizontal);
t.is(currentVerticalPosition, t.context.rabbit.position.vertical);
});
t.is(currentHorizontalPosition, t.context.rabbit.position.horizontal)
t.is(currentVerticalPosition, t.context.rabbit.position.vertical)
})
44 changes: 22 additions & 22 deletions src/models/__tests__/wolf.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import Wolf from "../wolf.js";
import World from "../world.js";
import test from "ava";
import Wolf from '../wolf.js'
import World from '../world.js'
import test from 'ava'

test.beforeEach((t) => {
const world = new World();
t.context.wolf = new Wolf(world);
});
test.beforeEach(t => {
const world = new World()
t.context.wolf = new Wolf(world)
})

test("wolf must be able to reproduce themself by default", (t) => {
t.true(t.context.wolf.canBeReproduced());
});
test('wolf must be able to reproduce themself by default', t => {
t.true(t.context.wolf.canBeReproduced())
})

test("wolf doesn't have barriers in front of them", (t) => {
const size = { horizontal: 30, vertical: 40 };
t.true(t.context.wolf.noBarriers(size, size));
});
test("wolf doesn't have barriers in front of them", t => {
const size = { horizontal: 30, vertical: 40 }
t.true(t.context.wolf.noBarriers(size, size))
})

test("set to new home-place must changes the position", (t) => {
const currentHorizontalPosition = t.context.wolf.position.horizontal;
const currentVerticalPosition = t.context.wolf.position.vertical;
test('set to new home-place must changes the position', t => {
const currentHorizontalPosition = t.context.wolf.position.horizontal
const currentVerticalPosition = t.context.wolf.position.vertical

t.context.wolf.chase();
t.context.wolf.moveTowards();
t.context.wolf.chase()
t.context.wolf.moveTowards()

t.is(currentHorizontalPosition, t.context.wolf.position.horizontal);
t.is(currentVerticalPosition, t.context.wolf.position.vertical);
});
t.is(currentHorizontalPosition, t.context.wolf.position.horizontal)
t.is(currentVerticalPosition, t.context.wolf.position.vertical)
})
Loading

0 comments on commit 7084aa5

Please sign in to comment.