java


If statement concerning matrix cell not returning true(java)


I have a project for a class that is to solve a maze(matrix), which is simple enough, however i'm having some trouble with an if statement that should validate the usability of the matrix cell, to see if that number is a part of the path.
Here's the test Maze I've created:
// 0 = start
// 1 = path
// 2 = wall
// 3 = end
// 5 = tried
// 6 = final path
int [][] maze = {{2,2,2,0,2,2,2,2,2,2},
{2,2,2,1,2,2,1,1,1,2},
{2,2,2,1,2,2,2,2,1,2},
{2,2,2,1,2,2,2,1,1,2},
{2,2,1,1,2,2,1,2,1,1},
{2,1,1,0,2,2,2,2,2,2},
{2,1,2,0,2,2,2,2,2,2},
{2,1,1,0,2,2,2,2,2,2},
{2,2,3,0,2,2,2,2,2,2},};
And here is the method to check if the current cell is valid to walk on:
private boolean valid (int row, int column) {
boolean result = false;
// checks if cell is inside of the matrix
if (row >= 0 && row < maze.length &&
column >= 0 && column < maze[0].length) {
// checks if cell is not blocked, if it has previously been tried or it's the end
if (maze[row][column] == 1 || maze[row][column] == 3 || maze[row][column] == 0 || maze[row][column] == 5) {
result = true;
}else{
result = false;
}
}
return result;
}
From using print statements I've seen that the problem maybe in the nested if statement. But there might be another problem I am not sure of, which is on the solving method.
public boolean solve(int row, int column ) {
boolean solved = false;
if (valid(row, column)) {
maze[row][column] = 5;
if (maze[row][column] == 1 || maze[row][column] == 0){
if( !solved){//it's going to call the function it self and move if possible.
solved = solve(row + 1, column); // South
if (!solved)
solved = solve(row, column + 1); // East
if (!solved)
solved = solve(row - 1, column); // North
if (!solved)
solved = solve(row, column - 1); // West
}
if (solved) // part of the final path
maze[row][column] = 7;
}else if (maze[row][column] == 3) {
solved = true;
System.out.println("lol the end");
}
//exception here not to leave the maze and case there's no 0
}
return solved;
}
Put the statement
maze[row][column] = 5;
just after the next (if) statement:
if (maze[row][column] == 1 || maze[row][column] == 0){
as it protect to correctly evaluate the condition in this if statement.

Related Links

AES decryption after sending message to IP address
JAK generated KML not work with Google My Maps
Gradle exception while building GraniteDS project
Can I synchronize reads of control variables?
Struts 2 get Json from DAO into the action class
Read faster a file & convert it into HEX
Are static variables initialised from a file initialised only once
Query Does not return results Sqlite
wildcards in preparedstatement not executing
Creating a HTTP Server in Node.js service POST with file
I just send parts of the image through socket using Java
Getting NullPointerException when call Location from Activity
Use java to parse xml with several parents
Need assistance with java morse code translator
ArrayIndexOutOfBoundsException in 3D array
Where to copy ITelephony.java in android studio and how to import it? Please don't close this without answering

Categories

HOME
keycloak
single-sign-on
angular-material
raspberry-pi
electron
tizen-web-app
sharepoint-designer
electronics
cakephp-2.5
disassembler
cross-browser
elasticsearch-hadoop
ssl-client-authentication
handsontable
jplayer
windows-server-2012
ghc
invantive-sql
autocad-plugin
kvc
telerik-reporting
icloud-api
underflow
msys2
jspm
libssl
javascriptcore
yadcf
code-contracts
grails-3.1
avcapturesession
accelerate-framework
android-kernel
language-concepts
mpmediaquery
retina-display
withings
ansible-playbook
splice
cookiecutter-django
gzipstream
android-mediaprojection
thin
outlook-2013
rainbowtable
prolog-setof
hls.js
diagnostics
sqlclient
memory-alignment
orthogonal
google-cdn
nested-sets
parallel-data-warehouse
visual-c++-2008
infix-notation
adxstudio-portals
qpid
react-native-listview
whois
dataview
browser-link
ford-fulkerson
natvis
msys
tarjans-algorithm
ionic
interrupted-exception
iiviewdeckcontroller
dundas
e10s
nsmutabledictionary
camanjs
mesa
article
ms-project-server-2010
dbconnection
ftps
bulkloader
batterylevel
flashvars
robotics-studio
factory-method
cascalog
hosts-file
jquery-ui-layout
stage
subgurim-maps
coercion
xmemcached
xtype
oncheckedchanged
floating
.nettiers
dentrix
suppress
bespin
substrings
memory-size
boost-smart-ptr
scripting-languages
dbisam
ajax-forms

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile