Tic Tac Toe Game in Java - Full Tutorial with Source

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 พ.ย. 2020
  • Complete Java course: codingwithjohn.thinkific.com/...
    Programming a Tic Tac Toe game with a 2D array in Java from scratch!
    GET THE SOURCE CODE HERE: codingwithjohn.com/tic-tac-to...
    Learn how to create a game of Tic Tac Toe programming in Java using a 2D array in this video tutorial lesson.
    Also known as Naughts and Crosses!
    Learn or improve your Java programming by watching it being coded live in tutorial lessons! This is a beginner Java coding lesson where we create a full Tic Tac Toe (Naughts and Crosses) game using a 2D array in Java.
    Hey, I'm John! I'm a Lead Java Software Engineer who has been in the industry for over a decade and I love teaching what I know.
    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
  • ภาพยนตร์และแอนิเมชัน

ความคิดเห็น • 85

  • @bodinian
    @bodinian 2 ปีที่แล้ว +29

    The moment you showed me "Extract Method" as a feature of my IDE I immediately saved your video. That is EXTREMELY useful for most of my troubles coding in java.

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว +10

      Awesome! I definitely remember all the simple OMG moments of learning all the great things IDEs can do - Extracting methods, auto-creating getters and setters, renaming everything. Might be a great subject for a future video. In the meantime, give it a right click and explore the menu to see what it can do for you!

  • @kxnyshk
    @kxnyshk 2 ปีที่แล้ว +9

    Wow this was so awesome! And easy to learn. One don't need much of exposure to Java, to understand this. Also the shortcut for converting code directly into a method was pretty awesome! I wasn't aware of that. 💙

  • @aniruddhkhatri9834
    @aniruddhkhatri9834 2 ปีที่แล้ว +4

    Thanks a lot for explaining, John. Because of you I am fully able to understand Java.

  • @m77mo65
    @m77mo65 2 ปีที่แล้ว +2

    I'm not a junior developer, but I don't know why I enjoy watching all your videos sir. Keep it up.

  • @thiagogaitkoski3562
    @thiagogaitkoski3562 3 ปีที่แล้ว +1

    Thanks for helping with my college work.
    ❤️From Brazil

  • @abhishek-gu6qt
    @abhishek-gu6qt ปีที่แล้ว +1

    i learned many thing throughout this like most of is eclipse shortcuts!
    thanks!

  • @mrmichalification
    @mrmichalification 3 ปีที่แล้ว +1

    I would like to thank You for this tutorial. It is focused to teach and show the idea. It helped me a lot. Thx.

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว +1

      Thank you for the kind words! Glad it helped you out.

  • @richardlink7291
    @richardlink7291 2 ปีที่แล้ว

    Learned a lot through this. Thanks!

  • @CosmonautCoding
    @CosmonautCoding 3 ปีที่แล้ว +7

    Awesome tutorial John! Thank you for sharing

  • @girishanker3796
    @girishanker3796 ปีที่แล้ว

    Thanks John. Really loved it!

  • @pardeepdangi292
    @pardeepdangi292 2 ปีที่แล้ว

    helped me a lot , thanx for your effort for making it look simple .

  • @Rooswarden
    @Rooswarden 3 ปีที่แล้ว +1

    Thank you from the Netherlands ❤️

  • @rodmenmx
    @rodmenmx ปีที่แล้ว

    Love it, thank you !!

  • @user-sc1zz1uo4b
    @user-sc1zz1uo4b 6 หลายเดือนก่อน

    I saw this and it was very helpful to me thank you very much

  • @piotrfengler7658
    @piotrfengler7658 2 ปีที่แล้ว

    Thx John!

  • @ismaelmontenegro9600
    @ismaelmontenegro9600 ปีที่แล้ว

    You saved my friends life mate. ❤

  • @prakashpatel104
    @prakashpatel104 3 ปีที่แล้ว +3

    Thanks for sharing the very nice tutorial for Tic Tac Toe. But, I would appreciate it if you upgrade the same tutorial with more classes and objects to add the Object-Oriented Flavor. Thanks.

  • @ludovicbocquet9783
    @ludovicbocquet9783 ปีที่แล้ว

    Thank you !

  • @ismaelmontenegro9600
    @ismaelmontenegro9600 ปีที่แล้ว

    You a real one 💯

  • @SoftwareTestSolutions
    @SoftwareTestSolutions 2 ปีที่แล้ว

    You are awesome!

  • @mouloudguell2763
    @mouloudguell2763 ปีที่แล้ว +1

    thank you for the tutorial that helps a lot with practicing .hopefully you will put more projects

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว +1

      Let me know if you have ideas for any!

    • @mouloudguell2763
      @mouloudguell2763 ปีที่แล้ว

      @@CodingWithJohn would it be possible please to do a tutorial on snake game that uses JFrame and JPanel ? or any more of this type of games as it really helps practicing . few youtubers actually do projects like this .

  • @TheGuroguro12
    @TheGuroguro12 2 ปีที่แล้ว

    Thanks

  • @seanchoi4698
    @seanchoi4698 3 ปีที่แล้ว

    cheers mate

  • @veereshchinna9934
    @veereshchinna9934 3 ปีที่แล้ว +2

    It's really fun nd cool.
    ❤️ *From 🇮🇳 india*

  • @Games_379
    @Games_379 3 ปีที่แล้ว

    Thanks sir I'm your subscriber

  • @parthgupta9754
    @parthgupta9754 2 ปีที่แล้ว

    thanks v sauce👍🏻

  • @MariaEduarda-sx3qy
    @MariaEduarda-sx3qy 3 ปีที่แล้ว

    awesome video

  • @theBATfamiliar
    @theBATfamiliar 11 หลายเดือนก่อน

    Great video, I'm really enjoying it, thanks!

  • @salamistu
    @salamistu ปีที่แล้ว

    I just looked at this video and I made a slot game hahaha
    good stuff Sir! thank you!

  • @Games_379
    @Games_379 3 ปีที่แล้ว

    Love from India

  • @buckygarner1124
    @buckygarner1124 2 ปีที่แล้ว

    Hello John, thank you for this video introduction to JAVA using Tic-Tac-Toe. Do you have a version of the TTT game that using object oriented programming? I would assume you could create a Square class, and create a board as an ArrayList of square objects. Just curious if you have a video or link to code for an OOP version. Thanks.

  • @MTB_Bay_Area
    @MTB_Bay_Area ปีที่แล้ว +1

    If that game changes from 3x3 to n x n this program will have to be modified. I suggest for the testing of if user won to take the user selection say "1" and call a method to test after the placement if row 0 and col 0 are a win. You can only win after you place a value, hence, it is unnecessary to validate location where the user did not place a value. Just check the row, column and diagonal after a placement. This will allow you also to make the game for any size of a matrix. The for loop will simply run for zero to length of the matrix.

  • @user-yn3gw7zp7b
    @user-yn3gw7zp7b 2 ปีที่แล้ว

    hi John thanks for the video it's really cool, but i have a question why you used the method isGameFinished and why we used a loop insid the other one

  • @youtuberyo7321
    @youtuberyo7321 3 ปีที่แล้ว +3

    This guy fucks! High quality code, great coding style.

  • @saran9161
    @saran9161 ปีที่แล้ว

    i just thought of a way to check if game ties , declare a global count variable and whenever a valid move is placed the count increments. when it hits 9 game ties. instead of checking using two nested loop i thought this will be efficient. good vid ❤

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว +1

      Interesting! That could possibly work depending on how you implemented it but you'd just have to be careful to not make it more confusing. Sometimes the ninth move doesn't result in a tie, but instead has a winner. Also it requires a global variable, which although it's not a huge deal in a small program like this, we typically try to avoid them.

  • @hamsejama
    @hamsejama 3 ปีที่แล้ว +5

    Thank you sooo much. You have a clear way of explaining. There are a lot and a lot of contents of how to something or explaining a principle but when it comes to applying that knowledge to a project, that is where you shine brighter. I am in the middle of the tutorial and i have a smile on my face as i admire your teaching style and that i finally found someone who can teach me the way i would prefer to. so a big tHaNk YoU
    `(*>﹏

  • @Vano-zg5xu
    @Vano-zg5xu 21 วันที่ผ่านมา

    So I'm very shocked because today Chat GPT-4 created this game in 10 seconds, even when I added some extra "easter eggs" like if the user presses "k", they can play a number guessing game. If they win this mini-game, they get an extra move in tic-tac-toe
    And Chat GPT-4 explained everything clearly. This code is very user-friendly, which is awesome.

  • @rebhuroy3713
    @rebhuroy3713 ปีที่แล้ว +1

    Hey your video was really helpful.. now I tried to implement a minimax algorithm on this tic tac toe version but unable to do that. Can you please help me work on that

  • @inrealife9126
    @inrealife9126 6 หลายเดือนก่อน

    This task came to me as an internship interview question

  • @JLSXMK8
    @JLSXMK8 3 ปีที่แล้ว +1

    char 'symbol', huh? I agree. You might have also named it a "playerIndicator", hence it indicates whether the current player is the computer ('o') or the user ('x').

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว +1

      True - there are always tons of ways you could do it, and the way I have it is certainly not the only way.

  • @duvindudinethmin6820
    @duvindudinethmin6820 2 ปีที่แล้ว

    🖤

  • @vageshnp6792
    @vageshnp6792 2 หลายเดือนก่อน

  • @sayliya
    @sayliya 3 ปีที่แล้ว

    This really helped me but I do ask that you don't do the shortcut keys for the software you are using because it made it harder to follow along since I use JGrasp for my coding and I am new.

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว

      Thanks for the feedback! There will be some times I use them and some times that I don't. If I don't use them, I always get comments like, "you know, you can just use 'sysout' to get System.out.println()". And I know many beginners like seeing shortcuts since they may never learn they exist if they don't see people using them. But I also understand not everyone uses Eclipse.

  • @jimg1928
    @jimg1928 2 ปีที่แล้ว

    Nice Video!
    However, can we assume that rand.nextInt(9) + 1 will produce all values between 1 and 9?
    In particular, let's say that position 5 is the only position left open on the board and that it is the computers turn.
    How do we know that rand.nextInt(9) + 1 will "ever" produce the number 5 to finish the game?!!!

    • @rihasanatrofolo2472
      @rihasanatrofolo2472 ปีที่แล้ว

      Nothing with computers is truly "random". There are certain tricks we use to make things look random. The chance of it never hitting 5 is pretty much zero when the interval is so small. Look into "true random" if you're interested, which is based on real time weather.

    • @MsLetishia
      @MsLetishia ปีที่แล้ว

      The while statement filters the computer's play. So the computer only makes valid moves

  • @Apo458
    @Apo458 15 วันที่ผ่านมา

    You can extract methods?? :0

  • @vpenywise
    @vpenywise 2 ปีที่แล้ว

    @21:24 - char... hm... "char xo" :)

  • @dem1152
    @dem1152 2 ปีที่แล้ว

    does anyone know how to do this using 1D array?

  • @mike85_pl
    @mike85_pl ปีที่แล้ว

    i have problem. when i type 6 nothing happens but when i type 9 it selects both 6 and 9. anyone know why? i have the same code

  • @gamerlag8621
    @gamerlag8621 3 ปีที่แล้ว +2

    Fact: The first program Bill Gates made was a tic tac toe game

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว +4

      Ah, we're so alike, Bill Gates and I.

  • @richardmeyer418
    @richardmeyer418 2 ปีที่แล้ว

    Thank you for this. I know it's many months later, but those switch statements are really ugly. I, personally would make the input into an integer (if it wasn't already) - check if it's 1

    • @beyond_akshii
      @beyond_akshii 2 ปีที่แล้ว

      you did a nice try over here but your method wouldnt work to let find out the correct row, for eg the user gave 5 as input, for extracting the row number if i follow your method i would subtract 1 from it resulting in 5-1=4 , and as u wrote int row = input/3; and input is also integer hence integer division would be done here, 4/3 , which would result in 1 which isnt my correct row number

    • @richardmeyer418
      @richardmeyer418 2 ปีที่แล้ว

      @@beyond_akshii - I'm afraid that it IS the correct row number (and column number for that matter). Remember you start counting from 0 - therefore the first row is row 0 and the second row (where the 5 is), is row 1. Similarly for the columns (4%3) gives a 1 - which is the second column (remember we start at 0).

    • @beyond_akshii
      @beyond_akshii 2 ปีที่แล้ว +1

      @@richardmeyer418 oh dang forgot about that....thanks!

    • @richardmeyer418
      @richardmeyer418 2 ปีที่แล้ว

      @@beyond_akshii It happens. We all have occasional blind spots.

  • @van1ty629
    @van1ty629 4 หลายเดือนก่อน

    i am trying to make it on IDE netbeans and it doesnt print the board... i did EXACTLY what u did

  • @anchs2501
    @anchs2501 ปีที่แล้ว

    Im just gonna comment for the algorithm.

    • @CodingWithJohn
      @CodingWithJohn  ปีที่แล้ว

      Replying for the viewer interaction

  • @veniaminarapidis8331
    @veniaminarapidis8331 11 หลายเดือนก่อน

    tttMark

  • @alexandrudescultescu9623
    @alexandrudescultescu9623 ปีที่แล้ว

    instead of hasContestantWon()... we can rename that method weHaveAWinner()...!

  • @sanjayraju8828
    @sanjayraju8828 3 ปีที่แล้ว

    I am really struggling with making one of these with two human players I tried it and i can never get it to work.

    • @CodingWithJohn
      @CodingWithJohn  3 ปีที่แล้ว

      Hmm, as long as you're OK with both players of course using the same computer and keyboard (and not doing some very complicated internet-based 2-player game), you can replace the computerTurn() calls with something like "player2Turn()", which can do pretty much the same thing the playerTurn() method does, just putting in the opposite symbol. Then just change the outputs for who wins ("You Win" might change to "player 1 wins", etc), and it should be a functioning game with two human players.
      If I have some time I'll try and get some functioning code up for that.

    • @sanjayraju8828
      @sanjayraju8828 3 ปีที่แล้ว

      @@CodingWithJohn Thank you very much.

  • @usebrain2897
    @usebrain2897 2 ปีที่แล้ว

    Anyone has the same problem as me? The "X" doesnt apper after writing a number 1-9....... Did legit everything the same and checked it 5 times.

    • @CodingWithJohn
      @CodingWithJohn  2 ปีที่แล้ว

      If you're comfortable posting your code, I or someone else on here can check it out!

    • @usebrain2897
      @usebrain2897 2 ปีที่แล้ว

      @@CodingWithJohn ahhh found the problem :D I wrote: "int input = new scanner.nextInt"
      and then in the switch I wrote: case '1'... Instead of just case 1 🤦‍♂️ These simple mistakes are killing me sometimes. Thanks for the reach out though!!!

  • @tallswede80
    @tallswede80 11 หลายเดือนก่อน

    using all those logical statements in the if structure is poor style. you are supposed to use boolean variables to make the if statements more readable.

  • @stevemiller7050
    @stevemiller7050 ปีที่แล้ว

    I like the tone and voice. but I don't like all the changes during the tutorial. in the end its a waste of time and seems pretty messed up. would be better to prepare yourself better and then run the tutorial without all the changes in method names etc.

  • @med_abdelghafour
    @med_abdelghafour ปีที่แล้ว

    chikor dnya ylfortas

  • @inchworm9311
    @inchworm9311 2 ปีที่แล้ว

    For the computerPlay could we do:
    int computerPlay = rand.nextInt(10);

    • @rihasanatrofolo2472
      @rihasanatrofolo2472 ปีที่แล้ว

      Yes you could definitely do that, but you'd have the issue with it choosing 0 sometimes, just adding unnecessary steps to the program, since 0 isn't a valid answer.
      The range is from 0 to your selected number. With the + 1, you won't have this problem.