Knowee
Questions
Features
Study Tools

var MovesCount=0function randomPos(){ var arr = []; while(arr.length < 9){ var r = ((Math.floor(Math.random() * 3)+1).toString())+((Math.floor(Math.random() * 3)+1).toString()); if(arr.indexOf(r) === -1) arr.push(r); } return arr}var RandomPos=randomPos()for(let i =0;i<document.getElementsByClassName("tile").length;i++){ document.getElementsByClassName("tile")[i].style.gridArea=RandomPos[i][0]+"/"+RandomPos[i][1]}function MoveMe(tile){ var EmptyTile=document.querySelector(".emtile") var Possibilties=[ parseInt(RandomPos[tile][0])+1==parseInt(RandomPos[8][0])&&parseInt(RandomPos[tile][1])==parseInt(RandomPos[8][1]), parseInt(RandomPos[tile][0])-1==parseInt(RandomPos[8][0])&&parseInt(RandomPos[tile][1])==parseInt(RandomPos[8][1]), parseInt(RandomPos[tile][1])+1==parseInt(RandomPos[8][1])&&parseInt(RandomPos[tile][0])==parseInt(RandomPos[8][0]), parseInt(RandomPos[tile][1])-1==parseInt(RandomPos[8][1])&&parseInt(RandomPos[tile][0])==parseInt(RandomPos[8][0]), ]if(Possibilties[0]||Possibilties[1]||Possibilties[2]||Possibilties[3]){ MovesCount++; EmptyTile.style.gridArea=RandomPos[tile][0]+"/"+RandomPos[tile][1]; document.querySelectorAll(".tile")[tile].style.gridArea=RandomPos[8][0]+"/"+RandomPos[8][1]; var CurrentTile=RandomPos[tile] RandomPos[tile]=RandomPos[8] RandomPos[8]=CurrentTile; NeededPos=["11","12","13","21","22","23","31","32","33"] if(RandomPos.join(".")==NeededPos.join(".")){ console.log("Game Beated"); document.querySelector(".blscreen").style.display='flex' document.querySelector(".MovesCount").innerHTML=MovesCount; var stars=0; if(MovesCount<100){ stars=3 }else if(MovesCount<200){ stars=2 }else if(MovesCount<300){ stars=1 }else{ stars=0 } for(let i=0;i<2;i++){ document.getElementsByTagName("path").style.fill="yellow" } }}}

Question

var MovesCount=0function randomPos(){ var arr = []; while(arr.length < 9){ var r = ((Math.floor(Math.random() * 3)+1).toString())+((Math.floor(Math.random() * 3)+1).toString()); if(arr.indexOf(r) === -1) arr.push(r); } return arr}var RandomPos=randomPos()for(let i =0;i<document.getElementsByClassName("tile").length;i++){ document.getElementsByClassName("tile")[i].style.gridArea=RandomPos[i][0]+"/"+RandomPos[i][1]}function MoveMe(tile){ var EmptyTile=document.querySelector(".emtile") var Possibilties=[ parseInt(RandomPos[tile][0])+1==parseInt(RandomPos[8][0])&&parseInt(RandomPos[tile][1])==parseInt(RandomPos[8][1]), parseInt(RandomPos[tile][0])-1==parseInt(RandomPos[8][0])&&parseInt(RandomPos[tile][1])==parseInt(RandomPos[8][1]), parseInt(RandomPos[tile][1])+1==parseInt(RandomPos[8][1])&&parseInt(RandomPos[tile][0])==parseInt(RandomPos[8][0]), parseInt(RandomPos[tile][1])-1==parseInt(RandomPos[8][1])&&parseInt(RandomPos[tile][0])==parseInt(RandomPos[8][0]), ]if(Possibilties[0]||Possibilties[1]||Possibilties[2]||Possibilties[3]){ MovesCount++; EmptyTile.style.gridArea=RandomPos[tile][0]+"/"+RandomPos[tile][1]; document.querySelectorAll(".tile")[tile].style.gridArea=RandomPos[8][0]+"/"+RandomPos[8][1]; var CurrentTile=RandomPos[tile] RandomPos[tile]=RandomPos[8] RandomPos[8]=CurrentTile; NeededPos=["11","12","13","21","22","23","31","32","33"] if(RandomPos.join(".")==NeededPos.join(".")){ console.log("Game Beated"); document.querySelector(".blscreen").style.display='flex' document.querySelector(".MovesCount").innerHTML=MovesCount; var stars=0; if(MovesCount<100){ stars=3 }else if(MovesCount<200){ stars=2 }else if(MovesCount<300){ stars=1 }else{ stars=0 } for(let i=0;i<2;i++){ document.getElementsByTagName("path").style.fill="yellow" } }}}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This is a JavaScript code. It seems to be a game where tiles are moved around a grid. The game is won when the tiles are arranged in a specific order. The number of moves taken to win the game is counted and based on this count, a star rating is given.

Here's a step-by-step breakdown:

  1. MovesCount is initialized to keep track of the number of moves.
  2. randomPos function generates an array of 9 unique random positions for the tiles.
  3. The positions of the tiles are set to these random positions.
  4. MoveMe function is defined to move a tile to the empty position if it is adjacent to it.
  5. If a move is made, MovesCount is incremented and the positions of the moved tile and the empty tile are swapped in the RandomPos array.
  6. After each move, it checks if the current positions of the tiles match the winning positions. If they do, it logs "Game Beated", displays the number of moves taken and calculates a star rating based on the number of moves.
  7. The star rating is displayed by filling in the corresponding number of stars.

Please note that the last part of the code that is supposed to fill in the stars seems to have an error. It tries to fill in the stars in a loop that runs only twice, regardless of the number of stars. Also, document.getElementsByTagName("path") returns a NodeList, so you need to specify an index to access a specific element.

This problem has been solved

Similar Questions

In the function, reassign computerNumber to a random number between 1 and 3 (use randomInt() )

n the function, reassign computerNumber to a random number between 1 and 3 (use randomInt() )

Examine the following code block.The code has either a syntax or logic error in one line only.Identify the problem line then write it out correctly in the box provided.Your answer must be exact, including correct spelling and spacing.void getRandNum(int low, int high) { // return a value from within the range given int myNum = 0; myNum = rand() % high + low; return myNum;}

Suppose the following set of random numbers is being used to simulate the event of a basketball player making three free throws in a row. How should the numbers be rearranged?860583 785814 010122 337198 621549 034076 796495 978078 433330 333153A.8605 8378 5814 0101 2233 7198 6215 4903 4076 7964 9597 8078 4333 3033 3153B.860 583 785 814 010 122 337 198 621 549 034 076 796 495 978 078 433 330 333 153C.860583 785814 010122 337198 621549 034076 796495 978078 433330 333153D.86058 37858 14010 12233 71986 21549 03407 67964 95978 07843 33303 33153SUBMITarrow_backPREVIOUS

what is random motion

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.