Demo: One-Two-Three Game
Updated at 16856861630001. 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
- Clone project from github
- Import it to you IDE (example: IntelliJ)
- If you want to update game logic, please take a look GameService class first
3. Run and Test
- Run ApplicationStartup by your IDE or you can follow by README to see how to run and deploy.
- Run Client1Test to connect player1 to server.
- Run Client2Test to connect player2 to server.
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!!!