Tic Tac Toe winning logic for programmers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone they share back again with other video and in this video I want to talk about tic-tac-toe and not ordinary tic-tac-toe I want to talk about in such a manner that each and every programmer is able to design the winning logic of the tic-tac-toe now I do totally understand that tic-tac-toe is a stupidly simple game but it is also the fact that not each and every one of us is able to design us and definitely that's the case because we know some of the programming languages may be seeing C++ Python Java Ruby angular react Android iOS but still when we are designing this particular game there are some hurdles which always comes to us now designing the game is the easy part because you just can align a few buttons and clicking on that button you can change the image to simply a zero or a cross but what's interesting is how we design the winning logic of this game now the winning logic behind this game is absolutely simple I would call it as ridiculously simple but the problem is that even if the winning logic is just composed of few and operator and a couple of if and else that's pretty much it still a lot of people still face that how am I gonna write the code for this winning logic I'm not able to even think so help me how I can think and process this information first and foremost it's totally ok that if you are not able to critically think and are able to design logic because it happens to each and every programmer some people like to accept it most simply like to deny it but it's not the case every single one of us face some trouble while designing this game for the very first time so first what you need to do is stop thinking about winning the game our brain is trained that this game is ridiculously simple and our brain just keep on switching into the winning logics so instead of understanding the game itself we try to move on to the phase where we are just trying to win the game so stop there for a moment and let me explain you how you can design a winning logic in any programming language whether that's Java JavaScript angular is Android whatever it is after watching this video you will fully understand that how we can write the code for the winning logic in tic-tac-toe now in order to explain it further I have to move to my iPad so let's try to understand this tic-tac-toe game here so I'm using my iPad and first and foremost we're gonna be deleting a lot of stuff so just focus on understanding this stuff so how does a tic-tac-toe game look like it has a box we have already seen that kind of stuff and in the box we have - this kind of stuff and then we have got - this kind of a stuff okay that's the very easy stuff and we have played this game too many times that's why you're focusing too much on winning so let's try to understand that we can call that each box that we are having this box is actually a button let's try to understand this so for easier understanding purpose I'm gonna call this as button now this button can have only three states so usually people like to call this straight as these states are 0 1 & 2 by the state term I simply means each of them is available and each variable can have only 3 values now some people like to call them as simply a 0 1 & 2 and internally they decide that 0 is gonna be like empty nobody has clicked on it 1 means cross and 2 means zeros now I find it really really confusing so I don't use it at all in fact I'm gonna just delete that part of stuff what I like to do in this particular case I like to give it the actual value so I regularly like to use strings so first one is gonna be empty so I'm gonna call these button cells literally empty and then we're gonna use crosses and 0 so just call it as a cross or in case you are using any colors that's fine too so just call it as red for cross and simply similarly we can use like let's just say green for zeros so there we go 0 terrible handwriting I do totally understand that so this is the basic stuff so first thing that we have learned here is every button is going to have 3 state empty red and 0 or empty cross you got the point okay so let's delete that part okay this is totally clear and we understand that part let's move back and switch what is how we can play this game okay that part is clear now also I would like to name each of these buttons now since I am a programmer I start to count from 0 so we're going to name them as 0 1 2 3 4 5 6 7 & 8 okay that's pretty easy stuff underst now let's try to play this game for once and then we are going to understand how the winning logic actually works so let's go up and I'm gonna choose first and foremost red so let's just say red actually played here as cross okay that's fine now for a moment stop thinking about winning and let's just say the other guy is playing a little bit dumb and he also played here that is zero okay that's fine now comes up the red guy again he played up here and he mugged an X here again now technically what has happened your brain is actually programmed to write a zero on position two but we are not gonna do that we are gonna understand this game so let's just say the other person is playing here and now what is going to happen is the red guy is going to play up here and there we go he got his first win now let's try to write a code for this particular situation and then we can easily figure it out that how we can write logic so we are going to simply use another color let's just say blue so obviously I want to check this so I'm gonna use an if statement here now this if statement is going to help us to evaluate and couple of ands as well like we just don't want to compare one condition there are multiple conditions going on in here so I'm going to call this as position 0 position 1 in position 2 so first and foremost I want to check that if the item at position 0 is empty or not and I want to check if it is not empty that's my first condition I want to check it ok that's it really simple and if that condition is like fulfilled that means somebody has played at position 0 I want to check a few more conditions let's go ahead and do that so obviously we're gonna use an and operator they're pretty simple stuff now what I want to do is I want to check what is the check I simply want to check that if at position 0 whatever the value is it can be it can be read definitely we have checked it's not empty so that's the one thing now the two situation that remains in front of us is either it is red or it is green so I want to check if the value at position 0 is equal to value at position one so that's my first check so I'm checking whether this POS one zero is equal to P US one okay I want to check one more thing here so I'm gonna just put in and another condition that I want to check and this time I want to check whether the position whatever the value is at 1 is also equal to value at position 2 and if that's the condition that's my winning logic because if or if the first value is not empty and the first value is equal to second value and second value is equal to third value that means you have got a winner here so in that case I can just print out a message simply print when message I'm gonna just call it as winning message so that's my very first and very simple case scenario so what can be the other place where user can win and writing code for that is ridiculously simple because we are not checking for post zero post 1 and post - so how many position do actually we have to check and that is really simple first and foremost we need to check up this one so that is a winning streak this is also a winning streak so you have to check at 3 4 & 5 you also have to check at 6 7 & 8 that is even a streak now you have to check for this streak as well which is 0 3 & 6 you have to check at this streak as well which is 1 4 & 7 you have to also check for this streak which is 2 5 & 8 you also have to check diagonally so we have to go this way also so you have to check at 0 4 & 8 as well and finally this one as well you have to check so that is 6 4 & 2 or 2 4 & 6 however you want to say that and that's it you have to write exact same if a statement for these values as well and there we go you have got your tic-tock tic-tock Tech game for winning logic and this logic can be applied for JavaScript for Ruby for python for OS iOS apps for Android apps and that's pretty much it so now if you are designing any tic-tac-toe game with this similar kind of logic let me know in the comment section as well I'd love to see what you are creating with this video I hope this video is explaining all the things like ridiculously simple way yep it's that simple now in case you enjoyed this kind of video let me know in the comments section as well and also make sure you hit that subscribe button because I keep up coming with these kind of amazing video at this channel hit that subscribe and let your friends know that there is an amazing programming channel at YouTube so tell your friends also that's it for this video and let's catch up in the next one
Info
Channel: Hitesh Choudhary
Views: 38,567
Rating: 4.783051 out of 5
Keywords: Programming, LearnCodeOnline, tic tac toe winning logic for programmers, tic tac toe, tic tac toy, game logic, pseudo code, design tic tac toe, tic tac toe logic
Id: qU_gdQ1FvjM
Channel Id: undefined
Length: 9min 20sec (560 seconds)
Published: Tue Feb 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.