[#KINECTONE] Rock, paper, scissors with #KinectForWindowsV2

Hello!

Christmas is over and also New Year festivities, I also have some party with my renewal as MVP for the 7th time, so I’ll go back and start again with some posts about Kinect for Windows V2. For this post I’ll make a game “Rock Paper and scissors” with the new Kinect for Windows V2.

Note: if you don’t know how to play Rock, paper or scissors, Wikipedia explains it to you here .

The truth is that implementing this game is quite simple with Kinect for Windows V2, since now the HandState property supports several States

  • Open
  • Closed
  • Lasso
  • NotTracked
  • Undefined

The first 3 allow us to have a match with the States of the game

  • Open, paper
  • Closed, rock
  • Lasso, scissors

In this way, the only thing we have to do is to validate hand States with a Random computer-generated (by the way, the routine that generates the Random of an Enum have taken it from StackOverflow is a Marvel).

The main view of the app has a button that pressed down, start a timer with a downward counter for 5 seconds.Every tick of the timer displays the status of the hand of the player (line 24), and when the counter reaches zero, it generates a random game (line 41) value and validated the possibilities to define a winner (lines 48 to 80).

   1: private string GetRockPaperScissorFromHandState(HandState handState)

   2: {

   3:     switch (handState)

   4:     {

   5:         case HandState.Closed:

   6:             return "Rock";

   7:         case HandState.Open:

   8:             return "Paper";

   9:         case HandState.Lasso:

  10:             return "Scissor";

  11:     }

  12:     return "not defined, please play fair !!!";

  13: }

  14:

  15: private void ButtonPlay_OnClick(object sender, RoutedEventArgs e)

  16: {

  17:     ButtonPlay.IsEnabled = false;

  18:     _gameCounter = 5;

  19:     _timerGame.Start();

  20: }

  21:

  22: private void GameTimerTick(object sender, EventArgs e)

  23: {

  24:     HumanPlay = GetRockPaperScissorFromHandState(_humanHandState);

  25:     if (_gameCounter == 0)

  26:     {

  27:         _timerGame.Stop();

  28:         ButtonPlay.IsEnabled = true;

  29:         DefineWinner();

  30:     }

  31:     else

  32:     {

  33:         SkynetPlay = _gameCounter.ToString(CultureInfo.InvariantCulture) + " seconds to play ...";

  34:         _gameCounter--;

  35:     }

  36: }

  37:

  38: private void DefineWinner()

  39: {

  40:     // get computer play and validate with last human play

  41:     var skynetPlay = RandomEnum<HandState>();

  42:     while (skynetPlay == HandState.NotTracked || skynetPlay == HandState.Unknown)

  43:     {

  44:         skynetPlay = RandomEnum<HandState>();

  45:     }

  46:     SkynetPlay = GetRockPaperScissorFromHandState(skynetPlay);

  47:

  48:     if (skynetPlay == _humanHandState)

  49:     {

  50:         TextBlockPlayResult.Foreground = new SolidColorBrush(Colors.Green);

  51:         PlayResult = "TIED !!!";

  52:     }

  53:     else if (

  54:         (skynetPlay == HandState.Open && _humanHandState == HandState.Closed) ||

  55:         (skynetPlay == HandState.Closed && _humanHandState == HandState.Lasso) ||

  56:         (skynetPlay == HandState.Lasso && _humanHandState == HandState.Open))

  57:     {

  58:         TextBlockPlayResult.Foreground = new SolidColorBrush(Colors.Red);

  59:         PlayResult = "SKYNET WINS !!!";

  60:     }

  61:     else if (

  62:         (_humanHandState == HandState.Open && skynetPlay == HandState.Closed) ||

  63:         (_humanHandState == HandState.Closed && skynetPlay == HandState.Lasso) ||

  64:         (_humanHandState == HandState.Lasso && skynetPlay == HandState.Open))

  65:     {

  66:         TextBlockPlayResult.Foreground = new SolidColorBrush(Colors.Green);

  67:         PlayResult = "YOU WIN !!!";

  68:     }

  69:     else

  70:     {

  71:         TextBlockPlayResult.Foreground = new SolidColorBrush(Colors.Red);

  72:         PlayResult = "Can't track your hand, please play fair !!!";

  73:     }

  74: }

  75:

  76: public T RandomEnum<T>()

  77: {

  78:     var values = (T[])Enum.GetValues(typeof(T));

  79:     return values[new Random().Next(0, values.Length)];

  80: }

Note: The truth is that the validation of results section could implement it “in amore fashion way”, however it will be more complicated to put it on the post.

In the app I also display the video feed and the body position to display different colors for different hand states. Right now we have to make some adjustments using the new SDK to coordinate both feed like Zubair explains in his post.

The video below shows an example of the app running. In it you can see how the Random favors so that you always win Skynet and also as change the color of the hand in the positions of stone, paper, or scissors. .

When you return to Madrid, I shall speak with gem or Marta to help me with the aesthetic part and leave the app a bit more “pretty”. In addition to changing her way of start, to make it as the classic stone, paper scissors.

Resources

And as always, the disclaimer

“This is preliminary software and/or hardware and APIs are preliminary and subject to change

Greetings @ Cordoba

El Bruno

imageimageimageGoogle

2 comments

  1. Hello, Do you have sample code for this? Iwould like to dive into how did you display the video feed and the body position to display different colors for different hand states. could you please send me an email and attach the sample code as .zip?

    Like

    1. Hi Charlie,
      You can easily build the App based on the code samples I put in the posts. The SDK is still not public, I’m not sharing code.
      Regards

      Like

Leave a Reply to Charlie Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: