Lucky Roshambo demonstrates how to create a Rock-Paper-Scissors game or Roshambo as it’s called in many parts of North America – the game logic for this is quite simple and is a good way of showing a game you’ve probably played in real-life converted into a game you can play on a Computer.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Threading.Tasks; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; public class Library { private const int size = 3; private const int win = 1; private const int draw = 0; private const int lose = -1; private int[,] match = new int[size, size] { { draw, lose, win }, { win, draw, lose }, { lose, win, draw } }; private readonly int[] values = new int[] { 0, 1, 2 }; private readonly string[] options = new string[] { "\uED5B", "\uE130", "\uE16B" }; private readonly Color[] colours = new Color[] { Colors.DarkRed, Colors.DarkBlue, Colors.DarkGreen }; private Random random = new Random((int)DateTime.Now.Ticks); private async Task<ContentDialogResult> ShowDialogAsync(string title, int option) { ContentDialog dialog = new ContentDialog() { Title = title, Content = GetShape(option, false), PrimaryButtonText = "OK" }; return await dialog.ShowAsync(); } private async void Choose(int option) { int player = values[option]; int computer = random.Next(0, size - 1); int result = match[player, computer]; string message = string.Empty; switch (result) { case win: message = "You Win!"; break; case lose: message = "You Lost"; break; case draw: message = "You Draw"; break; } await ShowDialogAsync($"Computer Picked - {message}", computer); } private Grid GetShape(int option, bool useEvent) { Grid grid = new Grid() { Tag = option, Margin = new Thickness(5), Height = 80, Width = 80, Background = new SolidColorBrush(colours[option]), }; TextBlock text = new TextBlock() { Text = options[option], FontSize = 66, FontFamily = new FontFamily("Segoe MDL2 Assets"), Foreground = new SolidColorBrush(Colors.White), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; if (useEvent) { grid.Tapped += (object sender, TappedRoutedEventArgs e) => { Grid selected = (Grid)sender; int tag = (int)selected.Tag; Choose(tag); }; } grid.Children.Add(text); return grid; } private void Layout(ref Grid grid) { grid.Children.Clear(); StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center }; for (int i = 0; i < size; i++) { panel.Children.Add(GetShape(i, true)); } grid.Children.Add(panel); } public void New(ref Grid grid) { Layout(ref grid); } }
There’s some values that are used to encapsulate the main component of the game itself, there’s the three states win, draw and lose plus there’s what matches of each type of item – Rock, Paper or Scissors result in those states, these are the values and then there’s the options which are the Characters of the Segoe MDL2 Aseets Font represent each of those states. There’s a Random for a random number generator and then a method for displaying a dialog – ShowDialogAsync, this will also show which option was seleccted by the random number generator. When you Choose an option this will randomly select a response then you can see if you Win, have Lost or the result was a Draw, there’s GetShape which returns a Grid and TextBlock to display one of the options plus there’s an event handler used when useEvent is true to call the Choose method with the selected option and there’s the Layout method to display the options when New is called.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Viewbox> <Grid Margin="50" Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Viewbox> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Page2" Label="New" Click="New_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a ViewBox containing a Grid Control that will be use for the contents of the game. The second block of XAML is is the CommandBar which contains New – to start a Game.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { library.New(ref Display); }
Below the MainPage() Method an instance of the Library Class is created, then New_Click is used to call the New method to setup the game and start playing.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running you can then select New to start the game then press on Rock – the first button, Paper – the second button or Scissors – the third button, then you can see what the Computer selects to see if you Win, Lose or Draw
Step 15
To Exit the Application select the Close button in the top right of the Application
This a simple game that most people have either played or heard of and can see how that translates to a Universal Windows Platform example or a chance to play it for the first time, with simple rules – Rock beats Paper, Paper beats Rock, Scissors Beats Paper, Rock Beats Scissors etc which may seem complex but all those rules are contained within this simple example.