Author Topic: array help  (Read 3431 times)

0 Members and 1 Guest are viewing this topic.

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
array help
« on: March 24, 2012, 11:26:50 am »
Ok, as the description says, im a newb to java.
im kinda a newb to arrays over 2 dims.
to learn java im making a small tic tac to game and here is what i want to do...

in a 3D array i want to store all of the possible win patterns, but i want to compare one of those patterns to a 2D array, how would i do that.

i know this might not be the best way to make a tic tac to game, but im trying to learn the language and work with diffrent sizes of arrays
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: array help
« Reply #1 on: March 24, 2012, 04:53:53 pm »
In Java, all a 3D array is, is an Array of arrays of arrays. As such, the declaration of one would look like this.

Code: [Select]
int[][][] myArray;
//or
int[][][] myArray2 = {{{0,0},
                       {0,0}},
                      {{0,0},
                       {0,0}}};//Creates a 2x2x2 array filled with 0

You can compare a 3D array to a 2D one with, say, a function compare(). compare() would have two arguments both 2D arrays. Here's how you would use it.

Code: [Select]
int[3][3] my2DArray;
int[3][3][3] my3DArray;

for(int[][] i : my3DArray[]){
    compare(i, my2DArray);
}
On a side note, having a 3D array holding all possible winning combinations is quite inefficient. If you want I could give you my own code that finds any winning combination that I had to do as an assignment in my Java class. :) You could use it to study from as well, if you wish.
« Last Edit: March 24, 2012, 04:55:14 pm by HOMER-16 »

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: array help
« Reply #2 on: March 25, 2012, 03:18:24 pm »
that would be great if i could see your code.

i tried your code, but i had to change it from compare to Array.equils, and then changed it again to work with my stuff.
im still running into problems tho...

here is the win pattern i want to loop through:

Code: [Select]
    int[][][] winpattern2 = new int[][][] {{{0, 2, 2},
                                            {2, 0, 2},
                                            {2, 2, 0}},
       
                                            {{2, 2, 0},
                                             {2, 0, 2},
                                             {0, 2, 2}},
                                           
                                            {{0, 0, 0},
                                             {2, 2, 2},
                                             {2, 2, 2}},
                                           
                                            {{2, 2, 2},
                                             {0, 0, 0},
                                             {2, 2, 2}},
                                           
                                            {{2, 2, 2},
                                             {2, 2, 2},
                                             {0, 0, 0}}

2 is actualy where no player has clicked, i would change it when i realized how stupid it is, but im to lazy

in order to match the pattern i used this code, since there could be a one and it wouldnt work anyway, also i didnt add the down patters yet

this is the code that loops through and finds out if the patterns are correct:

Code: [Select]
int sr = 0;
            for(int[][] farray : winpattern2)
            {
                   for(int bb = 0; bb <= 2; bb++)
                   {
                       for (int c = 0; c <= 2; c++)
                       {
                           if ((ss[c][bb] == 0) && (farray[c][bb] == 0))
                           {
                                sr += 1;
                           }
                       }
                   }
                }
                   
when sr = 3 then it would break out, but i didnt include that code

i was testing and the issue is here:

Code: [Select]
for(int[][] farray : winpattern2)
this is an issue because when it is done looping through the matrix it finds looks like this:

{{2, 2, 0}
  {2, 2, 0}
  {2, 2, 0}}

which isnt possible
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: array help
« Reply #3 on: March 25, 2012, 07:21:46 pm »
Well, for your code what you could do instead is use

Code: [Select]
int sr = 0;
            for(int i = 0; i < winpattern2.length; i++)
            {
                   for(int bb = 0; bb <= 2; bb++)
                   {
                       for (int c = 0; c <= 2; c++)
                       {
                           if ((ss[c][bb] == 0) && (winpattern2[i][c][bb] == 0))
                           {
                                sr += 1;
                           }
                       }
                   }
                }
                   

Although that part should work because it's basically making farray a reference to each array of arrays within the winpattern2 array.

This is the code I wrote to find if who won or of it's a tie for any Tic Tac Toe Game.

(It's actually a portion of a would-be larger tic tac toe class)

Code: [Select]
package cwk09;

/**
 *
 * @author HOMER-16
 */
public class TicTac {
    private String[][] grid;

    public String[][] getGrid() {
        return grid;
    }

    public void setGrid(String[][] grid) {
        boolean valid = true;
        if(grid.length == 3){
            for(String[] i : grid){
                if(i.length != 3){
                    valid = false;
                }
                for(String n : i){
                    if(!(n == "X" || n == "O" || n == " ")){
                        valid = false;
                    }
                }
            }
            if(valid){
                for(int i = 0; i < grid.length; i++){
                    System.arraycopy(grid[i], 0, this.grid[i], 0, grid[i].length);
                }
            }
        }
    }
   
    public TicTac(){
        grid = new String[3][3];
        for(String[] i : grid){//i holds a pointer to a String array
            for(int n = 0; n < i.length; n++){
                i[n] = " ";
            }
        }
    }
   
    public void set(int x, int y, String character){
        if((x >=0 && x < 3 && y >=0 && y < 3) && (character == "X" || character == "O")){
            if(grid[x][y] == " "){
                grid[x][y] = character;
            }
        }
    }

    public String ticTacWin(){
        for(int i = 0; i < grid.length; i++){
            String val = grid[i][0];
            if(grid[i][1] == val && grid[i][2] == val){
return val;
            }
        }
        for(int i = 0; i < grid.length; i++){
            String val = grid[0][i];
            if(grid[1][i] == val && grid[2][i] == val){
                return val;
            }
        }

        if((grid[0][0] == grid[1][1] && grid[2][2] == grid[1][1]) || (grid[0][2] == grid[1][1] && grid[2][0] == grid[1][1])){
            return grid[1][1];
        }

        return " ";

    }


    public String toString(){
        String gridString = "";
        for(int i = 0; i < grid.length; i++){
            for(int n = 0; n < grid[i].length; n++){
                gridString += grid[i][n] + ((n < 2) ? "|" : "\n");//Conditional for formatting purposes only
            }
            gridString += ((i < 2) ? "-----\n" : "");//Conditional for formatting purposes only
        }
        return gridString;
    }

}

Hope that helps :)

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: array help
« Reply #4 on: March 25, 2012, 10:14:22 pm »
wow, nice...
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads