What wrong with my code? count++ isnt incrementing.
Im working on a NxN tic tac toe game and making recursive function to check the position.
The recursive function uVertical is working correctly, however, the count++ on dVertical check is not being increment (it the same method as uVertical but it going in the opposite direction).
Please help im totally lost.
Code java:
//check Up Vertical
public static int uVeritcal(int row, int col, int count)
{
if(row == 0)
return count;
else if(board[row-1][col].equals("X") && count < 4)
{
count++;
return uVeritcal(--row, col, count);
}
else
return count;
}
//check down Veritcal This function check the board to see if there 4 X located below the initial location.
public static int dVeritcal(int row, int col, int count)
{
//SOP always return 0 here.
if(row == boardSize - 1)
{
return count;
}
else if(board[row+1][col].equals("X") && count < 4)
{
count++;
//SOP return 1
return rHorizantal(++row, col, count);
}
else
{
return count;
}
}
public static void winner(int move) //move is the location of the board
{
int row = move / boardSize;
int col = move % boardSize;
if(uVeritcal(row, col, 0) + dVeritcal(row, col, 0) >= 4)
gameOver = true;
}
Re: What wrong with my code? count++ isnt incrementing.
Hello godofdoom999!
It's a bit difficult to tell what's going on with your code, because there are methods and variables' declaration missing. Try printing the value of count and a message to know which count is returned (which block is executed). If you still can find what's going on I would suggest you posting a small program that can compile and execute and demonstrates your problem.
Hope it helps.
Re: What wrong with my code? count++ isnt incrementing.
Sorry, I updated the code with the function call. When I do sop in the following location as shown on the code I constantly get 0 and 1. The 2 function uVertical and dVertical are the same but in different direction. uVertical has no problem increment count.
Re: What wrong with my code? count++ isnt incrementing.
You might have a combination of two misunderstandings here.
First, changes you make to a local variable don't "stick". What I mean by that is:
Code java:
public static void main(String... args){
int x = 7;
increment(x);
System.out.println(x);
}
public static void increment(int x){
x++;
}
What do you think that prints out?
The other is that the ++ operator returns the original value, not the incremented value. Try this:
That's why you shouldn't pass x++ in as an argument to anything. Do the incrementing, then use it. Don't try to take shortcuts.
Re: What wrong with my code? count++ isnt incrementing.
Thank for the fast response, I just updated the code ... just found out i had count++ in the return function. however, I have count++ above the return that should have incremented count before return the statement. Using your first example as shown below.
Code java:
public static void main(String... args){
int x = 7;
System.out.println(increment(x));
}
public static int increment(int x){
x++;
return x;
}
Re: What wrong with my code? count++ isnt incrementing.
Quote:
Originally Posted by
godofdoom999
Thank for the fast response, I just updated the code ... just found out i had count++ in the return function. however, I have count++ above the return that should have incremented count before return the statement. Using your first example as shown below.
Code java:
public static void main(String... args){
int x = 7;
System.out.println(increment(x));
}
public static int increment(int x){
x++;
return x;
}
That code would print out 8.
Re: What wrong with my code? count++ isnt incrementing.
yup that what dVertical should be doing but it not ... kinda weird considering dVertical and uVertical is the same code going in different direction.
Re: What wrong with my code? count++ isnt incrementing.
Quote:
Originally Posted by
godofdoom999
yup that what dVertical should be doing but it not ... kinda weird considering dVertical and uVertical is the same code going in different direction.
I suggest you put together an updated SSCCE that demonstrates your newest code exhibiting the problem.
Re: What wrong with my code? count++ isnt incrementing.
never mind I found the solution im just dumb ... cant believe I didnt check the recursive return function ... I had the wrong return function in it.
Thank for all the helps KevinWorkman.