java - Sliding Block Puzzle, how to approach this -
i taking ai course, , first problem assigned create program solve sliding brick puzzle.
now, our professor hasn't given information on how approach this. 2 classes in , covered definition of ai, , agents.
i lost how approach problem. given text files represent board, , code set translate board 2d array, print out, etc. (the easy stuff). need work towards solving clueless about.
here example of board:
1,1,1,1,1,1, 1,3,2,2,4,1, 1,5,2,2,6,1, 1,7,7,8,8,1, 1,9,9,10,10,1, 1,0,0,0,0,1, 1,0,0,0,0,1, 1,1,-1,-1,1,1, 1 represents wall
0 represents blank space
-1 represents winning area
2 represents "special block"
anything greater 2 "non special" block
does know algorithms use approach problem with? or possibly resource.
here example of game itself: http://www.mathplayground.com/slidingblock.html
suppose represent states as
class state { int[][] a; } then should implement following functions
list<state> getneighbors(state s)- function should give possible states obtainedsmaking valid move. should find pieces , try translate them up, down, left, right.void solve(state s)- function should solve puzzle using standard algorithm dfs, bfs, a* etc. may want keep track of states you've visited in listlist<state> visitedor more in hash sethashset<state> visisted, hash set you'd have implement hash function.
note represent states @ string eliminate necessity implement hash function.
if don't want search bricks in every state represent bricks as
class brick { list<point> points; } class point { int x, y; } and, alternatively, represent state as:
class state { int[][] a; list<brick> bricks; } in way, finding of possible moves easier:
list<state> getneighbors(state s) { list<state> neighbors = new arraylist<state>(); (brick b : s.bricks) { // if can move brick // create new state new_state_up update 'a' // , 'bricks' , add neighbors. // same rest of directions } return neighbors; }
Comments
Post a Comment