Results 1 to 2 of 2
  1. #1
    HELPME2 is offline Member
    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Default Tic Tac Toe Code

    Above my tic tac toe grid, I want to be able to write if it is X`s turn or O`s turn. And the name of the game `Tic Tac Toe`

    Java Code:
    import java.awt.*; 
    import java.awt.event.*;
    import javax.swing.*; 
    import java.lang.String;
    import java.awt.Font;
    import java.awt.Color;
    
    public class NewTicTacToe implements ActionListener { 
      
        private JFrame window = new JFrame("Tic-Tac-Toe"); 
        private JButton button[] = new JButton[10]; 
        private String letter = ""; 
        private boolean winner = false; 
        public int turn = 1;
        
        public NewTicTacToe(){ 
        /*Create Window*/ 
        window.setPreferredSize(new Dimension(400,400)); 
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        window.setLayout(new GridLayout(3,3));
        
        /*Add Buttons To The Window*/ 
        for(int i = 1; i<=9; i++){ 
                button[i] = new JButton(); 
                window.add(button[i]); 
                button[i].addActionListener(this); 
                button[i].setBackground(Color.BLACK); 
        } 
        /*Make The Window Visible*/ 
        window.setVisible(true); 
        window.pack(); 
        } 
        
        public void actionPerformed(ActionEvent a) { 
          
                if (turn == 1 || turn==3 || turn==5 || turn==7 || turn== 9)
                { 
                   for(int i = 1; i<= 9; i++){ 
                   if(a.getSource() == button[i]){ 
                   button[i].setText("X"); 
                   button[i].setEnabled(true); 
                   button[i].setFont(new Font("Sans Serif", Font.BOLD, 75));
                   button[i].setForeground(new Color(30, 144, 255));
                   } 
                }
                   }
                
                else
                { 
                   for(int i = 1; i<= 9; i++){ 
                   if(a.getSource() == button[i]){ 
                   button[i].setText("O"); 
                   button[i].setEnabled(true); 
                   button[i].setFont(new Font("Sans Serif", Font.BOLD, 75));
                   button[i].setForeground(Color.YELLOW);
                 } 
                } }
          
          turn++; 
          
        //Finds out who won 
          
          if(turn % 2 == 0) 
            letter = "X"; 
          else 
            letter = "O"; 
          
        //Horizontal win 1
        if(button[1].getText()==button[2].getText() && button[2].getText() == button[3].getText() && button[3].getText()!="" )
        {
          winner=true;
          button[1].setBackground(new Color(35, 35, 35));
          button[2].setBackground(new Color(35, 35, 35));
          button[3].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }
        //Horizontal win 2
        else if(button[4].getText()==button[5].getText() && button[5].getText() == button[6].getText() && button[4].getText()!="" )
        {
          winner=true;
          button[4].setBackground(new Color(35, 35, 35));
          button[5].setBackground(new Color(35, 35, 35));
          button[6].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }
        //Horizontal win 3
        else if(button[7].getText()==button[8].getText() && button[8].getText() == button[9].getText() && button[7].getText()!="" )
        {
          winner=true;
          button[7].setBackground(new Color(35, 35, 35));
          button[8].setBackground(new Color(35, 35, 35));
          button[9].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }  
        //Vertical win 1
        else if(button[1].getText()==button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText()!="" )
        {
          winner=true;
          button[1].setBackground(new Color(35, 35, 35));
          button[4].setBackground(new Color(35, 35, 35));
          button[7].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }
        //Vertical win 2
        else if(button[2].getText()==button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText()!="" )
        {
          winner=true;
          button[2].setBackground(new Color(35, 35, 35));
          button[5].setBackground(new Color(35, 35, 35));
          button[8].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }
        //Vertical win 3
        else if(button[3].getText()==button[6].getText() && button[6].getText() == button[9].getText() && button[3].getText()!="" )
        {
          winner=true;
          button[3].setBackground(new Color(35, 35, 35));
          button[6].setBackground(new Color(35, 35, 35));
          button[9].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }  
        //Diagonal win 1
        else if(button[1].getText()==button[5].getText() && button[5].getText() == button[9].getText() && button[1].getText()!="" )
        {
          winner=true;
          button[1].setBackground(new Color(35, 35, 35));
          button[5].setBackground(new Color(35, 35, 35));
          button[9].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }
        //Diagonal win 2
        else if(button[3].getText()==button[5].getText() && button[5].getText() == button[7].getText() && button[3].getText()!="" )
        {
          winner=true;
          button[3].setBackground(new Color(35, 35, 35));
          button[5].setBackground(new Color(35, 35, 35));
          button[7].setBackground(new Color(35, 35, 35));
          JOptionPane.showMessageDialog(null, letter+ " wins!");
          System.exit(0);
        }
        else if(turn==10 && winner==false)
        { 
          JOptionPane.showMessageDialog(null, "Match is a draw");
          System.exit(0);
        }}
        
        public static void main(String[] args){ 
        new NewTicTacToe(); 
    
        } 
    }

  2. #2
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Tic Tac Toe Code

    Make it so it replaces the number or whatever you have on the board with X or an O. I had a Tic Tac Toe example somewhere on my computer but missplaced it. found it.

    Edit this concept into your button concept. If it presses a button then switch it out with this.


    Java Code:
    switch(keyboard.next().charAt(0))
    			{
    			case '1':
    				if(table[0][0]=='1')
    				{
    					table[0][0] = piece;
    				}
    				break;
    Last edited by Army; 01-18-2013 at 01:10 AM.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  4. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •