Rock-Paper-Scissors with Netduino and Arduino


Been constantly persuaded by my 6 years old daughter to play Rock-Paper-Scissors I decided that we can make it a bit more interesting, so I’ve put following schematics on the prototyping shield:

Rock-Paper-Scissors

Netduino and Arduino are pin compatible, but the output voltages are different: 3.3V for Netduino and 5V for Arduiono. For current limiting, I’ve selected 150Ohm resistor to avoid changing the elements, when change the platform. The 10K pull-down resistors assure false state on the digital inputs until button is pressed.

I’ve tested it with Netduino Plus, Arduino Uno and Shrimpuino (homebrewed Arduino Uno analog).

Netduino:

using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace RockPaperScissors
{
	public class Program
	{
		private static OutputPort redLed = new OutputPort(Pins.GPIO_PIN_D11, false);
		private static OutputPort greenLed = new OutputPort(Pins.GPIO_PIN_D12, false);
		private static OutputPort blueLed = new OutputPort(Pins.GPIO_PIN_D13, false);

		private static InputPort rockButton = new InputPort(Pins.GPIO_PIN_D10, true, ResistorModes.Disabled);
		private static InputPort paperButton = new InputPort(Pins.GPIO_PIN_D6, true, ResistorModes.Disabled);
		private static InputPort scissorsButton = new InputPort(Pins.GPIO_PIN_D2, true, ResistorModes.Disabled);

		private static InputPort stopButton = new InputPort(Pins.ONBOARD_SW1, true, Port.ResistorMode.Disabled);

		private enum Choice
		{
			None,
			Rock,
			Paper,
			Scissors
		}

		private enum Player
		{
			None,
			First,
			Second
		}

		public static void Main()
		{
			while (stopButton.Read())
			{
				AllLedsOff();
				IntroForPlayer(Player.First);
				Choice firstPlayer = GetChoice();

				AllLedsOff();
				IntroForPlayer(Player.Second);
				Choice secondPlayer = GetChoice();

				AllLedsOff();
				EvaluateAndShowResult(firstPlayer, secondPlayer);

				Thread.Sleep(2000);
			}
		}

		private static void IntroForPlayer(Player player)
		{
			switch (player)
			{
				case Player.First:
					greenLed.Write(true);
					break;
				case Player.Second:
					redLed.Write(true);
					break;
			}
		}

		private static Choice GetChoice()
		{
			var choice = Choice.None;
			while (choice == Choice.None)
			{
				if (rockButton.Read())
				{
					choice = Choice.Rock;
				}

				if (paperButton.Read())
				{
					choice = Choice.Paper;
				}

				if (scissorsButton.Read())
				{
					choice = Choice.Scissors;
				}
			}

			Thread.Sleep(500);
			return choice;
		}

		private static void EvaluateAndShowResult(Choice firstPlayerChoice, Choice secondPlayerChoice)
		{
			if (firstPlayerChoice == secondPlayerChoice)
			{
				ShowWinner(Player.None);
				return;
			}

			if ((firstPlayerChoice == Choice.Rock) && (secondPlayerChoice == Choice.Scissors) ||
				(firstPlayerChoice == Choice.Paper) && (secondPlayerChoice == Choice.Rock) ||
				(firstPlayerChoice == Choice.Scissors) && (secondPlayerChoice == Choice.Paper))
			{
				ShowWinner(Player.First);
			}
			else
			{
				ShowWinner(Player.Second);
			}
		}

		private static void ShowWinner(Player player)
		{
			for (var i = 0; i < 8; i++)
			{
				if (player == Player.None)
				{
					blueLed.Write(true);
				}
				else
				{
					IntroForPlayer(player);
				}

				Thread.Sleep(200);
				AllLedsOff();
				Thread.Sleep(100);
			}
		}

		private static void AllLedsOff()
		{
			redLed.Write(false);
			greenLed.Write(false);
			blueLed.Write(false);
		}
	}
}

Arduino:

int redLed = 11;
int greenLed = 12;
int blueLed = 13;

int rockButton = 10;
int paperButton = 6;
int scissorsButton = 2;

int choiceNone = 0;
int choiceRock = 1;
int choicePaper = 2;
int choiceScissors = 3;

int playerNone = 0;
int playerFirst = 1;
int playerSecond = 2;

void AllLedsOff() {
	digitalWrite(redLed, false);
	digitalWrite(greenLed, false);
	digitalWrite(blueLed, false);
}

void ShowWinner(int player) {
	for (int i = 0; i < 8; i++) {
		if (player == playerNone) {
			digitalWrite(blueLed, true);
		} else {
			IntroForPlayer(player);
		}
		delay(200);
		AllLedsOff();
		delay(100);
	}
}

int GetChoice() {
	int choice = choiceNone;
	while (choice == choiceNone) {
		if (digitalRead(rockButton) == HIGH) {
			choice = choiceRock;
		}
		if (digitalRead(paperButton) == HIGH) {
			choice = choicePaper;
		}
		if (digitalRead(scissorsButton) == HIGH) {
			choice = choiceScissors;
		}
		delay(10);
	}
	delay(500);
	return choice;
}

void IntroForPlayer(int player) {
	if (player == playerFirst) {
		digitalWrite(greenLed, true);
	} else if (player == playerSecond) {
		digitalWrite(redLed, true);
	}
}

void EvaluateAndShowResult(int firstPlayerChoice, int secondPlayerChoice) {
	if (firstPlayerChoice == secondPlayerChoice) {
		ShowWinner(playerNone);
		return;
	}
	if ((firstPlayerChoice == choiceRock) && (secondPlayerChoice == choiceScissors) ||
	    (firstPlayerChoice == choicePaper) && (secondPlayerChoice == choiceRock) ||
	    (firstPlayerChoice == choiceScissors) && (secondPlayerChoice == choicePaper)) {
		ShowWinner(playerFirst);
	} else {
		ShowWinner(playerSecond);
	}
}

void setup() {
	pinMode(redLed, OUTPUT);
	pinMode(greenLed, OUTPUT);
	pinMode(blueLed, OUTPUT);

	pinMode(rockButton, INPUT);
	pinMode(paperButton, INPUT);
	pinMode(scissorsButton, INPUT);
}

void loop() {
	while(true){
		AllLedsOff();
		IntroForPlayer(playerFirst);
		int firstPlayer = GetChoice();

		AllLedsOff();
		IntroForPlayer(playerSecond);
		int secondPlayer = GetChoice();

		AllLedsOff();
		EvaluateAndShowResult(firstPlayer, secondPlayer);

		delay(2000);
	}
}

What the code is doing and how to play:
After MCU initialise, RGB LED becomes steady GREEN thus indicating first player’s move. In secret from the other player, press the chosen button (from left to right in order: ROCK, PAPER, SCISSORS). RGB LED becomes steady RED and now this is the second player’s turn (at that stage of the game, the player may not press the button in secret any more). After the second player’s choice, the RGB LED will flash with the winners colour: GREEN (first player), RED (second player) and BLUE for TIE. Few seconds later, game restarts and you can play again.

Enjoy.

4 comments on “Rock-Paper-Scissors with Netduino and Arduino

  1. Pingback: Georgi's blog

  2. Pingback: Rock-Paper-[Netduino/Arduino] - Russian Coding 4 Fun - Site Home - MSDN Blogs

  3. Pingback: Камень-ножницы-[Netduino/Arduino] - Russian Coding 4 Fun - Site Home - MSDN Blogs

Leave a comment