Hi I need help with my tictactoe game code (player vs computer). It has 2 difficulty modes, easy and medium. Its almost done but I need better plays from the computer, if someone can improve my code I will really appreciate it.
Thx
Code:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Gato extends JFrame implements ActionListener{
/**
* Se declaran e inician los botones, vectores, barras de menu, etiquetas, campos, areas de texto, entre otrs que van a tener los eventos
*/
JPanel principal;
JPanel segundo;
JPanel tercero;
JButton modo1 = new JButton("Modo Facil");
JButton modo2 = new JButton("Modo Intermedio");
JButton c1 = new JButton("");
JButton c2 = new JButton("");
JButton c3 = new JButton("");
JButton c4 = new JButton("");
JButton c5 = new JButton("");
JButton c6 = new JButton("");
JButton c7 = new JButton("");
JButton c8 = new JButton("");
JButton c9 = new JButton("");
JLabel bienvenida = new JLabel("Bienvenidos al Gato");
JLabel p1 = new JLabel("Nombre del Jugador");
JMenuBar barraMenu;
JMenu archivo;
JMenu tipodejuego;
JMenuItem reiniciar = new JMenuItem("Reiniciar");
JMenuItem salir = new JMenuItem("Salir");
JMenuItem modofacil = new JMenuItem("Modo Facil");
JMenuItem modointermedio = new JMenuItem("Modo Intermedio");
JMenuItem status = new JMenuItem("Status de juegos");
Container contenedor = getContentPane();
String jugador = "";
String jugador2 = "";
Vector nombres = new Vector();
Vector ganados = new Vector();
Vector perdidos = new Vector();
Vector empatados = new Vector();
String x = "X";
String y = "O";
int j = 0;//Control para saber que jugador esta en turno
int r = 0;//No. de jugadres en el historial
int gj1 = 0;//Contador de juegos ganados por jugador 1
int gj2 = 0;//Contador de juegos ganados por jugador 2
int pj1 = 0;//Contador de juegos perdidos por jugador 1
int pj2 = 0;//Contador de juegos perdidos por jugador 2
int ej1 = 0;//Contador de juegos empatados por jugador 1
int ej2 = 0;//Contador de juegos empatados por jugador 2
boolean c = true;
int espacios[] = {0,0,0,0,0,0,0,0,0};
public Gato (String titulo){
/**
* Se llama al constructor padre
*/
super(titulo);
/**
* Se acomodan los componentes que se declararon anteriormente, se les asigna color y tamano
*/
getContentPane().setLayout(null);
barraMenu = new JMenuBar();
archivo = new JMenu("Archivo",true);
archivo.add(reiniciar);
archivo.add(status);
archivo.add(salir);
tipodejuego = new JMenu("Tipo de juego",true);
tipodejuego.add(modofacil);
tipodejuego.add(modointermedio);
barraMenu.add(archivo);
barraMenu.add(tipodejuego);
this.setJMenuBar(barraMenu);
reiniciar.addActionListener(this);
status.addActionListener(this);
salir.addActionListener(this);
modofacil.addActionListener(this);
modointermedio.addActionListener(this);
modo1.addActionListener(this);
modo2.addActionListener(this);
c1.addActionListener(this);
c2.addActionListener(this);
c3.addActionListener(this);
c4.addActionListener(this);
c5.addActionListener(this);
c6.addActionListener(this);
c7.addActionListener(this);
c8.addActionListener(this);
c9.addActionListener(this);
contenedor.setLayout(new BorderLayout());
principal = new JPanel();
principal.setLayout(new FlowLayout());
principal.add(bienvenida);
principal.setSize(400,30);
segundo = new JPanel();
segundo.setLayout(new GridLayout(2,2));
segundo.setBackground(Color.BLACK);
segundo.setSize(200,270);
segundo.add(modo1);
segundo.add(modo2);
tercero = new JPanel();
tercero.setLayout(new FlowLayout());
tercero.setBackground(Color.PINK);
tercero.add(p1);
p1.setVisible(false);
tercero.setSize(200,270);
contenedor.add(principal,BorderLayout.PAGE_START);
contenedor.add(segundo,BorderLayout.WEST);
contenedor.add(tercero,BorderLayout.CENTER);
}
/**
* Se crea el metodo actionPerformed que realiza la accion que indica cada boton y cada opcion de los menus,
* es decir que se escucha los actionlisteners de uno
* Al seleccionar un determinado boton o opcion de los menus,
* este metodo lo remite a la accion que indican los metodos que se declaran despues
*/
public void actionPerformed(ActionEvent evento){
if (evento.getSource()==reiniciar){
Reiniciar();
}
if (evento.getSource()==salir){
System.exit(0);
}
if (evento.getSource()==modofacil || evento.getSource()==modo1){
gj1=0;pj1=0;ej1=0;gj2=0;pj2=0;ej2=0;
modointermedio.setEnabled(true);
modofacil.setEnabled(false);
jugador2="COMPUTADORA";
try{
jugador = JOptionPane.showInputDialog(null, "Nombre del Jugador","Jugador",JOptionPane.QUESTION_MESSAGE).toUpperCase();
}
catch(NullPointerException e){
jugador = "JUGADOR";
}
bienvenida.setText("Turno de "+jugador);
contenedor.remove(tercero);
contenedor.remove(segundo);
segundo.removeAll();
segundo.setSize(400,270);
contenedor.add(segundo,BorderLayout.CENTER);
c = true;
construccionJuego();
Activar();
Limpiar();
j =1;
p1 = null;
}
if (evento.getSource()==modointermedio || evento.getSource()==modo2){
gj1=0;pj1=0;ej1=0;gj2=0;pj2=0;ej2=0;
modofacil.setEnabled(true);
modointermedio.setEnabled(false);
jugador2="COMPUTADORA";
try{
jugador = JOptionPane.showInputDialog(null,"Nombre del Jugador","Jugador",JOptionPane.QUESTION_MESSAGE).toUpperCase();
}
catch(NullPointerException e){
jugador = "JUGADOR";
}
bienvenida.setText("Turno de "+jugador);
contenedor.remove(tercero);
contenedor.remove(segundo);
segundo.removeAll();
segundo.setSize(400,270);
contenedor.add(segundo,BorderLayout.CENTER);
c = false;
construccionJuego();
Limpiar();
Activar();
p1 = null;
j = 1;
}
if (evento.getSource()==status){
Estado();
}
if (evento.getSource()==c1){
if(c){
if(j==1){
c1.setText(x);
espacios[0]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c1.setText(y);
espacios[0]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c1.setText(x);
espacios[0]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c1.setEnabled(false);
}
if (evento.getSource()==c2){
if(c){
if(j==1){
c2.setText(x);
espacios[1]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c2.setText(y);
espacios[1]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c2.setText(x);
espacios[1]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c2.setEnabled(false);
}
if (evento.getSource()==c3){
if(c){
if(j==1){
c3.setText(x);
espacios[2]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c3.setText(y);
espacios[2]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c3.setText(x);
espacios[2]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c3.setEnabled(false);
}
if (evento.getSource()==c4){
if(c){
if(j==1){
c4.setText(x);
espacios[3]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c4.setText(y);
espacios[3]=2;
bienvenida.setText("Turno de jugador");
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c4.setText(x);
espacios[3]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c4.setEnabled(false);
}
if (evento.getSource()==c5){
if(c){
if(j==1){
c5.setText(x);
espacios[4]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c5.setText(y);
espacios[4]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c5.setText(x);
espacios[4]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c5.setEnabled(false);
}
if (evento.getSource()==c6){
if(c){
if(j==1){
c6.setText(x);
espacios[5]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c6.setText(y);
espacios[5]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c6.setText(x);
espacios[5]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c6.setEnabled(false);
}
if (evento.getSource()==c7){
if(c){
if(j==1){
c7.setText(x);
espacios[6]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c7.setText(y);
espacios[6]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c7.setText(x);
espacios[6]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
turnoComputadora();
bienvenida.setText("Turno de "+jugador);
if(Gano(2)) yaGano(2);
else Empate();
}
c7.setEnabled(false);
}
if (evento.getSource()==c8){
if(c){
if(j==1){
c8.setText(x);
espacios[7]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c8.setText(y);
espacios[7]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
}
else{
c8.setText(x);
espacios[7]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c8.setEnabled(false);
}
if (evento.getSource()==c9){
if(c){
if(j==1){
c9.setText(x);
espacios[8]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(j)) yaGano(j);
else Empate();
j=2;
}
else {
c9.setText(y);
espacios[8]=2;
bienvenida.setText("Turno de "+jugador);
if(Gano(j)) yaGano(j);
else Empate();
j=1;
}
c9.setEnabled(false);
}
else{
c9.setText(x);
espacios[8]=1;
bienvenida.setText("Turno de "+jugador2);
if(Gano(1)) yaGano(1);
else Empate();
bienvenida.setText("Turno de "+jugador);
turnoComputadora();
if(Gano(2)) yaGano(2);
else Empate();
}
c9.setEnabled(false);
}
}
/**
* Se establecen los metodos que se van a asociar a cada evento, ya sea reiniciar, limpiar, salir, construir juego, entre otros
* Se les asignan a cada metodo las caracteristicas del mismo
*/
public void Reiniciar(){
c1.setText("");
c2.setText("");
c3.setText("");
c4.setText("");
c5.setText("");
c6.setText("");
c7.setText("");
c8.setText("");
c9.setText("");
c1.setEnabled(true);
c2.setEnabled(true);
c3.setEnabled(true);
c4.setEnabled(true);
c5.setEnabled(true);
c6.setEnabled(true);
c7.setEnabled(true);
c8.setEnabled(true);
c9.setEnabled(true);
for (int i=0;i<9;i++){
espacios[i]=0;
}
j=1;
}
public void construccionJuego(){
contenedor.remove(segundo);
segundo.removeAll();
segundo.setLayout(new GridLayout(3,3,10,10));
segundo.add(c1);
segundo.add(c2);
segundo.add(c3);
segundo.add(c4);
segundo.add(c5);
segundo.add(c6);
segundo.add(c6);
segundo.add(c7);
segundo.add(c8);
segundo.add(c9);
contenedor.add(segundo,BorderLayout.CENTER);
}
public boolean Gano(int j){
if(espacios[0]==espacios[1])
if(espacios[1]==espacios[2])
if (espacios[2]==j) return true;
if(espacios[3]==espacios[4])
if(espacios[4]==espacios[5])
if (espacios[5]==j)return true;
if(espacios[6]==espacios[7])
if(espacios[7]==espacios[8])
if (espacios[8]==j)return true;
if(espacios[0]==espacios[3])
if(espacios[3]==espacios[6])
if (espacios[6]==j)return true;
if(espacios[1]==espacios[4])
if(espacios[4]==espacios[7])
if (espacios[7]==j)return true;
if(espacios[2]==espacios[5])
if(espacios[5]==espacios[8])
if (espacios[8]==j)return true;
if(espacios[0]==espacios[4])
if(espacios[4]==espacios[8])
if (espacios[8]==j)return true;
if(espacios[2]==espacios[4])
if(espacios[4]==espacios[6])
if (espacios[6]==j)return true;
return false;
}
public void yaGano(int jugador){
if(c){
if (jugador==1){
bienvenida.setText("G A N A S T E ! ! ! F E L I C I D A D E S ! ! ! "+jugador);
gj1++;
pj2++;
}
else{
bienvenida.setText("G A N A S T E ! ! ! F E L I C I D A D E S ! ! ! "+jugador2);
gj2++;
pj1++;
}
}
else{
if(jugador==1){
bienvenida.setText("G A N A S T E ! ! ! F E L I C I D A D E S ! ! ! "+jugador);
gj1++;
pj2++;
principal.repaint();
}
else{
bienvenida.setText("P E R D I S T E ! ! ! "+jugador);
pj1++;
gj2++;
principal.repaint();
}
}
}
public void Empate(){
boolean empatados = true;
for (int i=0;i<9;i++){
if (espacios[i]==0) empatados = false;
}
if (empatados){
bienvenida.setText("E M P A T E ! ! !");
ej1++;
ej2++;
}
}
public void Estado(){
JTextArea areaSalida = new JTextArea();
String salida = "Nombre\tGanados\tPerdidos\tEmpatados\n";
salida+=jugador+"\t"+gj1+"\t"+pj1+"\t"+ej1+"\n";
salida+=jugador2+"\t"+gj2+"\t"+pj2+"\t"+ej2+"\n";
areaSalida.setText( salida );
JOptionPane.showMessageDialog(null,areaSalida,"Status de juegos",JOptionPane.INFORMATION_MESSAGE);
}
public void Activar(){
c1.setEnabled(true);
c2.setEnabled(true);
c3.setEnabled(true);
c4.setEnabled(true);
c5.setEnabled(true);
c6.setEnabled(true);
c7.setEnabled(true);
c8.setEnabled(true);
c9.setEnabled(true);
}
public void Limpiar(){
c1.setText("");
c2.setText("");
c3.setText("");
c4.setText("");
c5.setText("");
c6.setText("");
c7.setText("");
c8.setText("");
c9.setText("");
for(int i=0;i<9;i++){
espacios[i]=0;
}
}
public void turnoComputadora(){
if(espacios[4]==0){
espacios[4]=2;
c5.setText(y);
c5.setEnabled(false);
}
else if(espacios[0]==0){
espacios[0]=2;
c1.setText(y);
c1.setEnabled(false);
}
else if(espacios[2]==0) {
espacios[2]=2;
c3.setText(y);
c3.setEnabled(false);
}
else if(espacios[6]==0) {
espacios[6]=2;
c7.setText(y);
c7.setEnabled(false);
}
else if(espacios[8]==0) {
espacios[8]=2;
c9.setText(y);
c9.setEnabled(false);
}
else if(espacios[1]==0) {
espacios[1]=2;
c2.setText(y);
c2.setEnabled(false);
}
else if(espacios[3]==0) {
espacios[3]=2;
c4.setText(y);
c4.setEnabled(false);
}
else if(espacios[5]==0) {
espacios[5]=2;
c6.setText(y);
c6.setEnabled(false);
}
else if(espacios[7]==0) {
espacios[7]=2;
c8.setText(y);
c8.setEnabled(false);
}
}
}

