-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from xAlessandroC/feat/main-logic
Feat/main logic
- Loading branch information
Showing
14 changed files
with
759 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.