JavaScript Amnesty International for an HTML sliding tile puzzle


JavaScript to Amnesty International for an HTML sliding tile puzzle


Sam Lloyd ( 1841-1911), created for the American chess player along wthe itemh maker of the puzzle, along wthe itemh sliding tile puzzle inside the 1870s. Thcan be represents a puzzle by m × n network, where m can be the number of columns along wthe itemh n can be the number of rows, along wthe itemh each cell can be any value imaginable (a number or a letter or a picture, along wthe itemh so on.)

The purpose of thcan be can be a puzzle for re the inthe itemial configuration of tiles arranging to match the last configuration kright nown as the configuration goal . Thcan be can be achieved rearrange the task by swapping tiles empty wthe itemh some addthe itemional tile in all possible directions (up, down, left, right).

Sliding Tiles Puzzle
sliding tile puzzle.

can be supposed to empty tiles can not be moved outside of the Board of Directors: So, if located inside the first column, along wthe itemh the blank tile can not go left. And if located inside the far right column, the item can not be moving inside the right direction. The same can be true for the ranks of the matter moves ethe itemher up or down. You will get a solution to the puzzle the former inside the following steps.

How the item starts
how to start.


How the item should end
how the item should end

ascertainside the extent of the inthe itemial formations The goal right now can be the same; thcan be means we have completed the puzzle.

Thcan be article will be divided into two parts. First, we will provide a brief description of how to create along wthe itemh develop a sliding tile puzzle using HTML, CSS for the vcan beual aspects, along wthe itemh JavaScript for the transfer (by animation) tiles on the board. (We will need thcan be to illustrate the latter part of thcan be article.)

Second, we will the development of artificial intelligence, through the A * search algorthe itemhm able to find a solution wthe itemh the minimum number of moves to create a goal, which It provides the perfect solution. The various associated inference A * algorthe itemhm asscan bet in directing research, intelligence along wthe itemh heurcan betic, the item will be to find the optimal solution sooner. Each of the inference will be dcan beplayed inside the order of intelligence to the recipe. Therefore, the heurcan betic last feet will be stronger.

planning link

We will start by creating a corresponding sliding_tiles_puzzle.html file, which will be held inside the game. We will also create the following empty files:

  • styles.css
  • stpuzzle.js

Furthermore on the item, the item will be necessary to add jquery.js we will use to make our lives easier along wthe itemh get so much more elegant, readable code.

head should end up looking like thcan be:

       sliding tile puzzle    

efficiency, along wthe itemh we 'll add links to all the text inside the bottom of the page. Thcan be can be a common practice, where at the top of the pages are dcan beplayed down the way, along wthe itemh we want to load the page regularly as soon as possible; leave the functional texts to be downloaded inside the end, after the item was all vcan beual elements loaded correctly.


along wthe itemh priorthe itemy queue.js , hashtable.js along wthe itemh hashset.js will be used in artificial intelligence component to provide efficiency for our agent rational. They respectively represent priorthe itemy queues, hash tables along wthe itemh groups of retail data structures.

Now we will start the establcan behment of the page format. Inthe itemially, the plan will have to look like thcan be.


along wthe itemh container [1945054Category], located in styles.css View , also the item appeared inside the next block of styles.

  / * Development Arnaldo Perez Castano * / .container {width: 1024px; sidelines of the left: the automobiles. Margin-right: cars. Min-height: 380px;}  

panel can be simply a record that will we'l used for printing or showing the results associated wthe itemh the artificial intelligence component. In thcan be painting printing the optimal solution obtained by Amnesty International will be

  #panel {width: 100%; background-shade: RGB (180180180); Maine height: 1000px; whthe iteme shade; Line Weight: bold; padding: 5px; font-family: Ariel;}  

Since we want the global container to be inside the center of the page, we have developed a fixed width, along wthe itemh the charactercan betics of left the sidelines along wthe itemh the right margin of for the automotive industry - that will will control things inside the middle. Now we add container network Divcan beion, which, as the name implies, can be basically a Divcan beion that will will contain immediately the network, which represents the Council.


Inthe itemial Configuration

along wthe itemh container network class along wthe itemh determinants involved pointing out that will inside the following blocks.

  .grid container {float: left; width: 210px; height: 0px; text-align: center; width: 50%;} .grid container H2 {font-family: Tahoma members. }  

along wthe itemh we are left floating container network since we will put two of these in a row: one for each configuration (primary objective). Finally, add the Network Divcan beion.


inthe itemial configuration


along wthe itemh sliding tile puzzle grid conscan bets of three rows, each wthe itemh three cells. Thcan be makes the foundation of the whole network. Wthe itemh the proposed plan to achieve a very intuthe itemive way to represent the network. The network includes three children. Every child can be a row (element) Divcan beion. It contains a row along wthe itemh three children. And the item represents every child a cell (also div) element.

for can besues related to add programming feature , POS data to each cell, to indicate the posthe itemion of each cell on the board. The same applies to class start . We need to dcan betingucan beh between the inthe itemial configuration of the target can be configured as the latter does not receive input coming from the user. And start class will help us to achieve thcan be. Classes are mentioned inside the definthe itemion of the next lines lcan beted

  .grid {background shade: RGB (248248248); border: solid RGB 5px (249, 0, 0); width: 210px; height: 210px; margin of left: cars. Margin-right: cars. The border between the radius: 3PX. Shadow Box: 5px 5px # d8d8d8, 5px 5px # d8d8d8. Bypass: cars. } {.row Height: 33.3%. } {.cell Width: 32.3%. Height: 100%; float: left, text-align: center; FONT-SIZE: 150%; font-family: Ariel; font weight: bold; posthe itemion: relative. } .cell: Hover {background shade: RGB (221221221);} .cell period {dcan beplay: block. Convariation: translateY (70%);}  

The end result can be a complete 3 × 3 grid wthe itemh numbers coming from 1 to 9.

Final grid
final network .

for the formation of a goal inside the page we just copy the network divcan beion along wthe itemh all the items contents along wthe itemh rename start Class goal .


target configuration

Start along wthe itemh Final grids
inthe itemially final networks.

In conclusion, we add solution along wthe itemh step Dcan beplay yettons to the first network container

   solution   step dcan beplay   

The first yetton will lead to a rational agent; in addthe itemional words, the A * search algorthe itemhm. The second show vcan beually step of the solution obtained by the first. Thus, by pressing Dcan beplay step yetton n times where n can be the length of the solution, we will kright now how to solve the puzzle inside the least possible number of steps.

along wthe itemh right now we have some vcan beual efficiency Let's start building some functional competence. We need to make the game work - basically, that will translates to allow empty tiles to move around the board. To complete thcan be piece of development we will be using JavaScript. The first lines of stpuzzle.js View look like thcan be

  / * Development Arnaldo Perez Castano * / var emptytilePosRow = 1; var emptytilePosCol = 2; var cellDcan beplacement = "69px;"  

along wthe itemh emptytilePosRow along wthe itemh emptytilePosCol will tell us where the tiles empty at all times. Will be updated wthe itemh every step of progress.

along wthe itemh cellDcan beplacement variable indicates the value of the offset to be applied to a cell when the item can be made inside the field of animation. Notice how the cell category incorporates a posthe itemion set to close feature. We want to move freely cells on the plate using higher along wthe itemh correct properties animation. Will indicate cellDcan beplacement value of the fresh values ​​of higher along wthe itemh right charactercan betics, thereby moving the cells.

care function moves on the board like thcan be begins:

  function moveTile () {// get the current posthe itemion inside the POS element var = $ (thcan be) .attr ( 'data POS '); var posRow = parseInt (pos.splthe item (', ') [0]); var posCol = parseInt (pos.splthe item (', ') [1]);  

Notice how we are already using jQuery to select all the cells coming from starting. Also note the use of the class start . We want to maintainside the target board can be read-only, so we select all cells belonging to the network first - along wthe itemh only on the starting grid. Next up, we get into the cell to be determined. Remember, the posthe itemions stored " Q , p ': we get the rows along wthe itemh columns indexes in posRow along wthe itemh posCol variables

[1945024along wthe itemhdevotestherestofthejobtotheimplementationofthcan berightstep

  // move up if (posRow + 1 == emptytilePosRow && posCol == emptytilePosCol) {$ (thcan be) .animate ({ 'higher ': "+ =" + cellDcan beplacement // moves up});. $ (' # empty ') move ({' head ': "- =" + cellDcan beplacement // move down}); emptytilePosRow- = 1; $ ( thcan be) .attr ( 'POS data ", (posRow + 1) +", "+ posCol);} // move down if (posRow - 1 == emptytilePosRow && posCol == emptytilePosCol) {$ (thcan be) .animate ({ 'head': "- =" + cellDcan beplacement // move down});. $ ( '# Empty') move ({ 'higher': "+ =" + cellDcan beplacement // moves up}); emptytilePosRow + = 1; $ (thcan be) .attr ( 'POS data ", (posRow-1) + "," + posCol);} // move to the left if (posRow == emptytilePosRow && posCol + 1 == emptytilePosCol) {$ (thcan be) .animate ({ "right": "- =" + cellDcan beplacement // moves right});. $ ( '# empty') move ({ 'correct': "+ =" + cellDcan beplacement // moves left}); emptytilePosCol - = 1; $ (thcan be) .attr ( 'POS data, posRow + "," + (posCol + 1));} // If the right move (posRow == emptytilePosRow && posCol - 1 == emptytilePosCol) {$ (thcan be) .animate ({ 'right': "+ =" + cellDcan beplacement // moves left});. $ ( '# empty') move ({ "true": "- =" + cellDcan beplacement // moves right}); emptytilePosCol + = 1; $ (thcan be) .attr ( 'data points sales, posRow + "," + (posCol-1));} // updated $ empty posthe itemion ( '# empty') ATTR ( 'POS data, emptytilePosRow + "," + emptytilePosCol);}  

both four if data represents a different step for a blank tile. They share similarthe itemies also evaluates the major differences on the circumstances, signs along wthe itemh updating variables. The right move, for example, start by checking if the empty tiles to the left of the current cell before the condthe itemion: posRow == emptytilePosRow (the same as to say that will they are inside the same row) along wthe itemh posCol - 1 = = emptytilePosCol (the same as saying that will the tiles empty to the left of the current cell)

If the condthe itemion can be not satcan befied, then, using animation aftermarket, we change the value of property right to dcan beplace the current cell to the left along wthe itemh make the illusion Having swapped elements. And if statement ends by updating the variable emptytilePosCol (Add 1) incorporates also been moved to the right, along wthe itemh modernization of the posthe itemion of the cell that will have been moved to the left (subtract 1 coming from hcan be column posthe itemion). In the end, we update the empty tile posthe itemion.

Artificial Intelligence link

along wthe itemh search * aware of (Hart et al., 1968) can be a non-living factor rational that will we will be developing to solve a sliding tile puzzle. The agent said can be a rational entthe itemy, being part of some of the environment along wthe itemh can be subject to certain rules, can be not able to recognize in thcan be environment along wthe itemh acting rationally to these perceptions. Rationalthe itemy will be given before generating the right deccan beion for the worker, which can be considered appropriate if the item can be aimed toward maximizing some of the desired results. Artificial intelligence can be the same factor.

humans are rational (more than once) agents living because they belong to the environment (the universe) we are subject to certain environmental rules (along wthe itemh we can not live under extremely cold temperatures, for example); we get the perceptions of the environment ( We are cold) along wthe itemh we respond rationally (again, most of the time) to these perceptions (we wear coats).

In the context of the sliding tile puzzle, the environment can be represented inside the Council, rules or restrictions that will possible directions that will one can move the tiles empty (up, down, left, right), along wthe itemh before the implementation of a valid movement when swapping tiles empty wthe itemh any of the neighboring tiles. The perception corresponds to the current sthe itemuation of the composthe itemion contained a rational reaction to thcan be perception can be compatible wthe itemh thcan be step implementation. If rational, the move should be directed to get a configuration that will matches the target configuration.

What will be the A * search did you? link

along wthe itemh A * search, as the name implies, can be a search algorthe itemhm, along wthe itemh the items purpose to the intelligence, search the state space (each group of the Council configurations) along wthe itemh find the path of the inthe itemial composthe itemion of the composthe itemion of the target. Due to the intelligence along wthe itemh search by the number of countries he vcan bethe items: in less vcan bethe itemed quthe iteme a few countries, along wthe itemh more intelligent as the item can be along wthe itemh the sooner you will provide a solution. To navigate through the state space, we design the problem as a graph. In thcan be way, we consider that will the state B can be a child of the state along wthe itemh if the item can be to obtain B by moving the tiles empty in some of the benefthe item of the trend in A. In thcan be sense, a node on the graph can be at most four children, one for each possible direction.

Expalong wthe itemhed nodes inside the A* Search
enlarged nodes inside the A * search. ( send large copy )

along wthe itemh A * will be notified search as the item uses kright nowledge of the environment to determine the next step to continue the search. Thcan be kright nowledge can be linked to a numeric value wthe itemh each state ( for ), kright nown as along wthe itemh (s) , along wthe itemh then inside the year:

along wthe itemh (s) = g (s) + h (s)

where g (s) can be the cost of access to state image coming from the inthe itemial state, along wthe itemh h (s) can be the estimated cost to reach the goal of the current status or configuration state. It described thcan be relationship inside the following figure.

Cost of moving coming from inthe itemial config to the final config
cost of the transthe itemion coming from the inthe itemial configuration to the final configuration. ( a large replica watch )

to direct the search through the state enormous space we use heurcan betics. The heurcan betic can be the way that will we adhere to our kright nowledge of the experimental environment to the rational agent, in thcan be case A * search. It assumed that will the information provided by the heurcan betic to help find, a short path that will can be possible to configure the target.

Since we are designing the problem as a graph, along wthe itemh the basic structure of the A * search corresponding breadth-first search (BFS), along wthe itemh the classic search algorthe itemhm graph. The difference between the A * search along wthe itemh BFS can be that will the contract or the countries inside the A * associated wthe itemh the search wthe itemh some value along wthe itemh (s) , specified the following for repetthe itemion along wthe itemh the node can be the one wthe itemh the minimum along wthe itemh (s) . In BFS, all nodes possess the same value (1) so the item can be not actually important which one comes first, just that will their choice inside the order in which they are added to the queue (FIFO: first in, first out).

when developing heurcan betic can be important to make sure that will the item carries the admcan besion crthe itemeria. The heurcan betic acceptable if the item does not exaggerate the minimum cost of access to the target coming from the current configuration configuration. If accepted, the A * search algorthe itemhm always find the perfect solution.

As mentioned earlier we encode artificial intelligence in JavaScript. Some might think that will thcan be approach can be wcan bee, yet we will prove that will JavaScript can provide all we need to get a rational agent efficiency. We will start by creating the object node shown inside the following code.

  node function (value, state, emptyRow, emptyCol, depth) {thcan be.value = value of these .state = state thcan be.emptyCol = emptyCol thcan be.emptyRow = emptyRow thcan be.depth = depth of thcan be. strRepresentation = "" thcan be.path = "" // string representation of the state in CSV format for (var i = 0; i  

description of each are lcan beted next variable

  • value represents , (s ) value
  • state : The mkaliante of the Council as a two-dimensional group
  • emptyCol : refers to the column that will tile can be empty
  • emptyRow :. Refers to the row that will tile can be empty
  • depth : refers to the number of moves performed the inthe itemial configuration to the formation of thcan be node, g (s) value
  • strRepresentation : the representation of the Council of the series in CSV format. To configure the target string representation will be "1,2,3,4,5,6,7,8,0". Sliding tile puzzle can be a puzzle League: the formation of just one for After a series of moves that will we can go back to to , along wthe itemh therefore we will store the representation of each node expalong wthe itemhed to avoid these courses. We are for thcan be purpose used HashSet
  • path : stores at every step inside the chain ( "DLRU"), along wthe itemh therefore thcan be series represents a series of moves that will the inthe itemial configuration until the current node
  • size : size of the painting. Note that will we are assuming the dimensions of the Council of n , m where n = m .

Now that will we've introduced the object knot Let's clarify the implementation of the A * algorthe itemhm through an example. In thcan be example we will consider mcan beplaced tiles heurcan betic , probably the simplest, heurcan betic most common things that will puzzle. Tile heurcan betic mcan beplaced returns the number of tiles that will are mcan beplaced. That can be, incorrectly posthe itemioned when compared to the target configuration. It can be accepted as the number of returned not exaggerate the minimum number of moves required to reach the state goal. You have to move all the tiles out of place once at least to be able to take them to the posthe itemion of their goal. Thus, the item can be acceptable.

A* Search
A * search. ( send large copy )

to implement the A * algorthe itemhm we'll create object Astar wthe itemh the following chart:

  function Astar (
http://cheapestdigthe Amnesty International for an HTML sliding tile puzzle : JavaScript Amnesty International for an HTML sliding tile puzzle
Share on Google Plus

1 Response to " JavaScript Amnesty International for an HTML sliding tile puzzle "

  1. Hello guy
    I Like this page
    If u want play casino online all games
    Please Click Here this post
    Wish all u Good Luck
    thanks you!!!