Skip to content

Commit

Permalink
Merge pull request #4 from xAlessandroC/feat/main-logic
Browse files Browse the repository at this point in the history
Feat/main logic
  • Loading branch information
xAlessandroC authored Oct 26, 2023
2 parents 35eb6ce + 2311027 commit eb7906e
Show file tree
Hide file tree
Showing 14 changed files with 759 additions and 23 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/multi-platform-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ jobs:
- name: Run Flask server tests
run: python -m unittest test/app_test.py

windows-support:
runs-on: windows-latest
steps:
- name: Set up Python 3.10.9
uses: actions/[email protected]
with:
python-version: 3.10.9
- name: Print the version of Python # DEBUG
run: python --version
- name: Checkout the repository
uses: actions/[email protected]
- name: Print repo structure # DEBUG
run: dir && dir gameoflife && dir test
- name: Install dependencies
run: pip install -r ./requirements.txt
- name: Print dependencies # DEBUG
run: pip freeze
- name: Run Flask server tests
run: python -m unittest test/app_test.py
# windows-support:
# runs-on: windows-latest
# steps:
# - name: Set up Python 3.10.9
# uses: actions/[email protected]
# with:
# python-version: 3.10.9
# - name: Print the version of Python # DEBUG
# run: python --version
# - name: Checkout the repository
# uses: actions/[email protected]
# - name: Print repo structure # DEBUG
# run: dir && dir gameoflife && dir test
# - name: Install dependencies
# run: pip install -r ./requirements.txt
# - name: Print dependencies # DEBUG
# run: pip freeze
# - name: Run Flask server tests
# run: python -m unittest test/app_test.py

dockerize:
runs-on: ubuntu-22.04
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ RUN pip install -r requirements.txt
# Copy app source
COPY . .

WORKDIR /app/gameoflife
EXPOSE 5000
CMD [ "flask", "--app", "game-of-life/app.py", "--debug", "run", "--host", "0.0.0.0", "--port", "5000"]
CMD [ "flask", "--app", "app", "--debug", "run", "--host", "0.0.0.0", "--port", "5000"]
6 changes: 4 additions & 2 deletions gameoflife/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from flask import Flask
import os
from flask import Flask, render_template


app = Flask(__name__)

@app.route("/")
def hello_world():
return "<h1>Conway's Game of Life</h1>"
return render_template('main.html')
8 changes: 8 additions & 0 deletions gameoflife/static/js/cell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Cell{
constructor(x,y,alive){
this.x = x;
this.y = y;
this.alive = alive;
}

}
58 changes: 58 additions & 0 deletions gameoflife/static/js/grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class GridQT{
//starting point, width, height
constructor(rectangle){
this.rectangle=rectangle;

this.matrix = [];
for(var i=this.rectangle.start_col; i<this.rectangle.width; i++) {
this.matrix[i] = [];
for(var j=this.rectangle.start_row; j<this.rectangle.height; j++) {
this.matrix[i][j] = new Cell(i,j,0);
}
}
}

get start_col(){
return this.rectangle.start_col
}

get start_row(){
return this.rectangle.start_row
}

get width(){
return this.rectangle.width
}

get height(){
return this.rectangle.height
}

insert(cell){
this.matrix[cell.x][cell.y]=cell;
}

contains(cell){
var res=this.matrix[cell.x][cell.y]
return res.alive
}

substitute(newMatrix,rectangle){
for(var i=rectangle.start_col; i<rectangle.start_col+rectangle.width; i++) {
for(var j=rectangle.start_row; j<rectangle.start_row+rectangle.height; j++) {
if(newMatrix[i][j].alive===true){
if(grid.matrix[i][j].alive!==true){
quadtree.insert(new Cell(i,j,true))
grid.matrix[i][j]=new Cell(i,j,true)
}
}else{
if(grid.matrix[i][j].alive===true){
quadtree.remove(i,j)
grid.matrix[i][j]=new Cell(i,j,false)
}
}
}
}
}

}
86 changes: 86 additions & 0 deletions gameoflife/static/js/logic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function getNeighbourAlive(col,row){
var result=0;
var i,j;
for( i=col-1;i<=col+1;i++) {
for( j=row-1;j<=row+1;j++) {
if(i>=0 && j>=0 &&
i<grid.start_col+grid.width &&
j<grid.start_row+grid.height
&& !(i==col && j==row)) {

if(grid.matrix[i][j].alive) {
result++;

}
}
}
}
return result;
}

function nextRectGen(rect,result){
//rectangle=rect;
var newSC=rect.start_col;
var newSR=rect.start_row;
var newH=rect.height;
var newW=rect.width;

if(rect.start_col>0){
newSC=newSC-1
newW=newW+1
}
if(rect.start_row>0){
newSR=newSR-1
newH=newH+1
}
if(grid.start_col+grid.width-1>rect.start_col+rect.width-1)
newW=newW+1
if(grid.start_row+grid.height-1>rect.start_row+rect.height-1)
newH=newH+1

var rectangle=new Rectangle(newSC,newSR,newW,newH)

var newMatrix=[];
for(var i=rectangle.start_col; i<rectangle.start_col+rectangle.width; i++) {
newMatrix[i] = [];
for(var j=rectangle.start_row; j<rectangle.start_row+rectangle.height; j++) {
newMatrix[i][j] = []
}
}

for( i=rectangle.start_col; i<rectangle.start_col+rectangle.width; i++) {
for(var j=rectangle.start_row; j<rectangle.start_row+rectangle.height; j++) {
var numberAlive=this.getNeighbourAlive(i, j);
var settato=false;
if(grid.matrix[i][j].alive) {
if(numberAlive<2 || numberAlive>3) {
newMatrix[i][j]=new Cell(i,j,false);
settato=true;
}else{
newMatrix[i][j]=new Cell(i,j,true);
}
}else {
if(numberAlive==3) {
newMatrix[i][j]=new Cell(i,j,true);
settato=true;
}else{
newMatrix[i][j]=new Cell(i,j,false);
}
}
}
}

result.push([newMatrix,rectangle])
}

function nextGen(){
regions = quadtree.query()
var result = []
regions.forEach(function(e){
nextRectGen(e,result)
});

result.forEach(function(e){
grid.substitute(e[0],e[1],grid)
});
}
48 changes: 48 additions & 0 deletions gameoflife/static/js/normalLogic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function normalGetNeighbourAlive(col,row){
var result=0;
var i,j;
for( i=col-1;i<=col+1;i++) {
for( j=row-1;j<=row+1;j++) {
if(i>=0 && j>=0 && i<grid.start_col+grid.width && j<grid.start_row+grid.height && !(i==col && j==row)) {
if(grid.matrix[i][j].alive) {
result++;
}
}
}
}
return result;
}

function normalNextGen(){
var newMatrix=[];
for( i=grid.start_col; i<grid.start_col+grid.width; i++) {
newMatrix[i] = [];
for(var j=grid.start_row; j<grid.start_row+grid.height; j++) {
newMatrix[i][j] = []
}
}

for( i=grid.start_col; i<grid.start_col+grid.width; i++) {
for(var j=grid.start_row; j<grid.start_row+grid.height; j++) {
var numberAlive=this.normalGetNeighbourAlive(i, j);
var settato=false;
if(grid.matrix[i][j].alive) {
if(numberAlive<2 || numberAlive>3) {
newMatrix[i][j]=new Cell(i,j,false);
settato=true;
}else{
newMatrix[i][j]=new Cell(i,j,true);
}
}else {
if(numberAlive==3) {
newMatrix[i][j]=new Cell(i,j,true);
settato=true;
}else{
newMatrix[i][j]=new Cell(i,j,false);
}
}
}
}

grid.matrix=newMatrix;
}
111 changes: 111 additions & 0 deletions gameoflife/static/js/pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
function setPattern(pattern){
mousePattern=pattern
console.log("pattern settato: ", pattern)
}

function showPattern(x,y,pattern){
fill(255,255,255);
if(pattern === "insert"){
drawRect(x,y)
}
if(pattern === "glider"){
drawRect(x,y-1)
drawRect(x,y+1)
drawRect(x+1,y)
drawRect(x+1,y+1)
drawRect(x-1,y+1)
}
if(pattern === "oscillator"){
drawRect(x,y)
drawRect(x,y-1)
drawRect(x,y+1)
}
if(pattern === "10row"){
drawRect(x,y)
drawRect(x-1,y)
drawRect(x-2,y)
drawRect(x-3,y)
drawRect(x-4,y)
drawRect(x-5,y)
drawRect(x+1,y)
drawRect(x+2,y)
drawRect(x+3,y)
drawRect(x+4,y)
}
if(pattern === "tumbler"){
drawRect(x-1,y)
drawRect(x-1,y-1)
drawRect(x-1,y-2)
drawRect(x-1,y+1)
drawRect(x-1,y+2)
drawRect(x+1,y)
drawRect(x+1,y-1)
drawRect(x+1,y-2)
drawRect(x+1,y+1)
drawRect(x+1,y+2)
drawRect(x-2,y-1)
drawRect(x-2,y-2)
drawRect(x+2,y-1)
drawRect(x+2,y-2)
drawRect(x+2,y+3)
drawRect(x-2,y+3)
drawRect(x+3,y+3)
drawRect(x+3,y+2)
drawRect(x+3,y+1)
drawRect(x-3,y+3)
drawRect(x-3,y+1)
drawRect(x-3,y+2)
}
}

function insertPattern(x,y,pattern){
if(pattern === "glider"){
fill(255,255,255);
quadtree.insert(new Cell(x,y-1,true));
quadtree.insert(new Cell(x,y+1,true));
quadtree.insert(new Cell(x+1,y,true));
quadtree.insert(new Cell(x+1,y+1,true));
quadtree.insert(new Cell(x-1,y+1,true));
}
if(pattern === "oscillator"){
quadtree.insert(new Cell(x,y,true));
quadtree.insert(new Cell(x,y+1,true));
quadtree.insert(new Cell(x,y-1,true));
}
if(pattern === "10row"){
quadtree.insert(new Cell(x,y,true));
quadtree.insert(new Cell(x-1,y,true));
quadtree.insert(new Cell(x-2,y,true));
quadtree.insert(new Cell(x-3,y,true));
quadtree.insert(new Cell(x-4,y,true));
quadtree.insert(new Cell(x-5,y,true));
quadtree.insert(new Cell(x+1,y,true));
quadtree.insert(new Cell(x+2,y,true));
quadtree.insert(new Cell(x+3,y,true));
quadtree.insert(new Cell(x+4,y,true));
}
if(pattern === "tumbler"){
quadtree.insert(new Cell(x-1,y,true));
quadtree.insert(new Cell(x-1,y-1,true));
quadtree.insert(new Cell(x-1,y-2,true));
quadtree.insert(new Cell(x-1,y+1,true));
quadtree.insert(new Cell(x-1,y+2,true));
quadtree.insert(new Cell(x+1,y,true));
quadtree.insert(new Cell(x+1,y-1,true));
quadtree.insert(new Cell(x+1,y-2,true));
quadtree.insert(new Cell(x+1,y+1,true));
quadtree.insert(new Cell(x+1,y+2,true));
quadtree.insert(new Cell(x-2,y-1,true));
quadtree.insert(new Cell(x-2,y-2,true));
quadtree.insert(new Cell(x+2,y-1,true));
quadtree.insert(new Cell(x+2,y-2,true));
quadtree.insert(new Cell(x+2,y+3,true));
quadtree.insert(new Cell(x-2,y+3,true));
quadtree.insert(new Cell(x+3,y+3,true));
quadtree.insert(new Cell(x+3,y+2,true));
quadtree.insert(new Cell(x+3,y+1,true));
quadtree.insert(new Cell(x-3,y+3,true));
quadtree.insert(new Cell(x-3,y+1,true));
quadtree.insert(new Cell(x-3,y+2,true));
}
}
Loading

0 comments on commit eb7906e

Please sign in to comment.