Rock Paper Scissors Game

rpsThis robot is designed to play a game of rock paper scissors with one person. It uses one EV3 control brick, three NXT touch sensors, and three legacy RCX lamps. The program is written in leJOS EV3 code.

Playing the Game

When the program begins, the words “Rock”, “Paper”, “Scissors”, and “GO!” are flashed onto the screen, after which the user must choose a move be pressing one of the three touch sensors on the front of the robot. After the user has made a move, the robot will turn on the lamp corresponding to its move, which is decided randomly.

The LED lights on the EV3 brick will light up to indicate the result of the round. Green light means that the user has one the round, red light means that the user lost and the robot one, and orange light indicates a draw (i.e. the robot and the user choose the same move). The result is decided based on the traditional rules of rock paper scissors, with rock beating scissors, but losing to paper; paper beating rock, but losing to scissors; and scissors beating paper, but losing to rock.

During rounds a scoreboard is displayed on the LCD screen.  The top part displays the amount of moves made by each user, with the robot’s moves count in the top row, and the user count in the bottom. Below this on a single line is the count of wins, losses and draws attributed to the user. Here is a basic representation of how the scoreboard might look during a game:

  
     3   8   6
     R   P   S
     5   7   3

  W: 6  L: 5  D: 4

When the scoreboard is displayed after a round, a box is drawn around the most recently made moves, as well as the result of the round.

Program

The program is written in leJOS EV3 code, and consists of three Java classes. The main class is called RockPaperScissors. It starts the program, manges sensor and hardware operations, handles the exit button, and manages threads. The second class RPS manages all functionality to do with the rock paper scissors game. The last class Scoreboard is used by the RPS class to store a log of moves, wins and draws for both the robot and the user, and then draws the scoreboard onto the screen.

The entire program code can be found on GitHub.

Issues and Improvements

Drawing centered text

One of the limitations I discovered in the leJOS programming language is that you cannot draw text on the LCD screen in real X and Y positions as you would shapes or pixels. Instead, the screen is divided into a grid based on the text height and width. While this may seem convenient, it makes it difficult to draw text in exactly the center of the screen, as I could with NXC.

The only way I could overcome this limitation was to fork the lejos.hardware.lcd.LCD.drawString() method and remove the grid code. This was a bit confusing at first because that method deals with drawing on the LCD at a very low-level, but I managed to figure it out. I made the code into a utility class that can be used across different programs.

Confusing messages

In the original version of the game, when the user one the message “You won” was displayed in the LCD screen, and when the robot one the message “I won” was displayed. This was confusing to people playing the game, so I changed the game’s messages to be more focused on the user, displaying whether they one or lost rather than who won.

Legacy lights

I’m pretty new to leJOS EV3, so I’m still discovering how to use different sensors and outputs with the leJOS API. For most sensors, I know how to look through the API docs or the source code to determine how to set up and use the sensor, but I couldn’t find how to use the legacy LEDs that were part of the RCX system. As it turns out, it is as simple as plugging them into an output port, and treating them as a motor.

Robot Design

The design is based around a square frame. On the front are three touch sensors in a line, corresponding to the three moves. This is where the user enters their move. Each button has a brick-built representation of their move attached to the front. To the rear of the frame are three lamps, lining up with the buttons, and corresponding to the same moves. This is where the robot indicates its move to the user. Attached to the side of the frame is the EV3 brick.

Leave a Reply

Your email address will not be published. Required fields are marked *