java - Trying to go through a 2d array, and not getting the correct values? -


so making maze game, , of course maze game has have walls. walls need stop player going on them. having problem checking collision. works in places, , others not. current method going through 2d array, finds number player attempting go on dividing current x , y 50, , using 2 numbers try , see if player colliding wall or not. happening stops player moving walls, , not. in addition stops player in places there no wall (a value 2). feel getting messed math, can't figure out what. here code how make maze, along array being made from:

private int[][] mazewalls = { //top of maze         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},         {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},         {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},         {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},         {1, 1, 1, 1, 2, 2, 2, 2, 2, 1}, /*left side*/{1, 2, 2, 2, 2, 2, 2, 2, 2, 1}, //right side         {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},         {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},         {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}                                 //bottom of maze };  public void paintmaze(graphics g){     for(int row = 0; row < mazewalls.length; row++){ //example of loops         for(int col = 0; col < mazewalls[row].length; col++){ //getting positions set             if(mazewalls[row][col] == 1){                 g.setcolor(color.red); //color of walls                 g.fillrect(col * 50, row * 50, 50, 50); //col times 50 x coordinate, , row times 50 y coordinate             }         }     } } 

here code how check collision in same class:

public void collisionchecker(int playerx, int playery){ //takes user's location , checks if running wall     if(mazewalls[playerx / 50][playery / 50] == 1 ){         setcollision(true);     }     else if(mazewalls[playerx / 50][playery / 50] != 1){         setcollision(false); //just in case     } } 

i feel should working, reason (i think numbers after dividing player's coordinates, or something) not.

the first index in 2d array convention row index , second column index, co-ordinates wrong way round:

public void collisionchecker(int playerx, int playery){ //takes user's location , checks if running wall     if(mazewalls[playery / 50][playerx / 50] == 1 ){         setcollision(true);     }     else if(mazewalls[playery / 50][playerx / 50] != 1){         setcollision(false); //just in case     } } 

this code can simplified

public void collisionchecker(int playerx, int playery){ //takes user's location , checks if running wall     setcollision(mazewalls[playery / 50][playerx / 50] == 1); } 

Comments