Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hi I am trying to make a two player tic tac toe game in java I am a beginner

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi I am trying to make a two player tic tac toe game in java I am a beginner

    I need to make a two player tic tac toe game in java for the following
    has to have two players
    using button array create 3x3 grid layout and using border layout that will display south bond as a text field to display who won
    create menu system with file clear exit and about this game
    comment your code
    add backgrounds and sound
    This is what I have so far but I am stuck and Its not working right can someone help me
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import datatransfer.*;

    public class TicTacToeLove implements ActionListener {

    private JFrame window = new JFrame("Tic-Tac-Toe");
    private JButton button1 = new JButton("");
    private JButton button2 = new JButton("");
    private JButton button3 = new JButton("");
    private JButton button4 = new JButton("");
    private JButton button5 = new JButton("");
    private JButton button6 = new JButton("");
    private JButton button7 = new JButton("");
    private JButton button8 = new JButton("");
    private JButton button9 = new JButton("");
    private String letter = "";
    private int count = 0;
    private boolean win = false;

    public TicTacToeLove(){

    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.setLayout(new GridLayout(3,3));


    window.add(button1);
    window.add(button2);
    window.add(button3);
    window.add(button4);
    window.add(button5);
    window.add(button6);
    window.add(button7);
    window.add(button8);
    window.add(button9);


    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);

    public class TicTacToeLove{
    {
    MenuBar mnuBar = new MenuBar();
    setMenuBar(mnuBar);


    Menu mnuFile = new Menu("File", true);
    mnuBar.add(mnuFile);
    MenuItem mnuFileExit = new MenuItem("Exit");
    mnuFile.add(mnuFileExit);


    Menu mnuEdit = new Menu("Edit", true);
    mnuBar.add(mnuEdit);
    MenuItem mnuEditClear = new MenuItem("Clear");
    mnuEdit.add(mnuEditClear);
    mnuEdit.insertSeparator(1);
    MenuItem mnuEditCopy = new MenuItem("Copy");
    mnuEdit.add(mnuEditCopy);
    MenuItem mnuEditPaste = new MenuItem("Paste");
    mnuEdit.add(mnuEditPaste);


    Menu mnuAbout = new Menu("About", true);
    mnuBar.add(mnuAbout);
    MenuItem mnuAboutTicTacToelove = new MenuItem("About Tic Tac Toe");
    mnuAbout.add(mnuAboutTicTacToeLove);


    mnuFileExit.addActionListener(this);
    mnuEditClear.addActionListener(this);
    mnuEditCopy.addActionListener(this);
    mnuEditPaste.addActionListener(this);
    mnuAboutTicTacToeLove.addActionListener(this);


    mnuFileExit.setActionCommand("Exit");
    mnuEditClear.setActionCommand("Clear");
    mnuEditCopy.setActionCommand("Copy");
    mnuEditPaste.setActionCommand("Paste");
    mnuAboutTicTacToeLove.setActionCommand("About");



    window.setVisible(true);
    }

    public void actionPerformed(ActionEvent a) {
    count++;


    if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
    letter = "X";
    } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
    letter = "O";
    }


    if(a.getSource() == button1){
    button1.setText(letter);
    button1.setEnabled(false);
    } else if(a.getSource() == button2){
    button2.setText(letter);
    button2.setEnabled(false);
    } else if(a.getSource() == button3){
    button3.setText(letter);
    button3.setEnabled(false);
    } else if(a.getSource() == button4){
    button4.setText(letter);
    button4.setEnabled(false);
    } else if(a.getSource() == button5){
    button5.setText(letter);
    button5.setEnabled(false);
    } else if(a.getSource() == button6){
    button6.setText(letter);
    button6.setEnabled(false);
    } else if(a.getSource() == button7){
    button7.setText(letter);
    button7.setEnabled(false);
    } else if(a.getSource() == button8){
    button8.setText(letter);
    button8.setEnabled(false);
    } else if(a.getSource() == button9){
    button9.setText(letter);
    button9.setEnabled(false);
    }


    //horizontal wins
    if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
    win = true;
    }
    else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
    win = true;
    }
    else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
    win = true;
    }

    //virticle wins
    else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
    win = true;
    }
    else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
    win = true;
    }
    else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
    win = true;
    }

    //diagonal wins
    else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
    win = true;
    }
    else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
    win = true;
    }
    else {
    win = false;
    }


    if(win == true){
    JOptionPane.showMessageDialog(null, letter + " WINS!");
    } else if(count == 9 && win == false){
    JOptionPane.showMessageDialog(null, "Tie Game!");
    }
    }

    public static void main(String[] args){
    new TicTacToeLove();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi I am trying to make a two player tic tac toe game in java I am a beginner

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Look at using an array vs 9 separate variables.

    Use the reference returned by getSource() to call the setText() method in place of using all the if/else if tests against each button reference.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with tic tac toe game
    By ogpg2006 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 07:34 AM
  2. (Beginner/Intermediate) Tic Tac Toe game issues
    By Sylentwolf8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 17th, 2012, 08:03 AM
  3. HOW TO MAKE a TIC TAC TOE GAME in Net beans as a desktop application ?
    By jude_pinas in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2011, 12:51 AM
  4. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  5. Tic Tac Toe = Naming Winning Player & Restarting Program
    By Override in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2010, 12:20 AM