Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What wrong with my code? count++ isnt incrementing.

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


    	//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;
    	}
    Last edited by godofdoom999; May 24th, 2012 at 12:08 PM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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:

    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:

    public static void main(String... args){
       int x = 7;
       System.out.println(x++);
       System.out.println(x);
       System.out.println(++x);
       System.out.println(x);
    }

    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    	public static void main(String... args){
    		   int x = 7;
    		   System.out.println(increment(x));
    		}
     
    		public static int increment(int x){
    			x++;
    			return x;
    		}

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What wrong with my code? count++ isnt incrementing.

    Quote Originally Posted by godofdoom999 View Post
    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.
    	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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What wrong with my code? count++ isnt incrementing.

    Quote Originally Posted by godofdoom999 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. JInternalFrame isnt working why
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 17th, 2012, 01:42 PM
  2. Where I'm I wrong? I need to do a count of the number of each element in an array
    By NavagatingJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2011, 02:50 AM
  3. keylistner + applet isnt working
    By brandon95 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 4th, 2011, 07:16 AM
  4. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM

Tags for this Thread