xxxxxxxxxx
194
let n = 7;
const goodColor = 'purple';
const textColor = 'white';
const penaltySec = 3;
let table = [];
let scores = [];
let tableSize, unit, step, badStep, game
let startMoment, elapsedTime, penaltyTime, totalTime;
let comp = 'szürke / grey';
this.focus();
function setup() {
let startTime = year()+'.'+nf(month(),2)+'.'+nf(day(),2)+' '+nf(hour(),2)+';'+nf(minute(),2)+';'+nf(second(),2);
saveStrings([''], startTime + ' - A szürke 49 árnyalata', 'txt');
tableSize = min(windowWidth, windowHeight);
createCanvas(tableSize, tableSize);
rectMode(CENTER);
textStyle(BOLD);
textAlign(CENTER, CENTER);
noStroke();
startGame();
}
function startGame() {
unit = tableSize / n;
game = 0;
resetScores();
newGame();
}
function newGame() {
background(goodColor);
textSize(unit / 2);
resetTable();
shuffleTable(n * n);
drawTable(comp);
game++;
step = 1;
badStep = 0;
penaltyTime = 0;
totalTime = 0;
startMoment = now();
}
function resetScores () {
scores.splice(0);
}
function resetTable () {
let counter = 1;
for (let c = 1; c <= n; c++) {
table[c] = [];
for (let r = 1; r <= n; r++) {
table[c][r] = counter++;
}
}
}
function shuffleTable (swap) {
let ac, ar, bc, br, c;
for (let k = 1; k <= swap; k++) {
ac = randomFromTo(1, n);
ar = randomFromTo(1, n);
bc = randomFromTo(1, n);
br = randomFromTo(1, n);
c = table[ac][ar];
table[ac][ar] = table[bc][br];
table[bc][br] = c;
}
}
function drawTable (col) {
let shade, rComp, gComp, bComp;
for (let c = 1; c <= n; c++) {
for (let r = 1; r <= n; r++) {
shade = map(table[c][r], 1, n * n, 0, 255);
rComp = shade; gComp = shade; bComp = shade;
if (col == "piros / red") { rComp = 255 }
if (col == "zöld / green") { gComp = 255 }
if (col == "kék / blue") { bComp = 255 }
fill(rComp, gComp, bComp);
rect(colToX(c), rowToY(r), unit, unit);
}
}
}
function randomFromTo(from, to) {
if (from > to) { return(0) }
return(floor(random(to - from + 1)) + from);
}
function colToX(c) { return(c * unit - unit / 2) }
function rowToY(r) { return(r * unit - unit / 2) }
function xToCol(x) { return(floor(x / unit) + 1) }
function yToRow(y) { return(floor(y / unit) + 1) }
function fillGood(c, r) {
let x = colToX(c);
let y = rowToY(r);
fill(goodColor);
rect(x, y, unit, unit);
fill(textColor);
text(table[c][r], colToX(c), rowToY(r));
}
function drawResult() {
let txt;
background(goodColor);
textSize(26);
txt = "Játék / Game: " + game;
text(txt, width / 2, height / 2 - 100);
txt = n + ' x ' + n + ' ' + comp;
text(txt, width / 2, height / 2 - 60);
txt = "Eltelt idő / Elapsed time: " + formatTime(elapsedTime);
text(txt, width / 2, height / 2 - 20);
penaltyTime = badStep * penaltySec;
txt = "Büntető idő / Penalty time: " + formatTime(penaltyTime) + " (" + badStep +")";
text(txt, width / 2, height / 2 + 20);
totalTime = elapsedTime + penaltyTime;
txt = "Teljes idő / Total time: " + formatTime(totalTime);
text(txt, width / 2, height / 2 + 60);
txt = "Helyezés / Rank: #" + rankingScores(totalTime);
text(txt, width / 2, height / 2 + 100);
}
function rankingScores(newEntry) {
let c;
scores.push(newEntry);
let pos = scores.length;
if (pos > 1) {
for (let i = scores.length; i > 0; i--) {
if (scores[i - 1] > scores[i]) {
c = scores[i];
scores[i] = scores[i - 1];
scores[i - 1] = c;
pos--;
}
}
}
return pos;
}
function now() {
let ho = hour();
let mi = minute();
let se = second();
return (3600 * ho + 60 * mi + se); // in seconds
}
function formatTime(sec) {
let del = ":"; // delimiter
let cmin = floor(sec / 60); // minute
let csec = sec - 60 * cmin; // second
if (csec < 10) { del += "0" }
return (cmin + del + csec); // [m:ss]
}
function mousePressed() {
let x = mouseX;
let y = mouseY;
if (x > 0 && x < tableSize && y > 0 && y < tableSize) {
if (step == n * n + 1) { step++; drawResult(); }
let c = xToCol(x);
let r = yToRow(y);
if (step <= table[c][r]) {
if (step == table[c][r]) {
fillGood(c, r);
step++;
if (step > n * n) { elapsedTime = now() - startMoment }
}
else { badStep++ }
}
}
}
function keyPressed() {
if (keyCode === 32) { newGame() }
if (keyCode === 83 && step == 1) { comp = 'szürke / grey'; drawTable(comp); }
if (keyCode === 80 && step == 1) { comp = 'piros / red'; drawTable(comp); }
if (keyCode === 90 && step == 1) { comp = 'zöld / green'; drawTable(comp); }
if (keyCode === 75 && step == 1) { comp = 'kék / blue'; drawTable(comp); }
if (keyIsDown(LEFT_ARROW)) {
if (n > 2) { n--; startGame(); }
}
if (keyIsDown(RIGHT_ARROW)) {
if (n < 16) { n++; startGame(); }
}
}
function draw() {
noLoop();
}