Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: jwalker 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
-
In Java, all a 3D array is, is an Array of arrays of arrays. As such, the declaration of one would look like this.
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.
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.
-
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:
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:
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:
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
-
Well, for your code what you could do instead is use
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)
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 :)
-
wow, nice...