CS 106 Winter 2018
Assignment 01: Arrays and Strings
Question 1 Toads and Frogs
Toads and Frogs is an abstract game, used by mathematicians to study the properties of games in general. Though it's easy to describe, it leads to some surprisingly complex mathematical ideas. Here we won't get into the math, but we'll develop a Processing sketch that allows two people to play Toads and Frogs interactively.
The game is played in a row of squares, where each square is either empty, or contains one toad or one frog. For example, let's consider a nine-square board, with three toads starting in the first three squares and three frogs starting in the last three. Toads move to the right, and frogs move to the left. The initial board might look like this:
Two players take turns making moves, with toads moving first. There are two legal moves:
- Step: If the square ahead of one of your pieces is empty, you can step into it.
- Hop: If the square ahead of one of your pieces contains your opponent's piece, and the square after that is empty, you can hop over your opponent into the empty square.
For example, the first row in the diagram below shows a small game in progres. From Step 1, one player hops his toad over his opponent's frog to get to the board in Step 2. The other player responds by stepping her frog one square forward, arriving at the board shown in Step 3.
The game ends when one of the two players has no legal moves left, at which point the other player (i.e., the last to move) wins.
Write a Processing sketch called ToadsAndFrogs to play this game. You must support at least the following features:
- You must display a board made up of a row of nine clearly distinct squares, each of size 100×100.
- The sketch must display the positions of three toads and three frogs. You can use any shape or image that you want, as long as toads are visibly distinct from frogs.
- When the sketch starts, the toads and frogs are in their initial positions as shown above, and it's toad's turn.
- When it's their turn, a player makes a move by clicking anywhere in one of the board squares. If that square represents a legal turn (it contains one of their pieces, and that piece can legally step or hop), then the move is made and it becomes the other player's turn. If the click doesn't represent a legal move, nothing happens. Note that pieces are not allowed to leave the board.
That's it. Note that you don't need to detect when the game is over. At some point, one of the players will simply have no legal moves left, and the game will stop responding to clicks. To start a new game, quit and restart the sketch.
If you're not sure where to begin, here's a suggested plan of attack for implementing the sketch.
- At the top of the sketch, declare an array of integers as a global variable, to hold the board. Declare a second integer global variable to keep track of whose turn it is. In the array, use the convention that +1 represents a toad, -1 represents a frog, and 0 represents an empty square. Similarly, use +1 and -1 for the turn variable to keep track of whether it's toad's turn or frog's turn.
- Write a setup() function to initialize the sketch. Set the size of the sketch to be large enough to hold the board. Initialize the global variables as needed (you can also initialize them as part of declaring them, in which case setup() can be very short).
- Write a draw() function. The function should use a loop to iterate over the board array, drawing every square. When drawing a square, check if it has a frog or toad in it. If so, draw some kind of marker for that piece. As mentioned above, you can use whatever graphics you want, as long as you can tell toads apart from frogs.
- Write a mousePressed() function to respond to clicks. The function must first detect which square was clicked. (Hint: if the sketch window is the exact size of the board, then every click is inside some square. You can detect the array index of the clicked square with a single line of code, by dividing mouseX by the square's side length.) Determine whether that square corresponds to a legal move for the player whose turn it is. If so, execute that move by modifying the array and setting the turn variable to the other player. If the click wasn't a legal move, do nothing.
You are invited to explore enhancements to this basic sketch to create a more fully featured game, as long as the core requirements above are supported. We will award bonus marks for attractive, creative, or interesting enhancements. Just include a comment at the top of the sketch to tell us what to look for. Here are some suggestions:
- Create compelling graphics for the board and pieces. You can draw these directly in code, or even look ahead to Module 03 to learn how to load and display images and (SVG) vector illustrations. Add an attractive border design, a title, instructions, etc.
- Add some sort of visual indication of whose turn it is.
- Highlight the legal moves available to a player.
- Detect when the game is over, and display information about who won.
- Add a control for restarting the game.
- Animate the stepping and hopping moves.
- Allow the user to control the number of squares in the board, the numbers and positions of the starting toads and frogs, etc.
- Develop and display "puzzles" for a solo user to solve. For example, in the starting configuration we're using, can you get the frogs and toads to swap places? That's a nice little puzzle.
Place your ToadsAndFrogs sketch inside a folder titled A01.
Question 2 This Is Just To Say
This Is Just To Say was composed in 1934 by the great imagist poet William Carlos Williams. A bit more recently, Mark Sample created the Twitter bot @JustToSayBot, a kind of "Mad Libs" mash-up of the poem with random words substituted in strategic locations. In this question, you will use the RiTa library, introduced in Lab 01, to reproduce the bot.
- If you haven't already done so, install the RiTa library by following Lab 01, Question 2, Step 2. Create a new sketch called JustToSay. As you did in the lab, include an import statement at the top, after your name and ID.
- Write a setup() function. Set the size of the sketch window to 300×500. After that, include the line textSize( 22 ); to make text drawn in the sketch window larger than the default.
- Now write a draw() function. Set the background colour of the sketch and choose a text colour (via fill()). You can choose whatever colours you want, as long as they go well together. Add the line noLoop(); at the end of draw().
- In draw(), between the colours and the call to
noLoop(), add code to generate a random variation of
the poem, and display it in the sketch window. Use the following
template, filling in the blanks with the required parts of the speech
and including the blank lines between stanzas (click to download a PDF):
- Finally, add a keyPressed() function that simply calls loop(). The idea is that we draw a single poem to the screen and then stop looping. By pressing a key, you instruct Processing to restart the program loop, which causes it to go through another round of draw(), displaying a new poem and then pausing again. The result is that you can press any key to move to a new poem.
There are a few different ways to generate and draw the poem above. The easiest way is probably to define a variable of type String that holds the complete poem, including "\n" to indicate line breaks and using the + operator to glue in random words when they're needed. If you have one long string with line breaks, you can draw it to the sketch window with a single call to text(). Make sure that every word is chosen separately from the others—your poem should not re-use the same adjective three times!
Put your solution in a sketch titled JustToSay in your A01 folder.
Submission
When you are ready to submit, please follow these steps.
Please ensure that any sketches you submit compile and run. It's better to submit a sketch that runs smoothly but implements fewer required features than one that has broken code for all features. If you get partway into a feature but can't make it work, comment it out so that the sketch works correctly without it.
If necessary, review the Code Style Guide and use Processing's built-in auto format tool. You do not need to use the precise coding style outlined in the guide, but whatever style you use, your code must be clear, concise, consistent, and commented.
If necessary, review the How To Submit document for a reminder on how to submit to LEARN.
Make sure to include a comment at the top of all source files containing your name and student ID number.
Create a zip file called A01.zip containing the entire A01 with its subfolders ToadsAndFrogs and JustToSay.
Upload A01.zip to LEARN. Remember that you can (and should!) submit as many times as you like. That way, if there's a catastrophe, you and the course staff will still have access to a recent version of your code.
If LEARN isn't working, and only if LEARN isn't working, please email your ZIP file to the course account (see the course home page for the address). In this case, you must mail your ZIP file before the deadline. Please use this only for emergencies, not "just in case". Submissions received after the deadline may receive feedback, but their marks will not count.