I developed a program with the cat game (also called tic-tac-toe or trés online) but I only have declared the ways to win, but I do not know how to ask or put a if
where I say nobody won.
This is my code:
package gato;
import java.awt.*;
import javax.swing.*;
public class Gato extends JFrame implements ActionListener{
JButton iniciar = new JButton("Iniciar Juego.");
JButton tablero [][];
JLabel fondo;
String jugador1,jugador2;
int turno=-1;
JLabel mensaje = new JLabel("Bienvenidos al Juego.");
JLabel mensaje1 = new JLabel();
Color colorB;
public Gato() {
this.setLayout(null);
mensaje.setBounds(100, 20, 400, 30);
mensaje.setFont(new Font("GARAGE IMPERIO", Font.PLAIN, 20));
mensaje.setForeground(Color.BLACK);
this.add(mensaje);
mensaje1.setBounds(150, 60, 500, 30);
mensaje1.setFont(new Font("GARAGE IMPERIO", Font.PLAIN, 20));
mensaje1.setForeground(Color.BLACK);
this.add(mensaje1);
iniciar.setBounds(125, 400,150,30);
iniciar.addActionListener(this);
this.add(iniciar);
tablero = new JButton [3][3];
for(int i=0 ; i< 3 ; i++) {
for(int j=0 ; j< 3 ; j++) {
tablero[i][j] = new JButton();
tablero[i][j].setBounds((i+1)*80,(j+1)*80,80,80);
this.add(tablero[i][j]);
tablero[i][j].addActionListener(this);
}
}
colorB = tablero[0][0].getBackground();
fondo = new JLabel();
fondo.setSize(400, 500);
fondo.setIcon(new ImageIcon("src/img/fondo.jpg"));
fondo.setVisible(true);
add(fondo);
}
public static void main(String[] args) {
Gato ventana = new Gato();
ventana.setDefaultCloseOperation(3);
ventana.setSize(400, 500);
ventana.setLocationRelativeTo(null);
ventana.setResizable(false);
ventana.setTitle("JUEGO DEL GATO");
ventana.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==iniciar) {
turno = 0;
JOptionPane.showMessageDialog(this, "Iniciando el juego");
jugador1 =JOptionPane.showInputDialog(this,"Escribe el nombre del 1°er jugador");
jugador2 =JOptionPane.showInputDialog(this,"Escribe el nombre del 2°do jugador");
mensaje.setText("Turno del jugador :" + jugador1);
limpiar();
}else {
JButton boton = (JButton) e.getSource();
if(turno == 0) {
if(boton.getText().equals("")) {
boton.setBackground(Color.cyan);
boton.setFont(new Font("STENCIL", Font.PLAIN, 40));
boton.setText("X");
boton.setEnabled(false);
turno = 1;
mensaje.setText("Turno del jugador: " + jugador2);
mensaje1.setText("Sigue O");
}
}else {
if(turno == 1) {
if(boton.getText().equals("")) {
boton.setBackground(Color.blue);
boton.setText("O");
boton.setFont(new Font("STENCIL", Font.PLAIN, 40));
boton.setEnabled(false);
turno = 0;
mensaje.setText("Turno del jugador: " + jugador1);
mensaje1.setText("Sigue X");
}
}
}
verificar();
}
}
public void verificar() {
int ganador = 0;
for(int i=0 ; i< 3 ; i++) {
if(tablero[0][i].getText().equals("X") && tablero[1][i].getText().equals("X") && tablero[2][i].getText().equals("X")){
ganador=1;
if(ganador==1) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador1 + " HAZ GANADO");
}
}
if(tablero[i][0].getText().equals("X") && tablero[i][1].getText().equals("X") && tablero[i][2].getText().equals("X")){
ganador=1;
if(ganador==1) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador1 + " HAZ GANADO");
}
}
if(tablero[0][0].getText().equals("X") && tablero[1][1].getText().equals("X") && tablero[2][2].getText().equals("X")){
ganador=1;
if(ganador==1) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador1 + " HAZ GANADO");
}
}
if(tablero[0][2].getText().equals("X") && tablero[1][1].getText().equals("X") && tablero[2][0].getText().equals("X")){
ganador=1;
if(ganador==1) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador1 + " HAZ GANADO");
}
}
if(tablero[0][2].getText().equals("X") && tablero[1][1].getText().equals("X") && tablero[2][0].getText().equals("X")){
ganador=1;
if(ganador==1) {
JOptionPane.showMessageDialog(this,"EMPATE");
}
}
for(int j=0 ; j< 3 ; j++) {
if(tablero[0][j].getText().equals("O") && tablero[1][j].getText().equals("O") && tablero[2][j].getText().equals("O")){
ganador=2;
if(ganador==2) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador2 + " HAZ GANADO");
}
}
if(tablero[j][0].getText().equals("O") && tablero[j][1].getText().equals("O") && tablero[j][2].getText().equals("O")){
ganador=2;
if(ganador==2) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador2 + " HAZ GANADO");
}
}
if(tablero[0][0].getText().equals("O") && tablero[1][1].getText().equals("O") && tablero[2][2].getText().equals("O")){
ganador=2;
if(ganador==2) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador2 + " HAZ GANADO");
}
}
if(tablero[0][2].getText().equals("O") && tablero[1][1].getText().equals("O") && tablero[2][0].getText().equals("O")){
ganador=2;
}
if(ganador==1) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador1 + " HAZ GANADO");
bloquear();
}
if(ganador==2) {
JOptionPane.showMessageDialog(this,"FELICIDADES JUGADOR " + jugador2 + " HAZ GANADO");
bloquear();
}
}
}
}
public void bloquear() {
for(int i=0 ; i< 3 ; i++) {
for(int j=0 ; j< 3 ; j++) {
tablero[i][j].setEnabled(false);
}
}
}
public void limpiar() {
for(int i=0 ; i< 3 ; i++) {
for(int j=0 ; j< 3 ; j++) {
tablero[i][j].setEnabled(true);
tablero[i][j].setText("");
tablero[i][j].setBackground(colorB);
}
}
}
}