The game “Rock – scissors – paper”

Tell me how to implement it.
Theoretically, I’ll write the code myself.
How to write a game with a robot – I know myself.
But how to implement 1-to-1 – I don’t understand.
Please explain.


Answer 1, authority 100%

  1. Make an authorization system, for example, using sessions.
  2. Connect a data storage source, such as a database (even if you do not need to save the results of the games, the session, unfortunately, will not work).
  3. The start of the game initiates the creation of a new entry in the database indicating the players involved.
  4. After that, both players can view the progress of the game on the page with the game ID known to them (incomplete games in which the player participates can be presented, for example, immediately after the player’s authorization).
  5. You can make periodic AJAX requests to display the current state.
  6. Player pressing the desired move button (rock, paper or scissors) will update the game record, so the next AJAX request from the other player will show the updated state (say, “the second one was like, waiting for you”).
  7. After specifying both moves, the game ends, the same AJAX request can, for example, lead to a redirect to a static (in the sense that without AJAXa) page with the result.

Answer 2

There are two players. In the simplest case, we will use two variables. $player1, $player2. There is an array that stores all the elements of the game – stone, scissors, paper, etc. – $elements = ['','','']or an associative array where you will assign a specific index, both numeric and non-numeric. Then each variable, i.e. to the player, we assign an element from the array, which is chosen arbitrarily with the help of mt_randand then these variables are compared. Well, you also need to take into account the priority of the elements, i.e. rock can break scissors, paper can wrap around rock, and scissors can cut paper.