Olá fiz um jogo da velha ebm legal ! ta ai !qualquer duvida é só postar!
import java.awt.;
import java.awt.event.;
import javax.swing.*;
/**
*/
//public class JogoDaVelha extends JFrame implements ActionListener
{
public JButton [][] botoes = {{null,null,null},{null,null,null},{null,null,null}};
public String jdv = “X”;
public JPanel p;
public int control = 0, control_1 = 0;
public int numLinhas = botoes[0].length;
public int numColunas = numLinhas;
public boolean acabou = false;
public ActionEvent myEvent;
public boolean empate = false;
public JLabel od;
public Container container;
public JMenu jogo, ajuda;
public JMenuItem novo, sair, sobre;
public JMenuBar menu;
public int contLV = 0, contCV = 0, indice = 0;
//public String vencedor = “”;
public JogoDaVelha()
{
container = this.getContentPane();
setTitle("Jogo Da Velha OBBPULS ++");
p = new JPanel();
p.setLayout(new GridLayout(3,3));
od = new JLabel(new ImageIcon("OutDoor.jpeg"));
container.add(od, BorderLayout.CENTER);
//Configura a GUI. Os menus, e os botões através de um laço, o que economiza linhas de código rsrs.. Paulo Junior
Toolkit tk = Toolkit.getDefaultToolkit();
setIconImage(tk.getImage("iconJDV.jpeg"));
// TROCA A IMAGEM DO JAVA NA BARRA DE TITULO
Image icon = Toolkit.getDefaultToolkit().getImage("asterisco.gif");
this.setIconImage(icon);
menu = new JMenuBar();
jogo = new JMenu("Jogo");
novo = new JMenuItem("Novo Jogo...");
novo.addActionListener(this);
jogo.add(novo);
jogo.addSeparator();
sair = new JMenuItem("Sair");
sair.addActionListener(this);
jogo.add(sair);
ajuda = new JMenu("Sobre");
sobre = new JMenuItem("Sobre o Jogo da Velha OBBPLUS++");
sobre.addActionListener(this);
ajuda.add(sobre);
menu.add(jogo);
// menu.add(ajuda);
setJMenuBar(menu);
for(int j = 0; j < numLinhas; j++)
{
for(int i = 0; i < botoes[j].length; i++)
{
botoes[j][i] = new JButton("");
botoes[j][i].setBackground(Color.white);
botoes[j][i].setFont(new Font("Times new Romam",Font.PLAIN, 120));
botoes[j][i].setToolTipText("Casa número: " + String.valueOf(i + 1)
+ " da linha " + String.valueOf(j + 1));
botoes[j][i].addActionListener(this);
p.add(botoes[j][i]);
}
}
setBounds(250,100,560,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
this.myEvent = e;
if(e.getSource() == novo)
{
remove(od);
setBounds(200,50,650,550);
container.add(p,BorderLayout.CENTER);
acabou = false;
limpar();
setVisible(true);
}
else if(e.getSource() == sair)
{
System.exit(0);
}
else if(e.getSource() == sobre)
{
JOptionPane.showMessageDialog(null,"JOGO DA VELHA");
}
else
{
configJDV();
}
}
public void configJDV()
{
int cont = 0;
if(!acabou)
{
for(int i = 0; i < botoes.length; i++)
{
for(int j = 0; j < botoes[i].length; j++)
{
if(myEvent.getSource() == botoes[i][j])
{
if(!botoes[i][j].getText().equals(""))
{
JOptionPane.showMessageDialog(this,"Jogue em outra posição...",
"[.:JOGUE EM OUTRO LUGAR!!!:.]",JOptionPane.ERROR_MESSAGE);
}
else
{
if(this.jdv == "X")
{
this.jdv = "0";
botoes[i][j].setForeground(Color.blue);
}
else
{
this.jdv = "X";
botoes[i][j].setForeground(Color.red);
}
botoes[i][j].setText(this.jdv);
vencedor = this.jdv;
}
}
}
}
/*********************************************************
* VERIFICA LINHAS
*********************************************************/
if(
//Verfifica primeira linha
botoes[0][0].getText().equals(botoes[0][1].getText()) &&
botoes[0][1].getText().equals(botoes[0][2].getText()) &&
!botoes[0][0].getText().equals(""))
{
pintarLinha(0,vencedor);
}
else if(//Verfifica segunda linha
botoes[1][0].getText().equals(botoes[1][1].getText()) &&
botoes[1][1].getText().equals(botoes[1][2].getText()) &&
!botoes[1][0].getText().equals(""))
{
pintarLinha(1,vencedor);
}
else if(//Verfifica Terceira linha
botoes[2][0].getText().equals(botoes[2][1].getText()) &&
botoes[2][1].getText().equals(botoes[2][2].getText()) &&
!botoes[2][0].getText().equals(""))
{
pintarLinha(2,vencedor);
}
/*********************************************************
* VERIFICA LINHAS
*********************************************************/
else if(
//Verfifica primeira coluna
botoes[0][0].getText().equals(botoes[1][0].getText()) &&
botoes[1][0].getText().equals(botoes[2][0].getText()) &&
!botoes[0][0].getText().equals(""))
{
pintarColuna(0,vencedor);
}
else if(//Verfifica segunda coluna
botoes[0][1].getText().equals(botoes[1][1].getText()) &&
botoes[1][1].getText().equals(botoes[2][1].getText()) &&
!botoes[0][1].getText().equals(""))
{
pintarColuna(1,vencedor);
}
else if(//Verfifica Terceira coluna
botoes[0][2].getText().equals(botoes[1][2].getText()) &&
botoes[1][2].getText().equals(botoes[2][2].getText()) &&
!botoes[0][2].getText().equals(""))
{
pintarColuna(2,vencedor);
}
else if(//Verfica as diagonais
botoes[0][0].getText().equals(botoes[1][1].getText()) &&
botoes[1][1].getText().equals(botoes[2][2].getText()) &&
!botoes[0][0].getText().equals(""))
{
pintarDiagonalPrinc();
}
else if(//Verfica as diagonais
botoes[2][0].getText().equals(botoes[0][2].getText()) &&
botoes[0][2].getText().equals(botoes[1][1].getText()) &&
!botoes[2][0].getText().equals(""))
{
pintarDiagonalSec();
}
else
{
//Verifica se o jogo terminou empatado
for(int i = 0; i < botoes.length; i++)
{
for(int j = 0; j < botoes.length; j++)
{
if(!botoes[i][j].getText().equals(""))
{
cont++;
}
}
if(cont == 9)
{
empatar();
}
}
}
}
else
{
JOptionPane.showMessageDialog(this,"[.:Reinicie o Jogo…:.]",“JOGO FINALIZADO!”,
JOptionPane.ERROR_MESSAGE);
}
}
public void empatar()
{
//Acaba o jogo!
this.acabou = true;
//Pinta de forma especial, para indicar o caso de empate!
for(int i = 0; i < botoes.length; i++)
{
for(int j = 0; j < botoes[i].length; j++)
{
botoes[i][j].setBackground(Color.black);
botoes[i][j].setForeground(Color.gray);
}
}
//Exibe mensagem de empate!
JOptionPane.showMessageDialog(this,"[.:Jogo Acabou empatado!:.]",
"EMPATE",JOptionPane.WARNING_MESSAGE);
}
public void pintarLinha(int indice, String vencedor)
{
//Acaba o jogo!
this.acabou = true;
//Pinta a linha do vencedor!
for(int i = 0; i < botoes[indice].length; i++)
{
botoes[indice][i].setBackground(Color.blue);
botoes[indice][i].setForeground(Color.white);
}
//Dá os parabéns ao vencedor!
parabéns();
}
public void pintarColuna(int indice, String vencedor)
{
//Acaba o jogo!
this.acabou = true;
//Pinta a linha do vencedor!
for(int i = 0; i < botoes[indice].length; i++)
{
botoes[i][indice].setBackground(Color.blue);
botoes[i][indice].setForeground(Color.white);
}
//Dá os parabéns ao vencedor!
parabéns();
}
public void pintarDiagonalPrinc()
{
//Acaba o jogo!
this.acabou = true;
for(int i = 0; i < botoes.length; i++)
{
for(int j = 0; j < botoes[1].length; j++)
{
if(j == i && j == 1)
{
botoes[i-1][j-1].setBackground(Color.blue);
botoes[i][j].setBackground(Color.blue);
botoes[i+1][j+1].setBackground(Color.blue);
botoes[i-1][j-1].setForeground(Color.white);
botoes[i][j].setForeground(Color.white);
botoes[i+1][j+1].setForeground(Color.white);
}
}
}
//Dá os parabéns ao vencedor!
parabéns();
}
public void pintarDiagonalSec()
{
//Acaba o jogo!
this.acabou = true;
//Pinta a diagonal principal
for(int i = 0; i < botoes.length; i++)
{
for(int j = 0; j < botoes[i].length; j++)
{
if(j == i && j == 1)
{
botoes[0][i*2].setBackground(Color.blue);
botoes[i*2][0].setBackground(Color.blue);
botoes[i][j].setBackground(Color.blue);
botoes[0][i*2].setForeground(Color.white);
botoes[i*2][0].setForeground(Color.white);
botoes[i][j].setForeground(Color.white);
}
}
}
//Dá os parabéns ao vencedor!
parabéns();
}
public boolean estáAcabado()
{
if(acabou)
return acabou;
else
acabou = false;
return acabou;
}
public void parabéns()
{
JOptionPane.showMessageDialog(this,vencedor + " Ganhou!",“PARABÉNS!!!”,
JOptionPane.WARNING_MESSAGE);
}
public void limpar()
{
for(int i = 0; i < botoes.length; i++)
{
for(int j = 0; j < botoes[i].length; j++)
{
botoes[i][j].setText("");
botoes[i][j].setBackground(Color.white);
}
}
}
public void setJDV()
{
if(this.control%2 == 0)
{
this.jdv = “X”;
}
else
this.jdv = "0";
}
/Desenvolvimento Paulo Roberto Luiz Junior!/
public static void main(String args[])
{
//new JogoDaVelha();
}
}