hi everybody, i'm a student and i'm sick of going around this. lol
i have to make a game of tic tac toe, and i did everything, and everything is working fine exept this. this is a method to see if there is a victory in a line, it does all that, see's the possibilitys, and only writes when there's actually a victory, but the problem is, it's supose to return a boolean vic that if true the game stops(allready made in another method) if false does nothing(it's in another method). but in this case it's allways returning false, how do i make it return true?

here's the code!

public static boolean testVictoriaLinha(char []posicaoNoTabuleiro){
		boolean vic=false;
		int sizeBoard = (int) Math.sqrt(posicaoNoTabuleiro.length);
		for(int i=0; i<posicaoNoTabuleiro.length; i=i+sizeBoard){
			if(posicaoNoTabuleiro[i]=='X'||posicaoNoTabuleiro[i]=='O'){ //it only enters the if(vic) if in those positions
			vic=true;													//theres a X or O;
			}
			for(int j=0; j<sizeBoard-1; j++){
				if(posicaoNoTabuleiro[i+j]!=posicaoNoTabuleiro[i+j+1]){// se if the positions are diferent
					vic=false;	
					break;
				}
			}
			if(vic){
				Tabuleiro tabuleiro = new Tabuleiro();
				tabuleiro.board(posicaoNoTabuleiro);
				System.out.println();
				System.out.println("victoria1 "+posicaoNoTabuleiro[i] );
			}
			
		}
		return vic;//it is supose to return vic insted is returning false allways false
	}

thanks!

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

If it is always returning false then it is likely the criteria for passing true is not being reached. Put some System.prints in there to check.

the criteria is right. it only says true when it's suposed to say it, the problem is in the return statement it doesnt receive the afectation of vic inside the for statement!

the problem is in the return statement it doesnt receive the afectation of vic inside the for statement!

What is the value of vic that it is not receiving?
How do you know what the value of vic is when it is being returned by the method?

Is the code on line 6 ever executed? How do you know?

the method sees if the first position of every line has the same char that is in the second position, and the first problem was if i dont play in a line it gives true cause all the position in the line had a char space, so i put the line six to only read when theres a X or a O, than i know it's doing the right thing cause when i play it says victory only when there's realy victory (with the if(vic)) but it doesnt return true... so what happens in the for cicle stays in the for cicle doesnt get to the return vic. :P sory if i'm confusing when explaining i'm not english. :P

but it doesnt return true

Add a println("vic=" + vic) after every statement where you change the value of vic. Look at the output and you will see what is happening.

See the second post on this thread.

xxxD before i say this remenber i'm just a student :P i found the problem i forgot a break in the if(vic) situation xxxD so stupid :P thanks everybody!
here's the code correct oh and i'm puting the method that call's this one :D!

public boolean victoria(char []posicaoNoTabuleiro,boolean jogo,int j,int primeirojogar){
		jogo=true;
		jogo=empate(j,primeirojogar,posicaoNoTabuleiro,jogo);
		for(int i=0;i<5;i++){
			
				if(i==0)vic=testVictoriaLinha(posicaoNoTabuleiro);if(vic)break;
				if(i==1)vic=testVictoriaColuna(posicaoNoTabuleiro);if(vic)break;
				if(i==2)vic=testDiagonalPrincipal(posicaoNoTabuleiro);if(vic)break;
				if(i==3)vic=testDiagonalMenor(posicaoNoTabuleiro);if(vic)break;
			
		}
		if(vic){
		jogo=false;
		}
		return jogo;
	}
public static boolean testVictoriaLinha(char []posicaoNoTabuleiro){
		boolean vic=false;
		int sizeBoard = (int) Math.sqrt(posicaoNoTabuleiro.length);
		for(int i=0; i<posicaoNoTabuleiro.length; i=i+sizeBoard){
			if(posicaoNoTabuleiro[i]=='X'||posicaoNoTabuleiro[i]=='O'){ //it only enters the if(vic) if in those positions
			vic=true;													//theres a X or O;

			}
			for(int j=0; j<sizeBoard-1; j++){
				if(posicaoNoTabuleiro[i+j]!=posicaoNoTabuleiro[i+j+1]){// se if the positions are diferent
					vic=false;	

					break;
				}
			}
			if(vic){
				Tabuleiro tabuleiro = new Tabuleiro();
				tabuleiro.board(posicaoNoTabuleiro);
				System.out.println();
				System.out.println("victoria1 "+posicaoNoTabuleiro[i] );
				break; //the missing break lol
			}

		}

		return vic;//it is supose to return vic insted is returning false allways false
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.