patternjavascriptMinor
Main implementation of Game of Life
Viewed 0 times
lifeimplementationgamemain
Problem
This is my implementation of Conway's Game of Life in JavaScript. While I know that it runs slowly, I do not ask for performance improvements, but for a review on the code quality in general from professional JavaScript programmers.
Working HTML/CSS/JS files can be found here.
Here is the main part of the code:
```
(function(){
'use strict';
function getControl(api){
var buttonsControll = document.getElementsByTagName('canvas'),
buttonsRuleAlive = document.querySelectorAll('.btn_ruleAlive'),
buttonsRuleBorn = document.querySelectorAll('.btn_ruleBorn'),
button;
function drawRectangle(canvas, x){
canvas.fillRect(x, 1, 5, 10);
}
function drawTriangle(canvas, x, direction){
canvas.beginPath();
canvas.moveTo(x, 1);
canvas.lineTo(x, 11);
canvas.lineTo(x + 10 * direction, 6);
canvas.lineTo(x, 1);
canvas.fill();
}
function getIntervalTime(){
return parseInt(document.getElementById('tb_msPerFrame').value);
}
function getFields(){
return parseInt(document.getElementById('tb_fields').value);
}
function setNeighbors(){
var neighbors = parseInt(document.getElementById('sl_neighbors').value);
for(var i = 0; i indexOf){
setRulesBorn(preset[i], 1);
}
}
}
function setRulesAlive(i, rule){
var ruleName = ['dieing', 'alive'];
if(buttonsRuleAlive[i].className != 'btn_rule btn_ruleAlive btn_none'){
buttonsRuleAlive[i].className = 'btn_rule btn_ruleAlive btn_' + ruleName[rule];
}
api.setRulesAlive(i, rule);
}
function setRulesBorn(i, rule){
var ruleName = ['dieing', 'born'];
if(buttonsRuleBorn[i].className != 'btn_rule btn_ruleBorn btn_none'){
but
Working HTML/CSS/JS files can be found here.
Here is the main part of the code:
```
(function(){
'use strict';
function getControl(api){
var buttonsControll = document.getElementsByTagName('canvas'),
buttonsRuleAlive = document.querySelectorAll('.btn_ruleAlive'),
buttonsRuleBorn = document.querySelectorAll('.btn_ruleBorn'),
button;
function drawRectangle(canvas, x){
canvas.fillRect(x, 1, 5, 10);
}
function drawTriangle(canvas, x, direction){
canvas.beginPath();
canvas.moveTo(x, 1);
canvas.lineTo(x, 11);
canvas.lineTo(x + 10 * direction, 6);
canvas.lineTo(x, 1);
canvas.fill();
}
function getIntervalTime(){
return parseInt(document.getElementById('tb_msPerFrame').value);
}
function getFields(){
return parseInt(document.getElementById('tb_fields').value);
}
function setNeighbors(){
var neighbors = parseInt(document.getElementById('sl_neighbors').value);
for(var i = 0; i indexOf){
setRulesBorn(preset[i], 1);
}
}
}
function setRulesAlive(i, rule){
var ruleName = ['dieing', 'alive'];
if(buttonsRuleAlive[i].className != 'btn_rule btn_ruleAlive btn_none'){
buttonsRuleAlive[i].className = 'btn_rule btn_ruleAlive btn_' + ruleName[rule];
}
api.setRulesAlive(i, rule);
}
function setRulesBorn(i, rule){
var ruleName = ['dieing', 'born'];
if(buttonsRuleBorn[i].className != 'btn_rule btn_ruleBorn btn_none'){
but
Solution
There is a ton here, these are my 2 cents:
- Read about Model View Controller, and implement it
- More specifically, knowing the state of a cell by checking the class name is bad form
- Read about the cool Array functions that exist ( filter, forEach etc. ), they could make a huge difference in your logic code
- Read about DRY ( Dont Repeat Yourself ), your code should be much DRYer
Context
StackExchange Code Review Q#20250, answer score: 2
Revisions (0)
No revisions yet.