import javax.swing.*; import java.awt.*; import java.awt.event.*; class cRodada { static private int caracter; static private int matriz[][]; cRodada () { int x=0,y=0; matriz = new int [5][5]; for (x=0;x <= 2;x++) for (y=0;y<=2;y++) matriz[x][y]= x+3*(y*4); } void atribuirMatriz(int x,int y,int caract) { matriz[x][y] = caract; } int obterChar() { return caracter; } void troca() { if (caracter == 1) caracter = 0; else caracter = 1; } int verifica () { int x=0; for (x=0;x <= 2;x++) { if (matriz[0][x] == matriz[1][x] && matriz[2][x] == matriz[0][x] ) return 1; if (matriz[x][0] == matriz[x][1] && matriz[x][2] == matriz[x][0] ) return 1; } if (matriz[0][0] == matriz[1][1] && matriz[1][1] == matriz[2][2] ) return 1; if (matriz[0][2] == matriz[1][1] && matriz[1][1] == matriz[2][0] ) return 1; return 0; } } class Formulario extends JDialog implements ActionListener, ItemListener { private JButton btn1; private JButton btn2; private JButton btn3; private JButton btn4; private JButton btn5; private JButton btn6; private JButton btn7; private JButton btn8; private JButton btn9; public void actionPerformed(ActionEvent e) { cRodada usuario; usuario =new cRodada(); if (e.getSource() == btn1) { if (btn1.getText()!="X" && btn1.getText()!="O") { if (usuario.obterChar()==1) btn1.setText("X"); else btn1.setText("O"); usuario.atribuirMatriz(0,0,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn2) { if (btn2.getText()!="X" && btn2.getText()!="O") { if (usuario.obterChar()==1) btn2.setText("X"); else btn2.setText("O"); usuario.atribuirMatriz(0,1,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn3) { if (btn3.getText()!="X" && btn3.getText()!="O") { if (usuario.obterChar()==1) btn3.setText("X"); else btn3.setText("O"); usuario.atribuirMatriz(0,2,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn4) { if (btn4.getText()!="X" && btn4.getText()!="O") { if (usuario.obterChar()==1) btn4.setText("X"); else btn4.setText("O"); usuario.atribuirMatriz(1,0,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn5) { if (btn5.getText()!="X" && btn5.getText()!="O") { if (usuario.obterChar()==1) btn5.setText("X"); else btn5.setText("O"); usuario.atribuirMatriz(1,1,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn6) { if (btn6.getText()!="X" && btn6.getText()!="O") { if (usuario.obterChar()==1) btn6.setText("X"); else btn6.setText("O"); usuario.atribuirMatriz(1,2,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn7) { if (btn7.getText()!="X" && btn7.getText()!="O") { if (usuario.obterChar()==1) btn7.setText("X"); else btn7.setText("O"); usuario.atribuirMatriz(2,0,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn8) { if (btn8.getText()!="X" && btn8.getText()!="O") { if (usuario.obterChar()==1) btn8.setText("X"); else btn8.setText("O"); usuario.atribuirMatriz(2,1,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } if (e.getSource() == btn9) { if (btn9.getText()!="X" && btn9.getText()!="O") { if (usuario.obterChar()==1) btn9.setText("X"); else btn9.setText("O"); usuario.atribuirMatriz(2,2,usuario.obterChar()); if(usuario.verifica() == 1) { JOptionPane.showMessageDialog(this,"Usuario Ganhou!!"); System.exit(0); } usuario.troca(); } } } public void itemStateChanged(ItemEvent e) { } public Formulario() { this.setTitle("Programa 1"); //this.setLayout(null); this.getContentPane().setLayout(null); setSize(190,190); setLocation((1024-getWidth())/2,(768-getHeight())/2); //Botao 1 btn1 = new JButton(" "); btn1.setSize(50,50); btn1.setLocation(10,10); btn1.addActionListener(this); getContentPane().add(btn1); //Botao 2 btn2= new JButton(" "); btn2.setSize(50,50); btn2.setLocation(60,10); btn2.addActionListener(this); getContentPane().add(btn2); //Botao 3 btn3= new JButton(" "); btn3.setSize(50,50); btn3.setLocation(110,10); btn3.addActionListener(this); getContentPane().add(btn3); //Botao 4 btn4= new JButton(" "); btn4.setSize(50,50); btn4.setLocation(10,60); btn4.addActionListener(this); getContentPane().add(btn4); //Botao 5 btn5 = new JButton(" "); btn5.setSize(50,50); btn5.setLocation(60,60); btn5.addActionListener(this); getContentPane().add(btn5); //Botao 6 btn6= new JButton(" "); btn6.setSize(50,50); btn6.setLocation(110,60); btn6.addActionListener(this); getContentPane().add(btn6); //Botao 7 btn7= new JButton(" "); btn7.setSize(50,50); btn7.setLocation(10,110); btn7.addActionListener(this); getContentPane().add(btn7); //Botao 8 btn8= new JButton(" "); btn8.setSize(50,50); btn8.setLocation(60,110); btn8.addActionListener(this); getContentPane().add(btn8); //Botao 9 btn9= new JButton(" "); btn9.setSize(50,50); btn9.setLocation(110,110); btn9.addActionListener(this); getContentPane().add(btn9); } } public class exercicio5 { public static void main(String s[]) { Formulario frm = new Formulario(); frm.show(); } }