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.
Answer 1, authority 100%
- Make an authorization system, for example, using sessions.
- 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).
- The start of the game initiates the creation of a new entry in the database indicating the players involved.
- 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).
- You can make periodic AJAX requests to display the current state.
- 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”).
- 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.
There are two players. In the simplest case, we will use two variables.
$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.