Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Refactor and fix puzzle move output
Browse files Browse the repository at this point in the history
Also make file extension of include more accurate
  • Loading branch information
Nixinova committed Jul 1, 2021
1 parent 3f255cc commit 1c5b90c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion layouts/menu.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</head>

<body>
{% include "header.njk" %}
{% include "header.html" %}

<main id="main">
{{ content | safe }}
Expand Down
2 changes: 1 addition & 1 deletion pages/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
layout: menu.njk
---

<h2>Select opponent</h2>
<h1>CarbonChess</h1>

<div class="cards showcase">
<a href="/menu/bot/" class="card big" style="grid-area: computer;">
Expand Down
2 changes: 1 addition & 1 deletion pages/play.njk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ permalink: /play/

<body class="norotate">

{% include "header.njk" %}
{% include "header.html" %}

<div id="winner" class="resettable"></div>

Expand Down
26 changes: 2 additions & 24 deletions scripts/play/game-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,8 @@ function hasClicked(cell) {
checkGameEnding();

// check if correct puzzle move has been made
if (window.gameOptions.puzzles && puzzleColour === global.currentTurn && movesToMake) {
if (startCell === movesToMake[0].slice(0, 2).toUpperCase() && endCell === movesToMake[0].slice(2, 4).toUpperCase()) {
movesToMake.shift();
if (movesToMake.length > 0) {
$.id('winner').innerHTML = 'Correct, now find the next move'
setTimeout(puzzleMove, 500);
}
else {
$.id('winner').innerHTML = 'Well done';
$.id('next-puzzle').classList.remove('hide');
window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, window.puzzleHintUsed ? 0 : 1);
saveUserData();
}
}
else {
undoLastMove();
$.id('winner').innerHTML = 'Wrong, try again';
if (window.failedPuzzleAttempts === 0) {
window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, 0);
saveUserData();
}
window.failedPuzzleAttempts++;
}
$.id('puzzle-attempts-value').innerText = window.failedPuzzleAttempts;
if (window.gameOptions.puzzles && puzzleColour === global.currentTurn && window.movesToMake) {
puzzleMoveOutput(startCell, endCell);
}

// send to server
Expand Down
37 changes: 32 additions & 5 deletions scripts/play/puzzles.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const puzzleCache = 10;
let savedPuzzles;
let movesToMake;
let puzzleColour;
let puzzlePosition = 0;
let hasLoadedCustom = false;
window.savedPuzzles;
window.movesToMake = [];
window.puzzleColour;
window.puzzlePosition = 0;
window.hasLoadedCustom = false;

function processData(allText) {
const allTextLines = allText.split(/\r\n|\n/);
Expand Down Expand Up @@ -75,6 +75,7 @@ function nextPuzzle() {
window.ingame = true;
window.points = { w: 0, b: 0 };
window.failedPuzzleAttempts = 0;
window.puzzleHintUsed = false;
$.id('puzzle-attempts-value').innerText = window.failedPuzzleAttempts;
$$('td').forEach(elem => elem.setAttribute('class', ''));
if (puzzlePosition >= puzzleCache - 1) {
Expand All @@ -89,6 +90,32 @@ function nextPuzzle() {
$.id('winner').innerHTML = 'Find the best move';
}

function puzzleMoveOutput(startCell, endCell) {
if (startCell === window.movesToMake[0].slice(0, 2).toUpperCase() && endCell === window.movesToMake[0].slice(2, 4).toUpperCase()) {
window.movesToMake.shift();
if (window.movesToMake.length > 0) {
$.id('winner').innerHTML = 'Correct, now find the next move'
setTimeout(puzzleMove, 500);
}
else {
$.id('winner').innerHTML = 'Well done';
$.id('next-puzzle').classList.remove('hide');
window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, window.puzzleHintUsed ? 0 : 1);
saveUserData();
}
}
else {
undoLastMove();
$.id('winner').innerHTML = 'Wrong, try again';
if (window.failedPuzzleAttempts === 0) {
window.userPuzzlesElo = calculateElo(window.userPuzzlesElo, gameOptions.difficulty, 0);
saveUserData();
}
window.failedPuzzleAttempts++;
}
$.id('puzzle-attempts-value').innerText = window.failedPuzzleAttempts;
}

function showPuzzleHint() {
window.puzzleHintUsed = true;
$.id(movesToMake[0].slice(0, 2).toUpperCase())?.classList.add('valid');
Expand Down
2 changes: 1 addition & 1 deletion styles/common.nvss
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ summary {
margin: 1em;
padding: 0.5em;
background: #0004;
text-align: center;
text-align: right;

&-elo {
margin-left: 4px;
Expand Down

0 comments on commit 1c5b90c

Please sign in to comment.