Permalink
Browse files

refactored a lot of shit

  • Loading branch information...
oaishllib committed Oct 12, 2017
1 parent ee3d3e2 commit 3127d614611dd28c1fb9186c9930f630f7c61f39
Showing with 875 additions and 276 deletions.
  1. +111 −0 js/refactored.js
  2. +66 −2 js2/adj.js
  3. +59 −0 js2/app.js
  4. +28 −0 js2/arr.js
  5. +3 −3 js2/board.js
  6. +10 −1 js2/board2.js
  7. +64 −17 js2/cell2.js
  8. +0 −30 js2/functionobject.js
  9. +239 −0 js2/legacy.js
  10. +178 −223 js2/main2.js
  11. +117 −0 js2/render.js
View
@@ -0,0 +1,111 @@
function handleClick(evt) {
var cell = game.retrieveCell(...parse(evt.target.id));
var neighbors =
function neighbors(param) {
return adjacenters(...parse(cell.id)).forEach(function(idx) { console.log( GAME.board.retrieveCell(idx[0], idx[1])[param])});
}
if (!cell.revealed) {
cell.revealed = true;
}
adjCellOpen(cellies);
render.stateChange();
if (GAME.flag === true) {
GAME.board.setCell(clicked);
} else {
if (clicked.mine) {
GAME.lose();
console.log('mine');
}
}
}
const props = {};
const game = {};
function setGameProps(evt) {
var event = evt.target.id;
if (event === 'minecount_input') {
props.mineCount = evt.target.value;
} else if (event === 'width_input') {
props.widthInput = evt.target.value;
} else if (event === 'length_input') {
props.lengthInput = evt.target.value;
}
}
function display(mines) {
game = new Board(props.widthInput, props.lengthInput);
if (mines) {
mines(props.mineCount, (props.widthInput*props.lengthInput));
}
}
function mines(p, q) {
if (p === 0) {
return
}
let n = GAME.board.retrieveCell(...parse(Math.floor(Math.random() * q)));
n.mine ? mines(p, q) : (
n.mine = true,
mines(p - 1, q)
);
}
document.getElementById('minecount_input').addEventListener('input', setGameProps);
document.getElementById('width_input').addEventListener('input', setGameProps);
document.getElementById('length_input').addEventListener('input', setGameProps);
document.getElementById('play_button').addEventListener('click', display);
document.getElementById('button').addEventListener('click', render.createCells);
function render() {
this.createCells = function() {
let table = document.createElement('table');
for (let i = 0; i < props.width; i++) {
let row = document.createElement('tr');
for (let j = 0; j < props.length; j++ ) {
let cell = document.createElement('td');
cell.id = i * props.width + j;
row.appendChild(cell);
}
table.appendChild(row);
};
table.addEventListener('click', handleClick);
document.body.appendChild(table);
};
this.stateChange = function() {
for (let i = 0; i < props.width; i++) {
for (let j = 0; j < props.length; j++ ) {
let td = document.getElementById(i * props.width + j);
game.retrieveCell(i, j).revealed ? (
td.classname,
td.textContent = game.retrieveCell(i, j).number
) : '';
}
}
}
}
View
@@ -1,8 +1,72 @@
function handleClick(evt) {
var cell = GAME.board.retrieveCell(...parse(evt.target.id));
var neighbors =
function neighbors(param) {
return adjacenters(...parse(cell.id)).forEach(function(idx) { console.log( GAME.board.retrieveCell(idx[0], idx[1])[param])});
}
if (!cell.revealed) {
cell.revealed = true;
}
adjCellOpen(cellies);
render.stateChange();
if (GAME.flag === true) {
GAME.board.setCell(clicked);
} else {
if (clicked.mine) {
GAME.lose();
console.log('mine');
}
}
}
function mines(p, q) {
if (p < 1) {
return;
}
let n = GAME.board.retrieveCell(...parse(Math.floor(Math.random() * q)));
n.mine ? mines(p, q) : (
n.mine = true,
mines(p - 1, q)
);
}
var corners = {
topLeft: this.id - row - 1
const props = {};
function setGameProps(evt) {
var event = evt.target.id;
if (event === 'minecount_input') {
props.mineCount = evt.target.value;
} else if (event === 'width_input') {
props.widthInput = evt.target.value;
} else if (event === 'length_input') {
props.lengthInput = evt.target.value;
}
}
function display() {
mines(props.mineCount, (props.widthInput*props.lengthInput));
}
document.getElementById('minecount_input').addEventListener('input', setGameProps);
document.getElementById('width_input').addEventListener('input', setGameProps);
document.getElementById('length_input').addEventListener('input', setGameProps);
document.getElementById('play_button').addEventListener('click', display);
View
@@ -0,0 +1,59 @@
function parse(id) {
var y = id % GAME.length;
var x = (id - y ) / GAME.width;
return [x, y]
}
function adjacenters(coordX, coordY) {
var neighbors;
var maxL = GAME.board.length - 1;
var maxW = GAME.board.width - 1;
var adj = {
lc: [coordX, coordY - 1],
lt: [coordX - 1, coordY - 1],
lb: [coordX + 1, coordY - 1],
rc: [coordX, coordY + 1],
rt: [coordX - 1, coordY + 1],
rb: [coordX + 1, coordY + 1],
ct: [coordX - 1, coordY],
cb: [coordX + 1, coordY],
};
neighbors = (
coordX === 0 ?
coordY === 0 ? [adj.rc, adj.rb, adj.cb] :
coordY === maxW ? [adj.lc, adj.lb, adj.cb] : [adj.lc, adj.lb, adj.rc, adj.rb, adj.cb] :
coordY === maxW ?
coordX === 0 ? [adj.lc, adj.lb, adj.cb] :
coordX === maxL ? [adj.lc, adj.lt, adj.ct] : [adj.lc, adj.lt, adj.lb, adj.ct, adj.cb] :
coordX === maxL ?
coordY === maxW ? [adj.lc, adj.lt, adj.ct] :
coordY === 0 ? [adj.rc, adj.rt, adj.ct] : [adj.lc, adj.lt, adj.ct, adj.rt, adj.rc] :
coordY === 0 ?
coordX === maxL ? [adj.rc, adj.rt, adj.ct] :
coordX === 0 ? [adj.rb, adj.rc, adj.cb] : [adj.rc, adj.rt, adj.rb, adj.ct, adj.cb] :
Object.values(adj)
);
return neighbors
}
function adjCellOpen(array, callback) {
let l = array.length;
if (l > 0) {
let x = array[0][0];
let y = array[0][1];
let cell = GAME.board.retrieveCell(x, y);
if (callback) {
callback(cell);
}
//console.log(cell);
array = array.slice(1);
adjCellOpen(array, callback);
} else {
return;
}
}
function cb(cell) {
console.log(cell);
}
function number(cell) {
if (!cell.mine)
}
View
@@ -0,0 +1,28 @@
var map = [
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[1, 0, 0, 1, 1],
[1, 0, 0, 1, 1],
[1, 0, 1, 0, 1]
];
function solver(arr) {
function row() {
var island = 0;
arr.forEach(function(x, i, a) {
x.forEach(function(x, i, a) {
//console.log(x)
//console.log(a[i]);
if (x > 0 && a[i - 1] > 0) {
console.log('adding')
island + 1;
}
})
})
}
return row(arr);
}
solver(map);
View
@@ -7,7 +7,7 @@ class Board {
for (var i = 0; i < this.width; i++) {
this.field[i] = [ ...Array(length).keys()];
for (var j = 0; j < this.length; j++) {
this.field[i][j] = new Cell(i * width + j);
this.field[i][j] = new Cell(i, j, width);
}
}
}
@@ -20,8 +20,8 @@ class Board {
return this.field[x][y]
}
retrieveCell(coordX, coordY) {
return this.field[coordX][coordY]
retrieveCell(x, y) {
return this.field[x][y]
}
setCell(cell) {
this.flagged = true;
View
@@ -1,6 +1,11 @@
class Board {
constructor(length, width) {
if ()
if (this.row !== length) {
this.row.push(this.col);
if (this.col !== width) {
this.col.push(new Cell())
}
}
}
getCell(id) {
@@ -14,6 +19,10 @@ class Board {
retrieveCell(coordX, coordY) {
return this.field[coordX][coordY]
}
c(a, b) {
return this.field[a][b]
}
setCell(cell) {
this.flagged = true;
if (this.flagged === false) {
Oops, something went wrong.

0 comments on commit 3127d61

Please sign in to comment.