Need help with tic tac toe game

• August 2nd, 2012, 12:31 AM
ogpg2006
Need help with tic tac toe game
Hi everybody!

The reason from this post is to get some help with my program, which is a tic tac toe game. The objective of the program is to make a game between the computer and the human player, in which the player always lose.
The problem is that the computer is marking the X two times in the computer's turn I don't know why it happened.

Code :

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.Math.*;

public class gato2 extends Applet implements ActionListener{
private Button un, deux, trois, quatre , cinq, six, sept, huit, neuf,one,two, three, four, five,six6,seven,eight,nine, AC,start;
private int x,y,z, uno = 0, dos = 0, tres = 0, cuatro = 0, cinco =0, seis =0, siete =0, ocho=0, nueve=0,k,a2,a3,a4,a5,a6,a7,a8,a9,ac=0,inic=0, um=0, dois=0,três=0, quatro=0, cinco5=0, seis6=0, sete=0, oito=0, nove=0, c0,c2,c3,c4,c5,c6,c7,c8,c9;
public Panel a1,b1,c1,start1;

public void init (){

setLayout(new FlowLayout());
a1 = new Panel (new GridLayout (3,3));
b1 = new Panel (new GridLayout (1,1));
c1 = new Panel (new GridLayout (3,3));
start1= new Panel (new GridLayout(1,1));

start= new Button("Inicio");start.setBackground(Color.green);start.setForeground(Color.WHITE);
un= new Button("1");un.setBackground(Color.BLACK);un.setForeground(Color.WHITE);
deux = new Button("2");deux.setBackground(Color.BLACK);deux.setForeground(Color.WHITE);
trois = new Button("3");trois.setBackground(Color.BLACK);trois.setForeground(Color.WHITE);
quatre = new Button("4");quatre.setBackground(Color.BLACK);quatre.setForeground(Color.WHITE);
cinq = new Button("5");cinq.setBackground(Color.BLACK);cinq.setForeground(Color.WHITE);
six = new Button("6");six.setBackground(Color.BLACK);six.setForeground(Color.WHITE);
sept = new Button ("7");sept.setBackground(Color.BLACK);sept.setForeground(Color.WHITE);
huit = new Button ("8");huit.setBackground(Color.BLACK);huit.setForeground(Color.WHITE);
neuf = new Button ("9");neuf.setBackground(Color.BLACK);neuf.setForeground(Color.WHITE);
one = new Button ("1");one.setBackground(Color.blue);one.setForeground(Color.WHITE);
two = new Button ("2");two.setBackground(Color.blue);two.setForeground(Color.WHITE);
three = new Button ("3");three.setBackground(Color.blue);three.setForeground(Color.WHITE);
four = new Button ("4");four.setBackground(Color.blue);four.setForeground(Color.WHITE);
five = new Button ("5");five.setBackground(Color.blue);five.setForeground(Color.WHITE);
six6 = new Button ("6");six6.setBackground(Color.blue);six6.setForeground(Color.WHITE);
seven = new Button ("7");seven.setBackground(Color.blue);seven.setForeground(Color.WHITE);
eight = new Button ("8");eight.setBackground(Color.blue);eight.setForeground(Color.WHITE);
nine = new Button ("9");nine.setBackground(Color.blue);nine.setForeground(Color.WHITE);
AC = new Button ("AC");AC.setForeground(Color.black);

}

public void actionPerformed(ActionEvent e) {
Object fuente = e.getSource();
k=0;

if(fuente==start){
inic=1;
}

if(fuente== deux){
dos=2;
}

if(fuente== trois){
tres=3;

}

if(fuente== quatre){
cuatro=4;

}

if(fuente== cinq){
cinco=5;

}

if(fuente== six){
seis=6;

}

if(fuente== sept){
siete=7;

}

if(fuente== huit){
ocho=8;

}

if(fuente== neuf){
nueve=9;

}

if(fuente== one){
um=1;
}

if(fuente== two){
dois=2;
}

if(fuente== three){
três=3;
}

if(fuente== four){
quatro=4;
}

if(fuente== five){
cinco5=5;
}

if(fuente== six6){
seis6=6;
}

if(fuente== seven){
sete=7;
}

if(fuente== eight){
oito=8;
}

if(fuente== deux){
nove=9;
}

if(fuente== AC){
uno = 0;
dos = 0;
tres = 0;
cuatro = 0;
cinco =0;
seis =0;
siete =0;
ocho=0;
nueve=0;
um=0;
dois=0;
três=0;
quatro=0;
cinco5=0;
seis6=0;
sete=0;
oito=0;
nove=0;

ac=1;
}

repaint();
}

public void paint(Graphics g) {

/*botón para borrar*/

if (ac==1){
g.setColor(Color.white);
g.fillRect(0,0,900,900);
ac=0;
}

if(dos==2){
g.drawOval(275,75,50,50);
a2=2;
}

if(tres==3){
g.drawOval(360,75,49,49);
a3=3;
}

if(cuatro==4){
g.drawOval(185,160,49,49);
a4=4;
}

if(cinco==5){
g.drawOval(275,160,50,50);
a5=5;
}

if(seis==6){
g.drawOval(360,160,49,49);
a6=6;
}

if(siete==7){
g.drawOval(185,250,49,49);
a7=7;
}

if(ocho==8){
g.drawOval(275,250,49,49);
a8=8;
}

if(nueve==9){
g.drawOval(360,250,49,49);
a9=9;
}

if(um==1){
g.drawLine(175,75,250,135);
g.drawLine(250,75,175,135);
c0=1;
}

if(dois==2){
g.drawLine(250,75,350,135);
g.drawLine(350,75,250,135);
c2=2;
}

if(três==3){
g.drawLine(350,75,425,135);
g.drawLine(425,75,350,135);
c3=3;
}

if(quatro==4){
g.drawLine(175,135,250,235);
g.drawLine(250,135,175,235);
c4=4;
}

if(cinco5==5){
g.drawLine(250,135,350,235);
g.drawLine(350,135,250,235);
c5=5;
}

if(seis6==6){
g.drawLine(350,135,425,235);
g.drawLine(425,135,350,235);
c6=6;
}

if(sete==7){
g.drawLine(175,235,250,300);
g.drawLine(250,235,175,300);
c7=7;
}

if(oito==8){
g.drawLine(250,235,350,300);
g.drawLine(350,235,250,300);
c8=8;
}

if(nove==9){
g.drawLine(350,235,425,300);
g.drawLine(425,235,350,300);
c9=9;
}

/*líneas del gane*/

/*diagonales*/
if((um==1)&&(cinco5==5)&&(nove==9)){
g.drawLine(175,75,425,300);
}

if((três==3)&&(cinco5==5)&&(sete==7)){
g.drawLine(425,75,175,300);
}

/*verticales*/
if((um==1)&&(quatro==4)&&(sete==7)){
g.drawLine(212,75,212,300);
}

if((três==3)&&(seis6==6)&&(nove==9)){
g.drawLine(387,75,387,300);
}

/*horizontales*/
if((um==1)&&(dois==2)&&(três==3)){
g.drawLine(212,105,387,105);
}

if((sete==7)&&(oito==8)&&(nove==9)){
g.drawLine(175,267,425,267);
}

if(inic==1){

/*inicio del juego*/
g.drawLine(250,75,250,300);
g.drawLine(350,75,350,300);
g.drawLine(175,135,425,135);
g.drawLine(175,235,425,235);
um=1;
}

if (a2==2){
cinco5=5;
if (a3==3){
nueve=9;
System.out.println("PERDISTE");
}
if(a4==4){
nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
if(a6==6){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
if(a7==7){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if(a8==8){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if(a9==9){

sete=7;

if(a4==4){

três=3;

g.drawLine(425,75,175,300);
System.out.println("PERDISTE");
}
if(a3==3){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}
if(a6==6){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}
if(a8==8){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}
}
}

if (a3 == 3){

sete=7;

if (a2 ==2){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a5 ==5){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a6 ==6){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a8 ==8){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}
if (a9 ==9){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a4 ==4){

nove=9;

if (a2 == 2){

oito=8;

g.drawLine(175,267,425,267);
System.out.println("PERDISTE");
}

if (a5 ==5){

oito=8;

g.drawLine(175,267,425,267);
System.out.println("PERDISTE");
}

if (a6==6){

oito=8;

g.drawLine(175,267,425,267);
System.out.println("PERDISTE");
}

if (a8 == 8){

cinco5=5;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
}
}

if (a4 == 4){

cinco5=5;

if (a2 ==2){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
if (a3 ==3){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
if (a6 ==6){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
if (a7 ==7){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}
if(a8==8){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if (a9==9){

três=3;

if(a6==6){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a7==7){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a8==8){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a2==2){

sete=7;

g.drawLine(425,75,175,300);
System.out.println("PERDISTE");
}
}
}

if (a5 == 5){

nove=9;

if (a2 == 2){

oito=8;
g.drawLine(250,300,350,235);
if (a3==3){

sete=7;

g.drawLine(175,267,425,267);
System.out.println("Perdiste :P");
}
if (a4==4){

sete=7;

g.drawLine(175,267,425,267);
System.out.println("Perdiste :P");
}
if (a6==6){

sete=7;

g.drawLine(175,267,425,267);
System.out.println("Perdiste :P");
}

if (a7==7){

três=3;

if (a4 ==4){

seis6=6;

g.drawLine(387,75,387,300);
System.out.println("Perdiste! :P");
}

if(a6 ==6){

quatro=4;

System.out.println ("Empate :S");
}
}
}

if (a3 ==3){

sete=7;

if (a2 ==2){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("Perdiste!!!! :)");
}

if (a8==8){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("Perdiste!!!! :)");
}

if (a4==4){

oito=8;
g.drawLine(250,300,350,235);
g.drawLine(175,267,425,267);
System.out.println("Perdiste!!!! :)");
}

if(a6==6){

oito=8;
g.drawLine(250,300,350,235);
g.drawLine(175,267,425,267);
System.out.println("Perdiste!!!! :)");
}
}

if (a4==4){

seis6=6;

if(a2==2){

três=3;

g.drawLine(387,75,387,300);
System.out.println("Perdiste!!!! :)");
}
if(a7==7){

três=3;

g.drawLine(387,75,387,300);
System.out.println("Perdiste!!!! :)");
}
if(a8==8){

três=3;

g.drawLine(387,75,387,300);
System.out.println("Perdiste!!!! :)");
}
if (a3==3){

sete=7;

if (a2==2){

oito=8;
g.drawLine(250,300,350,235);
g.drawLine(175,267,425,267);
System.out.println("Perdiste!!!! :)");
}
if (a8==8){

dois=2;

System.out.println("Empate :S");
}
}
}

if (a6==6){

quatro=4;

if (a2==2){

sete=7;

g.drawLine(212,75,212,300);
System.out.println("Perdiste :)");
}
if (a3==3){

sete=7;

g.drawLine(212,75,212,300);
System.out.println("Perdiste :)");
}
if(a8==8){

sete=7;

g.drawLine(212,75,212,300);
System.out.println("Perdiste :)");
}

if (a7==7){

três=3;

if (a8==8){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("Perdiste :)");
}
if (a2==2){

oito=8;
g.drawLine(250,300,350,235);
System.out.println("Empate :S");
}
}
}

if(a7 ==7){

três=3;

if (a2 ==2){

seis6=6;

g.drawLine(387,75,387,300);
System.out.println("Perdiste :)");
}

if (a8==8){

seis6=6;

g.drawLine(387,75,387,300);
System.out.println("Perdiste :)");
}

if (a4==4){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("Perdiste!!!! :)");
}

if(a6==6){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("Perdiste!!!! :)");
}
}

if(a8==8){

dois=2;

if(a4==4){

três=3;

g.drawLine(175,105,425,105);
System.out.println("Perdiste!!!! :)");
}
if (a6==6){

três=3;

g.drawLine(175,105,425,105);
System.out.println("Perdiste!!!! :)");
}
if(a7==7){

três=3;

g.drawLine(175,105,425,105);
System.out.println("Perdiste!!!! :)");
}

if (a3==3){

sete=7;

if (a6==6){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("Perdiste!!!! :)");
}

if (a4==4){

seis6=6;

System.out.println("Empates, :S");
}
}
}
}

if (a6 == 6){

cinco5=5;

if (a2 == 2){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("Perdiste!!");
}
if (a3==3){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("Perdiste!!");
}
if (a4==4){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("Perdiste!!");
}
if (a7==7){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("Perdiste!!");
}
if (a8==8){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("Perdiste!!");
}

if(a9==9){

três=3;

if (a2==2){

sete=7;

g.drawLine(425,75,175,300);
System.out.println("Perdiste!!! :D");
}
if (a4==4){

dois=2;

g.drawLine(212,105,387,105);
System.out.println("Gané!!! Yei!!! :D");
}
if (a7==7){

dois=2;

g.drawLine(212,105,387,105);
System.out.println("Gané!!! Yei!!! :D");
}
if(a8==8){

dois=2;

g.drawLine(212,105,387,105);
System.out.println("Gané!!! Yei!!! :D");
}
}
}
if (a7 == 7){

três=3;

if (a4 == 4){

dois=2;

g.drawLine(212,105,387,105);
System.out.println ("Gané!!! :D");
}

if(a5==5){

dois=2;

g.drawLine(212,105,387,105);
System.out.println ("Gané!!! :D");
}

if(a6==6){

dois=2;

g.drawLine(212,105,387,105);
System.out.println ("Gané!!! :D");
}

if(a8==8){

dois=2;

g.drawLine(212,105,387,105);
System.out.println ("Gané!!! :D");
}

if(a9==9){

dois=2;

g.drawLine(212,105,387,105);
System.out.println ("Gané!!! :D");
}

if (a2==2){

nove=9;

if (a4==4){

seis6=6;

g.drawLine(387,105,387,267);
System.out.println("Perdiste!!! =)");
}

if(a5==5){

seis6=6;

g.drawLine(387,105,387,267);
System.out.println("Perdiste!!! =)");
}

if(a8==8){

seis6=6;

g.drawLine(387,105,387,267);
System.out.println("Perdiste!!! =)");
}

if (a6==6){

cinco5=5;

g.drawLine(175,75,425,300);
System.out.println("Perdiste!!! =)");
}
}
}

if (a8 == 8){

cinco5=5;

if (a2 ==2){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if (a3 ==3){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if (a4 ==4){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if (a6 ==6){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if (a7 ==7){

nove=9;

g.drawLine(175,75,425,300);
System.out.println("PERDISTE");
}

if (a9 ==9){

sete=7;

if (a2 == 2){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a3 ==3){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a4 ==4){

três=3;

g.drawLine(425,75,175,300);
System.out.println("PERDISTE");
}

if (a6 == 6){

três=3;

g.drawLine(425,75,175,300);
System.out.println("PERDISTE");
}
}
}

if (a9 == 9){

três=3;

if (a4 ==4){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a5 ==5){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a6 ==6){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a7 ==7){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a8 ==8){

dois=2;

g.drawLine(175,105,425,105);
System.out.println("PERDISTE");
}

if (a2 ==2){

sete=7;

if (a5 == 5){

quatro=4;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a6 ==6){

oito=8;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a8 ==8){

oito=8;

g.drawLine(212,75,212,300);
System.out.println("PERDISTE");
}

if (a4 == 4){

cinco5=5;

g.drawLine(425,75,175,300);
System.out.println("PERDISTE");
}
}
}

}
}

• August 2nd, 2012, 07:34 AM
Norm
Re: Need help with tic tac toe game
Try debugging the code by adding lots of println statements to show when different parts of the code is being executed and what the values of the variables are when it is executing.