JavaScript Tic Tac Toe Project Tutorial - Unbeatable AI w/ Minimax Algorithm

  Рет қаралды 268,316

freeCodeCamp.org

freeCodeCamp.org

Күн бұрын

Пікірлер: 309
@freecodecamp
@freecodecamp 6 жыл бұрын
Check out this playlist of more game development tutorials: kzbin.info/aero/PLWKjhJtqVAbmqFs83T4W-FZQ9kK983tZC
@education8020
@education8020 5 жыл бұрын
What tools and software you are using to make this tutorial video . can you tell me? And how can make this types of videos?
@OliverMensahDev
@OliverMensahDev 7 жыл бұрын
Doing more of projects like this will help us a lot
@tewarishivam
@tewarishivam 6 жыл бұрын
Who knows plz Relpply. I'm new in coding world and this video is great but my simple doubt is how to save apk file in laptop and also how to add Banner ad in this games...
@PaoloDiBello98
@PaoloDiBello98 6 жыл бұрын
Well, it wasn't designed to be an Android application. It's called Web Development for a reason.
@harjitsingh7308
@harjitsingh7308 6 жыл бұрын
Channel Channel look into google flutter for android apps that's been great for me
@Lino9026
@Lino9026 7 жыл бұрын
Man not much can match the feeling of getting to the end result and it works!!! I love that feeling of accomplishment! Thanks man! Definitely make more of these!
@brambakker1788
@brambakker1788 5 жыл бұрын
The tie game at 39:00 is not a tie game. Field 0 - 4 - 8 were filled by an "o". Simple fix is to check if there is a win before checking if it is a tie, and than make sure the tie isn't checked (or you might get both)
@premshah7685
@premshah7685 2 жыл бұрын
Yupp correct ...!
@dj_tika
@dj_tika 3 жыл бұрын
This video has been helpful for not only getting a better understanding of how functions calling other functions works, but also for getting a better understanding of how algorithms work, thank you
@EvaRadio
@EvaRadio 7 жыл бұрын
This is so helpful. Its simple but creates an end project which feels like you've accomplished something
@lolettoa2971
@lolettoa2971 5 жыл бұрын
Nice tutorial thanks. However just a side note here: at 38:59 it was not a tie game, you actually won the game but the program did not stop when you won, rather it continued to evaluate if(!checkTie()) which caused the declareWinner("Tie Game") to execute. I fixed this by having a global variable "var finish= null". I set this var to true once I win the game, and I add it to be checked at if(!finish && !checkTie()). This var will also fix the scenario of the computer still playing his turn even though you won the game. Of course this var needs to be reset to false once you click on reset otherwise the computer wont play his turns next game.
@noah77
@noah77 4 жыл бұрын
I always dreamed of that these robots that could control themselves with AI was just mind blowing. Now I couldn't believe that I am only creating this AI. Thank you very much! *MIND BLOWN*
@fantasmagoria00
@fantasmagoria00 6 жыл бұрын
It would be better tutorial if you'd showed your thought process and order in which you would actually create those functions. You just follow line by line from the finished code trying to explain why. Too much of: 'You will see later, we will need later on, You'll understand when I get to it...' etc. which is confusing. In real life you don't know exactly what you will need later.
@abhinabacharya7398
@abhinabacharya7398 6 жыл бұрын
Agreed. First focus on algorithm to solve the problem then create whatever you need for that. That's why it is difficult to understand codes without documentation, you'll know the "what" but takes a long time to figure out "why".
@jovannovakovic5975
@jovannovakovic5975 6 жыл бұрын
​@@abhinabacharya7398 Like @Code Explained. He goes first to algorithm how to solve the problem and then actually starts coding. In this order you actually start developing your programming mind. Here is his youtube channel: kzbin.info/door/8n8ftV94ZU_DJLOLtrpORA
@summertime_sadness456
@summertime_sadness456 5 жыл бұрын
True
@simplydesign5972
@simplydesign5972 4 жыл бұрын
this is what called Curse of knowledge
@anishmadan13
@anishmadan13 4 жыл бұрын
Exactlyyyyyyyy😭😭😭😭
@sugarcaneross
@sugarcaneross 7 жыл бұрын
Love it! This might now be my favorite video on this channel. The more like this the better.
@Ninja747A
@Ninja747A 5 жыл бұрын
I believe there is a flaw in your algorithm for checking tie games, at 39:01 it says Tie Game even though the human player won.
@AryanSingh2512.
@AryanSingh2512. 5 жыл бұрын
Read the description.
@macnlinux69
@macnlinux69 5 жыл бұрын
Yes, I added a little check to fix that. I added a global "winner" variable that starts as "false" and is set to "true" when a winner is discovered in "checkWin" reset it to "false" again at startGame.Then when I call "the basic AI" I check to also make sure there isn't a winner. This solves that problem. But yes there is a flaw. Good tutorial still thought thanks!
@jakemoseley7750
@jakemoseley7750 5 жыл бұрын
@@macnlinux69 would you be able to show me your code for this step? I understand your logic/reasoning but as im pretty new to coding my knowledge of javascrpt syntax isnt great.
@antonsmid4362
@antonsmid4362 7 жыл бұрын
It's a great tutorial, I really enjoy it and wish you do more of this sort - but at 39:02 you actually win. It' s not a Tie game... And after implementing minimax you win again at 44:45 - and the display Tie game is wrong... You can always win, if you play 1, 3, 8, 6, 9 sequence (up left 1, down right 9).
@stefanocarniel4015
@stefanocarniel4015 6 жыл бұрын
I tried this sequence in the tic tac toe I programmed and can't win. Whenever you win, minimax is not implemented correctly
@misterhtmlcss
@misterhtmlcss 6 жыл бұрын
I was just laughing at the 39:02 mark too! It's because he said if all squares are chosen with either an X or O then it's a Tie, but as we all now know it's not correct. I thought that was an amusing moment personally. Just goes to show you that having another pair or eyes is so important in our work. Good video otherwise though.
@georgijalekseev5271
@georgijalekseev5271 6 жыл бұрын
You are right with regard to the code shown within the video. However, he removed the error within the code uploaded on Git. The solution for this problem is to change the following line of code: if (checkWin(newBoard, player)) { to if (checkWin(newBoard, huPlayer)) {
@antonsmid4362
@antonsmid4362 6 жыл бұрын
@@stefanocarniel4015 My comment was for the code in this tutorial at the time it was published. If you played the sequence I mention, you won. The code was latter corrected (on GIT) and yes, you don't have a chance to win.
@michaelpimenteljr3530
@michaelpimenteljr3530 7 жыл бұрын
You my friend are a genius! Love this. keep it up!
@tewarishivam
@tewarishivam 6 жыл бұрын
Who knows plz Relpply. I'm new in coding world and this video is great but my simple doubt is how to save apk file in laptop and also how to add Banner ad in this games...help
@meteachesprogramming9395
@meteachesprogramming9395 5 жыл бұрын
@@tewarishivam .apk files are for android devices bro for a laptop there are .exe files
@LuisMorales-yx8di
@LuisMorales-yx8di 4 жыл бұрын
@@tewarishivam2 years has passed, how are you doing, i bet in 2 years you must already get a job in the industry
@Ninja747A
@Ninja747A 5 жыл бұрын
Says: "It's not possible to win" just after beating the AI at 44:53 The minimax algorithm needs to be fixed!
@Nsane-One
@Nsane-One 6 жыл бұрын
Hey dude, Many thanks for the video. Has been great to go through it. Learned a lot. I noticed some errors, which I think allows the algorithm to be beaten. Another user in the comments also mentioned it. at 41.20 Line 94 should be huPlayer, and not Player. With it being Player, I could always win if I played 1,8,6,3,9. But once that was corrected, I couldn't win. Also I'm still learning a lot, but early on you state the game is a tie, however you clearly won. I think that is because the checkTie function only checks for empty spaces, and not if the game has already been won (because of this when testing early on I noticed, if you win, and there are still spaces on the board, the aiPlayer takes a turn. I guess this doesn't matter as with the minimax algorithm, its impossible to win, so the best you can hope for is a tie). Hope this helps other people who go through this video. Really awesome, and thank you so much for sharing.
@LasTCursE69
@LasTCursE69 5 жыл бұрын
0:10 How is that game a Tie since there is three O's in the right column? lol
@anuragbudakoti755
@anuragbudakoti755 4 жыл бұрын
True
@mrvic3952
@mrvic3952 4 жыл бұрын
@@anuragbudakoti755 Something is false;
@salih-khan
@salih-khan 5 жыл бұрын
its more beautiful when you understand most of the code if its the html and CSS or JavaScript its amazing
@youssefyoussef3266
@youssefyoussef3266 5 жыл бұрын
in the last attempt there's a case when u hit the 3 cross but the program consider it like it's a tie
@afnan1354
@afnan1354 4 жыл бұрын
5:00 ah i see, you are a man of culture as well, Beau
@josselinmilon7616
@josselinmilon7616 3 жыл бұрын
Super great tuto Beau!! Keep them coming!!
@kristopherwerlinder6446
@kristopherwerlinder6446 5 жыл бұрын
0:10 Is not a Tie Game, it is Win game for circle. Someone know what is missing for this algorithm to work?
@user_avadakedavra
@user_avadakedavra 5 жыл бұрын
it was not the game of complete code
@kristopherwerlinder6446
@kristopherwerlinder6446 5 жыл бұрын
@@user_avadakedavra 44:40 aswell. It is the final version that is not working. It says tie game there aswell.
@SrynYT
@SrynYT 5 жыл бұрын
I had to introduce a gameAlreadyOver boolean/variable @ line 2, set it to false at the start of startGame, check for it in turnClick function ..if(!gameAlreadyOver && !checkTie()) {.. and set it to true at the end of gameOver function
@jakemoseley7750
@jakemoseley7750 5 жыл бұрын
@@SrynYT Im very new to coding so excuse the stupid question but how to you initially write the gameAlreadyOver boolean/variable? I put "var GameAlreadyOver" on line 2, "Let GameAlreafyOver= false" at the start of the startGame function then at the end of the GameOver function put "Let GameAlreadyOver = True" . However this didnt have the desired result
@SrynYT
@SrynYT 5 жыл бұрын
jake moseley I don’t think JS uses LET. Just type ‘gameAlreadyOver = false;’.
@matarloum2894
@matarloum2894 6 жыл бұрын
at 41 min, line 94 in the video, the line should be - if(checkWin(newBoard, huPlayer)){
@Franee741852
@Franee741852 4 жыл бұрын
ahi ta paaaa, era esto fierita
@akshitsingh9080
@akshitsingh9080 4 жыл бұрын
There are many bugs. the code is great but lengthy, doesn't take the fastest route possible and an example of the bug is go to 39:00 it says tie game whereas the human won
@akshitsingh9080
@akshitsingh9080 4 жыл бұрын
@Symbol Thanks a lot. Your video is really great. I just commented that to report it to you. NO OFFENSE
@drinkeatandthink
@drinkeatandthink 6 жыл бұрын
there is a problem in the logic..you can find it in a 39:00 minute..when it pops up saying tie game instead of saying "You Win" otherwise it is very informative.. thank you..
@sarvarkhalimov111
@sarvarkhalimov111 4 жыл бұрын
Complex, but very iteresting and beneficial project. I have been watching and trying to code like 5 or 7 times)) Now, I am starting to understand... After coding them myself maybe the same times) I hope I can write similar codes on my own. Thanks a lot for this tutorial.
@josephhauger5701
@josephhauger5701 5 жыл бұрын
This is awesome and your lesson and explanations are very helpful!
@alonattar3836
@alonattar3836 6 жыл бұрын
* If only one square is marked on the board and the middle slot is free - mark the middle square. i am coding my tic tac toe by this logic , and until now it wotks great * If a row, column, or diagonal is marked with two slots of the computer - mark my disruptor. * If the row, column or diagonal is marked with two squares of the opponent - mark the third slot. * If three squares are marked and my central squares - to mark a block in a row or line in which one of the opponent's squares is marked, preferably a corner slot. * If there is a vacant corner and on either side of it two empty lines - mark the corner * If the middle slot is free, mark it * If there are three available slots in a row, in a row or diagonally - mark the corner * If there is an available slot - mark it
@ahmadabdolsaheb
@ahmadabdolsaheb 7 жыл бұрын
Great explanation. I wish this video was around when I did my tic tac toe. It took me more than 20 tries just to wrap my head around minimax. :..)
@BeauCarnes
@BeauCarnes 7 жыл бұрын
Thanks, I had to read your article quite a few times to wrap my head around minimax. :)
@awekeningbro1207
@awekeningbro1207 5 жыл бұрын
i would have used settimeout function just to delay the AI's play so that it feels a lot like playing with an opponent.
@piyushagarwal3262
@piyushagarwal3262 5 жыл бұрын
I want to delay ai turn. please help me how should i do that.
@GamingFan666
@GamingFan666 5 жыл бұрын
at 44:45. Didn't you win from the combo [2,5,8]. So shouldn't it be you win and not a tie?
@Felixxxxxxxxx
@Felixxxxxxxxx 5 жыл бұрын
This is great stuff, I was lost at 20:00 , the 0-marker did not show. I tried to find it out first by using the console, then by comparing code. I must obviously have done some type of error but I did not find it. In many other KZbin videos I would be stuck at this stage, and I found it to be very good that one can download your code for each chapter. Helped out a lot. Thanks man!
@LumasTV
@LumasTV 7 жыл бұрын
I loved this tutorial, thank you so much!
@luki121212
@luki121212 5 жыл бұрын
Another bug when you win with last move the game is also draw. I know that Player vs minmax will never win. However if someone want to apply PvP have to fix that :)
@i0o201
@i0o201 6 жыл бұрын
I remember having to do this without a GUI.It was fun to find a way to create a playable tic tac toe in text form purely.
@sauravbharti2709
@sauravbharti2709 2 жыл бұрын
Some people are here saying human can win game, but there was an error in 1st if else statement of minmax function. In video, he has used player but should be human. They have corrected it, check their github.
@EpicBizHero
@EpicBizHero 5 жыл бұрын
THANK YOU! I appreciate ALL of the time you put into teaching this!!! "THANK YOU"!!!
@simonburyan1669
@simonburyan1669 7 жыл бұрын
great tutorial, however i checked the gih hub repo and there are 0 comments in the JS code, zou have comments in the tutorial, bus still might be better do always add comments in the JS code you are writing...
@alonattar3836
@alonattar3836 6 жыл бұрын
its funny that you declare also a huPlayer win text , but it's impossible to win
@sandordeli8735
@sandordeli8735 6 жыл бұрын
At 39:05 it wasnt a TIE, than you WON actually (altough all the fields are reserved, you have 3 squares in a row from the top left to the bottom right)... Anyway I really appreciate your video it has helpd a me a lot. Thank you for it.
@freecodecamp
@freecodecamp 6 жыл бұрын
Yeah... 🙈
@gremlincc
@gremlincc 6 жыл бұрын
Please zoom out and scroll down so not all of the code relevant to what you're talking about is covered up by the progress bar on KZbin, for those of us who are constantly pausing to try and keep up with your super fast autocomplete, that I am very jealous of.
@sumanmaurya1326
@sumanmaurya1326 4 жыл бұрын
superb tutorial the thing is it is not coming winner or losser
@vineetkapoor6950
@vineetkapoor6950 7 жыл бұрын
Thank you for the video! Just a small question, Why are we using '==' instead of '===' @35:21 ln:66 & 74? Isn't it better to use '==='?
@xGanariax1
@xGanariax1 7 жыл бұрын
Good question! I just started learning JS this past week but I think that '===' makes more sense since it's only deciding "is the value 0 or not?" however I don't believe it matters as both operations are equally efficient. correct me if I'm wrong!
@shaileshsundram
@shaileshsundram 7 жыл бұрын
It's definitely not like that. '==' allows type coercion whereas '===' doesn't. stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript It is advised to always use '==='.
@xGanariax1
@xGanariax1 7 жыл бұрын
Good resource, thanks!
@BeauCarnes
@BeauCarnes 7 жыл бұрын
Yeah, your right, I should have used '==='. I would have been following my own advice from this video: kzbin.info/www/bejne/oYeynpZtg7Bkg5I
@misterhtmlcss
@misterhtmlcss 6 жыл бұрын
Yeah I was amused by that, since your whole code base allows for coercion in this video, but let's be honest at this point in FCC no one is going to care that you did that or didn't. I think it would actually matter if it was an earlier video like some of your others, but this one it's more like 'whatever'. Nice effort to contribute btw; I'm sure we all appreciate it.
@ldwabananattqw
@ldwabananattqw 5 жыл бұрын
thanks a lot I made my first js game . .. I can't understand how is the Algorithm works but it's a great start . . . + the links in the description are so helpful 3>
@rodrigoniederauer
@rodrigoniederauer 5 жыл бұрын
Every time the game ends with all the cells values === numbers, the result will be "Tie Game". After verify the cells values, you have to check if any player had won the game.
@davidgrig4608
@davidgrig4608 4 жыл бұрын
definitely like and subscribe clean and understandable code also great explanation thank you very much
@lilmint6514
@lilmint6514 5 жыл бұрын
hi man, im russian and i understood everything, very cool lesson)
@kamaboko1
@kamaboko1 6 жыл бұрын
Great tutorial. I will be chewing on this one for a while.
@AnishSah_artist
@AnishSah_artist 5 жыл бұрын
Brilliant tutorial. Btw can you tell me how you got the browser output display to the right of the editor? Thanks, Anish
@chrisjones469
@chrisjones469 7 жыл бұрын
Most of the books on the subject are obfuscating and lacking in sufficient examples and projects to master the concepts. I have a book called ‘A Smarter Way to Learn Javascript’ on the way from Amazon. I’m not giving up in general, but I lack the experience and knowledge to comb through this code to find the problem. I can make the example work by using a method that is simpler to me.
@animeloverpakbj8229
@animeloverpakbj8229 3 жыл бұрын
Thumbnail is epic!!!!! 🤣🤣🤣🤣🤣🤣
@sorrychou
@sorrychou 6 жыл бұрын
Great tutorial! But in Part6, when huPlay win at final step the result shows Tie Game!
@buddhaburrito
@buddhaburrito 5 жыл бұрын
found the same thing, major bug there
@ashishrao3069
@ashishrao3069 5 жыл бұрын
@@buddhaburrito He has updated the code. Check the Github repo for part 7.
@blinktheworld33
@blinktheworld33 6 жыл бұрын
Trying to work out a lil bug on my end. My problem is with the minimax function. I get the whole, score is undefined. I will be able to work it out, but just wondering does the .score property become defined to the minimax function at line 95 and line 97, in the code written by you guys at 41:53?
@zealace8573
@zealace8573 6 жыл бұрын
have you already fix 'score is undefined'?how?
@blinktheworld33
@blinktheworld33 6 жыл бұрын
zeal ace I did. I’m on mobile so it hard to explain right now, but my problem was I made a typo with some of the operators in the last portion of my code. The last bunch of for loops and if statements. I think i added the wrong > or < operator somewhere. I had to look at the source code and methodically replace portions of my code with the source code. Eventually I tracked my error to the last few for loops and if statements. Although I am not sure what you may be seeing was the same problem I was seeing. Just fyi
@blinktheworld33
@blinktheworld33 6 жыл бұрын
zeal ace but I can say the source code provided really does work. So it’s really helpful in see potential mistakes in your code when u compare.
@kevinhess9760
@kevinhess9760 5 жыл бұрын
@@blinktheworld33 I had the same issue. I found I had put a "> bestScore" in both my if and else statements. Changing to a "< bestScore" in the else statement fixed mine. I too found it by comparing to the source code on GitHub.
@kneekoo
@kneekoo 6 жыл бұрын
51:10 - It looks like the game cares more about the human player not to win, than defeating the player. Instead of making the winning move on cell 4, it blocked the human player (cell 2) from winning. :P
@zero_cool
@zero_cool 7 жыл бұрын
Nice tutorial. A couple of things: that's an awful way of centering simple elements like a table. You could have used "auto" for the left and right margins. Also, that class of "cell" was unnecessary. Even in the CSS you targeted the cells by their tag name. Why not do the same in the JS? Still, I think you did a great job. Keep up the good work.
@ninjaasmoke
@ninjaasmoke 3 жыл бұрын
Great video! But the checkTie function needs improvement even before adding minimax algorithm. The current logic first checks for empty squares and then for a win. But it is entirely possible to win when all the last square has been filled.
@likeashitremixit
@likeashitremixit 6 жыл бұрын
dang you type fast bro, great video
@Siderite
@Siderite 7 жыл бұрын
44:52, you actually won, and the game said Tie Game anyway.
@alejanbdroechezuria
@alejanbdroechezuria 7 жыл бұрын
yes,
@alejanbdroechezuria
@alejanbdroechezuria 7 жыл бұрын
they solve it in the code on GitHub.
@EwokPanda
@EwokPanda 7 жыл бұрын
I spotted this while following along. Simply add an enclosing if statement to turnClick() before the if !checkTie with an if !checkWin(origBoard, player)
@markh9646
@markh9646 6 жыл бұрын
Thank you!!
@ufotofu9
@ufotofu9 6 жыл бұрын
I know right. And it happens in my games too! Thanks!
@bottletyre
@bottletyre Жыл бұрын
12:19 - I didn't understand a diagonal win combination [0, 3, 6] ?
@DarkAnarquI
@DarkAnarquI 5 жыл бұрын
Great video! I have a question, this example is 3x3, how would it be with 4x4? any tip?
@bigpusha11
@bigpusha11 7 жыл бұрын
I know you can pause the tutorial at any point but it's funny to see how fast you type when you know what you're doing and typing.. I promise it looked like you were FF lol.. In comparison to me who can type but I have no clue yet lol
@cardcode8345
@cardcode8345 5 жыл бұрын
19. document.querySelector(".endgame").style.display = "none"; this part is pain in the neck every time I enter it inside my code it displays this message "Uncaught TypeError: Cannot set property 'display' of null Line: 19"
@niclee1863
@niclee1863 5 жыл бұрын
Same.
@asmat.soomro
@asmat.soomro 5 жыл бұрын
Use querySelectorAll();
@dlxa9207
@dlxa9207 4 жыл бұрын
30:25 an error appears in which elem is not defined.
@tartancub8836
@tartancub8836 7 жыл бұрын
I was trying to understand the code as I was testing around before the min/max function was added. When the bestMoves is just calling the first index. If I go on cell 3, 4, 6 and I'm suppose to be the winner it shows computer as a winner. After adding the minimax function I was able to beat the AI by going to these cells in this order [5, 6, 0, 1, 7] even tho there is a but that shows it as a tie. Nonetheless it was a helpful video maybe you can make one to address and fix some issues to better logic in checking wins and the order.
@freecodecamp
@freecodecamp 7 жыл бұрын
Your right, there is a bug in the code. It has been fixed in the code on GitHub.
@LordJesus1447
@LordJesus1447 6 жыл бұрын
man you're a genius
@ThiagoLimaRocks
@ThiagoLimaRocks 2 жыл бұрын
I think there is something wrong in that code, because I beat the AI. I'll try to figure out where is the error, but I think it's the checkTie function, cause in my tests I beat the AI, but the game said it was a tie. Anyway.. really good video. And it was awsome to do some like that. Thanks.
@prajaktabhalerao1239
@prajaktabhalerao1239 2 жыл бұрын
Same happened with me. Did you find the error??
@santiagotonda8872
@santiagotonda8872 Жыл бұрын
@@prajaktabhalerao1239 at 41:20, Line 94 should be huPlayer, not player. That way you can't beat the IA
@EwokPanda
@EwokPanda 7 жыл бұрын
0 -> 2 -> 7 -> 5 -> 8 = I win Btw, with all of the newbies trying to learn as much as they can, I feel like you should be really trying to focus on best practices, like consistency in your for loops of using let.
@wowotuninggonzalezmesa1513
@wowotuninggonzalezmesa1513 6 жыл бұрын
good tutorial, really well explained but i would like to put some pictures instead of X or O
@TheMetalhead1311
@TheMetalhead1311 6 жыл бұрын
You can do this pretty easily.. refer to this stackoverflow link.. hope it helps :) stackoverflow.com/questions/35397728/storing-images-in-javascript-variables
@silkshocker
@silkshocker 7 жыл бұрын
anyone else getting an error 'cannot read property 'style' of null at the 'startgame' function?
@silkshocker
@silkshocker 7 жыл бұрын
Never mind. Fixed!
@MrPacalicious
@MrPacalicious 7 жыл бұрын
sorry, how did you fix it
@powerfulduasandwazifaz
@powerfulduasandwazifaz 6 жыл бұрын
YES! How did u fix it? :(
@haythemsaidi91
@haythemsaidi91 6 жыл бұрын
Put in the end of the body and the problem solved.
@haythemsaidi91
@haythemsaidi91 6 жыл бұрын
powerful duas and wazifas Put in the end of the body and the problem solved.
@Ninja747A
@Ninja747A 5 жыл бұрын
If the ai is suppose to be unbeatable then how come at 44:45 it says Tie game when O actually won?
@Stevesteacher
@Stevesteacher 4 жыл бұрын
I always see people using var instead of let... I know the difference, but I almost always use let... Is it just personal preference?
@thrisharamkumar9566
@thrisharamkumar9566 6 жыл бұрын
At 38:44 after you won, the computer makes a new move ! The game must be over by then and it should not be possible for the computer to make another move..
@funkyghost8751
@funkyghost8751 3 жыл бұрын
There is an error in your githubrepo of part 5 at line 44 ) is missing which you initially declared at if statement
@myloginistaken
@myloginistaken 7 жыл бұрын
In 0:10 why does it say "tie game" if O clearly won? Also in 0:16, why doesn't the AI put X in the bottom in order to win?
@BeauCarnes
@BeauCarnes 7 жыл бұрын
Hmmm... good questions. Looks like this game needs some refactoring. 😀
@myloginistaken
@myloginistaken 7 жыл бұрын
Ok, I finished watching the video, it was very educational, thank you! I managed to fix the faux-tie situation by adding checkWin function to turnClick, so now it looks like that: function turnClick(square) { if (typeof origBoard[square.target.id] == 'number') { turn(square.target.id, huPlayer); let gameWon = checkWin(origBoard, huPlayer); if (gameWon) { gameOver(gameWon); } else if (!checkTie()) { turn(bestSpot(), aiPlayer); } } } and it properly reports when the human player wins in the last round. As it was, it wasn't running the win-check after the last move, just assuming it's a tie because no empty fields were left. No idea if it's optimal, because I'm only beginning to learn coding, so feel free to correct me if need be. It's easy to test because the AI (contrary to the video title ;-) is beatable every single time, just go with this (human clicks): 0, 8, 7, 2, 5 - and win every time. So that's another thing that needs fixing, but I haven't tried to do it yet. ;-)
@sandoxs
@sandoxs 5 жыл бұрын
change line 94, instead: if (checkWin(newBoard, player)) make it: if (checkWin(newBoard, huPlayer)) and evrything is going to be ok, and you will never win the game again ;)
@juanandresrodriguezpedreir8922
@juanandresrodriguezpedreir8922 5 жыл бұрын
Hi. Im not geeting the id of the square by clicking it. Did someone else got this problem?
@yervandbaghdasaryan8156
@yervandbaghdasaryan8156 6 жыл бұрын
Thank You very much!
@lisaford7376
@lisaford7376 5 жыл бұрын
when i win sometimes it still tells me its a tie...i have checked the arrays in the const winCombos and all the posibilties are there...is anyone else running up on this problem
@ufotofu9
@ufotofu9 6 жыл бұрын
44:52 How is that a win for O? In fact, the code gets ties wrong pretty often.
@ratanthatikonda2599
@ratanthatikonda2599 3 жыл бұрын
also i get to a point is the 6th section where is i win where the entire board is filled it still says tie
@BKofficer23
@BKofficer23 6 жыл бұрын
I am at 20:33, just got done with the turn function. It seems in the turn function that origBoard can also be huPlayer or aiPlayer and still work. There must be a reason to choose origBoard over the other two. Anyone know why? Another way: function turn(squareId, player) { origBoard[squareId] = player; document.getElementById(squareId).innerText = player; } can be: function turn(squareId, player) { huPlayer[squareId] = player; document.getElementById(squareId).innerText = player; } and still work. Thanks you geniuses.
@yashmundada2483
@yashmundada2483 2 жыл бұрын
why would you not write the arrow functions in full syntax. it makes it far easier to understand. without the braces etc, life becomes far harder. apart from that, excellent video
@yashmundada2483
@yashmundada2483 2 жыл бұрын
same with usage of foreach, map instead of loops and .every
@Urketadic
@Urketadic 6 жыл бұрын
Isn't it 308px in width not 310? There is 4 borders and each one is 2px, so three boxes plus borders is not 310.
@user_avadakedavra
@user_avadakedavra 5 жыл бұрын
when AI plays X, why AI's first move isn't 5th cell?
@mkayisforme123
@mkayisforme123 5 жыл бұрын
Playing squares 5, 0, 6, 8, 7 in that order beats the algorithm yet displays tie game.
@UnworthyRellik
@UnworthyRellik 5 жыл бұрын
Also happens with 5,1,4,0,8
@sandoxs
@sandoxs 5 жыл бұрын
change line 94, instead: if (checkWin(newBoard, player)) make it: if (checkWin(newBoard, huPlayer)) and evrything is going to be ok, and you will never win the game again ;)
@joshuaoconnell2384
@joshuaoconnell2384 6 жыл бұрын
Playing 5, 4, 3 will result in a loss. This should be a win. How do I fix this? I tried it with my code and the GitHub provided code.
@ninadvirkar9097
@ninadvirkar9097 5 жыл бұрын
When you get the moves of player in the array simply sort in ascending order
@ЏонМастерман
@ЏонМастерман 5 жыл бұрын
Im new to coding, but very curious as to why not use margin: 0 auto to center it instead of that odd looking styling ?
@richardramos7267
@richardramos7267 6 жыл бұрын
on 20:00 wow , on the first time I've watch this I'm having a hard time figuring out where that getElementByID(squareID) came from, I'm so slow I guess, because I thought you can only use getElementById by only getting the id when you declare it on the DOM, I was so frustrated that I stop watching this, now for the second time, I figure it now, so that squareID is a reference of the Cell ID itself and you also don't need to put a a quote to the getElementById("squareID") because the event squareId came from a square.targe.id so it is an id by itself, AM I right ? May I also ask if there is any way I can do that by using the querySelector ? like this document.querySelector(squareId).innerText = player; so only getElementById is allowed for this?
@thodorisspirantis1557
@thodorisspirantis1557 5 жыл бұрын
can you explain to me where the square in "function turnClick(square)" comes from?
@JenniferStewart100
@JenniferStewart100 6 жыл бұрын
What code editor are you using?
@tokenhempshire
@tokenhempshire 2 жыл бұрын
When I was doing this, I did 0,2,7,5,8 and won. Was there something I coded wrong or did I find an actual win con?
@joellongie
@joellongie 7 жыл бұрын
Great video!
@tewarishivam
@tewarishivam 6 жыл бұрын
Who knows plz Relpply. I'm new in coding world and this video is great but my simple doubt is how to save apk file in laptop and also how to add Banner ad in this games...
@vartikasinghania9723
@vartikasinghania9723 3 жыл бұрын
i have little knowledge in JS should I first work on JS or am i good to go with little knowledge for this tictac
@TapanAnand
@TapanAnand 7 жыл бұрын
You could have simply centered the board by using margin-left: 50px auto; on the table
@onionskins
@onionskins 4 жыл бұрын
This code before the ai functions in place has a problem. If human player wins at the last move the system return "Tie Game" checkTie finction needs to be readdressed here
@sarahamin5869
@sarahamin5869 6 жыл бұрын
thanks so much for your effort, i'm learning javascript and i have a question, what S in line 66 stands for i'm confused and also a and e in line 43?
@freecodecamp
@freecodecamp 6 жыл бұрын
The "s" in line 66 is arbitrary. It is just a variable name and could be switched to any other string. I think it is supposed to stand for 'square'. In line 43, the parameters that can be passed into a reduce function are part of JaveScript. You can read more about reduce here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce . A stands for accumulator and e stands for element but like before, the variable names can really be anything. Hope this helps!
@HamzaSayyed404
@HamzaSayyed404 2 жыл бұрын
22:37 rather than this fancy way , can someone please provide the easily understandable code it can be long but easy
@binniealiabdullahi8345
@binniealiabdullahi8345 6 жыл бұрын
so the only thing I'm confused about is lets say huPlayer has three in a row but on the next move the aiPlayer has a space that would allow them to win the game it still says ai wins although the huPlayer took the first move how would you fix that?
@saksahmkumarsaksham
@saksahmkumarsaksham 6 жыл бұрын
i am getting an error that score is not defined
@zealace8573
@zealace8573 6 жыл бұрын
have you fix it??
@anshikarao5352
@anshikarao5352 4 жыл бұрын
When did you define 'Square' parameter or how did the computer know that 'Square' is referring to the 'Ids' if the cells ?
@ismailelayade9402
@ismailelayade9402 4 жыл бұрын
on turnClick he pass Square as Event cuz every Eventlistener has event you can call it square or e or event it doesn't matter
@just9015
@just9015 5 жыл бұрын
If both human and ai looses the game still it shows the result as tie ....
@awekeningbro1207
@awekeningbro1207 5 жыл бұрын
Uh.... Yup that's what tie means.
@fuhelen180
@fuhelen180 6 жыл бұрын
Revised method in Github to prevent player winning: in the minmax function change the line ***moves.push(move);*** to: if ((player === aiPlayer && move.score === 10) || (player === huPlayer && move.score === -10)) return move; else moves.push(move);
@yungaudacity8613
@yungaudacity8613 5 жыл бұрын
Need someone to explain what the reduce is doing in 22:45 please.. thank you
Minimax: How Computers Play Games
14:37
Spanning Tree
Рет қаралды 217 М.
Coding Adventure: Boids
8:35
Sebastian Lague
Рет қаралды 1,6 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 797 М.
Minimax Algorithm for Tic Tac Toe (Coding Challenge 154)
26:33
The Coding Train
Рет қаралды 827 М.
How is this Website so fast!?
13:39
Wes Bos
Рет қаралды 1,3 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
These CSS PRO Tips & Tricks Will Blow Your Mind!
8:48
Coding2GO
Рет қаралды 545 М.
A game of TicTacToe written in JavaScript ⭕
18:57
Bro Code
Рет қаралды 132 М.
I made an unbeatable Tic Tac Toe AI (Minimax algorithm)
9:19
nextProgram
Рет қаралды 134 М.
Simple Explanation of the Minimax Algorithm with Tic-Tac-Toe
4:18
Science Buddies
Рет қаралды 48 М.
Tic Tac Toe Java Game in Under 15 Minutes
17:16
Kunal Kushwaha
Рет қаралды 126 М.
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН