1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Java

Discussion in 'Software' started by Cerberus90, 2 Mar 2011.

  1. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    My brain doesn't seem to be functioning on this one, :D

    I've got an array of ints.

    They repeat in a pattern
    2,4,1,4,1,4,1,4,1,4,1,4,1,4,3

    a set number of times depending on the resolution of the screen.

    So rather than having a very large set of this pattern going into the int array, and rather than reading from a txt file, is there a way of putting this pattern into the array using a for loop?
     
  2. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    phew, think I've got something that should work. Might not be the most efficient.

    Code:
    int a = getHeight() / 32;       
            int b = a*32;
           
            int[] roadMap = new int[b];
            for(int i = 0; i < b; i++)
            {
                int cols = 15;
                for(int j = 0; j < 15; j++)
                {
                    if(i == 0) {
                        switch(j) {
                            case 0: roadMap[i*j] = 2;break;
                            case 1: roadMap[i*j] = 4;break;
                            case 2: roadMap[i*j] = 1;break;
                            case 3: roadMap[i*j] = 4;break;
                            case 4: roadMap[i*j] = 1;break;
                            case 5: roadMap[i*j] = 4;break;
                            case 6: roadMap[i*j] = 1;break;
                            case 7: roadMap[i*j] = 4;break;
                            case 8: roadMap[i*j] = 1;break;
                            case 9: roadMap[i*j] = 4;break;
                            case 10:roadMap[i*j] = 1;break;
                            case 11:roadMap[i*j] = 4;break;
                            case 12:roadMap[i*j] = 1;break;
                            case 13:roadMap[i*j] = 4;break;
                            case 14:roadMap[i*j] = 3;break;
                            
                        }
                    }
                    else
                        switch(j) {
                            case 0: roadMap[(i*j) +cols] = 2;cols--;break;
                            case 1: roadMap[(i*j) +cols] = 4;cols--;break;
                            case 2: roadMap[(i*j) +cols] = 1;cols--;break;
                            case 3: roadMap[(i*j) +cols] = 4;cols--;break;
                            case 4: roadMap[(i*j) +cols] = 1;cols--;break;
                            case 5: roadMap[(i*j) +cols] = 4;cols--;break;
                            case 6: roadMap[(i*j) +cols] = 1;cols--;break;
                            case 7: roadMap[(i*j) +cols] = 4;cols--;break;
                            case 8: roadMap[(i*j) +cols] = 1;cols--;break;
                            case 9: roadMap[(i*j) +cols] = 4;cols--;break;
                            case 10:roadMap[(i*j) +cols] = 1;cols--;break;
                            case 11:roadMap[(i*j) +cols] = 4;cols--;break;
                            case 12:roadMap[(i*j) +cols] = 1;cols--;break;
                            case 13:roadMap[(i*j) +cols] = 4;cols--;break;
                            case 14:roadMap[(i*j) +cols] = 3;cols--;break;
                            
                        }
                    
                }
                
                
                
            }
    
     
  3. RadEd

    RadEd What's a Dremel?

    Joined:
    4 Aug 2010
    Posts:
    24
    Likes Received:
    0
    If I understand this correctly, each screen line should reference an element in the in array;
    So for example

    line 0 to 2
    line 1 to 4
    line 2 to 1
    line 3 to 4
    line 14 to3

    Then starts back at the beginning of the int array
    line 15 to 2
    line 16 to 4

    If so, why not use the moulo if the screen line index:

    int[] roadMap = new int[] {2,4,1,4,1,4,1,4,1,4,1,4,1,4,3};
    int value = roadMap[index % 15];
     
  4. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    Each row has 15 tiles.

    2,4,1,4 etc.

    And then there's ScreenHeight()/32 rows.


    I'm basically making two tiled images that are the height of the screen, these will then wrap around the screen scrolling downwards.

    roadMap is the array that needs to have height/32 rows, with the 15 column pattern.

    Each number refers to a tile in an image.
     
  5. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    I don't really understand what you're doing but here's a noob's opinion anyway (just in case it helps :p):

    You could define a function which takes one array and concatenates it on the end of a second array and use a for loop to loop through that. Pass the function roadMap and the array {2,4,1,4...} and let it concatenate the base array on the end of roadMap as many times as you want.

    You could also define a single {2,4,1,4...} array for the class and then have roadmap be an array of pointers which you can point to {2,4,1,4...} as many times as you want with a for loop (this might be a bit C++ish and idk if it'll work in Java).

    Following that above idea if pointers don't work maybe an array of int arrays would work where you could add the {2,4,1,4...} array to it as many times as you want.

    That's all I can think of right now hope it helps some :).
     
  6. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    erm................yeah................:thumb::D

    Not sure, Java doesn't have pointers per say.

    I could use concatenate, if Java ME has it. J2Me is seriously cut down from the full SE version.


    I've not spent anymore time on it just yet, been trying to work on other things, :D
     
  7. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    GO TO NEXT POST >.<

    Never mind the first idea, it's going to be more trouble than it's worth.

    I think a variation of the second/third idea will work better (if I understood you correctly). I can't seem to find a way to do pointers in Java (how disappointing that would've been the easiest solution) and defining an integer array of integer arrays doesn't seem to be possible either (I think you'd have to use a list).

    What I did was define an extra class just to hold the base array:
    Code:
    public class BaseArray {
        private static final int[] base = {2,4,1,4,1,4,1,4,1,4,1,4,1,4,3};
    
        public int[] getArray(){
            return base;
        }
    
        public int length(){
            return base.length;
        }
    
        public int getElement(int i){
            return base[i];
        }
    }
    input more BaseArrays by initializing BaseArrays in the slots with a for loop, and then just have a nested for loop to access every element.

    Code:
    BaseArray array[] = new BaseArray[someValue];
    for(int i = 0; i < someValue; i++){
        array[i] = new BaseArray();
    }
    for(int i = 0; i < someValue; i++){
        for(int j = 0; j < array[i].length(); j++){
            System.out.println(array[i].getElement(j));
        }
    }
    You can also do it with a list a bit easier:

    Code:
    List<int[]> baseList = new ArrayList<int[]>();
    int[] base = {2,4,1,4,1,4,1,4,1,4,1,4,1,4,3};
    for(int i = 0; i < someValue; i++){
        baseList.add(base);
    }
    for(int i[] : baseList){
        for(int j = 0; j < i.length; j++){
            System.out.println(i[j]);
        }
    }
    With this solution it's very hard to access a specific number with the array (like the real 23rd element would have to be accessed by finding array[1] and saying getElement(7) there). To get around that you could define a function like the one below to access the number you want (I only did it for the list version but it's a similar strategy with the array version).

    Code:
    private int getElement(List<int[]> list, int i){
        if(i < 15*list.size()){
            int listPosition = i/15;
            int arrayPosition = i - 15*listPosition;
            return list.get(listPosition)[arrayPosition];
        }
        return -1;
    }
    The way I see it you're either going to be taking a lot of process cycles to input that base array into your main array a bunch of times but you'll have easy access to each element, or you'll have an easy time inputting the base array into the main one but you'll have a more complex element access routine. The way that I'm proposing won't have much of an effect on the access time if you just iterate through the entire thing though (same total number of elements, might be a bit slower b/c of the function calls but otherwise pretty much the same). If you need to access the array a lot in your program to either iterate through it or get values individually I would suggest going with your switching method which will generate an array of integers which you can access quickly and easily (although setup time might be longer). I believe the solutions I present have a more efficient setup time (although I may be wrong :p) but will have a longer access time. If nothing else it's at least more readable.
     
    Last edited: 4 Mar 2011
  8. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    OR... you can totally ignore what I posted up there because I'm obviously a noob programmer who over-complicates simple problems >.<

    Code:
    // Insert {2,4,1,4,1,4,1,4,1,4,1,4,1,4,3} at position start in array
    private static void concat(int[] array, int start){
        if(start+15 < array.length){
            array[start++] = 2;
            for(int i = 0; i < 6; i++){
                array[start++] = 4;
                array[start++] = 1;
            }
            array[start++] = 4;
            array[start] = 3;
        }
    }
    Define that function and then go:

    Code:
    for(int i = 0; i < someValue; i++){
        concat(array, i*15);
    }
    And boom you got as many repetitions of {2,4,1,4,1,4,1,4,1,4,1,4,1,4,3} as you want nicely done in an integer array without any switching, extra classes, or lists.

    I'm sorry for burdening you with my stupidity :(
     
    Last edited: 4 Mar 2011
    Cerberus90 likes this.
  9. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    That's a nice solution.


    But, here comes another problem.

    Java doesn't seem to like uninitialised variables.

    so when I define int[] roadmap; and then try to pass it into the function, something funny happens. The size of the array must be defined before you start putting stuff into it, otherwise it thinks its empty and won't put anything into it. Gives a null pointer exception.

    Thanks for the ideas though, +rep.
     
  10. tristanperry

    tristanperry Minimodder

    Joined:
    22 May 2010
    Posts:
    922
    Likes Received:
    41
    That's because the Array class takes up a constant block of memory when it's created. So it can't then have a flexible size.

    Hence why you need to do int[] roatmap = new int[ n ]; (where n is some integer) before using the array.

    If you want to have a flexible size 'array', check out the ArrayList class. It's pretty useful to get used to.

    Either that or you'll need to do some check after adding each value to an array and if the array is full, use System.arraycopy and create a new array with (say) double the size.
     
  11. Dae314

    Dae314 What's a Dremel?

    Joined:
    3 Sep 2010
    Posts:
    988
    Likes Received:
    61
    I didn't include the declarations and such (like the number "someValue" is whatever you figure out is the amount of times the pattern needs to be input). In your original code up on top you had:

    Code:
    int a = getHeight() / 32;       
    int b = a*32;
    int[] roadMap = new int[b];
    Keep that and define a function like concat and then replace the for loop you had up there with the concat for loop I demonstrated. As long as you got the size of the array correct in that declaration then there should be no problems. ArrayList is awesome for dynamic size, but it is slower to access specific entries than an array is.
     

Share This Page