An array in a loop king of thing

Hi Thallius;

Thank for your help. :+1: 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. :slightly_smiling_face:

I expected this answer as most people take the easy way. But I can tell, that are not the people who become good in what they are doing :slight_smile:

No problem. Happy to help.

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.

But thanks again for trying to help. :slightly_smiling_face:

Hi James;

Me again. The whole thing is done and added to the few other small other games I have online.

You solution certainly saved me a lot of lines of code for the rest; managing all seeds and so on…

There is one thing though. It’s this:

const gameState = Array.from({ length: SLOTS }, (_, index) => ({
  id: index + 1,
  seeds: SEEDS
}));

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? :grinning:

Also; why do you use const? Isn’t a constant a data that doesn’t change?

Hi,

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?

1 Like

Ok. const is all clear now.

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…

Thanks again. :+1:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.