//***************************************** 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(){
@@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
@@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...
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); }
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......
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.
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
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.
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
// 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
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
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 ?
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
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
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! :)))
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!!!!!!
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 :))
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
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
//*****************************************
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
You didn't add tie situation when no one wins
it doesn't work .
Error: Main method not found in class TicTacToe, please define the main method as:
public static void main(String[] args)
help me please
@@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
@@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...
Finally i am able to make my first ever java game because of youuuuuu.thankyou soooo muchhhh!I wish you success in your lifeeee
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.
I hope you become a millionaire through this channel.
Yeah! He will!!! Is he is already a Billionaire.
I think he is already a millionaire, facts he knows
@@schneggeraah….married?
Finally we have a game. I really like games
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!
Good project bro especially for java beginners people like me
Thanks I Really Enjoyed
This is excellent, thanks. I am going to use this with my Java high school class to introduce GUI dev.
Comment for the channel growth
Thanks so much bro.....this is just very easy to learn and we'll tutored. You deserve a million dollar.
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); }
Your videos are very useful thank you very much
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......
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.
@@BroCodez Yes, please do
very helpful, I did this without any Java experience and the troubleshooting and learning was very fun and overall good experience
Bro Code__ you are Amazing__ I now Understand Java Because of your insightful videos__thank you✌
Nice video
Awesome step by step Video. thank you very much
Very good video bro love your work it helps me so much
hi sir , really awesome keep it up , from Srilanka
Nice Video Bro !
Excellent Tutorial
😍
Thank you Bro u are help me a lot to study java
You're a Legend BRO!!🦾🦾
Great 👍
this is amazing you explain it step by step kudos , this realy motivate me
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
You're a king. Great video!
yeah, totally
Like the way you explain stuff
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.
Nice and well explained
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
Wow what a nice guy
// 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
Thanks u very much i could not make this tie situation as simple else if wasnt working
@@BlueOrcas this rounds out the project to make it a complete game. thanks!
@@jgino7514 you’re welcome but I haven’t gotten the tie condition yet
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
I also used the Random class to add a PVE mode against the computer for the fan of it😂
Thank you SO much sir, i learned lot from you about java GUI, SIr Please keep uploading interesting projects.
awesome java project for beginners
I love the way you teach, but please explain some difficult method or flag some times will be good for beginner. Thank you........
Best video
You did a great job bro... Keep it up...
Thanks for helping me with the college project, Bro.
T
his comment is to support your channel. Great video man
Great video, thanks👍🏻
Tic-Tac-Toe, I'm glad i have my Bro
Man, your tutorials are a GREAT help, thak you!
Here we call this game "Jogo da velha". Thank you, I loved this tutorial!💚
Thank uuu,we need more of these👍🏻
impressive 👊🏼👊🏼👊🏼👊🏼😍😍
Thanks bro for your knowledge
this videos are the best! it is so fun to learn to code!
Thank you very match for this video
You are doing a good Job
very nice.
Fantastic tutorial!
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 ?
Same thing is needed by me
its easier than coding the game bro
Waoo that's awesome
I will just code games like this when I am bored at work......kkkkkkk THANKS BRO, YOU ARE THE BEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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
like from Khorezm
Thank you so much for teaching me this and I use IntelliJ and I still got it
Thank you so much
It was so nice 🌹
Thanks for thas nice Tutorial!!!
cool stuff bro
thankuuuuu soo much
we need a spring video soon .
The video was so helpful
HI Bro.....I.like your tutorials.............question.....how can I add a reset button to this application?
thanks
Good video
good explain love it
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
Awesomeness bro. Thanks so much.
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...
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! :)))
Yes please. I was wondering if I could snag it and modify it. I'm making a heavily modified version of Tic Tac Toe.
@@lilsquirt9248 I will not make it today but I will send it tommorow, nice anyone interested :))
>>>> This is the second half of the code, just paste underneath the previous part...
@@MatejDrbohlav01 Thanks. Do you have Discord by chance?
Nice Job, Can you put the link so we can play it?
bro you are superrrrrrr
Thanks for watching Saroj!
@@BroCodez keep uploading sir
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
Thank you so much for teaching me this
Sooo underrated
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!!!!!!
Simply Love it!
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^^
Thank U so much ! can u make a video of how to export these games and make them a stand alone game?
I really need this Thanks
Thanks bro, was really helpful
You are super! Thanks!
Nice sir but there's one thing youve forgot the tie hehe. But Thanks its very helpful
Thank God am here
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 :))
Thanks helped me a lot also commenting for the algorithm
Finally I got it thanks bro
That was ultra cool
amazing
thanks mate, would had been great if you explained the code a bit but i guess thats too much to ask for.
Nice :o
good
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
great video how you can make bigger board. 10x10?
nice
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
Draw statement nhi run ho rha
i tried but then it puts the draw statment even if 1 of the win conditions isnt happening
plz help
This is so good!
you deserve way more subs man
speed video in 2x sees making of tictactoe in 15 min
why stop there? You can alter the javascript code for this webpage and set video speed x100.
Learn tictactoe in 18 seconds lol
@@BroCodez ähm "learn" in 2x u are able to learn but not in 100x
Yeah bro I learned the logic behind the game. Btw, just posted this to "Help you, help ME"..
Thank you soo much thats very helpful.
●● I like your theme of Eclips, can you send its link please??? Thanx again
Can you make a video how to make a main menu