Why do these things fail sometimes? Annoying program!
Hiya, I have been working on a program that looks for 4 in a line in a 2D array. I have made several methods such as
Code :
if(token(i,j) == token(i+1,j) {
if(token(i+1,j) == token(i+2,j) {
Counter++;
}
}
These methods work fairly well sometimes and completely fail at others. I get all excited that my program works, and the next minute, using the same pattern, it doesnt! It also sees things that are not there.
It this a common problem in java for these methods to not always check very well. Even my Winner method sometimes fails even though if I re-do the pattern it works. Also I have had to keep several backups as slight changes to it when working make it return very strange things!
Why do my methods fail sometimes and not others? Is it my code, or some sort of slow java related problem?? I seem to get it working just by looking at it sometimes.
Where token is just return array[col][row];
Re: Why do these things fail sometimes? Annoying program!
Quote:
Originally Posted by
Scotty
Hiya, I have been working on a program that looks for 4 in a line in a 2D array. I have made several methods such as
[code]
if(token(i,j) == token(i+1,j) {
if(token(i+1,j) == token(i+2,j) {
Counter++;
}
}
These methods work fairly well sometimes and completely fail at others. I get all excited that my program works, and the next minute, using the same pattern, it doesnt! It also sees things that are not there. 1 doesnt not equal 0 or -1 as it sometimes thinks!
It this a common problem in java for these methods to not always check very well. Even my Winner method sometimes fails even though if I re-do the pattern it works. Also I have had to keep several backups as slight changes to it when working make it return very strange things!
Why do my methods fail sometimes and not others? Is it my code, or some sort of slow java related problem?? I seem to get it working just by looking at it sometimes.
Where token is just return array[col][row];
Does your code even compile?
You are missing close parentheses on both your if conditions.
Also could you provide more context?
For instance what does token(type1, type2) do? What are its return type and parameter types?
Re: Why do these things fail sometimes? Annoying program!
Sorry it was an example and not typed well. It returns an int
Code :
public int token(int col, int row) {
return array[col][row] }
simply
Code :
if (array[i][j] == array[i+1][j]) {
if (array[i+1][j] == array[i+2][j]) {
Counter += 3;
}
}
if (array[i][j] == array[i][j+1]) {
if (array[i][j+1] == array[i][j+2]) {
Counter += 3;
}
}
etc.
And searches for different size lines of the same Number adding points accordingly.
Is there a better way?
Re: Why do these things fail sometimes? Annoying program!
Quote:
Why do my methods fail sometimes and not others? Is it my code, or some sort of slow java related problem??
Java, or any other common programming language for that matter, would not be used worldwide in all sorts of distributed applications if it were unpredictable. Problems such as this are logic/algorithm related - in other words, its the programmers fault. It helps to think through the logic, draw out what happens, to help you debug. As is it is hard to see what's going on, so to help you break the problem down, try creating an SSCCE for both yourself and for us.
Re: Why do these things fail sometimes? Annoying program!
Sounds like Connect 4.
Something else seems to be at fault here, have you confirmed that the range of the loop variables, i and j, are correct? Hook us up with the code for the loops and the related variables that control the loops range.