 Okay, this tutorial is part of a series if you haven't watched the introduction to the series Go ahead and do that. Hopefully I remember to put an annotation up on the screen for the entire playlist but basically This is something new I'm trying. I'm going to create a script or a program and This week I'm going to show how you write the same program in different languages In this case, we're going to be working with bash or shell scripts today and we're going to be working with a dice game that I explained in the intro video called toss-up where you have a six-sided dice and they have one red side two yellow sides and three green sides and basically at any point you roll you have up to ten dice and Anytime you roll the dice as long as you have green you're good Green will give you points yellow or kind of neutral and red are bad if you don't have any green I explain all this probably better in the intro But the question is as you roll the dice You take your greens away. So you have less dice to roll. So as we go along. I Want to know what are my odds of rolling of losing or winning when I roll a certain number of dice So we're just going to write a quick little bash script that does just that so I'm going to use them as my text editor And I but you can use whatever text area you'd like. I Am going to start off with my little bit bigger Shabang line here telling my operating system to use the bash interpreter that this is a bash script Next we're going to create three variables. First, I'm going to say let Win equals zero the let part lets bash know that this is going to be an integer that we're going to be using it as a number and then let lose Equals zero and that will these are the variables we'll use to count the number of wins and the number of losses Next we're going to create an empty variable called x Then we're going to start and Depending on whether the user gives input at the beginning or not As an argument to the command we're either going to go with those arguments or ask the information needed basically when you know how many dice you want to roll and The number of rolls you want to do so what we're going to do is we're going to use an if then statement. I'm going to say if And we're going to say dollar sign pound here Dash LT for less than two so what this is saying is The if the number of arguments so dollar sign pound or number symbol whatever you call it in your country If that does the number of arguments so if the number of arguments is less than two then we're going to do something here and If it's less than two then we need to ask for the information the number of rolls and the number of dice I'm just going to finish out my if then statement so If we don't give it the information at the time of rolling or time of running the program we need to ask echo how many times to Do you want to Roll So and then we're going to put that into a variable here We'll say read the user's input and put it to a variable called rolls Next we'll ask it echo How many Dice do you want to roll and once again we'll read the user's input and we'll put that into a variable call Dice num for the number of dice but of course if the user gave that information at the time of Running the script we can just grab that information so I'm going to say rolls Equals ones the first argument and Dice num Equals the second argument now really we probably should Give make some sort of help file or some sort of output telling the user You know how to input that information, but it doesn't really matter. This is just an example, you know tutorial And if they don't give that information then it will ask for it So I'm not going to worry too much about that in this tutorial Next we're going to create an array and that array will be each side of the dice So there's six side to each each die one red two yellow and three green so we're gonna say red yellow yellow yellow Green green and green Great So we've created our die or our dice depending on how many we're going to roll each with six sides So now we're going to say four And I'm going to use a variable I in and they're going to pass at the output of sequencer Not sequencer seek this this command will give us the output of one Through the number of dice So it's doing a sequence For each loop it's going to start at one and it's going to keep looping until it reaches the max number of dice Which in the actual game the max you can have is 10 But in this script I guess you could do any number you want Now for each loop we are going to run a another for we're going to say for and we'll give it a variable I'll say why in and we're going to get the output of our sequence again. We'll once again start at one. Oh You know what? Let me change that We're going to first do the number of rolls Then we will do the number of dice so This and this are the variables that we retrieved either up here or as arguments and Now we're going to say do Done so each time we roll we are also going to roll the dice That's basically what we're doing and then we're going to say x which is the empty variable we create up top We're going to say x equals Whatever x or e equals and dice The array of dice and then we need to grab a side of the die So the die is six sides but arrays and most things in computer languages we start with zero which is the way it should be so we want to start from zero and Go through five. So we're going to say Roll the die So we're looking at the array here. I'm going to say dollar sign random percent five and then close those and We'll put a comma there comma there So we're rolling the die and by the way we're doing that is we're getting the value of x Which currently is nothing the first time we roll and we're going to say look at the array of die dice and Then we're going to grab a random number from zero through five, which would be one side of the die so each time we roll we're going to get a different output and we are going to Basically put those all on a line here in a second. So now that we have the value of x we have the I'm trying to explain this well the number of dice the number of rolls and then the number of dice So it's going to do that and it's going to make a line That is the number of rolls I'm sorry number of dice. I could do a bear job explain this. Let's see. Okay. Get in the game here Chris. Okay dollar sign X so we're going we're going to show the output of that on the screen So each time we roll we're going to show the output of that roll Depending on how many dice it is. So we'll roll once and if we're rolling three die dice We're going to have a line with three outputs of red yellow or green And then we're going to display that and then we're going to create another variable called outcome equals and Then we're going to say echo We're passing at the command echo dollar sign X and we're going to grep Grep type grep red Pipe that into grep dash V green So what we're doing here is We're taking the line that the roll whether it's of One die or ten dice and we're going to check each lot tech check that roll Did we roll any reds and then we're going to say are there no greens? Because if there is red and no greens the V is a reverse search We could we're going to lose if we if that's true, so we're going to say if dollar sign outcome equals nothing then Let win the number of wins equals itself plus one so it starts off at zero If we win now, we're going to add one to that and then next time if we win we'll add one to that else Let's lose plus equal one and then we're going to say Close our if statement and then we're going to reset X back to being blank So I don't think I did a great job of explaining that while we were going through it So I'm going to try to explain it better here. Okay, so we're going to say How many times are we going to roll and we're going to loop for each time and that's based on the user input either here? With the arguments or when they answer this question of how many rolls we want now each time we roll We need to roll a certain number of dice. Once again, that's what we get from the user input here or here So we might be rolling one die or ten dice We're going to grab a random side of that die Of each die and that's this array here So we're grabbing from zero through five eight randomly each time and then we're checking Are there any red lines that have red but no green because that would be a loss So if there If there are there would be a value in here So if there's a value we would lose if not then we win and we add to it each time and then we clear out X So now we just need we have our output going as far as each roll But now let's output the number of wins and number of losses So I'm going to say echo and I'll just do a little dividing line here And I'll say echo Wins Equals dollar sign Win which is our win vary our wins variable We're going to say echo Losses Equals dollar sign Loss and that's it. That's our whole script. Let's run it. Make sure I did everything properly Okay, here we go. We're going to run it. It's going to ask how many times This will say do you that's typo. Do you want to roll? We'll say a hundred times And it says how many dice do you want to roll? We'll say five And we'll run it and you can see this is each roll each time we roll We rolled a hundred times you get five outputs because we rolled five dice And at the very end you can see that we would win 69% of that's our 96% of the time and we would lose 4% of the time So continuing the game if you have five dice to roll the odds are with you We'll run it again, and we'll say how many times how many times you want to roll? We'll say a hundred again and We'll say how many dice do you want to roll? Let's say we only had one die to roll we'll do that and as you can see you start to lose more and Of course, we say we can add arguments this so I can say Here we'll roll one time I'm sorry roll roll a hundred times one die And if we do that a number of times you have a different output obviously because it is random Well as random as a computer can generate And you'll see it looks like if you only have one die you have a 20% Approximately chance of losing Let's add this up to a thousand rolls. Let's see if we get similar numbers So rolling it a thousand times and we're still getting about 18% loss there. We'll roll it again the same thing There we got about 19% loss, so let's say we're going to roll Three die there three dice a thousand times Our chances looks like went down that roll about 13% chance to 14% chance of loss losing there's about 15 So you can see this game the odds are pretty much always with you So If once again, if you haven't watched the beginning to real I explained the how the game works And I hope you enjoyed this tutorial. Let me go in here Vim And fix my typo here. How many times do you want to roll? Sorry if I got a little discombobulated in the middle of writing out the program there But I hope I explained all well by the end and we're going to do the same similar program In a few other languages as the week goes on probably see and JavaScript are probably two of the languages we're going to do so once again check out those tutorials and See how I would do them difference. I am not very good at C programming at all but I did come up with a solution to this and I just thank you for watching. Please is it films by Chris comm that's Chris the K There should be a link in the description as well as if I haven't forgotten a link to this the script So you cannot you know copy and paste it if you want, but I recommend typing it out And I just thank you for watching. I hope that you have a great day