Create a Sudoku Solver In Java In 20 Minutes - Full Tutorial

  Рет қаралды 318,928

Coding with John

Coding with John

3 жыл бұрын

Complete Java course: codingwithjohn.thinkific.com/...
Source code here: www.codingwithjohn.com/sudoku...
Let's create a Sudoku solver in Java!
Sudoku is a logic game, where you fill in numbers from 1-9 in a 9x9 grid. In Sudoku, each row, column, and 3x3 sub-grid must contain all numbers from 1-9.
Solving Sudoku in Java seems like it could be complicated - and some of it certainly is! But I'll break down the Java program for solving Sudoku into more simple pieces that we'll code together.
Learn or improve your Java by watching it being coded live!
Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java developers.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Phone I use for recording:
amzn.to/3HepYJu
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com

Пікірлер: 344
@19joni69
@19joni69 2 жыл бұрын
Making a sudoku solver was actually the very first project I programmed once I'd learned the basics. This takes me back.
@yahia1355
@yahia1355 2 жыл бұрын
Wow ! I have been coding for arround 5 years and could'nt come up with a solution!
@19joni69
@19joni69 2 жыл бұрын
@@yahia1355 oh I didn't do anything smart. I literally brute forced a solution. Basically going over every single square and trying the first number that works, then if I got to one that had no possible correct number I would go back until the first square that I could have put a different number in and tried the different number. Do that until you can fill every square correctly. Just literally trying every combination until it works. There are much better ways to solve this. But the not so smart solution works.
@user-cg4vi4kn4v
@user-cg4vi4kn4v 4 ай бұрын
@@19joni69 🤣🤣😂
@clo-1588
@clo-1588 2 жыл бұрын
You're an excellent teacher, John. I find your videos not only extremely helpful, but impressively well-made - and it's not even so much about the format (which is ideal), but the way you teach and the examples you give. You explain things in a logical, progressive way, and you're excellent at providing the right level of exposure. I do wish there were more teachers like you in universities - but then, we might not have you on KZbin ;) Please keep going with these tutorials, you are contributing to the community in a huge way.
@stormybear4986
@stormybear4986 2 жыл бұрын
This was, legitimately, the most interesting java tutorial I've seen in years! EXCELLENT!!!!!
@janekk3397
@janekk3397 Жыл бұрын
I'm so glad channels like this one exist. It was a part of my project and I would've never figured that out myself...
@saviobatista9619
@saviobatista9619 2 жыл бұрын
Thank you! Amazing video, where I work I tried several times create something to solve our work schedule with some kind of algorithm but always fail, this was just exactly what I needed and hope to solve my needs. Thank you!
@navyathamarreddy2807
@navyathamarreddy2807 Жыл бұрын
very nicely explained John, I saw few other videos as well for Sudoku Solver, but this is amazing!
@nikhilraov100
@nikhilraov100 2 жыл бұрын
This program works perfectly. What a flawless algorithm you have written .
@olayiwolaakinnagbe676
@olayiwolaakinnagbe676 Жыл бұрын
This is the best video I found online for this problem. You are a great teacher, thank you.
@rajeshg3570
@rajeshg3570 2 жыл бұрын
This is excellent .. i simply love the naming conventions of the vars..which makes its easy to read and understand the logic
@mdrwsh
@mdrwsh 2 жыл бұрын
it is amazing to see various algorithms that be used to solve sudoku, i made one but using while loop
@krishnawadhwani5393
@krishnawadhwani5393 2 жыл бұрын
I had recently discovered this channel, haha, and I started to watch your videos on TV Like I am watching some movies or some kind of entertainment, nice videos keep it up, subscribed after watching 2-3 videos
@MykhailoZubanych
@MykhailoZubanych 2 жыл бұрын
Same)
@jagi7976
@jagi7976 2 жыл бұрын
It’s beautiful when something that’s educational is also entertaining
@abdurrouf4159
@abdurrouf4159 2 жыл бұрын
You just nailed it. Mind blowing tutorial, no doubt.
@Nikolai4567
@Nikolai4567 2 жыл бұрын
I didn't think this solution would have an adequate time complexity. Thanks!
@zimbabwe8189
@zimbabwe8189 3 жыл бұрын
Great video, your explanation was pretty clear!
@penguin7863
@penguin7863 Жыл бұрын
Sir before I watch your video I was a student who was struggling to solve recursion problems for my upcoming test. Now I get the idea about how to backtrack in recursion :) Huge thanks from South Korean student!
@qurdedu4032
@qurdedu4032 Жыл бұрын
Great explanation and great mini project Thank you so much. Looking for more project like this
@carlostitlan
@carlostitlan Жыл бұрын
Your explanaition is soo cool! You even make feel this stuff is easy.
@staceyonuora5329
@staceyonuora5329 3 жыл бұрын
Great video, I really enjoyed your explanation. Super thanks, this was very helpful
@michaelkraemerman2009
@michaelkraemerman2009 Жыл бұрын
This was such a good explanation!!! I feel a lot more confident about implementing this. Also thanks for not acting like this is easy/obvious👏
@sharadgupta8576
@sharadgupta8576 2 жыл бұрын
It was just really awesome and in so nice way u described it's just really wow... M so surprised why just so less likes I have watched couple of videos before but u just explained really understanding with gotcha learn some new concept as well successfully found my final year project as well thanks for making this video... I will surely check out your more videos.. Great job 👍
@guidopiotrowski7900
@guidopiotrowski7900 2 жыл бұрын
Thanks for the great explanation! Keep it up with the channel!
@tonyz2203
@tonyz2203 2 жыл бұрын
OMG, I understand how to solve this complex problem now. Thank you so much!
@sigfigronath
@sigfigronath 2 жыл бұрын
This was really nice, fun and informative!
@ulyses1018
@ulyses1018 Жыл бұрын
Great video! Thanks for this kind of tutorials John! :D
@francescopiazza4882
@francescopiazza4882 2 жыл бұрын
Great coding John! I got a "Hard Sudoku" solved in a few ms !
@abidoyevictor2780
@abidoyevictor2780 3 жыл бұрын
Well explained! Thanks John
@siddhantanand5751
@siddhantanand5751 2 жыл бұрын
Hey, John I loved the way you went forward with explaining the video , you made it quite easy to learn the algorithm and fun too, i was curious if we can attach some image recognition tools like OpenCV etc for Java , so that we can scan a sudoku at run time and give an image back? I would love if you could show us how to do this if you like the idea.
@Garrison86
@Garrison86 2 жыл бұрын
Amazing! Thanks for this, this helps me stay engaged in java
@mthsffc
@mthsffc Жыл бұрын
This is exactly what I was looking for to complement my java studies. Projects with step-by-step explained. Please, keep it going, unfortunately here on KZbin, everything seems to be only JavaScript or Python. It would be great more projects with OOP.
@jhuluan-jyun2594
@jhuluan-jyun2594 2 жыл бұрын
No wonder you’re a lead! Very clear sir, thank you
@mira1m198
@mira1m198 2 жыл бұрын
Great explanation, easy to understand, even for beginners :-) Thanks!
@vivekjj2986
@vivekjj2986 2 жыл бұрын
Thanks so much John. I spent a lot of time searching on youtube for a good tutorial on this. I wish yours was the first video so i could have saved time :(
@ChristForAll-143
@ChristForAll-143 2 жыл бұрын
Simply Nailed It, AWESOME EXPLANATION
@Bury11
@Bury11 2 жыл бұрын
cool project :) i'm fairly new to java, i programmed a small snake'ish game, so i understood the 2d array way better then i thought :D keep up the good work!
@kunalkheeva
@kunalkheeva 2 жыл бұрын
How did you make it so simple, I always appreciate your content. your content is limited on youtube but whenever I get stuck in any problem and you have a video on that, which means, my last destiny is your video. Thank you
@alzaeem79
@alzaeem79 Жыл бұрын
Beautifully well explained, thank you sir.
@razvanungureanu8897
@razvanungureanu8897 Жыл бұрын
I have reached the part where I didn't find a solution to erase the all the numbers of the board and to retry. I know it needed some recursion, but didn't figure out how. Your explanation was excellent and after reviewing your method, everything makes sense. Thanks and keep it up with these kind of videos!
@jaydoshi5394
@jaydoshi5394 2 жыл бұрын
Watching couple of videos from this channel every day in any order. But still makes sense
@sritamabanerjee1331
@sritamabanerjee1331 3 жыл бұрын
Loved your explanation..keep it up buddy 😊
@josueramirez7247
@josueramirez7247 2 жыл бұрын
In an intro to programming textbook I own, it mentions in the preface that one difference from prior printings is that it removes the sudoku solver example code from the multidimensional arrays chapter because apparently is it too complex at that point.
@PathWars
@PathWars 2 жыл бұрын
Awesome, exactly the kind of KZbin channel I wanted: Regularly uploaded coding videos.
@JonasKeil
@JonasKeil 2 жыл бұрын
This tutorial is excellent John.
@devoiddude
@devoiddude 3 жыл бұрын
Fantastic video and fantastic channel, your videos are great thanks .
@kingsuley0581
@kingsuley0581 10 ай бұрын
Loved this!
@Daniel-iy1ed
@Daniel-iy1ed 2 жыл бұрын
That was fantastic. Thanks 🙏
@northnorth9913
@northnorth9913 2 жыл бұрын
I like what you doing, Hope you get the chance to do Dijkstra’s algorithm, Time complexity and a few more interview related. I like your explanations
@joekagerer
@joekagerer 2 жыл бұрын
I wrote a Sudoku solver in 2008 in Javascript, but it didn't quite use full recursion (my mistake) so it wouldn't solve moderately difficult puzzles. Mine includes character recognition of uploaded websuduko puzzles. This week I added "paste from clipboard" so that I didn't have to save/load my puzzle images. Today, following your model I finally got mine solving even the "evil" puzzles. Thank You John for this great lesson. Although now I don't feel that "I wrote the code", my page works and I did right the optical character recognition parts. I don't believe that I can share a link here, but I can try.
@luigiq6429
@luigiq6429 3 жыл бұрын
Great video John. Congrats on 500 subs!
@CodingWithJohn
@CodingWithJohn 3 жыл бұрын
Thanks!!
@CodingWithJohn
@CodingWithJohn 2 жыл бұрын
Well at least I've certainly gone well past 500
@mastershonobi110
@mastershonobi110 2 жыл бұрын
Gr8 vid. Spent the last 2days working on this really breaking it down. I got a lot outta following and really understanding what’s going on here. Keep them coming…🙌🏿🙌🏿
@javawocky
@javawocky Жыл бұрын
Very nice. I do this stuff for fun. Recently did one of those word search generators which you see kids books all the time. Way more interesting to do than you may initially think.
@joedevenski3291
@joedevenski3291 2 жыл бұрын
Fantastic stuff!
@dunayqudretli3382
@dunayqudretli3382 2 жыл бұрын
Great tutorial !
@AdamantlyAdams
@AdamantlyAdams 2 жыл бұрын
Bro, thank you for teaching. You are the go to guy!
@footballalliance2412
@footballalliance2412 3 жыл бұрын
Hi John, i have learned a lot from your videos. I hope you won't stop making videos in upcoming days, it is really helpful for us. I can guarantee you will get a huge subscribers in coming days.
@CodingWithJohn
@CodingWithJohn 3 жыл бұрын
Thanks! Glad you're getting something out of them. I'll keep making them if people keep watching them!
@estebanlegare
@estebanlegare Жыл бұрын
Really helpful!
@amaralensheriff
@amaralensheriff Жыл бұрын
As always, thank you John.
@AkhtarReviews
@AkhtarReviews 2 жыл бұрын
Loved it! 🔥
@ponder2406
@ponder2406 2 жыл бұрын
Wow! That’s so cool! This was an awesome video! Very interesting :)
@Gandobilis
@Gandobilis Жыл бұрын
Very interesting tutorial!
@nezetech
@nezetech 3 жыл бұрын
Dude, thanks a lot!! You saved me
@user-jh9ut7in8i
@user-jh9ut7in8i 3 жыл бұрын
like your explain!! very clear!!!!!
@Darya-pu6ik
@Darya-pu6ik 2 жыл бұрын
its fantastic, i really like the way you teach, its intresting! However, could we do it using DP or is the an NP?
@mynameispooop
@mynameispooop Жыл бұрын
Awesome Tutorial
@sanchitghai1455
@sanchitghai1455 2 жыл бұрын
I like that you also play binding of issac 🙂. As for the program I loved it. It was well made video for beginners and one can't not understand the concept. Keep it up! 👍
@b9944236
@b9944236 Жыл бұрын
Now I can solve it by myself, thanks a lot.
@neeldiyora3575
@neeldiyora3575 Жыл бұрын
Excellent explanation 👏👏👏
@surajverma-ut4kj
@surajverma-ut4kj Жыл бұрын
Just one word for John 🙏🏼 Big ThankYou ❣️
@user-sr7ht8rm6r
@user-sr7ht8rm6r 10 ай бұрын
John, thank you for everything! Could you maybe make a video on backtracking specificaly? That would be awesome! :)
@rams2478
@rams2478 2 жыл бұрын
WOW.. you are amazing....clean.. detailed.. explanation. Please do Leetcode problems also.. It will help us.
@38YD
@38YD 2 жыл бұрын
love this!
@manikandanm5567
@manikandanm5567 2 жыл бұрын
This is cool, thanks man
@lukaskock7020
@lukaskock7020 2 жыл бұрын
Thank you very much!
@danielmago4327
@danielmago4327 2 жыл бұрын
amazing professor!
@obitouchiha7545
@obitouchiha7545 2 жыл бұрын
Wow sir you can explain things very well, let aside being multitasker.
@strawberrykitty8337
@strawberrykitty8337 4 ай бұрын
thank you so much!!!!!
@JannisAdmek
@JannisAdmek 2 жыл бұрын
You teaching skills are really impressive, great work! I have one minor point to critique, you hardcoded GRID_SIZE 9 but used 3 as a magic number. You could have computed it once sqrt(GRID_SIZE) or just declared it as a constant.
@Michael-se7ny
@Michael-se7ny 2 жыл бұрын
sqrt(GRID_SIZE) would make no sense at all
@JannisAdmek
@JannisAdmek 2 жыл бұрын
@@Michael-se7ny Really? Doesn't a Sudoku box always have side length of sqrt(GRID_SIZE)? But I guess the really clean way would be to define the sudoku in terms of the BOX_SIZE, since GRID_SIZE has to be square number. so a normal sudoku is BOX_SIZE = 3. (GRID_SIZE = BOX_SIZE * BOX_SIZE)
@p_varma17
@p_varma17 2 жыл бұрын
@@JannisAdmek right
@Pennervomland
@Pennervomland 2 жыл бұрын
I‘d love to see this exact algorithm but more efficient. Knuths Algorithm X for sudoku sounds very interesting and shows a pretty good method for backtracking. I‘m too dumb to understand how to implement it in java but it could be a cool challenge for you.
@stephanc7192
@stephanc7192 2 жыл бұрын
You make truly great videos
@cptsalazar4986
@cptsalazar4986 2 жыл бұрын
Awesome johnny...
@techtonight882
@techtonight882 2 жыл бұрын
I think we can use dynamic programming too, to reduce some of the complexity. We can use a HashMap to store if a number at particular position was a valid placement or not.
@kesarscorpio
@kesarscorpio 2 жыл бұрын
i guess this one is for beginners and that's why he chose this way of doing it
@cs0576
@cs0576 2 жыл бұрын
But that is dependent on the previous placements. So I do not immediately see how that improves this algorithm.
@smrtfasizmu6161
@smrtfasizmu6161 2 жыл бұрын
The argument of the hashmap needs to be both the board and the placement of the number. How does backtracking in your algorithm work? I am not sure how you intended to do it with hashmaps and make the code less complex than what you saw in this video. Can you elaborate further? One idea popped up in my mind, but it is more or less a copy of what you saw in the video but without recursion. First you have an array which holds the position which are given in the beginning. Then, you proceed filling in the board and every time you find out that you can't continue filling in the board you backtrack the i and j variables of the loop until you reach the next number which you filled in and continue trying filling it after setting number To Try to be 1+the previous numberToTry.
@mr.mirror1213
@mr.mirror1213 2 жыл бұрын
iirc the whole problem is a system of linear equation , and u can solve it in O(n) if u use multigrid method
@buddybuythis3889
@buddybuythis3889 2 жыл бұрын
Wow! Sooo nice and free! Instant sub and hooked. Binge time.
@nagasivakrishna5660
@nagasivakrishna5660 Жыл бұрын
wow,next level explanation
@GeorgeVrachliotis
@GeorgeVrachliotis 2 жыл бұрын
Brilliant!
@mohammadsadrayeganehfaal2323
@mohammadsadrayeganehfaal2323 Жыл бұрын
such a enjoyable algorithm!!!
@djneils100
@djneils100 2 жыл бұрын
this is a quality tutorial
@NiklasEngelLive
@NiklasEngelLive 2 жыл бұрын
I user "Thread.sleep(50);" with try and catch to print all numbers one by one. But now I also try to avoid this step for the part where I want to print the default grid with the zeros. Thanks for the Idea and I will play with it a little longer :)
@manusoftar
@manusoftar 2 жыл бұрын
I think that those nested for loops on the solve method are unecesary, you could add two additional params to send the current row and column and inside the function you just would need to either add the the rows or to the column depending on what you are doing... yes you will have to put some IF's, but the way you did it I think it will do unnecesary iterations, I mean, the last recursive call on a solvable board will put the needed number but there's nothing that would stop the initial call to the solve method from stopping it's iterations. Actually, on each recursive call it will have to traverse as far as possible on the board until sending back a false or a true if solved. You don't need the algorithm to traverse almost the whole board on each recursive call, you just need it to make a single step on the board instead.
@sweetysojrani9323
@sweetysojrani9323 Ай бұрын
Awesome algorithm. It would be nice, if you could also explain the time complexity of the algorithm in your video.
@samuelnilsson5505
@samuelnilsson5505 8 ай бұрын
Love this
@RomualdBrunet
@RomualdBrunet 2 жыл бұрын
Using static methods and passing the board as parameter hurts my brain for some reason (vs using the board as an instance of a class)
@ae6072
@ae6072 5 ай бұрын
same! Why doesnt he use the board as an instance? Is there any special reason for this?
@Kanha0321
@Kanha0321 2 жыл бұрын
Its amazing ❤️
@rakeshreddypallepati6845
@rakeshreddypallepati6845 2 жыл бұрын
Awesome Explanation......does the time complexity is ignored here as we are looping for a constant no of elements
@abubakaradamu6278
@abubakaradamu6278 2 жыл бұрын
I think, i just found my best youtuber. Keep up the good work John.
@patrickbradley4948
@patrickbradley4948 2 жыл бұрын
Same
@shintaromidorima1552
@shintaromidorima1552 Жыл бұрын
it is 100% working teacher john and thank you for the source code
@codegeek8256
@codegeek8256 2 жыл бұрын
Please make a video on the Hand Of Poker (Poker Hands) Game, it seems to be used alot on interview assessments this days. I am sure alot of people need it.
@jamesdepoorter726
@jamesdepoorter726 2 жыл бұрын
Hello John, why didn't you use TDD in this example? This could be a nice addition, because it constantly keeps the code in check
@pratikdhargalkar4876
@pratikdhargalkar4876 2 жыл бұрын
Subscribed !!
@wombozombo
@wombozombo Жыл бұрын
Fun way to learn about recursion 👍
@widearchshark3981
@widearchshark3981 2 жыл бұрын
Good video. I think you could have explained that some cells could contain more than one number. It might seem initially that sudoku boards have just one option per cell, which made the recursion discussion confusing... Maybe! Nice though, thank you.
Quicksort Sort Algorithm in Java - Full Tutorial With Source
24:58
Coding with John
Рет қаралды 235 М.
Это реально работает?!
00:33
БРУНО
Рет қаралды 3,3 МЛН
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 27 МЛН
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 609 М.
Python Sudoku Solver - Computerphile
10:53
Computerphile
Рет қаралды 1,2 МЛН
Multithreading in Java Explained in 10 Minutes
10:01
Coding with John
Рет қаралды 902 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Coding a Java Sudoku Solver - Java Programming
21:13
Will Tollefson
Рет қаралды 2,5 М.
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 78 М.
Optionals In Java - Simple Tutorial
15:53
Coding with John
Рет қаралды 205 М.
Binary Search in Java - Full Simple Coding Tutorial
17:48
Coding with John
Рет қаралды 114 М.