1 / 8

Two Additional Variations

As you arrive snarf the code for today’s class. Then go ahead and run Blob.java. Play around with running File->new blobs and File->count blobs. Two Additional Variations. Recursion on grids Recursion with 2 player games GridGame apt Maybe a brief visit back to stacks and queues. Blobs.

george
Download Presentation

Two Additional Variations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. As you arrivesnarf the code for today’s class. Then go ahead and run Blob.java. Play around with running File->new blobs and File->count blobs.

  2. Two Additional Variations • Recursion on grids • Recursion with 2 player games • GridGame apt • Maybe a brief visit back to stacks and queues

  3. Blobs • Modify Blobs.java to use BlobModel rather than IterativeBlobModel • Go into BlobModel and finish the blobFill Function (bear in mind it must both modify the board AND return the size of the final blob)

  4. 2 Player Games • Imagine a numbers game played with 2 players who takes turns • The game starts with a particular number • Each turn, a player can subtract 1 2 or 3 from the number • The player who reduces the number to 0 (or below) wins • Consider the function willFirstPlayerWin(intstartingNum) which determines if the first player wins if both players play perfectly • How can we solve this problem recursively? • HINT: willFirstPlayerWin(5) is truebecause willFirstPlayerWin(4) is false. In more normal terms, if the game starts at 5, player1 can reduce it to 4, and then player2 can’t win from 4.

  5. 2 Player Games • Imagine a numbers game played with 2 players who takes turns • The game starts with a particular number • Each turn, a player can subtract 1 2 or 3 from the number • The player who reduces the number to 0 (or below) wins • player 1 wins if player 2 can’t win • So player 1 wants to move the number to something unwinnable • If player 1 can’t do that, player 2 will win • Example: for 4…player 1 can move it to 1 2 3 – any one of those player 2 can win from. • So… if i can win the game right now, then return true for each number I can move the game to if that number is unwinnable (recursion!) return true return false

  6. GridGame • COMBINES grid work and 2 player recursive decdent • It’s available in the APT section of the website • My starting code is in the code you snarfed • There are HINTS below, but try to work with those nearby and solve the problem without hints • Not currently assigned, but I wouldn’t be surprised if it showed up on a later APT set so be sure to keep your code • If you finish early, take a look at IterativeBlobModel and try to understand how the non recursive blobFill function works

  7. Iterative Blob Model • Look and work with those nearby to write an English language description of how it works • You’ll know you really understand it when: • You know how you would change the algorithm to make it work for diagonals (small change) • You know if the algorithm would still work if you used a stack instead of a queue

  8. Which line would you need to change to make diagonals work? • Line 1 • Line 2 • Line 3 • Line 4 • Line 5

More Related