Skip to content

Commit

Permalink
fixed board editing, added shuffleBoard()
Browse files Browse the repository at this point in the history
The dragging functionality works as expected now, even when editing.
You can shuffle the board now, so that when creating a board, you can save it in a shuffled state.
  • Loading branch information
Catz1301 committed Sep 3, 2023
1 parent 3139030 commit 3e7a8be
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
54 changes: 46 additions & 8 deletions board.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
* @public
* @function
* @name createBoard
* @param {number} width The width of the board
* @param {number} height The height of the board
* @param {number} boardWidth The width of the board
* @param {number} boardHeight The height of the board
* @returns
*/

function createBoard(width, height) {
function createBoard(boardWidth, boardHeight) {
/**
* @var
* @name board
* @type {Square[][]}
*/
var board = [[]];
// let squareId = 0;
for (var x = 0; x < width; x++) {
for (var x = 0; x < boardWidth; x++) {
board[x] = [];
for (var y = 0; y < height; y++) {
for (var y = 0; y < boardHeight; y++) {
if (random(0, 1) <= chanceOfSquare) {
board[x][y] = new Square(x * gridCellSize, y * gridCellSize, squareId);
squareId++;
Expand All @@ -29,6 +29,17 @@ function createBoard(width, height) {
return board;
}

function createEmptyBoard(width, height) {
var board = [[]];
for (var x = 0; x < width; x++) {
board[x] = [];
for (var y = 0; y < height; y++) {
board[x][y] = undefined;
}
}
return board;
}

/**
* @public
* @function
Expand Down Expand Up @@ -93,14 +104,15 @@ function checkBoardForSquare(mx, my) {
* @description Clear the board
*/
function clearBoard() {
for (var x = 0; x < width; x++) {
for (var x = 0; x < gridWidth; x++) {
board[x] = [];
for (var y = 0; y < height; y++) {
for (var y = 0; y < gridHeight; y++) {
board[x][y] = undefined;
}
}
boardSet = true;
squareId = 0;

}

function setBoard(boardObj) {
Expand All @@ -114,5 +126,31 @@ function setBoard(boardObj) {
}
})
});
console.debug({status: "Board set", board});
doDebug ? console.debug({status: "Board set", board}) : undefined;
}

// shuffle the board by gathering all the squares and put them into an array. then clear the board, and put the squares back in the board in a random order.
function shuffleBoard() {
let squares = [];
board.forEach(function(e, index) {
e.forEach(function(d, index2) {
if (board[index][index2] != undefined) {
squares.push(board[index][index2]);
}
})
});
clearBoard();
// board = createEmptyBoard(gridWidth, gridHeight);
while (squares.length > 0) {
board.forEach(function(e, index) {
e.forEach(function(d, index2) {
if (random(0, 1) <= chanceOfSquare && squares.length > 0) {
let tempSquare = squares.pop();
tempSquare.x = index * gridCellSize;
tempSquare.y = index2 * gridCellSize;
board[index][index2] = tempSquare;
}
});
});
}
}
7 changes: 2 additions & 5 deletions editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ function readFile(inputFile) {
for (let x = 0; x < dataRead[0].length; x++) {
dataString += String.fromCharCode(dataRead[0][x]);
}
console.log(dataString);
boardObject = JSON.parse(dataString);
console.log(boardObject);

doDebug ? console.debug({status: "Data read", dataRead, boardObject}) : undefined;
doDebug ? console.debug({status: "Data read", dataRead, dataString, boardObject}) : undefined;
finishedData = true;
return;
}
Expand All @@ -114,7 +111,7 @@ function readFile(inputFile) {


function byteDataToHexStr(bytes) {
console.log(bytes)
doDebug ? console.debug({status: "Converting bytes to hex string", bytes}) : undefined;
let hexStr = ""
bytes.forEach(byte => {
let byteStr = byte.toString(16);
Expand Down
12 changes: 8 additions & 4 deletions events.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function mouseReleased() {
}
if (selectedSquare != undefined && holdingSquare != undefined) {
// holdingSquare = holdingSquare.release();
selectedSquare.release();
selectedSquare.color = color(255, 0, 0);
selectedSquare = selectedSquare.release();
holdingSquare = undefined;
}
}

Expand Down Expand Up @@ -154,7 +156,7 @@ function keyReleased(event) {
}
if (event.key === '2') {
// add a connector to the right of the selected square. If there is no selected square, do nothing. If there is already 4 connectors, remove all connectors.
console.log({status: "Adding connector to right of square", selectedSquare});
doDebug ? console.log({status: "Adding connector to right of square", selectedSquare}) : undefined;
if (selectedSquare != undefined) {
doDebug ? console.debug({status: "Adding connector to right of square", selectedSquare}) : undefined;
selectedSquare.addConnector("right");
Expand Down Expand Up @@ -214,13 +216,15 @@ function keyTyped() {
saveBoard();
}

if (key === 'r' || key === 'R') {
shuffleBoard();
}
}

/**
* @todo Develop a way to load a board from a file. and also parse the file. */
function loadFile(file) {
console.log("Loading file");
console.log(file);
doDebug ? console.debug({status: "Loading file", file}) : undefined;
if (fileInput != undefined) {
fileInput.remove();
}
Expand Down
10 changes: 4 additions & 6 deletions sketch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var gridWidth = 5;
var gridHeight = 5;
var gridWidth = 10;
var gridHeight = 10;
var gridCellSize = 10;
var isFullScreen = false;
var bgColor = 0x28262c;
var doDebug = true;
var doDebug = false;

var gridSizesSet = false;
var boardSet = false;
Expand Down Expand Up @@ -65,8 +65,6 @@ function draw() {
// use from "Square.js";

function drawGrid() {
var gridWidth = 10;
var gridHeight = 10;
gridCellSize = floor(height / gridHeight);
stroke(250);
fill(40,38,44);
Expand All @@ -80,6 +78,6 @@ function drawGrid() {
function displayStartScreen() {
fill(0);
textSize(32);
fill(255);
fill(lerpColor(color(0), color(255), 1.0));
text("Click to start", (width/2)-(7*16), (height/2)-16);
}

0 comments on commit 3e7a8be

Please sign in to comment.