Java tic tac toe game ⭕

Поділитися
Вставка
  • Опубліковано 27 січ 2025

КОМЕНТАРІ • 397

  • @BroCodez
    @BroCodez  4 роки тому +365

    //*****************************************
    public class Main {
    public static void main(String[] args) {
    TicTacToe tictactoe = new TicTacToe();

    }
    }
    //*****************************************
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    public class TicTacToe implements ActionListener{
    Random random = new Random();
    JFrame frame = new JFrame();
    JPanel title_panel = new JPanel();
    JPanel button_panel = new JPanel();
    JLabel textfield = new JLabel();
    JButton[] buttons = new JButton[9];
    boolean player1_turn;
    TicTacToe(){

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800,800);
    frame.getContentPane().setBackground(new Color(50,50,50));
    frame.setLayout(new BorderLayout());
    frame.setVisible(true);

    textfield.setBackground(new Color(25,25,25));
    textfield.setForeground(new Color(25,255,0));
    textfield.setFont(new Font("Ink Free",Font.BOLD,75));
    textfield.setHorizontalAlignment(JLabel.CENTER);
    textfield.setText("Tic-Tac-Toe");
    textfield.setOpaque(true);

    title_panel.setLayout(new BorderLayout());
    title_panel.setBounds(0,0,800,100);

    button_panel.setLayout(new GridLayout(3,3));
    button_panel.setBackground(new Color(150,150,150));

    for(int i=0;i

    • @tusharkhandejod8327
      @tusharkhandejod8327 4 роки тому +18

      You didn't add tie situation when no one wins

    • @hendlegleg2021
      @hendlegleg2021 4 роки тому +6

      it doesn't work .
      Error: Main method not found in class TicTacToe, please define the main method as:
      public static void main(String[] args)

    • @hendlegleg2021
      @hendlegleg2021 4 роки тому +1

      help me please

    • @divakarkumarjha1171
      @divakarkumarjha1171 4 роки тому

      @@hendlegleg2021 many videos are available on UA-cam to solve this problem so you can search to get proper solution you can check BS Sindhu channel here you will get solution

    • @gentworm2377
      @gentworm2377 4 роки тому +1

      @@hendlegleg2021 what, you have to know Java in order to do this
      The main class has to be marked with “public static void main(String[] args){
      }”
      This is how Java will know that as the main class...

  • @hamnabutt3000
    @hamnabutt3000 3 роки тому +9

    Finally i am able to make my first ever java game because of youuuuuu.thankyou soooo muchhhh!I wish you success in your lifeeee

  • @KLead963
    @KLead963 3 роки тому +14

    Man you helped me a lot! Your way is very easy to understand every single code! BIG THANKS. And I already subscribed and liked the video.

  • @mkhaled23
    @mkhaled23 4 роки тому +148

    I hope you become a millionaire through this channel.

    • @ShivamSingh-te9tl
      @ShivamSingh-te9tl Рік тому +1

      Yeah! He will!!! Is he is already a Billionaire.

    • @schnegger
      @schnegger Рік тому +4

      I think he is already a millionaire, facts he knows

    • @TitenKon
      @TitenKon 7 місяців тому

      @@schneggeraah….married?

  • @girl6994
    @girl6994 4 роки тому +19

    Finally we have a game. I really like games

  • @wolanus
    @wolanus 4 роки тому +7

    Another comment for the channel growth. Haven't been here for some time, but I see you've got much more subs. Well deserved! Thanks Bro!

  • @arfan6108
    @arfan6108 Рік тому +1

    Good project bro especially for java beginners people like me

  • @The-MaliX
    @The-MaliX 7 днів тому

    Thanks I Really Enjoyed

  • @themasterrogerdelgado
    @themasterrogerdelgado 11 місяців тому

    This is excellent, thanks. I am going to use this with my Java high school class to introduce GUI dev.

  • @supanam
    @supanam 3 роки тому

    Comment for the channel growth

  • @wiggletv1567
    @wiggletv1567 8 місяців тому

    Thanks so much bro.....this is just very easy to learn and we'll tutored. You deserve a million dollar.

  • @levirann
    @levirann 4 роки тому +35

    Another easier, yet not completely optimal, way to code the winning condition, is to set the variables for A, B, C and the winnter "X" or "O" first, that way you can change only those variables and run the same if statement again and again like so:
    int A = 0;
    int B = 1;
    int C = 2;
    String testWinner = "X";
    if((buttons[A].getText()==testWinner) && (buttons[B].getText()==testWinner) && (buttons[C].getText()==testWinner)){ xWins(A,B,C); }
    A = 0;
    B = 1;
    C = 2;
    testWinner = "O";
    if((buttons[A].getText()==testWinner) && (buttons[B].getText()==testWinner) && (buttons[C].getText()==testWinner)){ oWins(A,B,C); }

  • @biancaserea5479
    @biancaserea5479 Рік тому

    Your videos are very useful thank you very much

  • @bossali2527
    @bossali2527 4 роки тому +5

    Hi bro code I love your videos hoping you will add java applet to your java course and we will do game development like creating adventure games and also we will do some algorithm and data structure and it will be more fun if we learn it through game development like development a sport game!!!!! thanks for video it has help me a lot......

    • @BroCodez
      @BroCodez  4 роки тому +6

      I was planning on adding a few small games for now, but I am open to suggestions for future videos. However it make take some time since I'm covering a few different languages on this channel.

    • @stephanieezat-panah7750
      @stephanieezat-panah7750 2 роки тому

      @@BroCodez Yes, please do

  • @hayyannadeem4346
    @hayyannadeem4346 Рік тому

    very helpful, I did this without any Java experience and the troubleshooting and learning was very fun and overall good experience

  • @ntlakusojunior695
    @ntlakusojunior695 Рік тому

    Bro Code__ you are Amazing__ I now Understand Java Because of your insightful videos__thank you✌

  • @ValeriaGarciaHernandez
    @ValeriaGarciaHernandez Рік тому

    Nice video

  • @girishpoojari2910
    @girishpoojari2910 4 роки тому

    Awesome step by step Video. thank you very much

  • @aussmohammed9865
    @aussmohammed9865 7 місяців тому

    Very good video bro love your work it helps me so much

  • @gobikrishnasharma5041
    @gobikrishnasharma5041 3 місяці тому

    hi sir , really awesome keep it up , from Srilanka

  • @ErrorNotFound-rv1ql
    @ErrorNotFound-rv1ql 3 роки тому +1

    Nice Video Bro !

  • @rajeevranjanmishra1188
    @rajeevranjanmishra1188 Рік тому

    Excellent Tutorial
    😍

  • @viniciusgentile2151
    @viniciusgentile2151 9 місяців тому

    Thank you Bro u are help me a lot to study java

  • @remarkmacasieb1280
    @remarkmacasieb1280 12 днів тому

    You're a Legend BRO!!🦾🦾

  • @alexbagirov2583
    @alexbagirov2583 2 роки тому

    Great 👍

  • @UssopSamah
    @UssopSamah 2 роки тому

    this is amazing you explain it step by step kudos , this realy motivate me

  • @gennius9726
    @gennius9726 3 роки тому +13

    thank you so much for this example .... :D
    very useful .....I'm currently playing on 2 tables so to speak: taking your 5 hours long tutorial on GUI in Eclipse, and at the same time going trough mini-projects just like this one :)
    cheers

  • @lucadestroyer98
    @lucadestroyer98 4 роки тому +3

    You're a king. Great video!

  • @hubert8856
    @hubert8856 3 роки тому +1

    Like the way you explain stuff

  • @YM-ix8uw
    @YM-ix8uw 3 роки тому +4

    Thanks for the code in the comments!
    I was trying to learn how to use GUI (I'm a beginner). I somewhat know what you were doing while I was following the tutorial.

  • @ucee
    @ucee 3 роки тому

    Nice and well explained

  • @maxwong1768
    @maxwong1768 2 роки тому +18

    There are 4 improvements can be made .
    1) Disable the button before Thread.sleep(2000) , otherwise 2 X/O may appear continuously .
    2) Set up the tie condition .
    3) Add a reset button .
    4) another way to check win condition .
    // use 2D array and 2 arraylists .
    1)
    /*
    if we don't disable the buttons before 2s is up , the order may go wrong .
    e.g. two same X/O continuously appear .
    1) x_turn is default to false , when I click button before 2s from the beginning , O will definitely appear .
    2) Then the x_turn should switch to true but if the random is 0 after 2s , x_turn will change back to false so O will appear again .
    */
    disableButtons() ;
    // Wait for 2s before start .
    try {
    Thread.sleep(2000) ;
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    // restore the buttons .
    enableButtons() ;
    ......
    // game finishes .
    // the text color of buttons will be disabled as well so they will turn gray .
    public void disableButtons() {
    for (int b=0 ; b

    • @BlueOrcas
      @BlueOrcas 2 роки тому +1

      Wow what a nice guy

    • @BlueOrcas
      @BlueOrcas 2 роки тому +2

      // Mention with the other panels, frame, and buttons
      JPanel rpanel = new JPanel();
      JButton resButton = new JButton();
      /* For the reset Button */
      rpanel.setLayout(new BorderLayout());
      resButton.setText("Reset");
      resButton.setSize(100, 50);
      resButton.addActionListener(this);
      // I altered my code at the end to fix the Panels on the North, Center, and South Borders
      title_panel.add(textfield);
      rpanel.add(resButton);
      frame.add(title_panel, BorderLayout.NORTH);
      frame.add(button_panel, BorderLayout.CENTER);
      frame.add(rpanel, BorderLayout.SOUTH);
      frame.setLocationRelativeTo(null);
      //this should be under actionperformed
      if (e.getSource() == resButton) {
      frame.remove(button_panel);
      button_panel = new JPanel();
      button_panel.setLayout(new BorderLayout());
      button_panel.setLayout(new GridLayout(3, 3));
      button_panel.setBackground(new Color(150, 150, 150));
      frame.add(button_panel, BorderLayout.CENTER);
      for (int i = 0; i < 9; i++) {
      buttons[i] = new JButton();
      button_panel.add(buttons[i]);
      buttons[i].setFont(new Font("Fira Code", Font.BOLD, 120));
      buttons[i].addActionListener(this);
      }
      if (random.nextInt(2) == 0) {
      // int is 2 for the two players
      player1_turn = true;
      textfield.setText("X turn");
      } else {
      player1_turn = false;
      textfield.setText("O turn");
      }
      SwingUtilities.updateComponentTreeUI(frame);
      }
      }
      // I hope anyone who needs this, understands

    • @sounavamandal9f223
      @sounavamandal9f223 2 роки тому

      Thanks u very much i could not make this tie situation as simple else if wasnt working

    • @jgino7514
      @jgino7514 2 роки тому +1

      @@BlueOrcas this rounds out the project to make it a complete game. thanks!

    • @BlueOrcas
      @BlueOrcas 2 роки тому

      @@jgino7514 you’re welcome but I haven’t gotten the tie condition yet

  • @kenkei.mwaniki
    @kenkei.mwaniki 7 місяців тому

    Nice
    I watched this then went and made the game but used JavaFX instead of Swing
    Learnt both from you and im super grateful.
    Also advanced the music player program you showed in the JavaFX lessons to have all the features a typical music player would have (shuffle, loop, playlists, search, access to audio metadata, library (artists, albums etc)
    Was really fun as my first project with Java.
    Could you make a video on how to create a runnable JAR file from JavaFX programs?
    The method for swing programs doesn't involve adding VM arguments and I've been struggling with this

    • @kenkei.mwaniki
      @kenkei.mwaniki 7 місяців тому

      I also used the Random class to add a PVE mode against the computer for the fan of it😂

  • @anilsuyal
    @anilsuyal 3 роки тому +1

    Thank you SO much sir, i learned lot from you about java GUI, SIr Please keep uploading interesting projects.

  • @playerno1234
    @playerno1234 3 роки тому

    awesome java project for beginners

  • @krishnaadhikari7098
    @krishnaadhikari7098 3 роки тому

    I love the way you teach, but please explain some difficult method or flag some times will be good for beginner. Thank you........

  • @cookwitharjunchef6336
    @cookwitharjunchef6336 3 роки тому

    Best video

  • @localbus7008
    @localbus7008 2 роки тому

    You did a great job bro... Keep it up...

  • @KaushikUpadhyay-od4xl
    @KaushikUpadhyay-od4xl 9 місяців тому +1

    Thanks for helping me with the college project, Bro.

  • @manthanpatki146
    @manthanpatki146 3 роки тому

    T
    his comment is to support your channel. Great video man

  • @mullim9860
    @mullim9860 2 роки тому

    Great video, thanks👍🏻

  • @janekk3397
    @janekk3397 2 роки тому

    Tic-Tac-Toe, I'm glad i have my Bro
    Man, your tutorials are a GREAT help, thak you!

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 4 місяці тому

    Here we call this game "Jogo da velha". Thank you, I loved this tutorial!💚

  • @the_craziest_edits
    @the_craziest_edits 2 роки тому

    Thank uuu,we need more of these👍🏻

  • @murtazagawhary4373
    @murtazagawhary4373 3 роки тому

    impressive 👊🏼👊🏼👊🏼👊🏼😍😍

  • @richardgujja7460
    @richardgujja7460 3 роки тому

    Thanks bro for your knowledge

  • @iceblockgaming9792
    @iceblockgaming9792 3 роки тому

    this videos are the best! it is so fun to learn to code!

  • @justinbanza4751
    @justinbanza4751 3 роки тому

    Thank you very match for this video
    You are doing a good Job

  • @_aumaca
    @_aumaca Рік тому

    very nice.

  • @kingannon4131
    @kingannon4131 10 місяців тому

    Fantastic tutorial!

  • @Tebek
    @Tebek 4 роки тому +25

    Hi, can you please make a follow up tutorial to this where you add a screen before the game where it asks for player1's name and then for player 2's name ?

    • @__ArnobShariar
      @__ArnobShariar 3 роки тому

      Same thing is needed by me

    • @goplay1121
      @goplay1121 2 роки тому +5

      its easier than coding the game bro

  • @manishpatidar5379
    @manishpatidar5379 4 роки тому

    Waoo that's awesome

  • @hanibaltesfamichael5936
    @hanibaltesfamichael5936 2 роки тому

    I will just code games like this when I am bored at work......kkkkkkk THANKS BRO, YOU ARE THE BEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @Shivam_R_Divakar
    @Shivam_R_Divakar 8 місяців тому +2

    For draw condition,
    -Create a,"int counter=0" at start and increment it in the void actionPerformed function.
    -add a drawCheck() inside the void actionPerformed function after where the check() is called.
    -At end, inside the class itself define the drawCheck().
    //Draw
    public void drawCheck(){
    if(counter==9){
    if(textfield.getText()!="X wins" || textfield.getText()!="O wins"){
    for(int i=0;i

  • @khazarasp
    @khazarasp 3 роки тому

    like from Khorezm

  • @jonneelycrisp1930
    @jonneelycrisp1930 3 роки тому +5

    Thank you so much for teaching me this and I use IntelliJ and I still got it

  • @SiminSibi
    @SiminSibi Рік тому

    Thank you so much
    It was so nice 🌹

  • @iven.jsr07
    @iven.jsr07 3 роки тому

    Thanks for thas nice Tutorial!!!

  • @mxolisisivuku3079
    @mxolisisivuku3079 2 роки тому

    cool stuff bro

  • @PPTV07
    @PPTV07 2 роки тому

    thankuuuuu soo much
    we need a spring video soon .

  • @ashishprakash2879
    @ashishprakash2879 3 роки тому

    The video was so helpful

  • @omarmorales7117
    @omarmorales7117 3 роки тому +1

    HI Bro.....I.like your tutorials.............question.....how can I add a reset button to this application?
    thanks

  • @user-wn1db9fm4o
    @user-wn1db9fm4o 4 роки тому

    Good video

  • @Yasmin-uc8vj
    @Yasmin-uc8vj 4 роки тому +1

    good explain love it

  • @erickim5712
    @erickim5712 2 роки тому +3

    Hello, I was trying to use the same concept to make a connect 4 to implement a way so I can code on my own. I guess only for the check function I would be coding on my own. However, when trying to draw on the each button panel it would only work once drawing a O. I put I

  • @superkingpunga
    @superkingpunga 3 роки тому

    Awesomeness bro. Thanks so much.

  • @dihanahmed965
    @dihanahmed965 4 роки тому

    thanks a lot brother ... you saved my day... thank you so much ...
    it will be a great help if you make a video about AI Play in TIC TAC TOE...

  • @MatejDrbohlav01
    @MatejDrbohlav01 3 роки тому +1

    Hello guys! It was so much fun! I liked it so much, that I created "Five in a row" modification with 12 rows and columns (I am now working on making the grid size customizable) If anyone is interested I will post the code here! :)))

    • @lilsquirt9248
      @lilsquirt9248 3 роки тому +1

      Yes please. I was wondering if I could snag it and modify it. I'm making a heavily modified version of Tic Tac Toe.

    • @MatejDrbohlav01
      @MatejDrbohlav01 3 роки тому

      @@lilsquirt9248 I will not make it today but I will send it tommorow, nice anyone interested :))

    • @MatejDrbohlav01
      @MatejDrbohlav01 3 роки тому

      >>>> This is the second half of the code, just paste underneath the previous part...

    • @lilsquirt9248
      @lilsquirt9248 3 роки тому

      ​@@MatejDrbohlav01 Thanks. Do you have Discord by chance?

  • @Neelpl80
    @Neelpl80 4 роки тому +1

    Nice Job, Can you put the link so we can play it?

  • @sarojmaharjan3986
    @sarojmaharjan3986 4 роки тому

    bro you are superrrrrrr

  • @daizdamien1409
    @daizdamien1409 3 роки тому

    I main thing i watched this video was to check how the ai worked. Bro can you do that? Btw the prog is cool :D

  • @abdurrochman1093
    @abdurrochman1093 2 роки тому

    Thank you so much for teaching me this

  • @ultimatelegoman1279
    @ultimatelegoman1279 3 роки тому +4

    Sooo underrated

  • @shamc8096
    @shamc8096 3 роки тому

    Sir, actually you are crazy and also you are the most underrated diamond I think so, I learned a lot of thinks and thank you for this kind of awesome programs. I request you to not leave this and please provide us many videos like this!!!!!!

  • @dragomirpetrov5138
    @dragomirpetrov5138 2 роки тому

    Simply Love it!

  • @kimpaulson5542
    @kimpaulson5542 4 місяці тому

    I don't know if anyone will see this..I can't make the font change :D I'm new to Java and I don't know what I'm doing here^^

  • @itamaryuran8640
    @itamaryuran8640 3 роки тому

    Thank U so much ! can u make a video of how to export these games and make them a stand alone game?

  • @ctluwua7695
    @ctluwua7695 3 роки тому

    I really need this Thanks

  • @sidharth5849
    @sidharth5849 3 роки тому

    Thanks bro, was really helpful

  • @sergeybernatovich5706
    @sergeybernatovich5706 2 роки тому

    You are super! Thanks!

  • @jaylordjl6337
    @jaylordjl6337 2 роки тому

    Nice sir but there's one thing youve forgot the tie hehe. But Thanks its very helpful

  • @matthewade7256
    @matthewade7256 2 роки тому

    Thank God am here

  • @MatejDrbohlav01
    @MatejDrbohlav01 3 роки тому +22

    Hello, it was fun, I just added "DRAW" condition there is a code:
    this edit on the start of check() method:
    public void check() {

    // check draw
    int i = 0;
    while (buttons[i].getText() != "") {
    if (i == buttons.length - 1) {
    draw();
    break;
    }
    i++;
    }
    // check X win conditions
    ....
    and this draw() method added:
    public void draw() {
    for (int i = 0; i < buttons.length; i++) {
    buttons[i].setEnabled(false);
    }
    textField.setText("Game is draw!");
    }
    I am looking forward to doing more customizations! Thinking about doing five-in-a-row with 2D array buttons :))

  • @waterfallz3768
    @waterfallz3768 3 роки тому

    Thanks helped me a lot also commenting for the algorithm

  • @dailystory9006
    @dailystory9006 2 роки тому

    Finally I got it thanks bro

  • @Wonderland8183
    @Wonderland8183 3 роки тому

    That was ultra cool

  • @younesyar
    @younesyar 2 роки тому

    amazing

  • @debamchakraborty1143
    @debamchakraborty1143 2 роки тому

    thanks mate, would had been great if you explained the code a bit but i guess thats too much to ask for.

  • @ktos2144
    @ktos2144 3 роки тому

    Nice :o

  • @kyawlinn4461
    @kyawlinn4461 Рік тому

    good

  • @danishbelal1292
    @danishbelal1292 3 роки тому +2

    Code is good , there is just a suggestion rather than creating every winning combination , you can just use a checking condition , if x enter check for that i,j index for up down left and right if x is present that will be winner

  • @grzegorzszymonowicz3500
    @grzegorzszymonowicz3500 3 роки тому

    great video how you can make bigger board. 10x10?

  • @playmaxz4335
    @playmaxz4335 3 роки тому

    nice

  • @amanthakur6913
    @amanthakur6913 3 роки тому

    by putting another else statement in check() method we can also check for draw and and call a draw method in which we can set the textfield as game tied

    • @rahilkhan8567
      @rahilkhan8567 3 роки тому

      Draw statement nhi run ho rha

    • @arielminin873
      @arielminin873 2 роки тому

      i tried but then it puts the draw statment even if 1 of the win conditions isnt happening

    • @arielminin873
      @arielminin873 2 роки тому

      plz help

  • @lucyledezma709
    @lucyledezma709 4 роки тому +1

    This is so good!

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 4 роки тому

    you deserve way more subs man

  • @stormz9937
    @stormz9937 4 роки тому +3

    speed video in 2x sees making of tictactoe in 15 min

    • @BroCodez
      @BroCodez  4 роки тому +8

      why stop there? You can alter the javascript code for this webpage and set video speed x100.
      Learn tictactoe in 18 seconds lol

    • @stormz9937
      @stormz9937 4 роки тому +1

      @@BroCodez ähm "learn" in 2x u are able to learn but not in 100x

  • @tnnm2022
    @tnnm2022 Рік тому

    Yeah bro I learned the logic behind the game. Btw, just posted this to "Help you, help ME"..

  • @helpandsupportcenter8353
    @helpandsupportcenter8353 3 роки тому

    Thank you soo much thats very helpful.
    ●● I like your theme of Eclips, can you send its link please??? Thanx again

  • @pvprogres3637
    @pvprogres3637 3 роки тому

    Can you make a video how to make a main menu