Code Tic Tac Toe in Java

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2024

Комментарии • 51

  • @fersahahmet9597
    @fersahahmet9597 Год назад +7

    Thank you for your effort Kenny , this is a very good tutorial for an average programmer. Please keep up the good work like this one. I have subcribed recently and watching all of your prev. tutorials

  • @aravinds6406
    @aravinds6406 11 месяцев назад +2

    Very useful tutorial. The thing I like about the video is , each line of the code you explained what it does.

    • @olehliashyk6988
      @olehliashyk6988 11 месяцев назад

      Help me plz about tile in 12 minute on video. I do not write “tile” because I have an issue😢

  • @Peleg-q7u
    @Peleg-q7u Год назад +3

    you are a king i have no idea how to code and i just made a game

  • @incarnateblizzard4874
    @incarnateblizzard4874 4 месяца назад +2

    you could also - to keep the code as much the same as possible change setWinner for anti-diagionally to this:
    for (int i = 0; i

  • @ValentyneMu
    @ValentyneMu 3 месяца назад +1

    Impossible to dislike this... YOU SAVED MY LIFE!!!

  • @NareshKumar-vt4sh
    @NareshKumar-vt4sh Год назад +1

    Yeahhhh 🎉 thank you kenny. Pls be continue in java projects...

  • @philip3746
    @philip3746 5 месяцев назад

    Great video, very clear. I am going to try to modify your code to where the computer can play one of the players. I have seen it done in Python so I will try in Java.

  • @prajwalkamble4913
    @prajwalkamble4913 3 месяца назад

    Thank you for this amazing explanation 🎉

  • @FariaNourin-p3o
    @FariaNourin-p3o Год назад +1

    next time please add the replay button. im currently following your all game tutorial. so it will be very helpful.

    • @KennyYipCoding
      @KennyYipCoding  Год назад +3

      I assume people will follow the tutorial to learn and later improve on the project. I purposely left out the replay button so that it would be something for you to figure out on your own.
      Shouldn't be too difficult since following the tutorial will teach you how to add a button, add an action listener to that button when clicked, and at the end of the video I also identified which variables you should update when resetting the game.

  • @pland4r133
    @pland4r133 Год назад

    keep up man uare the best keep going i'm learning so much from you

  • @ImHlkari
    @ImHlkari 4 месяца назад

    Super underrated

  • @deactivatedchannel4362
    @deactivatedchannel4362 21 день назад

    super helpful my g
    :)

  • @dannita246
    @dannita246 10 месяцев назад

    does this code work in ide on cellphone? like Jvdroid?

  • @lizarose3095
    @lizarose3095 4 месяца назад

    Thanks for the tutorial :-)

  • @useruser15734
    @useruser15734 Год назад

    very cool explanation! Will there be more JavaScript content? will there be any JavaScript tutorials or maybe you will understand the algorithms and Data Structures in JavaScript? And yet, can I contact you personally, write to you?

    • @KennyYipCoding
      @KennyYipCoding  Год назад +1

      Yes, there will be more JavaScript content in the future. Currently I’m just taking some time to focus on Java so that I can expand my content to those who prefer coding in Java. But I will continue to upload JS content

    • @KennyYipCoding
      @KennyYipCoding  Год назад +1

      Unfortunately, I don’t do personal one on ones but I usually reply to comments/questions on my videos

    • @useruser15734
      @useruser15734 Год назад

      please tell me where did you learn javascript? maybe you can advise some literature on this programming language to read?! you really are the best at explaining anyone I've seen on RUclips!!!!

  • @sohailrafique
    @sohailrafique Месяц назад

    new to coding, every time I type frame.setVisible (true); I get an error about Boolean, what is that?????

    • @KennyYipCoding
      @KennyYipCoding  Месяц назад

      Would recommend you learn the basics of coding before hopping into a project

    • @KennyYipCoding
      @KennyYipCoding  Месяц назад

      What does the error say?

  • @MuskanRai-f3p
    @MuskanRai-f3p 3 месяца назад

    I am getting -
    Error: Could not find or load main class TicTacToe
    Caused by: java.lang.ClassNotFoundException: TicTacToe
    in my project
    can you tell how to resolve this problem

    • @factibul5595
      @factibul5595 3 месяца назад

      Maybe you aren't have main class in your project

  • @olehliashyk6988
    @olehliashyk6988 11 месяцев назад

    Why my Inteliji IDE has not see "tile"? on 12m14sec. Help me plz!!!!

  • @user-Red_Haired1984
    @user-Red_Haired1984 10 месяцев назад +1

    playerX ? playerO : playerX;
    I've never seen this kind of code before. what is that mean? sorry, im a begginer of Java.

    • @KennyYipCoding
      @KennyYipCoding  10 месяцев назад +1

      it is the ternary operator syntax,
      you can mentally read the syntax like so:
      currentPlayer = is (currentPlayer == playerX) ? set to playerO : else set to playerX
      this syntax is shorthand for the following:
      if (currentPlayer == playerX) {
      currentPlayer = playerO;
      } else {
      currentPlayer = playerX;
      }

    • @user-Red_Haired1984
      @user-Red_Haired1984 10 месяцев назад

      @@KennyYipCoding Thank you for the explanation!

  • @chapolmazumder1059
    @chapolmazumder1059 12 дней назад

    Thank you

  • @GodStories1306
    @GodStories1306 Год назад

    In java compiler main class" Public static void main (String [] args ) not found error is getting ?.
    Solution plz🙏

    • @KennyYipCoding
      @KennyYipCoding  Год назад +1

      Are you using vs code?

    • @GodStories1306
      @GodStories1306 Год назад

      @@KennyYipCoding .
      No i am not using vs code...
      Will it work on it???

    • @KennyYipCoding
      @KennyYipCoding  Год назад

      @@GodStories1306 no, it’s not necessary, but do you have a file with the main function?

  • @KGanapathi-b3f
    @KGanapathi-b3f 26 дней назад +1

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TicTacToc {
    int boardWidth = 600;
    int boardHeight = 650;//50px for the text panel on top
    JFrame frame =new JFrame( "Tic-Tac-Toc");
    JLabel textLabel = new JLabel();
    JPanel textPanel = new JPanel();
    JPanel boardPanel = new JPanel();
    JButton[][] board = new JButton[3][3];
    String playerX = "X";
    String playerO = "O";
    String currentPlayer = playerX;
    boolean gameOver = false;
    TicTacToc() {
    frame.setVisible(true);
    frame.setSize(boardWidth, boardHeight);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    textLabel.setBackground(Color.darkGray);
    textLabel.setForeground(Color.white);
    textLabel.setFont(new Font("Arial", Font.BOLD,50));
    textLabel.setHorizontalAlignment(JLabel.CENTER);
    textLabel.setText("Tic-Tac-Toc");
    textLabel.setOpaque(true);
    textPanel.setLayout(new BorderLayout());
    textPanel.add(textLabel);
    frame.add(textPanel, BorderLayout.NORTH);
    boardPanel.setLayout(new GridLayout(3,3));
    boardPanel.setBackground(Color.darkGray);
    frame.add(boardPanel);
    for (int r = 0; r < 3; r++ ) {
    for (int c = 0; c< 3; c++) {
    JButton tile = new JButton();
    board[r][c] = tile;
    boardPanel.add(tile);
    tile.setBackground(Color.darkGray);
    tile.setForeground(Color.white);
    tile.setFont(new Font("Arial", Font.BOLD, 120));
    tile.setFocusable(false);
    // tile.setText(currentPlayer);
    tile.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (gameOver) return;
    JButton tile = (JButton) e.getSource();
    if (tile.getText() == "") {
    tile.setText(currentPlayer);
    checkWinner();
    if (!gameOver){
    currentPlayer = currentPlayer == playerX ? playerO : playerX;
    textLabel.setText(currentPlayer + "'s turn.");
    }
    }
    }
    });
    }
    }
    }
    void checkWinner() {
    //horizontal
    for (int r = 0; r < 3; r++) {
    if (board[r][0].getText()== "") continue;
    if (board[r][0].getText() == board[r][1].getText() &&
    board[r][1].getText() == board[r][2].getText ()) {
    for (int i =0; i < 3; i++) {
    setWinner(board[r][i]);
    }
    gameOver = true;
    return;
    }
    }
    //vertical
    for (int c = 0; c

  • @GodStories1306
    @GodStories1306 Год назад

    Why I can't see code on your site????

    • @KennyYipCoding
      @KennyYipCoding  Год назад +1

      The GitHub link is in the video description

  • @tayyabahamed3736
    @tayyabahamed3736 3 месяца назад

    The code compiles and interprets but it doesnt give the output🥲 any solution??

    • @KennyYipCoding
      @KennyYipCoding  3 месяца назад

      I’m not sure what you’re referring to

    • @junaidsultanxyz
      @junaidsultanxyz 3 месяца назад

      did u set the visibility of ur jframe to true ?

  • @peanut5280
    @peanut5280 7 месяцев назад +3

    Sombody have a restart button?😢

    • @tormentor2285
      @tormentor2285 День назад

      i guess a button and then a loop if clicked to reset all buttons

  • @ArrowO_76
    @ArrowO_76 2 месяца назад

    I using Replit java on phone and it keeps come up with an errors! Am i doing something wrong? Is the video wrong? What is going on?! 😭

    • @KennyYipCoding
      @KennyYipCoding  2 месяца назад

      You should code in a proper environment, there’s probably limitations coding on a website, especially on a phone

    • @KennyYipCoding
      @KennyYipCoding  2 месяца назад

      Also you should read the errors, they tell you what’s wrong

    • @ArrowO_76
      @ArrowO_76 2 месяца назад

      @@KennyYipCoding they dont tell me much sadly

    • @LimeBytesYT
      @LimeBytesYT Месяц назад +2

      Bros committed o7

    • @ArrowO_76
      @ArrowO_76 Месяц назад

      @@LimeBytesYTyes i am

  • @Mago77_
    @Mago77_ Год назад

    Amazing!

  • @sadiulhakim7814
    @sadiulhakim7814 4 месяца назад

    Thanks