This post describes simple ‘paper-rock-scissors’ ActiveMQ Java example. ActiveMQ is the Apache response of MSMQ. There is a good installation for all OS in the book ‘ActiveMQ in Action’ or in here
We got the following task:
- Each application randomly picks one of the three options and using messaging transmits it to the opponent.
- An outcome is determined and displayed on the console.
Source code: paper-rock-scissors-project
I did it by making a simple Ant project runned with ‘ant game_demo’.
The part of the build.xml file responsible for that build is:
The class from which both Player_1 and Player_2 extends is Player. The class Player contains all common for the players method such as: choosing random guess (getRandomGuess); selecting winner from two guesses (getWinner).
I chose to represent the values Paper, Rock, Scissors with numbers recpectively 1, 2, 3. The main reason for that decition was that the getWinner algorithm became really short and cute
Now, to the main part.
Player_1 and Player_2 are both run as Threads in the GameDemo class.
From there Player_1 have 2 Queues ‘player_1’ for consuming messages sent from Player_2 and ‘player_2’ for sending messages to Player_2.
Player_1 makes 5 guesses and sends them to Player_2. After every send message it waits for response from Player_2 and when it receive it it computes the final result from the game and it shows it in the terminal.
In controversy, Player_2 has the ‘player_1’ queue in order to produce messages to Player_1 and ‘player_2’ queue in order to get messages from Player_1.
I will be happy to here if you have some ideas how this code can be done better or how it can be expand