Skip to content

Commit 4f2edfc

Browse files
author
Your Name
committed
Nearly done
1 parent 91e006d commit 4f2edfc

File tree

9 files changed

+608
-45
lines changed

9 files changed

+608
-45
lines changed

app.js

Lines changed: 92 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,101 @@
1+
// NPM moudles
2+
var express = require('express');
3+
var app = express();
4+
var http = require('http').Server(app);
5+
var io = require('socket.io')(http);
6+
7+
app.use(express.static('.'));
8+
9+
app.get('/', function (req, res) {
10+
res.redirect('/index.html');
11+
});
12+
13+
http.listen(3000, function () {
14+
console.log('listening on *:3000');
15+
});
16+
17+
118
var grassArray = [];
219
var grassEaterArray = [];
320
var predatorArray = [];
421
var humanArray = [];
522
var fireArray = [];
6-
var matrix = [ //xotakerner@ ev gishatichner@ stugox matric -- piti uten xoter@ bazmanan heto satken (hnarhavor e cnvi gishatich)
23+
/*var matrix = [ //xotakerner@ ev gishatichner@ stugox matric -- piti uten xoter@ bazmanan heto satken (hnarhavor e cnvi gishatich)
724
[1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
825
[0, 2, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
926
[1, 0, 1, 1, 2, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
1027
[0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
1128
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
12-
]
13-
// some npm installations
14-
var express = require('express');
15-
var app = express();
16-
var server = require('http').Server(app);
17-
var io = require('socket.io')(server);
18-
// get all the character modules
19-
var Grass = require("./public/finalproject/grass.js");
20-
var grassEater = require("./public/finalproject/grassEater.js");
21-
var predator = require("./public/finalproject/predator.js");
22-
var human = require("./public/finalproject/human.js");
23-
var fire = require("./public/finalproject/fire.js");
24-
25-
function random(array){ // there is no p5.js on server side so need to replace all it's functions
26-
if(Array.isArray(array)){
27-
var index = Math.floor(Math.random() * array.length);
28-
return array[index];
29-
}
30-
}
29+
]*/
30+
function getRandomMatrix() {
31+
var n = 10; // y arancq
32+
var m = 20; // x arancq
33+
var percent1 = 30;
34+
var percent2 = 2;
35+
var greenNumber = parseInt(n * m * percent1 / 100);
36+
var greenEaterNumber = parseInt(n * m * percent2 / 100);
37+
var humanNumber = 1;
38+
var matrix = [];
39+
for (var i = 0; i < n; i++) { //create empty array
40+
var arr = [];
41+
for (var j = 0; j < m; j++) {
42+
arr[j] = 0;
43+
}
44+
matrix.push(arr);
45+
}
46+
for (var i = 0; i < greenNumber; i++) { //fill with 1 => green (random slot);
47+
var x = Math.floor(Math.random() * matrix[0].length);
48+
var y = Math.floor(Math.random() * matrix.length);
49+
if (matrix[y][x] == 0) { //check if the slot is empty
50+
matrix[y][x] = 1;
51+
}
52+
else { // if not try once more
53+
i--;
54+
continue;
55+
}
56+
}
3157

32-
app.use(express.static(".")); // set the directory
58+
for (var j = 0; j < greenEaterNumber; j++) { //fill with 2 => yellow (random slot);
59+
var x = Math.floor(Math.random() * matrix[0].length);
60+
var y = Math.floor(Math.random() * matrix.length);
61+
if (matrix[y][x] == 0) { //check if the slot is empty
62+
matrix[y][x] = 2;
63+
}
64+
else { // if not try once more
65+
j--;
66+
continue;
67+
}
68+
}
3369

34-
app.get("/", function(req, res){ //redirect to html file
35-
res.redirect("index.html");
36-
});
70+
for (var e = 0; e < humanNumber; e++) { // add a hunter in a random empty slot
71+
var x = Math.floor(Math.random() * matrix[0].length);
72+
var y = Math.floor(Math.random() * matrix.length);
73+
if (matrix[y][x] == 0) { //check if the slot is empty
74+
matrix[y][x] = 7;
75+
}
76+
else { // if not try once more
77+
e--;
78+
continue;
79+
}
80+
}
81+
return matrix;
82+
}
83+
// get all the character modules
84+
var Grass = require("./grass.js");
85+
var grassEater = require("./grassEater.js");
86+
var predator = require("./predator.js");
87+
var human = require("./human.js");
88+
var fire = require("./fire.js");
3789

38-
app.listen(3000, function(){ // run the port
39-
console.log("Example is running on port 3000");
40-
});
90+
/*function random(array) { // there is no p5.js on server side so need to replace all it's functions
91+
if (Array.isArray(array)) {
92+
var index = Math.floor(Math.random() * array.length);
93+
return array[index];
94+
}
95+
}
4196
4297
43-
for (var y = 0; y < matrix.length; y++) {
98+
for (var y = 0; y < matrix.length; y++) {
4499
for (var x = 0; x < matrix[y].length; x++) {
45100
if (matrix[y][x] == 1) {
46101
var xot = new Grass(x, y, 1);
@@ -59,9 +114,9 @@ for (var y = 0; y < matrix.length; y++) {
59114
humanArray.push(mard);
60115
}
61116
}
62-
}
117+
}*/
63118

64-
function drawInfo(){
119+
/*function drawInfo(){
65120
if (fireArray[0]) {
66121
fireArray[0].spread();
67122
}
@@ -84,7 +139,11 @@ function drawInfo(){
84139
85140
console.log(matrix);
86141
}
87-
setInterval(drawInfo, 1000);
142+
setInterval(drawInfo, 1000);*/
143+
88144
io.on('connection', function (socket) {
89-
io.sockets.emit("get new matrix", matrix);
90-
})
145+
console.log('a user connected');
146+
setInterval(function () {
147+
socket.emit("display new matrix", getRandomMatrix());
148+
}, 200)
149+
});

class.main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = class Main{
2+
constructor(x, y, index){
3+
this.x = x;
4+
this.y = y;
5+
this.count = 0;
6+
this.energy = 5;
7+
this.directions = [];
8+
}
9+
findSlots(identifier) {
10+
this.directions = [
11+
[this.x - 1, this.y - 1],
12+
[this.x, this.y - 1],
13+
[this.x + 1, this.y - 1],
14+
[this.x - 1, this.y],
15+
[this.x + 1, this.y],
16+
[this.x - 1, this.y + 1],
17+
[this.x, this.y + 1],
18+
[this.x + 1, this.y + 1]
19+
];
20+
var found = [];
21+
for (var i in this.directions) {
22+
var x = this.directions[i][0];
23+
var y = this.directions[i][1];
24+
if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) {
25+
if (matrix[y][x] == identifier) {
26+
found.push(this.directions[i]);
27+
}
28+
}
29+
}
30+
return found;
31+
}
32+
}

drawscript.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11

22
var socket = io.connect('http://localhost:3000'); // connect
3-
var matrix = [];
43

54
socket.on("send image", function (data) {
6-
matrix = data;
5+
matrix = data;
76
})
87

98
var side = 20;
@@ -12,38 +11,34 @@ var side = 20;
1211
function setup() {
1312
frameRate(4);
1413
noStroke();
15-
createCanvas(matrix[0].length * side, matrix.length * side);
16-
background('#ee4400');
14+
createCanvas(60 * side, 40 * side);
15+
background('#acacac');
1716
}
1817

1918

20-
function draw() {
19+
socket.on("display new matrix", function (matrix) {
20+
background("#acacac");
2121
for (var y = 0; y < matrix.length; y++) {
2222
for (var x = 0; x < matrix[y].length; x++) {
2323
if (matrix[y][x] == 1) {
2424
fill('green');
25-
rect(x * side, y * side, side, side);
2625
}
2726
else if (matrix[y][x] == 2) {
2827
fill('yellow');
29-
rect(x * side, y * side, side, side);
3028
}
3129
else if (matrix[y][x] == 3) {
3230
fill('black');
33-
rect(x * side, y * side, side, side);
3431
}
3532
else if (matrix[y][x] == 4) {
3633
fill("red");
37-
rect(x * side, y * side, side, side);
3834
}
3935
else if (matrix[y][x] == 7) {
4036
fill("#404efd");
41-
rect(x * side, y * side, side, side);
4237
}
4338
else {
4439
fill('#acacac');
45-
rect(x * side, y * side, side, side);
4640
}
41+
rect(x * side, y * side, side, side);
4742
}
4843
}
49-
}
44+
})

fire.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
module.exports = class fire{
2+
constructor(x, y){
3+
this.x = x;
4+
this.y = y;
5+
this.count = 0;
6+
this.directions = [
7+
[this.x - 1, this.y - 1],
8+
[this.x, this.y - 1],
9+
[this.x + 1, this.y - 1],
10+
[this.x - 1, this.y],
11+
[this.x + 1, this.y],
12+
[this.x - 1, this.y + 1],
13+
[this.x, this.y + 1],
14+
[this.x + 1, this.y + 1]
15+
];
16+
this.toRemove = [];
17+
}
18+
spread(){ // spread once per 2 tacts
19+
if(this.count == 1){
20+
for(var i in this.directions){
21+
var slot = this.directions[i];
22+
if(matrix[slot[1]]){
23+
if(matrix[slot[1]][slot[0]] != undefined){
24+
this.toRemove.push(slot); // add slot to an array
25+
if(matrix[slot[1]][slot[0]] == 1){ // is grass -- remove
26+
for(var i = 0; i < grassArray.length; i++) {
27+
if(slot[0] == grassArray[i].x && slot[1] == grassArray[i].y) {
28+
grassArray.splice(i, 1);
29+
break;
30+
}
31+
}
32+
}
33+
else if(matrix[slot[1]][slot[0]] == 2){ // is grassEater -- remove
34+
for(var i = 0; i < grassEaterArray.length; i++) {
35+
if(slot[0] == grassEaterArray[i].x && slot[1] == grassEaterArray[i].y) {
36+
grassEaterArray.splice(i, 1);
37+
break;
38+
}
39+
}
40+
}
41+
else if(matrix[slot[1]][slot[0]] == 3){ // is predator -- remove
42+
for(var i = 0; i < predatorArray.length; i++) {
43+
if(slot[0] == predatorArray[i].x && slot[1] == predatorArray[i].y) {
44+
predatorArray.splice(i, 1);
45+
break;
46+
}
47+
}
48+
}
49+
matrix[slot[1]][slot[0]] = 4;
50+
}
51+
}
52+
}
53+
}
54+
else if(this.count == 2){
55+
this.directions = [ //another 16 slots =(
56+
[this.x - 2, this.y - 2],
57+
[this.x - 1, this.y - 2],
58+
[this.x, this.y - 2],
59+
[this.x + 1, this.y - 2],
60+
[this.x + 2, this.y - 2],
61+
[this.x + 2, this.y - 1],
62+
[this.x + 2, this.y],
63+
[this.x + 2, this.y + 1],
64+
[this.x + 2, this.y + 2],
65+
[this.x + 1, this.y + 2],
66+
[this.x, this.y + 2],
67+
[this.x - 1, this.y + 2],
68+
[this.x - 2, this.y + 2],
69+
[this.x - 2, this.y + 1],
70+
[this.x - 2, this.y],
71+
[this.x - 2, this.y - 1],
72+
];
73+
for(var i in this.directions){
74+
var slot = this.directions[i];
75+
if(matrix[slot[1]]){
76+
if(matrix[slot[1]][slot[0]] != undefined){
77+
this.toRemove.push(slot); // add slot to the toRemove array
78+
if(matrix[slot[1]][slot[0]] == 1){ // is grass -- remove
79+
for(var i = 0; i < grassArray.length; i++) {
80+
if(slot[0] == grassArray[i].x && slot[1] == grassArray[i].y) {
81+
grassArray.splice(i, 1);
82+
break;
83+
}
84+
}
85+
}
86+
else if(matrix[slot[1]][slot[0]] == 2){ // is grassEater -- remove
87+
for(var i = 0; i < grassEaterArray.length; i++) {
88+
if(slot[0] == grassEaterArray[i].x && slot[1] == grassEaterArray[i].y) {
89+
grassEaterArray.splice(i, 1);
90+
break;
91+
}
92+
}
93+
}
94+
else if(matrix[slot[1]][slot[0]] == 3){ // is predator -- remove
95+
for(var i = 0; i < predatorArray.length; i++) {
96+
if(slot[0] == predatorArray[i].x && slot[1] == predatorArray[i].y) {
97+
predatorArray.splice(i, 1);
98+
break;
99+
}
100+
}
101+
}
102+
matrix[slot[1]][slot[0]] = 4;
103+
}
104+
}
105+
106+
}
107+
}
108+
else if(this.count >= 5){ // clean the area with fire at the end
109+
for(var e in this.toRemove){
110+
var slot = this.toRemove[e];
111+
matrix[slot[1]][slot[0]] = 0;
112+
}
113+
matrix[fireArray[0].y][fireArray[0].x] = 0;
114+
fireArray = []; // empty fireArray
115+
}
116+
this.count++;
117+
}
118+
}

0 commit comments

Comments
 (0)