Results 1 to 2 of 2
Thread: Tic Tac Toe Code
- 01-17-2013, 11:56 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 13
- Rep Power
- 0
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(); } }
- 01-18-2013, 01:08 AM #2
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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
-
this code is not working I Dont know why ? Public void close the last code!!
By lukote04 in forum New To JavaReplies: 1Last Post: 03-25-2012, 02:40 AM -
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks