Skip to content

Commit

Permalink
auto reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
x-tr4ce committed Dec 27, 2024
1 parent ed9f8d3 commit 9e46735
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 49 deletions.
40 changes: 20 additions & 20 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
</head >
<body >

<div id="scoreboard">
<div class="player" id="player1">
<span class="player-name">Player 1</span>
</div>
<span class="score-separator">VS</span>
<div class="player" id="player2">
<span class="player-name">Player 2</span>
</div>
</div>
<div id="scoreboard" >
<div class="player" id="player1" >
<span class="player-name" >Player 1</span >
</div >
<span class="score-separator" >VS</span >
<div class="player" id="player2" >
<span class="player-name" >Player 2</span >
</div >
</div >


<div class="app-disabled-wrapper">
<div class="app" id="app"></div>
</div>
<div class="app-disabled-wrapper" >
<div class="app" id="app" ></div >
</div >


<div class="game-controls">
<div class="game-controls" >
<div class="game-management" >
<button id="newGame" class="newGame">New Game</button >
<button id="stepBack" disabled>Step back</button >
<button id="newGame" class="newGame" >New Game</button >
<button id="stepBack" disabled >Step back</button >
</div >
<div class="state-management" >
<button id="loadState" >Load Game</button >
Expand All @@ -43,9 +43,9 @@

</body >

<script src="javascript/utils.js" defer type="module"></script >
<script src="javascript/animations.js" defer type="module"></script >
<script src="javascript/board.js" defer type="module"></script >
<script src="javascript/game.js" defer type="module"></script >
<script src="javascript/lib/suiweb_1-1.js" defer type="module"></script >
<script src="javascript/utils.js" defer type="module" ></script >
<script src="javascript/animations.js" defer type="module" ></script >
<script src="javascript/board.js" defer type="module" ></script >
<script src="javascript/game.js" defer type="module" ></script >
<script src="javascript/lib/suiweb_1-1.js" defer type="module" ></script >
</html >
2 changes: 1 addition & 1 deletion public/javascript/animations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ function applyFallAnimation(pieces, colIndex, rowIndex, adjustedDelay) {
}
}

export { applyFallAnimation };
export {applyFallAnimation};
38 changes: 24 additions & 14 deletions public/javascript/board.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { elt } from "./utils.js";
import { render, useSJDON} from "./lib/suiweb_1-1.js";
import {elt} from "./utils.js";
import {render, useSJDON} from "./lib/suiweb_1-1.js";
// Field component - represents a single field on the board
const Field = ({ id, rowIndex, colIndex }) => {
const Field = ({id, rowIndex, colIndex}) => {
return [
"div",
{ className: "field" ,
id: `cell-${rowIndex}-${colIndex}`,
{
className: "field",
id: `cell-${rowIndex}-${colIndex}`,
},
...(id ? [["div", { className: `piece player${id}` }]] : [])
...(id ? [["div", {className: `piece player${id}`}]] : [])
];
};

// Board component - contains the board and the fields
const Board = ({ board }) => {
const Board = ({board}) => {
return [
"div",
{ className: "board", id: "board" },
{className: "board", id: "board"},
...board.map((row, rowIndex) => [
"div",
{ className: "row" },
...row.map(((cell, colIndex) => [Field, { id: cell?.id, rowIndex, colIndex }]))
{className: "row"},
...row.map(((cell, colIndex) => [Field, {id: cell?.id, rowIndex, colIndex}]))
])
];
};

// App component
const App = ( { state }) => [Board, { board: state.board }];
const App = ({state}) => [Board, {board: state.board}];

useSJDON(Field, Board, App);

Expand All @@ -40,7 +41,7 @@ const app = document.getElementById("app");
function showBoard(state) {
const app = document.querySelector(".app")
app.innerHTML = ""
render([App, { state }], app)
render([App, {state}], app)
}

/**
Expand All @@ -57,7 +58,7 @@ function updateCell(state, rowIndex, colIndex) {

const cell = state.board[rowIndex][colIndex];
if (cell) {
const pieceDiv = elt("div", {class: `piece player${cell.id} piece-fall` });
const pieceDiv = elt("div", {class: `piece player${cell.id} piece-fall`});
cellElement.appendChild(pieceDiv);
}

Expand Down Expand Up @@ -112,5 +113,14 @@ function enableStepBack() {
document.getElementById("stepBack").classList.add("stepBack");
}

export { showBoard, updateCell, disableBoard, enableBoard , disableLoadState, enableLoadState, disableStepBack, enableStepBack};
export {
showBoard,
updateCell,
disableBoard,
enableBoard,
disableLoadState,
enableLoadState,
disableStepBack,
enableStepBack
};

27 changes: 18 additions & 9 deletions public/javascript/game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import {showBoard, updateCell, disableBoard, enableBoard, disableLoadState, enableLoadState, disableStepBack, enableStepBack} from './board.js';
import {
showBoard,
updateCell,
disableBoard,
enableBoard,
disableLoadState,
enableLoadState,
disableStepBack,
enableStepBack
} from './board.js';
import {applyFallAnimation} from './animations.js';
import {setInList, setInObj, checkIfWinner} from "./utils.js";

Expand All @@ -23,7 +32,7 @@ document.addEventListener('DOMContentLoaded', () => {

const newGameButton = document.getElementById("newGame");
newGameButton.addEventListener('click', () => handleNewGameClick());

const stepBackButton = document.getElementById("stepBack");
stepBackButton.addEventListener('click', () => handleStepBackClick());

Expand All @@ -40,7 +49,7 @@ document.addEventListener('DOMContentLoaded', () => {
*/
function setupBoardEventListeners() {
const boardElement = document.getElementById("app");

boardElement.addEventListener('click', (event) => {
const cellDiv = event.target.closest('.field');
if (!cellDiv) return;
Expand Down Expand Up @@ -83,14 +92,14 @@ function handleCellClick(rowIndex, colIndex) {
let newRow = setInList(state.board[highestEmptyRow], colIndex, state.players[state.currentPlayerIndex]);
state = setInObj(state, 'board', setInList(state.board, highestEmptyRow, newRow));
const cell = updateCell(state, highestEmptyRow, colIndex);

cell.addEventListener("animationend", () => {
checkForWinner();
}, { once: true });
}, {once: true});

let nextPlayerIndex = state.currentPlayerIndex === 0 ? 1 : 0;
state = setInObj(state, 'currentPlayerIndex', nextPlayerIndex);

updateActivePlayer();
}
}
Expand Down Expand Up @@ -225,7 +234,7 @@ function checkForWinner() {

function highlightWinnerCells(winnerCells) {
console.log("Winner cells ", winnerCells);
winnerCells.forEach(({ r, c }) => {
winnerCells.forEach(({r, c}) => {
const cellElement = document.getElementById(`cell-${r}-${c}`);
if (cellElement) {
cellElement.setAttribute("data-winner", "true");
Expand Down Expand Up @@ -338,7 +347,7 @@ function scheduleAnimation(rowIndex, colIndex, cell, onAnimationEnd) {
const updatedCell = updateCell(state, rowIndex, colIndex);
updateActivePlayer();

updatedCell.addEventListener("animationend", onAnimationEnd, { once: true });
updatedCell.addEventListener("animationend", onAnimationEnd, {once: true});
}, (6 - rowIndex) * 100 + colIndex * 200); // Add delays for row and column
}

Expand Down
10 changes: 5 additions & 5 deletions public/javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function setInList(lst, idx, val) {
* @returns {Object} A new object with the updated attribute
*/
function setInObj(obj, attr, val) {
return { ...obj, [attr]: val };
return {...obj, [attr]: val};
}


Expand All @@ -76,12 +76,12 @@ function checkIfWinner(player, state) {
const c = col + i * colDir;

if (r >= 0 && r < rows && c >= 0 && c < cols && board[r][c] === player) {
cells.push({ r, c });
cells.push({r, c});
} else {
break;
}
}

return cells.length === 4 ? cells : [];
}

Expand All @@ -94,7 +94,7 @@ function checkIfWinner(player, state) {
checkDirection(row, col, 1, 1), // Diagonal down-right
checkDirection(row, col, 1, -1), // Diagonal down-left
];

for (const winnerCells of directions) {
if (winnerCells.length > 0) {
return winnerCells;
Expand All @@ -106,4 +106,4 @@ function checkIfWinner(player, state) {
return [];
}

export { elt, setInList, setInObj, checkIfWinner };
export {elt, setInList, setInObj, checkIfWinner};

0 comments on commit 9e46735

Please sign in to comment.