In this lesson, we're going to go through the solution to part one of building rock, paper, scissors.
Remember, the first task is to see if the user wants to play and we can pick up the user's answer using
scanned next line.
So we'll set a variable already is equal to scan.
Next line.
And that's really it.
I'm going to run my coat after each task because it's a good habit to keep testing your coat.
And it looks like the OP starts by asking the user if they want to play, to which we reply yes.
So far, so good.
Now, test tube was to set up the game, and so if the user enters, yes, we can start by setting up
the game, that is, if the answer is stored inside the ready variable.
Equals, yes.
We're going to print a new line.
Great, then we'll print rock, paper, scissors shoots.
Otherwise else.
We'll print Dan's some other time.
Now the game starts after rock, paper, scissors, shoot, so here the user needs to choose between
a rock, paper, scissors, and we can easily pick up the user's choice with scanned next line.
And that's all task to I can now safely delete it because we're done.
And all of these tasks are going to be done inside the if statement, so I'm going to move them here.
OK, I'm going to run a test case.
Where the user says yes.
I'll choose rock and good this worked out.
Now I'm going to run a test case where the user says anything else.
And it prints Darren some other time.
The app is coming together so we can move on to task three.
The next step is to write a function, it's going to let the computer pick randomly between rock, paper,
scissors, if you look at the instructions, it returns a string and takes no parameters.
So we'll say public static string.
The function is called computer choice, and inside the function, the first instruction is to pick
a random number between zero and two.
The first, I can use math at random to get a random decimal from zero to less than one.
And we can multiply the result by three to scale the range from zero to less than three, essentially
from zero to less than two.
So we'll make a comment that says it returns a number between zero and less than three.
We can convert this number to an integer by typecasting the double tint.
This is going to cut off all the decibels and so the integer variable is going to carry any hole number
between zero and less than three.
In other words, from zero to two.
And so the next step is to compare the random integer value against three cases, so we'll make a switch
statement that compares the random integer.
Against the three possible integers that it could be zero.
In case, Tim.
If it matches case zero, return the string rock.
Return paper for case one.
And scissors for case to.
And as always, we need to add a default case.
We're in we're just going to return an empty string because it's impossible for the injured to be any
number other than zero one or two.
So I don't expect default to ever run.
But we need to put it anyway because the function expects a return value no matter what.
OK, that's all we need for our function.
I can now call the function from Main.
And this function call is going to return a random string that can be rock, paper, scissors, and
we're going to store that value in a variable called computer choice.
OK, finally, we can test our code by putting in two temporary print statements, I'm going to print
first, I'll insert a new line of space for aesthetic purposes.
Then I'll say you chose Colon and I'll put a tab of space between the string and the value that you
chose.
I'll do another print.
We're all say the computer chose.
Colin and I'll put a tab of space between this string and the random value returned by the computer.
OK, we can perform a final test.
And that prince, my choice, as well as the computer's choice, I'm going to keep running my code and
as I'm doing that, the computer keeps picking random choices, which is what you'd expect from a game
of rock, paper, scissors.
OK, so the next step is to compare your choice against the computer's choice and then you're going
to write code that determines the winner of the game.
See you in part two.