-
This is a program created for a school event.
-
I wrote about it on Qiita.
-
It uses Multipeer Connectivity.
Mechanism Multipeer Connectivity uses Bluetooth or other means of communication. Since there may be a delay in receiving communication messages, two possible scenarios are considered. To determine which button was pressed first in each scenario, the following communication process takes place:
When one party presses the button significantly earlier than the other party (The terms “win” and “lose” refer to determining which party pressed the button first.) If we assume that the device that was pressed earlier is A, the following steps occur:
A informs B of the time when the button was pressed.
Since B has not yet pressed the button, B displays “lose” on the screen.
However, at this point, the A device does not know if it won or not. Therefore, after displaying “lose,” B informs A that it has lost.
When A receives this message, it realizes that it has won and displays “win” on the screen.
When the timing between the two teams is very close If B presses the button before receiving A’s message, the following steps occur:
When A presses the button, it sends the time when B pressed the button, just like before.
Before B receives this message, B also presses the button and sends the time when B pressed the button to A.
After that, A receives communication from B. B compares the time when it was pressed with the time when A pressed the button. As a result, B realizes that it has lost and displays “lose” on the screen.
Similarly, when A receives communication from B, it compares the times and realizes that it has won, displaying “win” on the screen.