bizarrelinkup-lordmixz
Version:
This repository contains the source code for the 'Bizarre Link Up' project.
198 lines (177 loc) • 6.06 kB
JavaScript
class GameBoard {
constructor(size) {
this.size = size;
this.board = [];
this.initBoard();
}
initBoard() {
const totalElements = this.size * this.size;
const elements = [];
for (let i = 0; i < totalElements / 2; i++) {
const element = this.getRandomElement();
elements.push(element, element);
}
this.shuffle(elements);
for (let i = 0; i < this.size; i++) {
this.board[i] = [];
for (let j = 0; j < this.size; j++) {
const index = i * this.size + j;
this.board[i][j] = elements[index];
}
}
}
getRandomElement() {
const possibleElements = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return possibleElements.charAt(Math.floor(Math.random() * possibleElements.length));
}
shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
findMatches() {
const matches = [];
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
if (this.board[i][j] !== null) {
this.searchForMatch(i, j, this.board[i][j], matches);
}
}
}
return matches;
}
searchForMatch(row, col, element, matches) {
for (let i = row; i < this.size; i++) {
for (let j = i === row ? col + 1 : 0; j < this.size; j++) {
if (this.board[i][j] === element) {
matches.push([[row, col], [i, j]]);
this.board[row][col] = null;
this.board[i][j] = null;
return;
}
}
}
}
}
class Game {
constructor(size) {
this.size = size;
this.board = new GameBoard(size);
this.start();
}
start() {
this.interval = setInterval(() => {
const matches = this.board.findMatches();
if (matches.length === 0) {
this.checkWinCondition();
}
}, 1000);
}
checkWinCondition() {
clearInterval(this.interval);
let remaining = 0;
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
if (this.board.board[i][j] !== null) {
remaining += 1;
}
}
}
if (remaining === 0) {
alert("You win!");
} else {
alert("Try again!");
}
}
}
(function() {
function Animat(odj, data) {
data = data || {};
this.odj = odj;
this.time = data.time || 0.5;
this.direction = data.direction || 'left';
this.shape = data.shape || 'width';
this.length = data.length || 100;
this.reduction = data.redu || false;
this.loop = data.loop || false;
this.boxLeft = odj.offsetLeft;
this.boxTop = odj.offsetTop;
this.boxWidth = odj.offsetWidth;
this.boxHeigth = odj.offsetHeight;
}
Animat.prototype.move = function() {
let num = this;
this.odj.style.transitionDuration = this.time + 's';
if (this.direction == 'top') {
this.odj.style.top = this.boxTop + this.length + 'px';
} else if (this.direction == 'left') {
this.odj.style.left = this.boxLeft + this.length + 'px';
};
if (this.reduction) {
setTimeout(function() {
if (num.direction == 'top') {
num.odj.style.top = num.boxTop + 'px';
} else if (num.direction == 'left') {
num.odj.style.left = num.boxLeft + 'px';
}
}, num.time * 1000);
};
if (this.loop) {
let ding = setInterval(function() {
num.move();
clearTimeout(ding);
}, (num.time * 2) * 1000)
}
};
Animat.prototype.variant = function() {
let num = this;
this.odj.style.transitionDuration = this.time + 's';
if (this.shape == 'width') {
this.odj.style.width = this.boxWidth + num.length + 'px';
} else if (this.shape == 'height') {
this.odj.style.height = this.boxHeigth + num.length + 'px';
};
if (this.reduction) {
setTimeout(function() {
if (num.shape == 'width') {
num.odj.style.width = num.boxWidth + 'px';
} else if (num.shape == 'height') {
num.odj.style.height = num.boxHeigth + 'px';
}
}, num.time * 1000);
};
if (this.loop) {
let ding = setInterval(function() {
num.variant();
clearTimeout(ding);
}, (num.time * 2) * 1000)
}
};
Animat.prototype.hide = function(data) {
let num = this;
let temp = 0;
let time;
clearTimeout(time);
time = setInterval(() => {
if (temp <= 100) {
if (data) {
num.odj.style.opacity = 1;
num.odj.style.opacity = (100 - temp) / 100;
} else {
num.odj.style.display = 'block';
num.odj.style.opacity = 0;
num.odj.style.opacity = temp / 100;
}
temp++;
} else {
clearTimeout(time);
temp = 0;
if (data) {
num.odj.style.display = 'none';
}
}
}, (num.time * 1000) / 100)
}
window.Animat = Animat;
})();