CSc 59866: Applied Cryptography & Information Security

Lab 1: Rock-Paper-Scissors MIDLet (User vs. SPOT)

Instructor: Nelly Fazio

Back to class webpage


[ Introduction | Design Guidelines | Collaboration Policy | Hand-in Procedure]


Introduction

In this lab, you will build a simple MIDLet application which will allow you to play the Rock-Paper-Scissors game against the (emulated) SunSPOT. By doing the lab, you will gain familiarity with some of the components of the SunSPOT devices (LEDs and switches) and learn how to deploy your own applications on emulated SPOTs.

Design Guidelines

As we have already seen, each SPOT is equipped with eight tricolor LEDs, L0, L1, L2, L3, L4, L5, L6 and L7:
LEDs
and two switches, SW1 (to the left) and SW2 (to the right).
Virtual SPOT

In the Count MIDlet that is bundled in the EmulatorDemo, the switches are programmed to light up the LEDs in a binary pattern and/or change their color. In this lab, you will program the right switch SW2 to cycle through the three possible selections (Rock, Paper, or Scissors), and the left switch SW1 to confirm the selection and reveal the SPOT's own move. More specifically, the three rightmost LEDs (L5, L6 and L7) shall display the user selection, while the three leftmost LEDs (L0, L1 and L2) shall display the computer selection. The two central LEDs (L3 and L4) will be used to display the winner. We will use different colors to display the user's and the SPOT's selections: green for the user LEDs and red for the SPOT's. Here is a pictorial representation of how to light the LEDs to display the computer and the user selection.

Selections on computer' and user's LEDs

The two central LEDs shall be solid blue while the user makes his/her choice, and turn to flushing green, red, or blue depending on whether the user won, lost, or tied the game. The initial LEDs configuration is depicted below:

Initial LEDs configuration

The user goes first, and uses switch SW2 to make his/her choice: one click for Rock, two clicks for Paper, and three clicks for Scissors. Further clicks shall cycle back through Rock, Paper, etc. When the user confirms his/her selection by pressing the SW1 switch, the SPOT reveals its own choice in its LEDs (more details on this below). Next, the Rock-Paper-Scissors rules are applied to declare the winner. Here is a pictorial representation of the rules of the game:

rock_paper_scissors rules
(From Wikipedia's rock-paper-scissor entry.)

The two middle switches (L3 and L4) will then blink for five times, in green if the user won; in red if the user lost, and in blue if there is no winner (the SPOT and the user made the same selection):

Final LEDs configuration

After blinking five times, the LEDs return to their initial configuration, ready for a new game:

Initial LEDs configuration

How to implement the SPOT's selection
When the user confirms his/her choice by pressing the switch SW1, it's time for the SPOT to make its own selection. You should implement a random strategy, that returns a number between 0 and 2 chosen uniformly at random (Rock=0, Paper=1 and Scissors=2). To this aim, you can use the random() method of the Math class of the Java platform.

The random() method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. A way to turn the result of random() into an integer between 0 and 2 is:

(int) (Math.random() * 3)

Collaboration Policy

You must write all the code you hand in for the programming assignments, except for code that is part of the EmulatorDemo. You are not allowed to look at anyone else's solution. You may discuss the assignments with other students, but you may not look at or copy each others' code.

Hand-In Procedure

To turn in your lab, e-mail your Rock-Paper-Scissors MIDLet application as a .zip file (your.name-lab1.zip) at "fazio AT cs DOT ccny DOT cuny DOT edu" by 11:59pm, October 4, 2009.


Copyright © Nelly Fazio