Coding

Programming Rock, Paper, Scissors with no If Statements (Python)

  • 00:00:00 what's up guys and welcome back to
  • 00:00:01 another video so in the last video I
  • 00:00:03 posted we programmed this
  • 00:00:05 rock-paper-scissors shoot game in Python
  • 00:00:08 that looked like this so basically it
  • 00:00:10 prompts you to make your choice you can
  • 00:00:12 type in rock paper or scissors tells you
  • 00:00:15 your choice the computer makes a random
  • 00:00:17 choice and then it tells you the outcome
  • 00:00:19 of that game and you can just keep doing
  • 00:00:21 this so I can type in scissors now it
  • 00:00:24 tells me the outcome and I can also type
  • 00:00:26 in something that's like an invalid like
  • 00:00:28 asdf and it tells me invalid choice try
  • 00:00:30 again so the goal of this video is to
  • 00:00:33 take that exam exact functionality from
  • 00:00:35 the last video that I use code that
  • 00:00:38 looked pretty much like this this is a
  • 00:00:40 slightly different but it's pretty much
  • 00:00:43 exactly the same as the last video if
  • 00:00:46 you want this code I posted it on my
  • 00:00:47 github page so just check the link in
  • 00:00:50 the description for that but so the the
  • 00:00:53 goal of this video is to take this code
  • 00:00:55 right here and as you can see it uses a
  • 00:00:57 lot of if Elif and else statements and
  • 00:01:00 we're trying to program that exact same
  • 00:01:02 functionality without any if statements
  • 00:01:05 so to begin I recommend that you take
  • 00:01:07 like a minute or two pause the video and
  • 00:01:09 like try to think about how you might be
  • 00:01:11 able to do this without any if
  • 00:01:13 statements and also you're not allowed
  • 00:01:15 to use any like while state while loops
  • 00:01:18 like cleverly where you just kind of go
  • 00:01:21 into one while loop and break right out
  • 00:01:22 so that's not allowed either the only
  • 00:01:24 wild loop whirl will allow is this while
  • 00:01:26 true so how do we do these comparisons
  • 00:01:30 right here without using any if or Elif
  • 00:01:33 statements so take a second pause the
  • 00:01:35 video and think about that alright the
  • 00:01:37 way I went around thinking about this
  • 00:01:38 problem was instead of using our if Elif
  • 00:01:41 and else statements we think of having a
  • 00:01:43 matrix so in Python that's a list of
  • 00:01:45 lists and each position in this matrix
  • 00:01:48 represents a different outcome that can
  • 00:01:51 happen in our rock-paper-scissors shoot
  • 00:01:52 game so to make that a little bit more
  • 00:01:54 explicit we think of each row
  • 00:01:58 representing either rock paper or
  • 00:02:00 scissors and we'll say the rows are our
  • 00:02:02 choice the player choice and then each
  • 00:02:04 column also represents rock paper or
  • 00:02:06 scissors and that will represent the
  • 00:02:08 computer choice and if we want to make
  • 00:02:11 this like more pythonic we can't just
  • 00:02:13 index
  • 00:02:13 rock paper and scissors so what we're
  • 00:02:15 gonna have to do is map these guesses
  • 00:02:19 these choices to actual numbers that we
  • 00:02:21 can index into this matrix so we have
  • 00:02:25 rocky meaning zero paper being one
  • 00:02:28 scissors two and so now what we get is
  • 00:02:32 if I choose rock and computer chooses
  • 00:02:36 rock then we should get a tie so we can
  • 00:02:39 represent that spot right here in this
  • 00:02:42 matrix with a value that represents tie
  • 00:02:45 so we can say that say zero represents
  • 00:02:47 tie so then somewhere we'll use like a
  • 00:02:50 key that represent that map's zero to
  • 00:02:54 the response it is a tie and we can also
  • 00:02:57 have like values that represent the we
  • 00:03:02 win condition and though we lose
  • 00:03:03 conditions so we'll say that one and two
  • 00:03:05 represent those so one means we win two
  • 00:03:09 means bummer we we didn't win we lost so
  • 00:03:14 right here the next position we would
  • 00:03:15 fill out is rock zero paper one so that
  • 00:03:21 case we would win or no we would lose
  • 00:03:23 because paper beats Rock so the opponent
  • 00:03:26 would win and we'd put a two in that
  • 00:03:28 spot on the matrix moving forward if we
  • 00:03:32 have Rock they have scissors
  • 00:03:33 we dominate them and we would put a one
  • 00:03:36 in that position moving onwards paper
  • 00:03:39 versus Rock we win in that case so we're
  • 00:03:41 gonna put a one and you kind of get the
  • 00:03:43 picture so we can fill out the rest of
  • 00:03:45 these with the values so this is a good
  • 00:03:48 start the one additional thing I want to
  • 00:03:50 mention is we also in our original game
  • 00:03:52 allowed the player to make a choice that
  • 00:03:55 wasn't valid so we might want to add an
  • 00:03:57 extra row to this matrix which
  • 00:03:59 represents like kind of the invalid
  • 00:04:00 condition so we'll say that in Val any
  • 00:04:05 invalid condition maps to three and we
  • 00:04:09 had an additional little thing in our
  • 00:04:11 key that says invalid try again here
  • 00:04:13 alright so now with this additional
  • 00:04:15 information I challenge you guys to try
  • 00:04:17 to implement this Rock Paper Scissors
  • 00:04:19 shoot game without any if statements and
  • 00:04:22 if you can't quite get it I will go
  • 00:04:24 through it right after you unpause the
  • 00:04:26 video
  • 00:04:27 okay let's start writing some code so to
  • 00:04:30 begin we can basically just copy some of
  • 00:04:33 the code from the first video because
  • 00:04:36 our goal is to replicate the same
  • 00:04:38 functionality but just not use any F
  • 00:04:39 statements so as you can see all the way
  • 00:04:42 through here we don't use any of
  • 00:04:43 statements so it's good for me to just
  • 00:04:45 go ahead and copy that so I'll begin by
  • 00:04:47 copying these like five lines or six
  • 00:04:50 songs I don't know how many lines were
  • 00:04:51 actually there so here we just have a
  • 00:04:55 while loop that's grabbing our choice
  • 00:04:58 lower casing it and then printing it out
  • 00:05:00 and reminder that because I'm using
  • 00:05:03 sublime text and it's not very friendly
  • 00:05:05 with this input built-in function right
  • 00:05:08 here I'm using sublime repl to run the
  • 00:05:13 file from sublime text and there is a
  • 00:05:15 link to download this in the description
  • 00:05:18 if you need it so rock my choice is Rock
  • 00:05:21 choice and as you can see if I also do
  • 00:05:24 some weird capitalization here it will
  • 00:05:27 lowercase it for me all right so that's
  • 00:05:30 what we have so far let's add some of
  • 00:05:32 the additional lines so let's now do the
  • 00:05:34 computer choice so the computer choice
  • 00:05:38 we can just copy those lines as well we
  • 00:05:40 have choices we're gonna copy all three
  • 00:05:44 of these lines we have the list of
  • 00:05:48 choices the computer can choose from we
  • 00:05:50 make a random choice out of those and
  • 00:05:52 then we can we print that random choice
  • 00:05:55 and one thing to note real quick is to
  • 00:05:58 use this random library I do need to go
  • 00:06:00 ahead and import that so don't want to
  • 00:06:03 forget to do that okay and let's just
  • 00:06:06 run again real quick Rock my choice is
  • 00:06:14 raw computer choices paper okay cool
  • 00:06:16 that's looking good so far
  • 00:06:18 next let's reference that diagram I was
  • 00:06:21 showing just a second ago real quick and
  • 00:06:23 looking at it you can see that one thing
  • 00:06:26 we want to do immediately to use this
  • 00:06:29 matrix that we have defined is we first
  • 00:06:31 need to convert our choice into a number
  • 00:06:37 between 0 and 3 so to do that we're
  • 00:06:40 going to
  • 00:06:40 dictionary so I'm just gonna call this
  • 00:06:43 choice dict and so in that this is what
  • 00:06:48 we're gonna do is if the choices Rock
  • 00:06:51 will map it to the 0th index if the
  • 00:06:54 choice is paper we're going to map that
  • 00:06:57 to the first index and then finally if
  • 00:06:59 the choice is scissors then we're going
  • 00:07:02 to map that to the 2 or the second index
  • 00:07:09 and I can just show you that this works
  • 00:07:13 so if I print choice Dix of choice
  • 00:07:24 choice as index so just I'll show you
  • 00:07:29 that this works real quick tools sublime
  • 00:07:32 repl Python run current file rock choice
  • 00:07:36 index 0 cool
  • 00:07:38 so it's grabbing rock and then it knows
  • 00:07:39 where to look for rock in the dictionary
  • 00:07:43 you just defined so what you might be
  • 00:07:45 wondering right now is what happens
  • 00:07:47 right now if we type in something that
  • 00:07:49 is invalid what number does that print
  • 00:07:52 out so we can go again I'm gonna rebuild
  • 00:07:57 this and now I'm going to type in rock
  • 00:08:02 it ship which will be invalid so it
  • 00:08:08 actually gives us an error so we need to
  • 00:08:09 work around this error for that invalid
  • 00:08:11 case that should map to 3 and this is
  • 00:08:15 something that I find really neat and
  • 00:08:16 actually learned fairly recently about
  • 00:08:19 dictionaries but within a dictionary so
  • 00:08:23 we're gonna set this as a variable so
  • 00:08:26 choice index equals so using the Python
  • 00:08:31 dictionaries there's actually this built
  • 00:08:33 in dot get method and so you can get the
  • 00:08:39 key just like we did before when we were
  • 00:08:40 doing the kind of traditional index
  • 00:08:42 method so when we were doing in that
  • 00:08:44 choice dict
  • 00:08:45 of choice right so we can pass in
  • 00:08:49 choices our first argument to this and
  • 00:08:51 that will be the exact same behavior
  • 00:08:54 what's neat about this get function
  • 00:08:56 though is it lets you have passing they
  • 00:08:59 kind of DeVault value for anything
  • 00:09:01 that's not found in the end or in the
  • 00:09:03 dictionary so for that we can map a
  • 00:09:06 three all right so let's see what
  • 00:09:12 happens with our choice index now if we
  • 00:09:14 print choice choice index all right run
  • 00:09:24 that real quick I hope I did the syntax
  • 00:09:30 right I'm not actually looking at any
  • 00:09:32 reference so we'll see Rock choice index
  • 00:09:35 0 good paper should be 1 scissors to
  • 00:09:42 cool now a moment of truth if I type in
  • 00:09:48 Keith is cool just giving myself a
  • 00:09:52 little ego boost here choice index 3
  • 00:09:56 cool it works so it recognize that keith
  • 00:09:59 is cool is not a valid choice for rock
  • 00:10:02 paper scissors and it gave me that third
  • 00:10:04 index like I wanted we can do the same
  • 00:10:08 exact thing for the computer index so
  • 00:10:11 I'm going to delete this real quick so
  • 00:10:13 we already check this so for computer
  • 00:10:17 index I'm just going to be the computer
  • 00:10:19 choice index I could call it but I'm
  • 00:10:20 just going to abbreviate that just do
  • 00:10:22 computer index so this will be choice
  • 00:10:24 dict get and we now need to pass in the
  • 00:10:29 computer choice and I don't actually
  • 00:10:31 need a pass in this 3 I can if I want
  • 00:10:33 but I'm going to leave it blank because
  • 00:10:35 this should always give us something
  • 00:10:37 valid cool we'll check that again too
  • 00:10:45 computer choice index just make sure
  • 00:10:50 that this works tools oh I didn't
  • 00:10:56 actually print anything out hoops
  • 00:11:01 computer index all right rock computer
  • 00:11:10 choice scissors computer choice indexes
  • 00:11:13 – that looks good to me I'll just do one
  • 00:11:15 more check paper computer choice index
  • 00:11:19 is one yep that looks good to me
  • 00:11:24 all right so we now are both getting our
  • 00:11:27 indexes so now we just need to create
  • 00:11:29 that result matrix that I showed and
  • 00:11:31 I'll just show it on the screen again
  • 00:11:32 real quick so we can replicate this by
  • 00:11:37 doing so it's gonna be a list of Lists
  • 00:11:41 so it's gonna be a list of three lists
  • 00:11:45 I'm gonna separate these lists by commas
  • 00:11:49 and actually there's going to be that
  • 00:11:50 fourth list that fourth row sorry about
  • 00:11:53 that
  • 00:11:54 that fourth row that represents the
  • 00:11:57 invalid and to make this a little easier
  • 00:11:59 to see I will go ahead and like put
  • 00:12:02 these on different lines just so it
  • 00:12:04 actually replicates the look of the
  • 00:12:06 matrix in the picture all right so we
  • 00:12:09 have zero to one here we have one zero
  • 00:12:13 two right here we have two one zero here
  • 00:12:18 and then finally we have three comma 3
  • 00:12:21 comma 3 for this fourth result so let's
  • 00:12:26 okay so just make sure do a sanity check
  • 00:12:29 kind of here so if we indexed the first
  • 00:12:33 index of this will get us the row
  • 00:12:36 because it'll get us the specific list
  • 00:12:39 within this list of Lists and then the
  • 00:12:42 second index will get us the specific
  • 00:12:44 value in that and that checks out with
  • 00:12:46 what we want so we can get the specific
  • 00:12:49 result index by doing result index
  • 00:12:52 equals result matrix
  • 00:12:57 choice index so that's our choice it's
  • 00:13:01 the row and then the if once now that we
  • 00:13:04 have the specific row we're looking at
  • 00:13:06 we need to do the computer index and
  • 00:13:10 let's see if that gives us the right
  • 00:13:14 thing so I'm going to just print out
  • 00:13:15 result index and we'll just run this
  • 00:13:18 real quick
  • 00:13:22 make your choice rock our choices rock
  • 00:13:27 they chose scissors so we should win and
  • 00:13:30 that is represented by one let's try it
  • 00:13:33 a couple more times rock paper paper
  • 00:13:36 beats rock we should lose that's a two
  • 00:13:38 and souvenir a tie how many times we'll
  • 00:13:41 have to run it to get a tie let's see
  • 00:13:43 damn random numbers okay there we go
  • 00:13:46 rock and rock and we get 0 that looks
  • 00:13:49 good and I guess I could do the invalid
  • 00:13:51 as well cool that gives us the 3 awesome
  • 00:13:55 that looks like just like what we want
  • 00:13:58 so finally I guess there's one more real
  • 00:14:02 step to do and that's going to be
  • 00:14:04 dessert to define our result messages
  • 00:14:08 messages so we have 0 was the it is a
  • 00:14:13 tie the 1 was you win the to index to
  • 00:14:20 note that it's actually the third thing
  • 00:14:22 but it has an index or two with Python
  • 00:14:25 sorry
  • 00:14:26 come on hue lose fairy face and then
  • 00:14:30 finally the fourth result would be
  • 00:14:34 invalid guess or India valid choice come
  • 00:14:39 on try again and now what we can do to
  • 00:14:42 put all this together is we can get the
  • 00:14:46 result by doing resultant messages with
  • 00:14:50 the index of the result index so now
  • 00:14:53 that we'll get whatever we got from this
  • 00:14:54 result matrix it will index that
  • 00:14:57 position into this array and it should
  • 00:15:00 print us out the result we want and I
  • 00:15:03 think after this we will have all the
  • 00:15:04 behavior we had in our previous game
  • 00:15:07 let's see
  • 00:15:09 this drum roll rock oh no what happened
  • 00:15:18 a result message is not to find okay I
  • 00:15:23 just need to do result messages rerun
  • 00:15:27 this rock rock computer traces scissors
  • 00:15:37 you win yes paper your traces paper
  • 00:15:43 computer choices paper it is a tie cool
  • 00:15:45 let's say scissors my trace scissors
  • 00:15:50 commuter choice paper you win let's see
  • 00:15:52 if we can get out you lose case damn I'm
  • 00:15:58 so good there we go
  • 00:16:03 finally got paper after many many tries
  • 00:16:06 sorry you lose cool and then the invalid
  • 00:16:09 and valid choice try again awesome we
  • 00:16:12 got it all working I guess the one last
  • 00:16:15 thing that I did in the last video is
  • 00:16:16 let's just print a new line just to make
  • 00:16:19 it a little bit neater between runs I
  • 00:16:25 cool now we have a little separation
  • 00:16:27 alright awesome so we replicated the
  • 00:16:29 game we made in that first video using
  • 00:16:32 absolutely no if-statements so that is
  • 00:16:34 pretty cool I think we'll end this video
  • 00:16:36 real quick
  • 00:16:37 doing a little bit of comparison of the
  • 00:16:39 two versions so on the Left I have the
  • 00:16:42 original with all the if statements on
  • 00:16:45 the right I have the new one with no if
  • 00:16:49 statements I'll make it a little bit
  • 00:16:51 smaller so you can see ok I guess it's a
  • 00:16:54 balance if I make it smaller you might
  • 00:16:56 have a trouble seeing on your screen but
  • 00:16:58 you can't see the full thing without me
  • 00:17:00 scrolling here but you get the idea so I
  • 00:17:03 mean why would you program one way over
  • 00:17:05 the other I would say this first way on
  • 00:17:07 the left that we did it's easier to read
  • 00:17:10 probably like or at least someone that
  • 00:17:13 is maybe a little bit newer to
  • 00:17:15 programming or someone looking at it
  • 00:17:16 quickly would more easily be able to
  • 00:17:19 look at all of this code and see oh ok
  • 00:17:21 I understand exactly how this is working
  • 00:17:23 for rock-paper-scissors it does get a
  • 00:17:27 little bit like messy I would say even
  • 00:17:29 though it is maybe easier to follow the
  • 00:17:32 flow it's just a lot to a lot of if at
  • 00:17:36 all if it seems like that code should be
  • 00:17:38 able to be shrunk so that's like a
  • 00:17:40 negative of this over here on the right
  • 00:17:43 side you know all the top stuff is the
  • 00:17:45 same as the left I would say negative of
  • 00:17:49 the right side is it might not be as
  • 00:17:52 obvious that you're playing
  • 00:17:53 rock-paper-scissors here you kind of
  • 00:17:55 have to like follow the flow a little
  • 00:17:56 bit more exact like what the heck is
  • 00:17:57 this result matrix and then you have to
  • 00:18:00 you know follow it down and see oh okay
  • 00:18:02 this result matrix somehow tells me one
  • 00:18:04 of these messages so it might be a
  • 00:18:06 little bit harder to follow initially
  • 00:18:08 one thing I like about the right side
  • 00:18:12 this know if method of doing it is you
  • 00:18:15 know first off it's shorter you can do
  • 00:18:18 it in less lines then you can do here 36
  • 00:18:21 versus 28 but really honestly I could
  • 00:18:26 also like remove some of this spacing
  • 00:18:29 and like you know this side on the right
  • 00:18:32 is definitely shorter and I could also
  • 00:18:34 like make all of this one line I guess
  • 00:18:37 you know length doesn't really matter
  • 00:18:39 but it is sometimes nice to just have
  • 00:18:40 things a little bit more succinct so
  • 00:18:42 that is nice about the right side
  • 00:18:43 another thing that is actually very good
  • 00:18:46 about this second implementation with
  • 00:18:48 the no ifs is imagine over here on the
  • 00:18:52 left that we had this
  • 00:18:53 rock-paper-scissors game that had like
  • 00:18:55 seven different choices imagine
  • 00:18:58 happening to like hard code all of those
  • 00:19:01 seven choices and all of the different
  • 00:19:04 possibilities between those seven
  • 00:19:06 choices with all these FNL if that would
  • 00:19:10 get really annoying and I also I feel
  • 00:19:12 like it would be kind of annoying to
  • 00:19:14 test your code meanwhile on the right
  • 00:19:17 side if we had to expand to you know
  • 00:19:20 seven different options all we really
  • 00:19:21 have to do is just make this matrix a
  • 00:19:23 little bit better bigger and add a few
  • 00:19:26 options to our choice decked it I think
  • 00:19:29 it's easier to adapt to this right side
  • 00:19:32 version all right with that I
  • 00:19:35 think we're gonna end this video hope
  • 00:19:37 you guys enjoyed watching this if you
  • 00:19:39 want the code for either of these files
  • 00:19:42 they both will be on my github page and
  • 00:19:44 the link to that will be in the
  • 00:19:45 description I hope this was an enjoyable
  • 00:19:48 like a little exercise hope it kind of
  • 00:19:49 helped you get a little bit more
  • 00:19:50 comfortable helps you think about
  • 00:19:52 different ways to solve the same problem
  • 00:19:55 if you enjoyed this video make sure to
  • 00:19:57 do not forget to leave it a big thumbs
  • 00:19:59 up and also subscribe so you don't miss
  • 00:20:01 any of the future videos also if you
  • 00:20:03 have any ideas for future videos please
  • 00:20:05 let me know because I am gonna try to
  • 00:20:07 start making some more videos more
  • 00:20:10 frequently now and I always say that but
  • 00:20:11 I think maybe this time I can do it that
  • 00:20:14 is I think all I have thanks again guys
  • 00:20:18 and peace out
  • 00:20:21 [Music]