Solved-Homework 02 -Solution

$30.00 $19.00

THIS ASSIGNMENT IS ONLY WORTH 10% OF YOUR FINAL GRADE. Instructions In this assignment, you will be required to write JavaScript functions that simplify navigating an elaborate maze. 1.1 Data File Speci cation An example of properly formatted le is shown in Figure 1. The rst le encodes a maze, the second le encodes the…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)

THIS ASSIGNMENT IS ONLY WORTH 10% OF YOUR FINAL GRADE.

  • Instructions

In this assignment, you will be required to write JavaScript functions that simplify navigating an elaborate maze.

1.1 Data File Speci cation

An example of properly formatted le is shown in Figure 1. The rst le encodes a maze, the second le encodes the directions inwhich to be moved.

1

part01test01.maze.emf

x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x x , – , – ,x , – , – ,x , – ,x , – , – , – , – , – ,x , – , – , – ,x , – , – , – , – , – , – , – ,x , – , x x , – , – , – , – ,x , – , – , – , – , – ,x ,x , – , – , – ,x , – , – , – , – , – , – , – ,x ,x , – , – , x x , – ,x ,x , – , – ,x , – , – ,x , – , – , – ,x ,x ,x ,x ,x ,x , – ,x , – ,x , – , – ,x ,x , – , x x , – ,x ,x ,x , – , – , – , – , – , – ,x , – ,x ,x ,x ,x ,x ,x , – ,x , – , – , – , – , – ,x , – , x x , – , – , – , – , – , – ,x , – , – ,x , – , – , – , – , – , – , – , – , – ,x , – ,x ,x ,x , – , – , – , x x ,x , – ,x ,x , – , – , – , – ,x , – ,x ,x , – ,x ,x , – , – ,x , – , – , – ,x ,x ,x ,x , – ,x , x x , – , – , – , – , – ,x , – , – ,x , – , – , – , – , – ,x , – , – , – , – , – , – , – ,g ,x ,x , – , – , x x , – ,x , – ,x , – , – , – , – ,x , – ,x ,x ,x , – , – , – , – , – , – ,x ,x ,x , – ,x ,x ,x , – , x x , – , – , – , – , – ,x , – , – , – , – , – , – , – , – ,x , – ,x , – , – ,x , – , – , – , – , – ,x , – , x x , – , – ,x , – , – , – , – ,x , – ,x , – , – ,x , – , – , – , – , – ,x , – , – , – ,x ,x , – , – , – , x x , – , – ,x , – ,x , – , – , – , – , – , – , – , – , – , – , – ,x , – , – ,x ,x , – , – , – , – , – ,x , x x , – , – , – , – , – , – ,x , – , – , – , – ,x , – , – ,x , – , – , – , – , – ,x ,x ,x , – ,x , – , – , x x ,x ,x , – ,x , – , – ,x , – ,x , – , – , – ,x , – ,x ,x , – , – ,x ,x , – ,x , – , – , – , – , – , x x , – , – , – , – , – , – , – , – , – , – ,x , – , – , – , – ,x , – , – , – ,x , – , – , – ,x , – , – ,x , x x , – , – , – , – ,1 , – , – ,x , – , – , – ,x ,x , – ,x , – , – ,x , – , – ,x ,x , – ,x , – , – , – , x x ,x , – , – ,x , – ,x , – , – , – ,x , – , – , – , – , – , – , – ,x ,x , – ,x ,x , – , – , – , – , – , x x , – , – , – , – , – , – ,x , – ,x ,x , – , – ,x ,x , – ,x , – ,x , – , – ,x , – , – ,x , – ,x ,x , x x ,x , – ,x , – , – , – ,x , – , – , – , – , – , – , – , – , – , – , – , – ,x , – , – , – , – , – ,x ,x , x x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

part01test01.moves.emf

l ,r ,l ,d ,r ,r ,l , u

Figure 1: A properly formatted encoding

  • One Player, One Move

The rst part (onePlayerOneMove in the le csce322homeWork02part01.js) will take in one (1) argument (a maze) and return a function that takes in one (1) argument (a move), and returns the maze that is the result of moving Player 1 in the given direction.

The rules for moving are

If a player is immediately blocked by a wall (x) or another player, they do not move

A player continues to move in a given direction as long as \forward” and \backward” are their only options to move in

\forward” is in the direction of travel and \backward” is opposite the direction of travel

If a player encounters an obstacle (a wall or another player) while moving, they will attempt to change their direction (with priority given to up, down, left, and right in that order), but will not reverse their direction.

A player will stop moving once they reach the goal (g), a dead-end where their only option is to reverse their direction, or when they reach a location where they have more options for travel than just \forward” and \backward”.

Page 2

x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x x , – , – ,x , – , – ,x , – ,x , – , – , – , – , – ,x , – , – , – ,x , – , – , – , – , – , – , – ,x , – , x x , – , – , – , – ,x , – , – , – , – , – ,x ,x , – , – , – ,x , – , – , – , – , – , – , – ,x ,x , – , – , x x , – ,x ,x , – , – ,x , – , – ,x , – , – , – ,x ,x ,x ,x ,x ,x , – ,x , – ,x , – , – ,x ,x , – , x x , – ,x ,x ,x , – , – , – , – , – , – ,x , – ,x ,x ,x ,x ,x ,x , – ,x , – , – , – , – , – ,x , – , x x , – , – , – , – , – , – ,x , – , – ,x , – , – , – , – , – , – , – , – , – ,x , – ,x ,x ,x , – , – , – , x x ,x , – ,x ,x , – , – , – , – ,x , – ,x ,x , – ,x ,x , – , – ,x , – , – , – ,x ,x ,x ,x , – ,x , x x , – , – , – , – , – ,x , – , – ,x , – , – , – , – , – ,x , – , – , – , – , – , – , – ,g ,x ,x , – , – , x x , – ,x , – ,x , – , – , – , – ,x , – ,x ,x ,x , – , – , – , – , – , – ,x ,x ,x , – ,x ,x ,x , – , x x , – , – , – , – , – ,x , – , – , – , – , – , – , – , – ,x , – ,x , – , – ,x , – , – , – , – , – ,x , – , x x , – , – ,x , – , – , – , – ,x , – ,x , – , – ,x , – , – , – , – , – ,x , – , – , – ,x ,x , – , – , – , x x , – , – ,x , – ,x , – , – , – , – , – , – , – , – , – , – , – ,x , – , – ,x ,x , – , – , – , – , – ,x , x x , – , – , – , – , – , – ,x , – , – , – , – ,x , – , – ,x , – , – , – , – , – ,x ,x ,x , – ,x , – , – , x x ,x ,x , – ,x , – , – ,x , – ,x , – , – , – ,x , – ,x ,x , – , – ,x ,x , – ,x , – , – , – , – , – , x x , – , – , – , – , – , – , – , – , – , – ,x , – , – , – , – ,x , – , – , – ,x , – , – , – ,x , – , – ,x , x x , – , – , – , – ,1 , – , – ,x , – , – , – ,x ,x , – ,x , – , – ,x , – , – ,x ,x , – ,x , – , – , – , x x ,x , – , – ,x , – ,x , – , – , – ,x , – , – , – , – , – , – , – ,x ,x , – ,x ,x , – , – , – , – , – , x x , – , – , – , – , – , – ,x , – ,x ,x , – , – ,x ,x , – ,x , – ,x , – , – ,x , – , – ,x , – ,x ,x , x x ,x , – ,x , – , – , – ,x , – , – , – , – , – , – , – , – , – , – , – , – ,x , – , – , – , – , – ,x ,x , x x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

l ,r ,l ,d ,r ,r ,l , u

Figure 2: Before onePlayerOneMove

Page 3

x x x x x x x x x x x x x x x x x x x x x x x x x x x x x

x – –

x – –

x -x – – –

-x – – –

x

—-

– – –

x – x

x – –

– -x

– – – – xx

– – -x –

—-

– xx

– – x

x – xx – –

x

– -x – –

xxxxxx -x -x

– – xx – x

x – xxx –

– – – -x – xxxxxx -x – –

– – –

x – x

x – –

– – –

x – -x –

—–

-x – xxx –

– – x

xx – xx –

– -x – xx – xx – –

x

– – – xxxx – xx

x – –

– – –

x

– -x – –

– -x – –

—-

gxx

– – x

x -x -x –

– -x – xxx – – – –

– xxx – xxx – x

x – –

– – –

x

—–

– -x -x

-x – –

– – –

x – x

x – –

x – –

-x -x –

x – – – –

x – – –

xx –

– – x

x – –

x -x

—–

– – – -x

– xx –

– – –

– xx

x – –

– – –

x – – – –

x

– -x – –

– – xxx -x

– – x

xxx -x –

x -x – –

x – xx –

xx -x

– – –

– – x

x – –

– – –

– – – -x

– – -x –

-x – –

-x –

– xx

x – –

-1 –

-x – – –

xx -x – -x – – xx -x –

– – x

xx –

-x -x

– – -x –

—–

xx – xx

– – –

– – x

x – –

– – –

x – xx –

xx -x -x

– -x –

-x – xxx

xx -x – –

x – – – –

—–

-x – –

– – –

xxx

x x x x x x x x x x x x x x x x x x x x x x x x x x x x x

Figure 3: After onePlayerOneMove

3 One Player, Many Moves

The second part (onePlayerManyMoves in the le csce322homeWork02part02.js) will take in one

  1. argument (a maze) and return a function that takes in one (1) argument (an array of moves), and returns the maze that is the result of Player 1 making all of the moves. If the maze is solved before the list of moves is completely processed, the resulting maze is returned.

Page 4

x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

x , – ,x , – , – , – , – , – ,x , – , x

x , – , – , – , – ,x , – , – ,x , – , x

x , – , – , – , – , – , – , – , – , – , x

x ,x ,x , – , – ,x , – ,x ,x , – , x

x , – , – , – , – , – ,x , – , – , – , x

x ,x , – , – , – , – , – ,x , – , – , x

x , – ,x , – ,x , – , – ,g , – , – , x

x , – ,x , – , – , – ,x ,x , – ,x , x

x , – , – , – ,x , – , – ,x , – , – , x

x , – ,x , – ,x ,x , – ,x ,x , – , x

x , – ,x ,x , – , – , – , – , – , – , x

x , – , – , – , – , – ,x , – ,x , – , x

x , – ,x , – ,x , – , – , – , – ,x , x

x , – , – , – ,x , – , – ,x , – ,x , x

x , – , – ,x ,x ,x , – ,x , – , – , x

x , – , – , – , – , – , – , – ,1 , – , x

x , – , – ,x , – , – , – ,x , – , – , x

x , – , – , – , – ,x , – , – , – ,x , x

x , – ,x ,x , – , – ,x , – , – , – , x

x , – , – , – , – , – ,x , – , – , – , x

x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

r ,d ,d ,l ,u ,l ,r ,l ,r ,r ,r ,u , r

Figure 4: Before onePlayerManyMoves

Page 5

x x x x x x x x x x x

x -x – –

– – -x – x

x – – – –

x – -x – x

x – – – –

– – – – – x

  1. – -x – xx – x x – – – – -x – – – x

  1. – – – – -x – – x

x -x -x –

-g – –

x

x -x –

– –

xx – xx

x – – –

x –

-x – –

x

x -x – xx – xx – x

x – xx –

– – –

– – x

x – –

– –

-x -x – x

x -x -x

– – –

– xx

x – –

-x

– -x – xx

x – –

xxx -x

– – x

x – –

– –

– – –

1 – x

x – –

x –

– -x

– – x

x – –

– –

x – –

– xx

x – xx –

-x –

– – x

x – –

– –

-x –

– – x

x x x x x x x x x x x

Figure 5: After onePlayerManyMoves

  • Many Players , One Move

The third part (manyPlayersOneMove in the le csce322homeWork02part03.js) will take in one

  1. argument (a maze) and return a function that takes in one (1) argument (a move), and returns the maze that is the result of Player 1 making that move. This di ers from the rst part in that there will be more than one (1) player in the maze.

Page 6

x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

x ,x ,x ,x , – , – , – , – , – , – ,x , – , – , x

x , – ,x , – , – , – ,x ,1 , x , – ,x , – , – , x

x , – , – , – ,x , – , – , – , – , – , – ,x , – , x

x , – , – , – , – ,x , – , – ,x , – , – , – , – , x

x , – ,x ,x ,x , – , – , – , – , – , – , – , – , x

x ,x , – ,x , – , – , – ,x ,x , – ,x , – , – , x

x ,x , – , – ,x ,x , – ,x ,x , – , – , – ,x , x

x , – , – , – , – , – , – ,x ,x ,x ,x , – ,3 , x

x , – ,x ,x , – ,x ,x ,x ,x ,x ,x ,x , – , x

x ,x , – ,x , – , – ,x ,x ,x , – , – , – , – , x

x , – , – , – ,x , – ,x ,x ,x , – ,x , – , – , x

x , – ,x , – , – , – , – ,x ,x , – ,x ,x , – , x

x , – , – , – ,x ,x , – ,x ,x , – , – , – , – , x

x , – , – ,x ,x , – , – ,x ,x , – ,x , – , – , x

x ,x , – , – , – , – , – , – ,x ,2 , – , – , – , x

x ,x ,x ,x , – , – ,x , – ,x ,x ,x ,x , – , x

x , – , – , – , – , – , – , – ,x ,x ,x ,x , – , x

x , – , – ,x , – ,x , – ,x ,x ,x ,x ,x , – , x

x ,x , – , – , – , – , – , – , – , – ,x , – , – , x

x , – , – ,x , – ,x , – , – ,x , – , – , – ,x , x

x ,x , – , – , – , – , – ,x , – , – ,x , – , – , x

x ,x ,x , – , – , – ,x ,x , – , – , – , – , – , x

x , – , – ,x , – , – , – , – ,x , – ,x ,x , – , x

x , – ,x ,x , – , – , – , – , – , – , – ,x , – , x

x , – , – , – , – ,x , – ,x , – ,x , – , – , – , x

x , – , – , – , – , – , – ,x , – , – , – , – ,x , x

x , – , – ,x , – , – , – , – , – ,g , – , – ,x , x

x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

d ,d ,r ,d ,d ,r ,l ,r ,u ,d ,d ,d ,r ,l , r

Figure 6: Before manyPlayersOneMove

Page 7

x x x x x x x x x x x x x x

  1. – – – – – -x – – x x -x – – -x -x -x – – x

x – – –

x

–1—

x – x

x – – –

x – -x – –

– – x

x – xxx

——

– – x

xx -x

– – xx -x

– – x

xx – –

xx – xx – –

– xx

x – – –

– – xxxx -3 x

x – xx – xxxxxxx – x

xx -x – –

xxx – – – –

x

x – –

-x – xxx -x – – x

x -x

– – –

– xx – xx – x

x – –

– xx – xx – – – –

x

x – –

xx –

– xx -x – –

x

xx –

– – –

– – x2 – – –

x

  1. – -x – xxxx – x x – – – – – – – xxxx – x x – -x -x – xxxxx – x xx – – – – – – – -x – – x

x – –

x -x – –

x – –

– xx

xx –

– –

– -x

– -x

– – x

xxx

– –

– xx

– – –

– – x

x – –

x –

– – –

x – xx – x

x – xx –

– – –

– – –

x – x

x – –

– –

x -x -x –

– – x

x – –

– –

– -x

– – –

– xx

x – –

x –

– – –

-g –

– xx

x x x x x x x x x x x x x x

Figure 7: After manyPlayersOneMove

  • Many Players , Many Moves

The fourth part (manyPlayersManyMoves in the le csce322homeWork02part04.js) will take in one (1) argument (a maze) and return a function that takes in one (1) argument (a list of moves), and returns the maze that is the result of each player making the next move in the list in turn (starting with Player 1). If the maze has already been solved, the maze is returned in that state.

Page 8

x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

x , – , – , – , – , – , – , – , – ,2 , – , x

x , – ,x ,x ,x , – ,x , – , – ,x , – , x

x , – ,x , – , – , – , – , – ,x ,x , – , x

x , – , – , – ,x , – , – , – ,x , – , – , x

x , – ,x , – , – , – ,x , – , – , – , – , x

x ,x , – ,x ,x , – , – , – ,x ,x , – , x

x , – , – , – , – , – ,x ,x ,g , – , – , x

x , – ,x ,x ,x ,x ,x ,x , – ,x , – , x

x , – , – ,x ,x , – , – , – , – , – , – , x

x ,x , – , – , – , – ,x , – , – , – ,1 , x

x ,x ,x ,x ,x ,x ,x ,x ,x ,x ,x , x

u ,d ,d ,l ,r ,l ,d ,r ,d ,l , l

Figure 8: Before manyPlayersManyMoves

x x x x x x x x x x x x

x – – –

– – –

2 –

– – x

x – xxx -x

– –

x – x

x -x –

– – –

– xx – x

x – – –

x – –

-x

– – x

x -x –

– -x

– –

– – x

  1. – xx – – – xx – x x – – – – – xxg – – x x – xxxxxx -x – x x – – xx – – – – – – x

  1. – – – -x -1 – – x x x x x x x x x x x x x

Figure 9: After manyPlayersManyMoves

  • Naming Conventions

Your les should follow the naming convention of

csce322homeWork02part01.js, csce322homeWork02part02.js, csce322homeWork02part03.js, and csce322homeWork02part04.js.

6.1 helpers.js

A le named helpers.js has been provided with the functionality to read the .emf les into matrices. If a modi ed helpers.js le is not included with your submission, the default will be used in its place.

Page 9

  • webgrader Note

Submissions will be tested with node.js, note the browser. cse.unl.edu is currently running version 4:8:4 of node.

8

Point Allocation

Component

Points

csce322homeWork02part01.js

1 20

Test Cases

Total

20

csce322homeWork02part02.js

1 20

Test Cases

Total

20

csce322homeWork02part03.js

1 30

Test Cases

Total

30

csce322homeWork02part04.js

1 30

Test Cases

Total

30

Total

100

9

External Resources

JavaScript Tutorial

Page 10