Clue Paths

Download Clue Paths

Post on 10-Feb-2016

31 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Clue Paths. The Challenge. Highlight all the squares that can be reached in # of steps shown on one die Move only up/down/left/right (no diagonal) Do not touch any location more than one time Highlight only the final squares, not the paths . Roll a 1. The Challenge. Roll a 2. - PowerPoint PPT Presentation

TRANSCRIPT

  • Clue Paths

  • Memory Alias ReviewRemember that its possible for two unique variables to point to the same memory locationmyListMyClass mc0x1000x100someFn (in another class){ localVar = mc.getMyList();}localVar0x100If I modify localVar, it also changes myList!

  • Memory Alias ContinuedIn C++, what was the purpose of the copy constructor?In Java, we also use copy constructor, to avoid shallow copy. myListMyClass mc0x100myListMyClass mc3 (created using copy, e.g., mc3 = new LinkedList(mc); )0x500myListMyClass mc2 (created by assignment, e.g., mc2 =mc; )0x1000x2000x3000x00x2000x3000x1000x6000x7000x00x6000x7000x500555575759595

  • Recursion Review*Base case causes recursion to endRecursive call to same functionProgress toward base case* the algorithm I describe uses recursion. Recursion is not absolutely required, but this is a good opportunity for review.

  • Simple Examplepublic static int factorial(int n) { if(n==1) return 1; return factorial(n-1) * n; }

    public static void main(String[] args) { System.out.println(factorial(3));}Very Quick Ex: How would factorial(3) be calculated? (trace the code)

  • Sometimes need setup firstExample: drawing a fractal drawFractal: setup center x, center y, x0 and y0Call drawFractalLine with length, theta, orderdrawFractalLine draws a line, does a recursive call with order-1Whats the point? User calls drawFractalrecursive function is drawFractalLine. Our algorithm has similar structurefrom: Thinking Recursively in Java, Eric Roberts

  • The ChallengeRoll a 1Highlight all the squares that can be reached in # of steps shown on one dieMove only up/down/left/right (no diagonal)Do not touch any location more than one timeHighlight only the final squares, not the paths

    RoomRoomWalkWalkRoomRoomDoorWalkWalkRoomWalkWalkWalkWalkRoomWalkPlayerWalkWalkWalkWalkWalkWalkWalkWalk

    RoomRoomWalkWalkRoomRoomDoorWalkWalkRoomWalkWalkWalkWalkRoomWalkPlayerWalkWalkWalkWalkWalkWalkWalkWalk

  • The ChallengeRoll a 2

    RoomRoomWalkWalkRoomRoomDoorWalkWalkRoomWalkWalkWalkWalkRoomWalkPlayerWalkWalkWalkWalkWalkWalkWalkWalk

    RoomRoomWalkWalkRoomRoomDoorWalkWalkRoomWalkWalkWalkWalkRoomWalkPlayerWalkWalkWalkWalkWalkWalkWalkWalk

  • A clarificationRoll a 4. (this is just one target)Note that the person CANNOT backtrack that is, visit the same cell twice. But the person is NOT required to go in a straight line. So you CANNOT just include all squares that are n away from the initial point.

    RoomRoomWalkWalkRoomRoomDoorWalkWalkRoomWalkWalkWalkWalkRoomWalkPlayerWalkWalkWalkWalkWalkWalkWalkWalk

    RoomRoomWalkWalkRoomRoomDoorWalkWalkRoomWalkWalkWalkWalkRoomWalkPlayerWalkWalkWalkWalkWalkWalkWalkWalk

  • Simplify for nowForget about doors and rooms. Lets do a simple 4x4 gridNumber the rows and columns. Assume a 2D grid, each containing a board cell.Create an adjacency list. NOTE: adjacency lists will be even more handy in the clue layout, because a square is not necessarily attached to each of its neighbors. By creating the adjacency list when the board is loaded, we can simplify the code for the path-finding algorithm.With your partner: What is this? How would you write a program to create? 0 1 2 30

    1

    2

    3Adjacency List (sample entries)[0][0] => { [0][1], [1][0] }

    [1][1] => { [0][1], [1][0], [1][2], [2][1] }

  • Calculating the Adjacency ListsNotice that for each cell, you have at most 4 neighbors.

    To calculate the adjacency list:for each cell look at each neighbor if it is a valid index add it to the adjacency listWhat data structure would you use for the adjacency list?(well use a common structure covered in a minute)0 1 2 30

    1

    2

    3

  • Calculating Neighbors

  • Choose Data StructuresWe want to be able to look up all the adjacencies for a given cell. This is a good application of a map:private Map adjMtx;

    What is a BoardCell? For now, a simple class with a row and column. More will be added in the Clue game.

    Why is the adjacency stored as a linked list? I wanted to add/remove easily at the end of the list. LinkedList supports easily. But theres no insert/remove in the middle of the list, so LinkedList choice is somewhat arbitrary. What are some other alternatives? To facilitate code swap, we will use the same data types but others might be equally valid. Adjacency List (sample entries)[0][0] => { [0][1]->[1][0] }

    [1][1] => { [0][1]->[1][0]->[1][2]->[2][1] }

  • 0 1 2 30123Goal: Targets with paths of length 2 from cell 4Answer: Shown in green belowadjacency matrix should have been calculated prior to calling calcTargets

    calcTargets: Set up for recursive callvisited list is used to avoid backtracking. Set to empty list.targets will ultimately contain the list of cells (e.g., [0][1], [1][2], [2][1], [3][0]) . Initially set to empty list.add the start location to the visited list (so no cycle through this cell)call recursive functionwhat will you name this function? I did findAllTargetswhat parameters does it need?Why cant these steps be in recursive fn?

  • 0 1 2 301230 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14]adjacencies (in condensed form, 0 is [0][0] etc.)findAllTargets pseudocodeParameters: thisCell and numSteps

    Set adjacentCells to all cells adjacent to thisCell that have not been visitedNOTE: do not modify adjacencies, create a new list!Suggest: helper function, keep code cleaner

    for each adjCell in adjacentCells -- add adjCell to visited list -- if numSteps == 1, add adjCell to Targets-- else call findAllTargets with adjCell, numSteps-1-- remove adjCell from visited listfindAllTargetsrecursive call!visited: [1][0]

    0123456789101112131415

  • 0 1 2 30123visited: [1][0]0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14]All adjacenciesTargets: (none yet)findAllTargets call: thisCell =[1][0], numSteps=2Set adjacentCells to all cells adjacent to [1][0] that have not been visited [[2][0], [1][1], [0][0]]

    (for each cell in adjacentCells)-- add adjCell to visited -- (if number of steps == 1 )-- else call findAllTargets with adjCell , numStep-1 -- (when we return.remove adjCell from visited)findAllTargets

    0123456789101112131415

  • 0 1 2 301230 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14]All adjacenciesTargets: (none yet)findAllTargets call: thisCell =[1][0], numSteps=2Set adjacentCells to all cells adjacent to 4 that have not been visited [8, 5, 0]

    (for each cell in adjacentCells current is [2][0])-- add [2][0] to visited list-- (if number of steps == 1 )-- else call findAllTargets with adjCell, numStep-1 -- (when we return.remove from visited)visited: [1][0][2][0]findAllTargets

    0123456789101112131415

  • 0 1 2 30123Targets: (none yet)findAllTargets call: thisCell =[1][0], numSteps=2Set adjacentCells to all cells adjacent to 4 that have not been visited [8, 5, 0]

    (for each cell in adjacentCells current is [2][0])-- add adjCell to visited -- (if number of steps == 1 its not)-- else call findAllTargets with adjCell ([2][0]), numStep-1 -- (when we return.set visited to False)recursive call! next slidevisited: [1][0][2][0]

    0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14]All adjacenciesfindAllTargets

    0123456789101112131415

  • 0 1 2 30123Targets: none yetSet adjacentCells to all cells adjacent to [2][0] that have not been visited [[3][0], [2][1]] not [1][0](but remember, dont modify list of all adjacencies!)

    (for each cell in adjacentCells)-- set visited[12] to True-- if number of steps == 1 yes! update Targets-- (else call calcTargets with adjCell, numSteps--)-- set visited to FalsefindAllTargets call: thisCell =[2][0], numSteps=1visited: [1][0][2][0]0 => [4, 1] 1 => [0, 5, 2] 2 => [1, 6, 3] 3 => [2, 7] 4 => [8, 5, 0] 5 => [4, 9, 6, 1] 6 => [2, 5, 10, 7] 7 => [11, 3, 6] 8 => [12, 9, 4] 9 => [13, 10, 5, 8] 10 => [14, 11, 6, 9] 11 => [15, 7, 10] 12 => [13, 8] 13 => [14, 9, 12] 14 => [15, 10, 13] 15 => [11, 14]All adjacenciesfindAllTargets

    0123456789101112131415