Thank for your help. But I think I’m gonna stick with James’ solution.
It’s a game with only 2 players. And it will be played online. So for the revert problem I will just need to keep track of the array in a data base and refresh the screen of the other player after a move has been made.
So it should not be too difficult from this point.
I would also consider investigating Thallius’ suggestions. If it’s just a small hobby project, then there is no need to go overboard, but that said, the DOM is a bad place to keep anything more than basic state.
The solution I posted above gives you a starter on how to do this, but you can of course adapt it to suit.
That might be quite a jarring experience. Websockets might be a better way to go?
Hmmm I know about websockets but I really don’t know how to use it and maybe too ‘heavy’ for such small games?
I think I will do like for other small games I have online: a loop checking if a certain value is set to TRUE in a database and then refresh the screen of the other player after a move has been made.
Other than your comment is more than a little deprecating; I’ m just a dilletante here. I’m a ‘Sunday’ coder and I grab what I can assimilate on my free time for fun only. Not a professional like you.
My little nothing is working the way it should and that gives me enough satisfaction.
Plus I’ve seen you use typeScript; I know it’s similar to javascript but I don’t know to which degree so I prefer to stick trying to learn javascript.
I used it and I know what it does. But I’m not quite at ease with those arrow functions and the way to implement them. Could you try to explain me like you are explaining to a small kid and not a too smart one either?
Also; why do you use const? Isn’t a constant a data that doesn’t change?
It’s just a shorter way to write a function. For example, instead of writing:
function doSomething(x) {
return x * 2;
}
You can write:
const doSomething = (x) => x * 2;
In your code, this part is the arrow function:
(_, index) => ({ id: index + 1, seeds: SEEDS })
Broken down:
_ is the first argument. You don’t need it, so it’s ignored.
index is the second argument used to calculate the id.
Arrow (=>) separates inputs (left) from the output (right).
Output: { id: index + 1, seeds: SEEDS } returns an object for each “slot”.
The brackets around the output are necessary, as you are returning an object.
So for each “slot” in the array, this arrow function builds an object like { id: 1, seeds: 4 }.
And why const?
const means the variable itself (gameState) cannot be reassigned to something else. However the contents of gameState (e.g. the objects inside) can be updated. In other (slightly more technical) words the const declaration creates an immutable reference to a value. It does not mean the value it holds is immutable.
You use const to ensure gameState always refers to the same data structure, even if you change its contents (e.g., adding or removing seeds).
Does all of that make sense, or would you like further explanation?
This is clear also. I can see also for the rest but it will take me time to get familiar with that kind of writing I’m afraid. With some practice eventually…