Dear programmers and lovers of programming! I develop the network version of the game "Chess" in JS and I have 2 boards ... one where only white moves ... the second where only black moves ... the problem is that these boards interact with each other, that is, move on one the white piece of the board on the second automatically the white piece becomes in the same cell ... I do not ask for the written program code, but I ask for help with an advice or an idea how to do it ... thanks in advance !!!
- Why two boards? - Photon
- Well, do you have a central server? Let the course go to the server on a move on one board, there it is checked for correctness, and the other board receives an update (for example, via longpoll). - VladD
- @Photon: for example, to have two people playing chess who are not sitting on the same host? - VladD
- each board has its own button for writing to the base of moves, that is, White writes to a certain cell in the base, and when the button is pressed on the black board, Black’s move completes the move of Black to the cell next to the white one, there are several moments thanks to which I did ((( - MikeVin
- @VladD, it’s still not clear .. If people are sitting on different machines, then everyone should see one board .. On which he makes his move himself, and someone else’s move just sees it. Animated - Photon
3 answers
It seems to me that it is easier for each cell to give a unique ID, there should be current games in the database, and there is infa where each figure stands and which player, let's say you decided to move the figure, immediately there is a query in the database, the position of this figure changes, and the other has an auto update, after 1 sec, so that there is a check whether nothing has changed there in the database, if the figurine has changed, you can do it on Node.JS / MongoDB so that the statuses are sent, also just to php / js / mysql
- This is all wrong as pipets .. How, for example, can the sequence of a game be displayed in this variant? - Photon
Store the batch notation in a single line in the database. In the simplest case, with a timer, each player receives this line and processes the changed "tail" of the line - this will be a maximum of one opponent’s move, which will have to be animated.
When you resume the game on this line, you can always recreate the position of the figures from scratch (with or without animation)
As an option without a base, you can make the game in frames via postMessage (generate one board and give access to it to two players via iframe)
I would do this:
In a DB, I would make a table with fields:
- batch id
- turn number
- black or white
- starting position
- end position
That is, the party will look like this:
- id 1 white e2 e4
- id 2 Black e7 e5
and so on
Players communicate with the server via webSocket, so as not to send requests every second. As follows:
- people making a move
- check the correctness of the course
- send emit to server
- the server rechecks the correctness and if all is well, sends the move to the database and the second player