javascript board game piece placement - javascript
I'm writing a board game in java script, and what i'm trying to accomplish is: layout the board(chess/checkers format) Then add pieces to the board based on position. So for example i want to be able to write code for piece a to be moved onto tile 10.
So far in my code i have a loop to create the board but don't a method to properly name the tiles, so that the piece can correctly be placed on the tile.
for (i=0; i<64; i++){
var tile = cc.Sprite.create(res.myTile_png);
this.addChild(tile,0);
x = centerpos.x + ((i % 8) - 3.5) * tile.getBoundingBox().width;
y = centerpos.y + (Math.floor(i / 8) - 3.5) * tile.getBoundingBox().height;
tile.setPosition(x,y);
}
One way to go about doing this would be to assign a unique integer identifier to every distinct piece in the game, and then maintain a matrix of dimensions equal to the # of rows x # of columns on the board, with values of the piece identifiers in the correct address in the matrix that would correspond to their position on the board.
For instance, the starting arrangement of pieces in checkers can be represented by:
[
[ 0, -1, 0, -1, 0, -1, 0, -1 ],
[ -1, 0, -1, 0, -1, 0, -1, 0 ],
[ 0, -1, 0, -1, 0, -1, 0, -1 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 1, 0, 1, 0, 1, 0, 1, 0 ],
[ 0, 1, 0, 1, 0, 1, 0, 1 ],
[ 1, 0, 1, 0, 1, 0, 1, 0 ]
]
with, say, -1 representing white, and 1 representing red pieces on the board.
The tile elements of the board can also be kept in a matrix, so that the two matrices can be iterated over together to place the pieces in corresponding locations.
The unique ids can also then be used as CSS class names, or image file names to be attached to the element representing the piece.
Related
Algorithm Fill Closed Area in Tile map JavaScript
sorry for my english. I have a problem and what is the next: Example, i have a map: var map = [[0,1,1,0,0,0,0,0,0,0], [0,1,0,1,0,1,1,0,0,0], [0,1,0,0,1,0,0,1,0,0], [0,1,0,0,0,0,0,0,1,0], [0,0,1,0,0,0,0,1,0,0], [0,0,0,1,0,0,0,1,1,0], [0,0,1,0,0,0,1,0,0,0], [0,1,0,0,0,0,0,1,0,0], [1,0,0,1,1,1,0,1,0,0], [0,1,1,0,0,1,1,1,0,0]]; Which contains a series of numbers 0 and 1 (For example). I need to fill in all the closed boxes that are on this map, for example using the number 2. Example: var map = [[0,1,1,0,0,0,0,0,0,0], [0,1,2,1,0,1,1,0,0,0], [0,1,2,2,1,2,2,1,0,0], [0,1,2,2,2,2,2,2,1,0], [0,0,1,2,2,2,2,1,0,0], [0,0,0,1,2,2,2,1,1,0], [0,0,1,2,2,2,1,0,0,0], [0,1,2,2,2,2,2,1,0,0], [1,2,2,1,1,1,2,1,0,0], [0,1,1,0,0,1,1,1,0,0]]; Taking into consideration that: Just as in this example there is only one closed figure, there can be several closed figures The sides of the map will not be taken into consideration If it is of any use, the numbers 1 (which would be the solid), will be generated as time passes, so the map will be constantly changing (like strokes in an array) I found a method called "Flood Fill" but however it depends on a starting point, in this case it has no starting point. The idea is that the code is in charge of finding the closed areas and filling them automatically.
If you don't have starting coordinates, one method to identify every 0 to be filled is to identify every 0 on the edges. Each of these zeros should not be filled, and each 0 eventually adjacent to these 0s should also not be filled. So, if you take the edge 0s as the "starting point" and iterate through all of their recursive neighbors, you'll have identified each coordinate which is a 0 but should not be filled in. Then, it's simple: just iterate over the input, and for every 0, check to see if the current coordinate is in that set of coordinates that shouldn't be filled. If the coordinate is not in that set, replace with a 2. var map = [[0,1,1,0,0,0,0,0,0,0], [0,1,2,1,0,1,1,0,0,0], [0,1,2,2,1,2,2,1,0,0], [0,1,2,2,2,2,2,2,1,0], [0,0,1,2,2,2,2,1,0,0], [0,0,0,1,2,2,2,1,1,0], [0,0,1,2,2,2,1,0,0,0], [0,1,2,2,2,2,2,1,0,0], [1,2,2,1,1,1,2,1,0,0], [0,1,1,0,0,1,1,1,0,0]]; const height = map.length; const width = map[0].length; const edgeZerosCoords = new Set(); map.forEach((arr, row) => { arr.forEach((num, col) => { if (num === 0 && (row === 0 || col === 0 || row === width - 1 || col === height - 1)) { edgeZerosCoords.add(`${row}_${col}`); } }) }); const doNotFillCoords = new Set(); const visited = new Set(); const checkCoord = (row, col) => { // Verify valid coord: if (row < 0 || col < 0 || row === width || col === height) return; const str = `${row}_${col}`; if (doNotFillCoords.has(str) || visited.has(str)) return; visited.add(str); const num = map[row][col]; if (num !== 0) return; doNotFillCoords.add(str); checkCoord(row + 1, col); checkCoord(row - 1, col); checkCoord(row, col + 1); checkCoord(row, col - 1); }; for (const str of edgeZerosCoords) { const [row, col] = str.split('_').map(Number); checkCoord(row, col) } map.forEach((arr, row) => { arr.forEach((num, col) => { const str = `${row}_${col}`; if (num === 0 && !doNotFillCoords.has(str)) { map[row][col] = 2; } }) }); console.log(JSON.stringify(map)); Result: [ [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 1, 0, 1, 1, 0, 0, 0], [0, 1, 2, 2, 1, 2, 2, 1, 0, 0], [0, 1, 2, 2, 2, 2, 2, 2, 1, 0], [0, 0, 1, 2, 2, 2, 2, 1, 0, 0], [0, 0, 0, 1, 2, 2, 2, 1, 1, 0], [0, 0, 1, 2, 2, 2, 1, 0, 0, 0], [0, 1, 2, 2, 2, 2, 2, 1, 0, 0], [1, 2, 2, 1, 1, 1, 2, 1, 0, 0], [0, 1, 1, 0, 0, 1, 1, 1, 0, 0] ]
copy THREE.js matrix from object
i need to to same matrix mulitiply in Three.js. I had an Object3D and i get the right matrix to console.log when doing this: console.log (scene.getObjectByName( "Pointer" ).matrix) the result is like: T…E.Matrix4 {elements: Float32Array[16]} elements: Float32Array[16] 0: 11: 02: 03: 04: 05: 16: 07: 08: 09: 0 10: 11 1: 0 12: -150 13: 0 14: 0 15: 1 note here that the 12th element have the value -150 (after a obj.translationX(-150)). var newMat = new THREE.Matrix4(); console.log(scene.getObjectByName("Pointer").matrix.elements) // output: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] newMat = newMat.copy(scene.getObjectByName("Pointer").matrix); console.log(newMat); // output:elements: Float32Array[16] 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1 giving back an identiy matrix (meaning the 12th element is: 0) Whats is wrong here? UPDATE: inside the renderloop... newMat.copy(...).. works fine!
three.js will update the object matrix when it render the page according the object position, scale, rotation. So when you set the object matrix, it will sooner be rewrited. To manually set the object matrix, you have to set autoupdate to false. object.matrixAutoUpdate = false; then use the your code. var newMat = new THREE.Matrix4(); console.log(scene.getObjectByName("Pointer").matrix.elements) // output: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] newMat = newMat.copy(scene.getObjectByName("Pointer").matrix); console.log(newMat);
Converting an array of bytes into a float32
I have a JavaScript application which receives a voltage value as an Uint8Array. Here are 2 examples of the received data: [3, 134, 46, 177, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [3, 127, 46, 170, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] I am trying to convert this Uint8Array to a float value, but I am not sure if it's a float32 or float64, if it's signed or not. What I do know is that this value is around 12. Can anyone help with a JavaScript snippet to do the conversion? Thank you in advance.
Each line appears to have two voltages in it. Voltages from devices usually come from ATOD converters that have between 8 and 16 bits of info and are scaled based on the circuitry. It looks like a 3 (unused and probably some sort of mode flag) followed by two pairs of bytes each one a voltage from a ATOD. I would guess that two bytes such as 177 and 46 should be interpreted as (177+46*256)/1000.0 based on 1 mv. scaling. This produces values just over 11.9 volts. Given the range of the two samples this would fit your expectations.
Sliding values of an array inside intervals
I ve created a array with eleven values . I am trying to slide the values of my array during intervals. What i am trying, every n ms to slide a value to the next position of the array that i ve created. Every inteval i initialize the first value, so i want the slide effect. var barArray = [0,0,0,0,0,0,0,0,0,0,0]; var interval = 0; setInterval(function() { temporal = getNewValue; //getting with a function new value barArray[0] = temporal; if(interval == barArray.length) { interval = 0; } for (var i = 0; barArray.length; i++){ // code missing } }, 1000); I am tried many things without finding a solution. Output: 1st interval: [76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 2nd interval: [55, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0] 3rd interval: [32,55, 76, 0, 0, 0, 0, 0, 0, 0, 0] 11th interval: [..., 32, 55, 76] 12th [..., 32,55] ect. `
What you have described here is a queue. You input elements at one end and silently drop them at the other end. JavaScript arrays have functions to add and extract elements at both ends of the array (push/pop and shift/unshift). In the end, a complete solution would be: var barArray = [0,0,0,0,0,0,0,0,0,0,0]; setInterval(function() { barArray.unshift(getNewValue()); barArray.pop(); }, 1000);
Unexpected value change in 2D array in JavaScript
I'm trying to modify one value in a 2D array. However I'm finding some weird behavior based on how the array is constructed. The only difference between matrix and matrix2 is how they're constructed. However when I change the [1][1] value, all of the [x][1] values in matrix2 are changed: Matrix: [ [ 0, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 0 ] ] Matrix2 (unexpected): [ [ 0, 1, 0 ], [ 0, 1, 0 ], [ 0, 1, 0 ] ] Code: var row = [0,0,0]; var matrix = [[0,0,0],[0,0,0],[0,0,0]]; var matrix2 = [row, row, row]; console.log(matrix); console.log(matrix2); matrix[1][1] = 1; matrix2[1][1] = 1; console.log(matrix); console.log(matrix2); Can anyone explain what's going on?
[row, row, row] You just made an array with three references to the same inner array. Changes to the inner array can be seen through any reference to it. You want to create three copies of the inner array. You can create a shallow copy of an array by calling .slice().