#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Operators not working on array of chars


    Ok, so this is really weird. I am making a simple disease-spreading game where the player has to avoid the disease as it spreads through the field. The field is divided into randomly generated regions represented by +, -, * and ~, e.g.
    Code:
     _ _ _ _
    |+|*|+|~|
    |*|-|-|*|
    |*|-|*|~|
    |+|*|+|-|
    Each turn the disease will spread to each of its horizontal and vertical neighbours following these rules:

    • If the region symbol under the disease matches the symbol in the new cell, then the new cell becomes diseased.
    • If the symbols don't match then the new cell becomes infected and will become diseased next turn.


    My algorithm for this spreading is as follows:

    Code:
    Loop through diseaseArray
        Check neighbouring cells regions against current region
        If regions are same:
            Set the neighbouring cell to 'X'
        Else:
            Set the neighbouring cell to 'Y'
    
    Loop through diseaseArray
        If cell = 'I':
            cell = 'D'
        Else If cell = 'X':
            cell = 'D'    
        Else If cell = 'Y':
            cell = 'I'
    I have implemented that as follows:

    Code:
    public char[][] spreadDisease (char regionGrid[][], char activeGrid[][])
    	{
    		char regionFlag;
    		int i;
    		int j;
    		int xDiff;
    		int yDiff;
    		
    		for (i = 0; i < 12; i++)
    		{
    			for (j = 0; j < 12; j++)
    			{
    				if (activeGrid[i][j] == 'D')
    				{
    					regionFlag = regionGrid[i][j];
    					
    					for (xDiff = -1; xDiff < 2; xDiff = xDiff + 2)
    					{
    						if (regionGrid[(i+xDiff)][j] == regionFlag && (activeGrid[(i+xDiff)][j] != 'D' || activeGrid[(i+xDiff)][j] != 'I'))
    						{
    							activeGrid[(i+xDiff)][j] = 'X';
    						}
    						else
    						{
    							activeGrid[(i+xDiff)][j] = 'Y';
    						}
    					}
    					for (yDiff = -1; yDiff < 2; yDiff = yDiff + 2)
    					{
    						if (regionGrid[i][(j+yDiff)] == regionFlag && (activeGrid[i][(j+yDiff)] != 'D' || activeGrid[(i+xDiff)][j] != 'I'))
    						{
    							activeGrid[i][(j+yDiff)] = 'X';
    						}
    						else
    						{
    							activeGrid[i][(j+yDiff)] = 'Y';
    						}
    					}
    				}
    			}
    		}
    		return activeGrid;
    	}
    	
    	public char[][] updateDisease (char activeGrid[][])
    	{
    		int i;
    		int j;
    		
    		for (i = 0; i < 12; i++)
    		{
    			for (j = 0; j < 12; j++)
    			{
    				if (activeGrid[i][j] == 'X')
    				{
    					activeGrid[i][j] = 'D';
    				}
    				else if (activeGrid[i][j] == 'Y')
    				{
    					activeGrid[i][j] = 'I';
    				}
    			}
    		}
    		return activeGrid;
    	}
    	
    	public char[][] changeInfected (char activeGrid[][])
    	{
    		int i;
    		int j;
    		
    		for (i = 0; i < 12; i++)
    		{
    			for (j = 0; j < 12; j++)
    			{
    				if (activeGrid[i][j] == 'I')
    				{
    					activeGrid[i][j] = 'D';
    					System.out.println("Changing [" + i + "][" + j + "]");
    				}
    				else
    				{
    					System.out.println("No I in [" + i + "][" + j + "]");
    				}
    			}
    		}
    		return activeGrid;
    	}
    Now my issue is for some reason lines like:
    "activeGrid[i][j] == 'I'"
    are returning false when they should be returning true, but the error doesn't happen all the time, only sometimes. The error can also be seen with the line:
    "(activeGrid[(i+xDiff)][j] != 'D' || activeGrid[(i+xDiff)][j] != 'I')"
    which should be stopping the disease spreading if the cell is already diseased or infected, however when the program is run random I's often appear in a sea of D's

    Any ideas?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Code:
      (activeGrid[(i+xDiff)][j] != 'D' || activeGrid[(i+xDiff)][j] != 'I')
    With OR the full condition returns true if any one of the sub conditions is true.
    If the array element holds 'I' the first condition is true
    If the array element holds 'D' the second condition is true.
    In other words that full condition will always return true,

    Using an AND operator requires that all the sub conditions in the full condition must be true for true to be returned.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Code:
      (activeGrid[(i+xDiff)][j] != 'D' || activeGrid[(i+xDiff)][j] != 'I')
    With OR the full condition returns true if any one of the sub conditions is true.
    If the array element holds 'I' the first condition is true
    If the array element holds 'D' the second condition is true.
    In other words that full condition will always return true,

    Using an AND operator requires that all the sub conditions in the full condition must be true for true to be returned.
    Ah, thank you! That should solve the phantom I's. I still have the other issues however.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Please explain what the problems are.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Its really hard to explain.

    Lines like "if (activeGrid[i][j] == 'I')" sometimes return false when they should return true. However the line "if (activeGrid[i][j] == 'X')" has no issues. The errors result in the disease getting stuck at random points, must usually when it is surrounded by I's like this:

    | | | | | | |
    | | |I|I| | |
    | |I|D|D|I| |
    | |I|D|D|I| |
    | |I|D|I| | |
    | | |I| | | |

    Which makes me think it is an error with the part of the code which changes I's to D's, however I also see things like this:

    | | | | | | |
    | | |I|I| | |
    | |I|D|D|I| |
    | |I|D|D|D| |
    | |I|D|D| | |
    | | |I| | | |

    Where the disease clearly has room to spread, but doesn't.

    I can upload the files if you want, its easier to understand if you can see it.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    "if (activeGrid[i][j] == 'I')" sometimes return false when they should return true.
    How do you know when " they should return true"? I doubt the code is not doing what it says it is going to do. More likely the data is not what you expect.

    A handy debugging tool to see what is in 2 dim arrays: Arrays class's method: deepToString()
    Code:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Ok, thanks guys ive solved it now. turned out the majority of the problems were coming from me calling methods in the wrong order so they were overwriting each other.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo