Demo: One-Two-Three Game

Updated at 1685686163000
It's a demo game for game-box and ezyfox-server but you can use it to build any your idea.

1. Game Play

Every game has 2 players. When a player access successfully to server, server will find an available room for the player. When the room has enough 2 members, a game will be started automatically. Players in the game will select an answer and send to server. The answer and comparation will be like this:

    public enum GameAnswer {
        ONE, // scissors
        TWO, // paper
        THREE; // hammer
        public static class ValueComparator implements Comparator {
            @Override
            public int compare(GameAnswer a, GameAnswer b) {
                if (a == ONE) {
                    if (b == TWO) {
                        return 1;
                    }
                    if (b == THREE) {
                        return -1;
                    }
                }
                if (a == TWO) {
                    if (b == ONE) {
                        return -1;
                    }
                    if (b == THREE) {
                        return 1;
                    }
                }
                if (a == THREE) {
                    if (b == ONE) {
                        return 1;
                    }
                    if (b == TWO) {
                        return -1;
                    }
                }
                return 0;
            }
        }
    }

When server received answer of all 2 players, the game will be finish, it will send to the players the game result. And then wait 15 seconds for result display to start a new game. When a player disconnect from server, the room will become be avaiable and the new game will not start util there is a new player come.

2. Setup Project

3. Run and Test

After that, please take a look to console log, you can see player1 and player2 will player game automatically with log:

join game: {players=[Player1], roomId=1}
another player join game: {otherPlayer=Player2}
new game started
game finished: {winner=Player1, answerByPlayer={Player2=TWO, Player1=ONE}}
new game started
game finished: {winner=Player1, answerByPlayer={Player2=ONE, Player1=THREE}}

Finish!!!