80 likes | 190 Views
In today's Java class, we'll dive into recursive programming and implement Blob.java to understand and modify it for various tasks. Engage with file operations by creating and counting blobs while also exploring grid-based recursion and two-player game dynamics. Learn to determine winning strategies with functions like willFirstPlayerWin, which analyzes turn-based games based on subtracting numbers. Extend your knowledge by examining both recursive and iterative approaches in BlobModel and discover how variations in algorithms can affect game outcomes.
E N D
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.
Two Additional Variations • Recursion on grids • Recursion with 2 player games • GridGame apt • Maybe a brief visit back to stacks and queues
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)
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.
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
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
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
Which line would you need to change to make diagonals work? • Line 1 • Line 2 • Line 3 • Line 4 • Line 5